-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileIOHelper.py
124 lines (103 loc) · 3.28 KB
/
fileIOHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import json
GESTURE_PREFIX='gesture:'
# Libinput gestures configuration file path
LIG_CONF_FILE_PATH_ROOT='/etc/libinput-gestures.conf'
# paths for gestures manager own configuration file
HOME = os.environ.get('HOME')
CONFDIR = HOME+('/.config')
LIG_USER_CONF_FILE_PATH = CONFDIR+'/libinput-gestures.conf'
GM_CONF_FILE_PATH = CONFDIR+'/gesturesmanger.json'
if not os.path.isdir(CONFDIR):
os.makedirs(CONFDIR)
if not os.path.isfile(LIG_USER_CONF_FILE_PATH):
out=open(LIG_USER_CONF_FILE_PATH, 'w')
out.write('# Gestures Manager created this configuration file for Libinput Gestures')
out.close()
LIG_CONF_FILE_COMMENTS= \
"""# Configuration file for libinput-gestures.
#
# The default configuration file exists at /etc/libinput-gestures.conf
# but a user can create a personal custom configuration file at
# ~/.config/libinput-gestures.conf.
#
# Lines starting with '#' and blank lines are ignored.
# At present only gesture lines are configured in this file.
#
# Each gesture: line has 3 [or 4] arguments:
#
# action motion [finger_count] command
#
# where action and motion is either:
# swipe up
# swipe down
# swipe left
# swipe right
# pinch in
# pinch out
#
# command is the remainder of the line and is any valid shell command +
# arguments.
#
# finger_count is optional (and is typically 3 or 4). If specified then
# the command is executed when exactly that number of fingers is used in
# the gesture. If not specified then the command is executed when that
# gesture is executed with any number of fingers. Gesture lines
# specified with finger_count have priority over the same gesture
# specified without any finger_count.
#
# Typically command will be xdotool, or wmctrl. See "man xdotool" for
# the many things you can action with that tool."""
kvpairs = dict()
if not os.path.isfile(GM_CONF_FILE_PATH):
gestures_f = open(GM_CONF_FILE_PATH, 'w')
gestures_f.close()
def string_gm_to_lig(gm_string):
words = gm_string.split()
lig_string = ' '.join([GESTURE_PREFIX, words[2], words[3], words[0]])
return lig_string
def save_gm_conf_file():
gestures_f=open(GM_CONF_FILE_PATH, 'w')
gestures_f.write(json.dumps(kvpairs))
gestures_f.close()
def save_lig_conf_file():
s=''
for key, value in kvpairs.items():
if value != '':
if key[0].isdigit():
s += string_gm_to_lig(key) + " " + value
else:
s += key + " " + value
s += '\n'
lig_f=open(LIG_USER_CONF_FILE_PATH, 'w')
lig_f.write(s)
lig_f.close()
def save_files():
save_gm_conf_file()
save_lig_conf_file()
def get_gesture_dict():
gesture_dict = dict()
for k, v in kvpairs.items():
if k[0].isdigit():
gesture_dict[k] = v
return gesture_dict
def add_elements(datadict):
for k, v in datadict.items():
kvpairs[k] = v
save_files()
def update_element(key, value):
kvpairs[key] = value
save_files()
def get_element(key, defaultval = ''):
return kvpairs.get(key, defaultval)
def remove_element(key):
kvpairs.pop(key)
save_files()
gestures_f = open(GM_CONF_FILE_PATH, 'r')
try:
gestures_text=gestures_f.read()
kvpairs=json.loads(gestures_text)
gestures_f.close()
except ValueError:
gestures_f.close()
save_gm_conf_file()