-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCEDataMgr.py
66 lines (64 loc) · 2.05 KB
/
PCEDataMgr.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
#!/usr/bin/env python
# Copyright (C) 2014 Chaserhkj
# This file is licensed under the MIT license
# For more details, see COPYRIGHT
import json,itertools
from lxml.etree import XML
class DataMgr(object):
def __init__(self):
self._repo_list = []
self._repos = []
@property
def repos(self):
return self._repo_list
@property
def cates(self):
return list(itertools.chain.from_iterable(self._repos))
@property
def emojis(self):
return [i["entries"] for i in self.cates]
@property
def names(self):
return [i["name"] for i in self.cates]
def delRepo(self, index):
del self._repo_list[index]
del self._repos[index]
def addJSONFile(self, path):
with open(path) as f:
data = json.load(f)
name = data["information"][0]
self._repo_list.append([name, path])
self._repos.append(data["categories"])
def addXMLFile(self, path):
with open(path, "rb") as f:
tree = XML(f.read())
name = tree.xpath("/emoji/infoos/info")[0].text
self._repo_list.append([name, path])
cates = []
for i in tree.xpath("/emoji/category"):
entries = []
cate = i.attrib["name"]
for j in i.xpath("entry"):
desc = j.xpath("note")
if not desc:
desc = ""
else:
desc = desc[0].text
content = j.xpath("string")
content = content[0].text
entries.append({
"description":desc,
"emoticon":content
})
cates.append({
"entries":entries,
"name":cate
})
self._repos.append(cates)
def addFile(self, path):
if path.lower().endswith(".json"):
self.addJSONFile(path)
elif path.lower().endswith(".xml"):
self.addXMLFile(path)
else:
raise(ValueError, "Can not determine file type from name.")