Debian Codegraph @

scripts/tellico2lesana

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import argparse
4 import datetime
5 import zipfile
6 from xml.etree import ElementTree
7
8 import lesana
9
10 NS = {'tellico': 'http://periapsis.org/tellico/'}
11
12 # https://docs.kde.org/trunk5/en/extragear-office/tellico/field-type-values.html
13 F_TYPE_MAP = {
14 '0': 'string', # not in the specs, but seen in the wild
15 '1': 'string',
16 '2': 'text',
17 '3': 'string',
18 '4': 'boolean',
19 '6': 'integer',
20 '7': 'url',
21 '8': 'list', # single column table
22 '10': 'file',
23 '12': 'date', # date
24 '14': 'integer', # rating
25 }
26
27
28 class T2L:
29 """
30 Manage collections
31 """
32
33 arguments = [
34 (
35 ['-c', '--collection'],
36 dict(
37 help='Name of the new lesana collection.' +
38 ' Default is <tellico file base name>.lesana',
39 default=None,
40 ),
41 ),
42 (['file'], dict(help='Tellico file to convert to lesana.',)),
43 ]
44
45 def _load_args(self):
46 self.parser = argparse.ArgumentParser()
47 for arg in self.arguments:
48 self.parser.add_argument(*arg[0], **arg[1])
49 self.args = self.parser.parse_args()
50
51 def read_field_data(self, xfield):
52 if xfield.tag in self.date_fields:
53 for child in xfield:
54 if 'year' in child.tag:
55 year = child.text
56 elif 'month' in child.tag:
57 month = child.text
58 elif 'day' in child.tag:
59 day = child.text
60 try:
61 data = datetime.date(int(year), int(month), int(day))
62 except ValueError:
63 data = None
64 elif xfield.iter().__next__():
65 data = []
66 for child in xfield:
67 data.append(self.read_field_data(child))
68 else:
69 data = xfield.text
70 return data
71
72 def main(self):
73 self._load_args()
74 with zipfile.ZipFile(self.args.file, 'r') as zp:
75 tree = ElementTree.parse(zp.open('tellico.xml'))
76 # open collection
77 xml_collection = tree.getroot().find('tellico:collection', NS)
78
79 # get collection settings
80 title = xml_collection.attrib['title']
81 xml_fields = xml_collection.find('tellico:fields', NS)
82 self.date_fields = []
83 fields = []
84 for xf in xml_fields:
85 if xf.attrib['type'] == '12':
86 self.date_fields.append(
87 '{' + NS['tellico'] + '}' + xf.attrib['name']
88 )
89 f_type = F_TYPE_MAP.get(xf.attrib['type'])
90 # TODO: support fields with the multiple values flag
91 # (they should probably become lists)
92 try:
93 flags = int(xf.attrib['flags'])
94 except ValueError:
95 flags = 0
96 if flags % 2 == 1:
97 l_type = f_type
98 f_type = 'list'
99 plural = 's'
100 else:
101 l_type = None
102 plural = ''
103 field = {
104 'name': xf.attrib['name'] + plural,
105 'type': f_type,
106 'help': xf.attrib['title'],
107 }
108 if l_type:
109 field['list'] = l_type
110 fields.append(field)
111 # Create a collection with the settings we have loaded
112 directory = self.args.collection or self.args.file.replace(
113 '.tc', '.lesana'
114 )
115 self.collection = lesana.collection.Collection.init(
116 directory=directory,
117 git_enabled=False,
118 settings={'name': title, 'fields': fields, },
119 )
120
121 # import data
122 for xe in xml_collection.findall('tellico:entry', NS):
123 data = {'eid': xe.attrib['id']}
124 for xfield in xe:
125 field_name = xfield.tag.replace('{' + NS['tellico'] + '}', '')
126 data[field_name] = self.read_field_data(xfield)
127 new_entry = lesana.collection.Entry(self.collection, data=data)
128 self.collection.save_entries([new_entry])
129 self.collection.update_cache()
130
131
132 def main():
133 T2L().main()
134
135
136 if __name__ == '__main__':
137 main()