From 22de81d16ae9b6f82367e31ee523eda10b9a63d2 Mon Sep 17 00:00:00 2001 From: Marcio Mazza Date: Thu, 23 Nov 2017 15:24:25 -0200 Subject: [PATCH] Adiciona dump de documentos do SDE --- sapl/legacy/scripts/exporta_zope/.gitignore | 1 + .../scripts/exporta_zope/exporta_zope.py | 54 +++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/sapl/legacy/scripts/exporta_zope/.gitignore b/sapl/legacy/scripts/exporta_zope/.gitignore index 98561c589..11c2f1d6b 100644 --- a/sapl/legacy/scripts/exporta_zope/.gitignore +++ b/sapl/legacy/scripts/exporta_zope/.gitignore @@ -1,2 +1,3 @@ Data*.fs* sapl_documentos +XSLT diff --git a/sapl/legacy/scripts/exporta_zope/exporta_zope.py b/sapl/legacy/scripts/exporta_zope/exporta_zope.py index b09d2e812..e0ae23f7a 100755 --- a/sapl/legacy/scripts/exporta_zope/exporta_zope.py +++ b/sapl/legacy/scripts/exporta_zope/exporta_zope.py @@ -89,14 +89,20 @@ def dump_file(doc, path): return id -def enumerate_folder(folder): - # folder vazio nao tem _objects - for entry in folder.get('_objects', []): - id, meta_type = entry['id'], entry['meta_type'] - obj = br(folder[id]) +def enumerate_by_key_list(folder, key_list, type_key): + for entry in folder.get(key_list, []): + id, meta_type = entry['id'], entry[type_key] + obj = br(folder.get(id, None)) yield id, obj, meta_type +enumerate_folder = partial(enumerate_by_key_list, + key_list='_objects', type_key='meta_type') + +enumerate_properties = partial(enumerate_by_key_list, + key_list='_properties', type_key='type') + + def enumerate_btree(folder): contagem_esperada = folder['_count'].value tree = folder['_tree'] @@ -125,11 +131,49 @@ def dump_folder(folder, path='', enum=enumerate_folder): return name +def decode_iso8859(obj): + return obj.decode('iso8859-1') if isinstance(obj, str) else obj + + +def read_sde(element): + + def read_properties(): + for id, obj, meta_type in enumerate_properties(element): + assert meta_type == 'string' + if id == 'title': + assert not obj + else: + yield id, decode_iso8859(obj) + + def read_children(): + for id, obj, meta_type in enumerate_folder(element): + assert meta_type == 'SDE-Document-Element' + yield id, read_sde(obj) + + data = dict(read_properties()) + children = list(read_children()) + if children: + assert all(k.startswith('SDE') for k, v in children) + data['children'] = children + return data + + +def dump_sde(strdoc, path): + id = strdoc['id'] + fullname = os.path.join(path, id + '.sde.yaml') + print(fullname) + sde = read_sde(strdoc) + with open(fullname, 'w') as arquivo: + yaml.safe_dump(sde, arquivo) + return id + + DUMP_FUNCTIONS = { 'File': dump_file, 'Image': dump_file, 'Folder': partial(dump_folder, enum=enumerate_folder), 'BTreeFolder2': partial(dump_folder, enum=enumerate_btree), + 'SDE-Document': dump_sde, # explicitamente ignorados 'ZCatalog': None,