def create_triple(collections, docid): triples = [] for key, value in collections.items(): if type(value) != list: value = [value] triples.append((key, docid, *value)) return triples
def load(self, collections): """ Load the keys from a mapping of collections """ for name, content in collections.items(): collection = self(name) collection.update(content) collection.save()
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('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('attachments', None) item.pop('collections', None) item.pop('__citekey__', None) item.pop('citationKey', 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)
def create_triple(self, collections, docid): triples = [] for key, value in collections.items(): triples.append((key, docid, value)) return triples
def create_collections(self, collections): """Create collections.""" for name, collection in collections.items(): order_by = collection.get('order_by', []) self.__collections[name] = Collection(self, collection['filter'], order_by)