def import_dic(parser, delete=True): # TODO: support for different dictionaries db.session.execute('SET FOREIGN_KEY_CHECKS=0;') if delete: # currently works only with delete because of integrity db.session.query(Word).delete() db.session.query(WordStat).delete() db.session.query(Source).delete() db.session.execute(sources_tags.delete()) db.session.commit() log('Dictionary import started') print('import_dic: Dictionary import started...') try: parser.parse() print('import_dic: inserting words') with db.engine.begin() as connection: connection.execute(Word.__table__.insert(). values(parser.words()).prefix_with("IGNORE")) db.session.commit() for batch in batch_prepare(parser.translations()): with db.engine.begin() as connection: connection.execute(En_Rus.__table__.insert(). values(batch).prefix_with("IGNORE")) db.session.commit() print('Dictionary imported successfully') except Exception as E: log_error(str(E)) raise E
if __name__ == '__main__': info = input( 'Path to txt-file with list of files-titles-tags: ').replace('"', '') parser = Parser(info) err = parser.get_errors() if err: if input('There are errors:\n' '%s\nProceed? (y\\n)' % err).lower() == 'n': sys.exit() for i, (source_path, title, author, year, *tags) in enumerate(parser): try: dest = generate_filename(app.config['SOURCE_DIR'], path.split(source_path)[1]) print('fill_db: Started processing' ' file ({} of {})'.format(i + 1, len(parser))) log('fill_db: Processing ' + source_path) source = create_source(title, author, year, tags, path.split(dest)[1]) copyfile(source_path, dest) process(source) print('fill_db: done') print('*' * 15) except SourceExistsException: print('Source "{}" already exists. Skipping'.format( title.encode("ascii", "ignore"))) log_warning('Source "{}" already exists. Skipping'.format( title)) except Exception as E: