def normalizeJSON(lib):
  un_multi(lib)

  lib.pop('config', None)
  lib.pop('keymanager', None)
  lib.pop('cache', None)

  itemIDs = {}
  for itemID, item in enumerate(lib['items']):
    itemIDs[item['itemID']] = itemID
    item['itemID'] = itemID

    item.pop('dateAdded', None)
    item.pop('dateModified', None)
    item.pop('uniqueFields', None)
    item.pop('key', None)
    item.pop('citekey', None)
    item.pop('attachments', None)
    item.pop('collections', None)
    item.pop('__citekey__', None)
    item.pop('uri', None)

    item['notes'] = sorted([html2md(note if type(note) == str else note['note']) for note in item.get('notes', [])])

    if 'note' in item: item['note']  = html2md(item['note'])

    item['tags'] = sorted([(tag if type(tag) == str else tag['tag']) for tag in item.get('tags', [])])

    for k in list(item.keys()):
      v = item[k]
      if v is None: del item[k]
      if type(v) in [list, dict] and len(v) == 0: del item[k]

  collections = lib.get('collections', {})
  while any(coll for coll in collections.values() if not coll.get('path', None)):
    for coll in collections.values():
      if coll.get('path', None): continue

      if not coll.get('parent', None):
        coll['path'] = [ coll['name'] ]
      elif collections[ coll['parent'] ].get('path', None):
        coll['path'] = collections[ coll['parent'] ]['path'] + [ coll['name'] ]

  for key, coll in collections.items():
    coll['key'] = ' ::: '.join(coll['path'])
    coll.pop('path', None)
    coll.pop('id', None)

  for key, coll in collections.items():
    if coll['parent']: coll['parent'] = collections[coll['parent']]['key']
    coll['collections'] = [collections[key]['key'] for key in coll['collections']]
    coll['items'] = [itemIDs[itemID] for itemID in coll['items']]

  lib['collections'] = {coll['key']: coll for coll in collections.values()}

  return strip_obj(lib)
Exemple #2
0
def clean_item(item):
    item = deepcopy(item)

    un_multi(item)

    item.pop('itemID', None)
    item.pop('version', None)
    item.pop('libraryID', None)
    item.pop('dateAdded', None)
    item.pop('dateModified', None)
    item.pop('uniqueFields', None)
    item.pop('key', None)
    item.pop('citekey', None)
    item.pop('collections', None)
    item.pop('__citekey__', None)
    item.pop('citationKey', None)
    item.pop('uri', None)

    item.pop('attachments', None)  # I'll need to get around to this eventually

    # make diffs more readable
    if 'extra' in item and type(item['extra']) != list:
        item['extra'] = item['extra'].split('\n')

    if 'notes' in item:
        item['notes'] = sorted(
            strip_obj([
                html2md(unnest(note, 'note'))
                for note in item.get('notes', [])
            ]))
        item['notes'] = [note.split('\n') for note in item['notes']]

    if 'note' in item:
        item['note'] = html2md(item['note']).split('\n')

    if 'tags' in item:
        item['tags'] = sorted(
            strip_obj(
                [html2md(unnest(tag, 'tag')) for tag in item.get('tags', [])]))

    if 'creators' in item:
        item['creators'] = strip_obj(item['creators'])

    item = strip_obj(item)
    return item
Exemple #3
0
def clean_item(item):
    item = deepcopy(item)

    un_multi(item)

    item.pop('itemID', None)
    item.pop('version', None)
    item.pop('libraryID', None)
    item.pop('dateAdded', None)
    item.pop('dateModified', None)
    item.pop('uniqueFields', None)
    item.pop('key', None)
    item.pop('citekey', None)
    item.pop('collections', None)
    item.pop('__citekey__', None)
    item.pop('citationKey', None)
    item.pop('uri', None)

    item.pop('attachments', None)  # I'll need to get around to this eventually

    if 'notes' in item:
        item['notes'] = sorted(
            strip_obj([
                html2md(unnest(note, 'note'))
                for note in item.get('notes', [])
            ]))

    if 'note' in item:
        item['note'] = html2md(item['note'])

    if 'tags' in item:
        item['tags'] = sorted(
            strip_obj(
                [html2md(unnest(tag, 'tag')) for tag in item.get('tags', [])]))

    if 'creators' in item:
        item['creators'] = strip_obj(item['creators'])

    return strip_obj(item)
Exemple #4
0
def normalizeJSON(lib):
    un_multi(lib)

    lib.pop('config', None)
    lib.pop('keymanager', None)
    lib.pop('cache', None)

    itemIDs = {}
    for itemID, item in enumerate(lib['items']):
        itemIDs[item['itemID']] = itemID
        item['itemID'] = itemID

        item.pop('dateAdded', None)
        item.pop('dateModified', None)
        item.pop('uniqueFields', None)
        item.pop('key', None)
        item.pop('citekey', None)
        item.pop('attachments', None)
        item.pop('collections', None)
        item.pop('__citekey__', None)
        item.pop('uri', None)

        item['notes'] = sorted([
            html2md(note if type(note) == str else note['note'])
            for note in item.get('notes', [])
        ])

        if 'note' in item: item['note'] = html2md(item['note'])

        item['tags'] = sorted([(tag if type(tag) == str else tag['tag'])
                               for tag in item.get('tags', [])])

        for k in list(item.keys()):
            v = item[k]
            if v is None: del item[k]
            if type(v) in [list, dict] and len(v) == 0: del item[k]

    collections = lib.get('collections', {})
    while any(coll for coll in collections.values()
              if not coll.get('path', None)):
        for coll in collections.values():
            if coll.get('path', None): continue

            if not coll.get('parent', None):
                coll['path'] = [coll['name']]
            elif collections[coll['parent']].get('path', None):
                coll['path'] = collections[coll['parent']]['path'] + [
                    coll['name']
                ]

    for key, coll in collections.items():
        coll['key'] = ' ::: '.join(coll['path'])
        coll.pop('path', None)
        coll.pop('id', None)

    for key, coll in collections.items():
        if coll['parent']: coll['parent'] = collections[coll['parent']]['key']
        coll['collections'] = [
            collections[key]['key'] for key in coll['collections']
        ]
        coll['items'] = [itemIDs[itemID] for itemID in coll['items']]

    lib['collections'] = {coll['key']: coll for coll in collections.values()}

    return strip_obj(lib)