def __init__(self, collection): if collection is None: collection = get_collection_path() self.collection_path = next(tempfile._get_candidate_names()) shutil.copy(src=collection, dst=self.collection_path) self.ankidirect = AnkiDirect(anki_database=self.collection_path)
def __init__(self, anki_database: str = None): if anki_database is None: anki_database = get_collection_path() try: assert 'Anki' not in (p.name() for p in psutil.process_iter()), \ "Please close Anki first before accessing Application Data collection.anki2 directly." except psutil.ZombieProcess as e: print(e) do_init = False if not os.path.exists(anki_database): do_init = True self.conn = sqlite3.connect(anki_database) if do_init: self.creator = AnkiContentCreator() write_anki_schema(self.conn) anki_collection = self.creator.new_collection() write_anki_table(self.conn, 'col', [anki_collection], do_commit=True) self._id_to_record = self.data else: self._id_to_record = self.data self.creator = AnkiContentCreator(self._id_to_record) self._name_to_id = self.name_to_id self.verify = AnkiContentVerify(self._id_to_record)
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)
import shutil from AnkiTools.tools.path import get_collection_path if __name__ == '__main__': shutil.copy2(src=get_collection_path(), dst='../tests/collection.anki2')
def get_file(): axsync = ObsoleteAnkiExcelSync('formatting.xlsx', get_collection_path()) axsync.save()
def save_data(): axsync = AnkiExcelSync('test.xlsx', get_collection_path()) axsync.save(formatted=True)
def get_data(): axsync = AnkiExcelSync('test.xlsx', get_collection_path()) with open('AnkiExcelSync.excel_raw.json', 'w') as f: json.dump(axsync.excel_raw, f, ensure_ascii=False, indent=2)
import os from AnkiTools.tools.path import get_collection_path if __name__ == '__main__': os.remove(get_collection_path()) print('Open Anki, and it will auto-generate the database')