示例#1
0
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)
示例#2
0
 def cards(self):
     yield from read_anki_table(self.conn, 'cards')
示例#3
0
 def notes(self):
     yield from read_anki_table(self.conn, 'notes')
示例#4
0
文件: app.py 项目: fenildf/AnkiTools
    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
示例#5
0
文件: app.py 项目: fenildf/AnkiTools
    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