def get_anki_defaults(file_output=None, formatted=False): """ :param None|bool|str file_output: :param bool formatted: :return: """ if formatted: default_filename = 'defaults_formatted.json' else: default_filename = 'defaults.json' file_output = { None: None, False: None, True: module_path(default_filename) }.get(file_output, file_output) defaults = OrderedDict() with sqlite3.connect(get_collection_path()) as conn: cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table';") for row in cursor: table_name = row[0] try: defaults[table_name] = next(read_anki_table(conn, table_name)) except StopIteration: defaults[table_name] = None continue if formatted is not None: for k, v in defaults[table_name].items(): if formatted is True: if isinstance(v, str): try: defaults[table_name][k] = { 'is_json': True, 'data': json.loads(v, object_pairs_hook=OrderedDict) } except JSONDecodeError: pass if file_output is None: print(json.dumps(defaults, indent=2)) else: with open(file_output, 'w') as f: json.dump(defaults, f, indent=2)
def cards(self): yield from read_anki_table(self.conn, 'cards')
def notes(self): yield from read_anki_table(self.conn, 'notes')
def get_cards(self): card_dict = dict() for card in read_anki_table(self.conn, 'cards'): card_dict[str(card['id'])] = card return card_dict
def get_notes(self): note_dict = dict() for note in read_anki_table(self.conn, 'notes'): note_dict[str(note['id'])] = note return note_dict