diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2017-05-21 13:13:43 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2017-05-21 13:13:43 +0200 |
commit | 15ae1a931e51f96b644c7003d6420bbddffbc2e1 (patch) | |
tree | be5321edd357f33acf97c05f891a6b4ee02f0baa /scripts | |
parent | 4fe7b851d6ffc86d70577407774532921403ac8d (diff) |
tellico2lesana: start creating entries
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/tellico2lesana | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/scripts/tellico2lesana b/scripts/tellico2lesana index 1c579c9..7b4df16 100755 --- a/scripts/tellico2lesana +++ b/scripts/tellico2lesana @@ -8,7 +8,7 @@ import lesana import guacamole -XML_NS = '{http://periapsis.org/tellico/}' +NS = {'tellico': 'http://periapsis.org/tellico/'} # https://docs.kde.org/trunk5/en/extragear-office/tellico/field-type-values.html F_TYPE_MAP = { @@ -43,32 +43,53 @@ class T2L(guacamole.Command): help='Tellico file to convert to lesana.', ) + def read_field_data(self, xfield): + if xfield.iter().__next__(): + data = [] + for child in xfield: + data.append(self.read_field_data(child)) + else: + data = xfield.text + return data + + def invoked(self, ctx): with zipfile.ZipFile(ctx.args.file, 'r') as zp: tree = ElementTree.parse(zp.open('tellico.xml')) # open collection - xml_collection = tree.getroot().find(XML_NS+'collection') + xml_collection = tree.getroot().find('tellico:collection', NS) # get collection settings title = xml_collection.attrib['title'] - xml_fields = xml_collection.find(XML_NS+'fields') + xml_fields = xml_collection.find('tellico:fields', NS) fields = [] for xf in xml_fields: - print(xf.attrib) f_type = F_TYPE_MAP.get(xf.attrib['format']) # TODO: support fields with the multiple values flag # (they should probably become lists) - fields.append({ + try: + flags = int(xf.attrib['flags']) + except ValueError: + flags = 0 + if flags % 2 == 1: + l_type = f_type + f_type = 'list' + else: + l_type = None + field = { 'name': xf.attrib['name'], 'type': f_type, 'help': xf.attrib['title'], - }) + } + if l_type: + field['list'] = l_type + fields.append(field) # Create a collection with the settings we have loaded directory = ctx.args.collection or ctx.args.file.replace( '.tc', '.lesana' ) - lesana.collection.Collection.init( + self.collection = lesana.collection.Collection.init( directory=directory, git_enabled=False, settings={ @@ -78,12 +99,19 @@ class T2L(guacamole.Command): ) # import data - for xe in xml_collection.findall(XML_NS+'entry'): + for xe in xml_collection.findall('tellico:entry', NS): data = { 'uid': xe.attrib['id'] } for xfield in xe.getchildren(): - print(xfield, xfield.text) + field_name = xfield.tag.replace( + '{'+NS['tellico']+'}', + '') + data[field_name] = self.read_field_data(xfield) + new_entry = lesana.collection.Entry(self.collection, data=data) + self.collection.save_entries([new_entry]) + self.collection.update_cache() + |