-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmapper.py
75 lines (63 loc) · 2.62 KB
/
mapper.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
import os.path
import styledprint
import utils
class Mapper:
def __init__(self, mappingfile):
if(os.path.exists(mappingfile)):
self.LINK = '<->'
self.mappingfile = mappingfile
self.mapping = utils.DictCaseInsensitive()
f = open(self.mappingfile, 'r', encoding='utf8')
for line in iter(f):
line = line.strip()
# remove the quotes needed to protect spaces
# at the end of names when steam messed up
line = line[1:-1]
if (self.LINK in line):
tup = line.split(self.LINK)
if (len(tup) <= 1):
styledprint.print_info('The line does not '
'contain enough members',
line)
continue
key = tup[0]
value = tup[1].lower().strip('/');
if (key in self.mapping):
styledprint.print_info('The key {0} is already in the mapper!'
.format(key))
continue
if (len(tup) == 2):
self.mapping[key] = (value,)
elif (len(tup) == 3):
self.mapping[key] = (value, tup[2])
else:
styledprint.print_info('More members in the line than excepted!')
def save_mapping(self):
if ((self.mapping == None) or (len(self.mapping) == 0)):
return
f = open(self.mappingfile, 'w', encoding='utf8')
for key in sorted(self.mapping):
line = '"{0}{1}{2}'.format(key, self.LINK, self.mapping[key][0])
if (len(self.mapping[key]) == 2):
line += '{0}{1}'.format(self.LINK, self.mapping[key][1])
line += '"'
f.write(line)
f.write(os.linesep)
def add_to_mapping(self, left, middle, right=None):
if (left not in self.mapping):
if (right):
self.mapping[left] = (middle.lower(), right)
else:
self.mapping[left] = (middle.lower(),)
else:
styledprint.print_info('Impossible to add mapping, {0} is already in the mapper'.
format(left))
def remove_from_mapping(self, left):
try:
del self.mapping[left]
except:
pass
def get_mapping(self, left):
if (left in self.mapping):
return self.mapping[left]
return None