1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import os |
---|
4 | import os.path |
---|
5 | import re |
---|
6 | import copy |
---|
7 | import getpass |
---|
8 | |
---|
9 | |
---|
10 | LLIUREX_DEFAULTS="/usr/share/lliurex-mime-settings/mimeapps.list" |
---|
11 | SYSTEM_DIR="/usr/share/applications/" |
---|
12 | SYSTEM_DEFAULTS=SYSTEM_DIR + "mimeapps.list" |
---|
13 | |
---|
14 | |
---|
15 | class DefaultListManager: |
---|
16 | |
---|
17 | def __init__(self): |
---|
18 | |
---|
19 | self.system_keys={} |
---|
20 | if not os.path.exists(SYSTEM_DIR): |
---|
21 | os.makedirs(SYSTEM_DIR) |
---|
22 | |
---|
23 | #def init |
---|
24 | |
---|
25 | def read_file(self,path): |
---|
26 | |
---|
27 | dic={} |
---|
28 | dic["default"]={} |
---|
29 | dic["added"]={} |
---|
30 | dic["removed"]={} |
---|
31 | |
---|
32 | added_found=False |
---|
33 | removed_found=False |
---|
34 | |
---|
35 | if os.path.exists(path): |
---|
36 | |
---|
37 | f=open(path,'r') |
---|
38 | lines=f.readlines() |
---|
39 | f.close() |
---|
40 | |
---|
41 | for line in lines: |
---|
42 | line=line.strip("\n") |
---|
43 | |
---|
44 | if "Added Associations" in line: |
---|
45 | added_found=True |
---|
46 | |
---|
47 | if "Removed Associations" in line: |
---|
48 | removed_found=True |
---|
49 | |
---|
50 | if "=" in line: |
---|
51 | tmp=line.split("=") |
---|
52 | if not removed_found and not added_found: |
---|
53 | dic["default"][tmp[0]]=tmp[1] |
---|
54 | elif added_found and not removed_found: |
---|
55 | dic["added"][tmp[0]]=tmp[1] |
---|
56 | else: |
---|
57 | dic["removed"][tmp[0]]=tmp[1] |
---|
58 | return dic |
---|
59 | |
---|
60 | #def readFile |
---|
61 | |
---|
62 | |
---|
63 | def merge_dic(self,old_dic,new_dic): |
---|
64 | |
---|
65 | |
---|
66 | ret=copy.copy(old_dic) |
---|
67 | |
---|
68 | for key in new_dic["default"]: |
---|
69 | ret[key]=new_dic["default"][key] |
---|
70 | |
---|
71 | for key in new_dic["added"]: |
---|
72 | ret[key]=new_dic["added"][key] |
---|
73 | |
---|
74 | for key in new_dic["removed"]: |
---|
75 | ret[key]=new_dic["removed"][key] |
---|
76 | |
---|
77 | |
---|
78 | return ret |
---|
79 | |
---|
80 | |
---|
81 | def print_keys(self,dic): |
---|
82 | |
---|
83 | for key in dic: |
---|
84 | print "%s = %s"%(key,dic[key]) |
---|
85 | |
---|
86 | |
---|
87 | def write_file(self,path,dic): |
---|
88 | |
---|
89 | if dic != None: |
---|
90 | f=open(path,'w') |
---|
91 | f.write("\n[Default Applications]\n\n") |
---|
92 | for key in dic["default"]: |
---|
93 | f.write(key + " = " + dic["default"][key] + "\n") |
---|
94 | |
---|
95 | f.write("\n[Added Associations]\n\n") |
---|
96 | for key in dic["added"]: |
---|
97 | f.write(key + " = " + dic["added"][key] + "\n") |
---|
98 | |
---|
99 | f.write("\n[Removed Associations]\n\n") |
---|
100 | |
---|
101 | for key in dic["removed"]: |
---|
102 | f.write(key + " = " + dic["removed"][key] + "\n") |
---|
103 | |
---|
104 | f.close() |
---|
105 | |
---|
106 | #def write_file |
---|
107 | |
---|
108 | #class DefaultListManager |
---|
109 | |
---|
110 | if __name__=="__main__": |
---|
111 | |
---|
112 | if getpass.getuser()!="root": |
---|
113 | print "[!] You need to execute this program with root priviledges" |
---|
114 | else: |
---|
115 | print "Lliurex update mime..." |
---|
116 | try: |
---|
117 | dlm=DefaultListManager() |
---|
118 | system=dlm.read_file(SYSTEM_DEFAULTS) |
---|
119 | lliurex=dlm.read_file(LLIUREX_DEFAULTS) |
---|
120 | |
---|
121 | final=dlm.merge_dic(lliurex,system) |
---|
122 | dlm.write_file(SYSTEM_DEFAULTS,final) |
---|
123 | |
---|
124 | except Exception as e: |
---|
125 | print e |
---|
126 | |
---|
127 | |
---|