From 935a32f3ad46cc24169afc635ffcd30ad00d5a98 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 22 Mar 2018 21:58:30 +0300 Subject: [PATCH] Feat(ml-import): Display tty progress bar during import --- README.txt | 2 +- docs-ru/command_line.rst | 3 ++- docs-ru/index.rst | 2 +- docs/command_line.rst | 3 ++- docs/index.rst | 2 +- docs/install.rst | 8 ++++++++ docs/news.rst | 12 +++++++++--- m_librarian/__version__.py | 2 +- m_librarian/inp.py | 15 ++++++++++++++- m_librarian/pbar.py | 25 +++++++++++++++++++++++++ scripts/ml-import.py | 20 +++++++++++++++++++- setup.py | 4 ++++ 12 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 m_librarian/pbar.py diff --git a/README.txt b/README.txt index be6364d..fffa1d7 100644 --- a/README.txt +++ b/README.txt @@ -1,6 +1,6 @@ m_Librarian. Author: Oleg Broytman . -Copyright (C) 2015-2017 PhiloSoft Design. +Copyright (C) 2015-2018 PhiloSoft Design. License: GPL. This hopefully will be a collection of programs to index LibRusEc and diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index 70cd7cf..c646cde 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -29,12 +29,13 @@ ml-import.py Использование:: - ml-import.py [-C] [-D] [file.inpx ...] + ml-import.py [-C] [-D] [-P] [file.inpx ...] Опции:: -C, --config config Путь к файлу конфигурации -D, --database database Использовать указанную БД + -P, --no-pbar Не показывать индикатор процесса Инициализирует базу данных, импортирует список жанров и список файлов INPX, перечисленных в командной строке. При повторных запусках не diff --git a/docs-ru/index.rst b/docs-ru/index.rst index a632b1d..6d6f993 100644 --- a/docs-ru/index.rst +++ b/docs-ru/index.rst @@ -34,7 +34,7 @@ Credits Автор: Олег Бройтман . -Copyright (C) 2015-2017 PhiloSoft Design. +Copyright (C) 2015-2018 PhiloSoft Design. License diff --git a/docs/command_line.rst b/docs/command_line.rst index 6258443..56824a9 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -29,12 +29,13 @@ ml-import.py Usage:: - ml-import.py [-C] [-D] [file.inpx ...] + ml-import.py [-C] [-D] [-P] [file.inpx ...] Options:: -C, --config config Configuration file -D, --database database Open this database by pathname/db uri + -P, --no-pbar Inhibit progress bar Initialize database, import genres list and import a list of INPX files listed in the command line. On subsequent runs doesn’t destroy DB or diff --git a/docs/index.rst b/docs/index.rst index bbfa794..4dc8750 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -35,7 +35,7 @@ Credits Created by Oleg Broytman . -Copyright (C) 2015-2017 PhiloSoft Design. +Copyright (C) 2015-2018 PhiloSoft Design. License diff --git a/docs/install.rst b/docs/install.rst index 0bcfde0..d950de1 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -22,6 +22,14 @@ Virtual envs pip install --trusted-host phdru.name --find-links=http://phdru.name/Software/Python/ m_librarian +Progress bar +============ + +To allow ``ml-import.py`` to display progress bar the program requires +library ``m_lib``. You can install the library separately using, e.g., +command ``pip install m_lib``. Or you can install the library with +``m_librarian``: ``pip install m_librarian[pbar]``. + Installation from sources ========================= diff --git a/docs/news.rst b/docs/news.rst index 13a4254..3e73f9a 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -1,6 +1,12 @@ News ==== +Version 0.0.16 (2018-03-22) +--------------------------- + +* Script ``ml-import.py`` displays progress bar. + Option ``-P`` prevents it. + Version 0.0.15 (2017-04-26) --------------------------- @@ -58,18 +64,18 @@ Version 0.0.8 (2016-05-29) Version 0.0.7 (2016-05-25) --------------------------- -* Extend ml-serach.py to search books by authors, extensions, genres, +* Extend ml-search.py to search books by authors, extensions, genres, * languages. Version 0.0.6 (2016-05-21) --------------------------- -* Extend ml-serach.py to search books by title, series, archive, file. +* Extend ml-search.py to search books by title, series, archive, file. Version 0.0.5 (2016-05-14) --------------------------- -* Extend script ml-serach.py to search extensions/genres/languages. +* Extend script ml-search.py to search extensions/genres/languages. Version 0.0.4 (2016-05-11) --------------------------- diff --git a/m_librarian/__version__.py b/m_librarian/__version__.py index 8ecd910..19c6940 100644 --- a/m_librarian/__version__.py +++ b/m_librarian/__version__.py @@ -1 +1 @@ -__version__ = '0.0.15' +__version__ = '0.0.16' diff --git a/m_librarian/inp.py b/m_librarian/inp.py index b3a3d84..76ab97b 100644 --- a/m_librarian/inp.py +++ b/m_librarian/inp.py @@ -80,12 +80,23 @@ def import_inp(archive, inp): import_inp_line(archive, parts) -def import_inpx(path): +def import_inpx(path, pbar_cb=None): inpx = ZipFile(path) + if pbar_cb: + inp_count = 0 + for name in inpx.namelist(): + ext = os.path.splitext(name)[1] + if ext == '.inp': + inp_count += 1 + pbar_cb.set_max(inp_count) + inp_count = 0 for name in inpx.namelist(): archive, ext = os.path.splitext(name) if ext != '.inp': continue + if pbar_cb: + inp_count += 1 + pbar_cb.display(inp_count) inp = inpx.open(name) sqlhub.doInTransaction(import_inp, archive + '.zip', inp) inp.close() @@ -95,3 +106,5 @@ def import_inpx(path): connection.query("VACUUM %s" % table.sqlmeta.table) elif connection.dbName == 'sqlite': connection.query("VACUUM") + if pbar_cb: + pbar_cb.close() diff --git a/m_librarian/pbar.py b/m_librarian/pbar.py new file mode 100644 index 0000000..7d3a6fa --- /dev/null +++ b/m_librarian/pbar.py @@ -0,0 +1,25 @@ + +try: + from m_lib.pbar.tty_pbar import ttyProgressBar +except ImportError: + ttyProgressBar = None + +if ttyProgressBar: + class ml_ttyProgressBar(object): + def __init__(self, width=20): + self.max = None + self.pbar = None + self.width = width + + def set_max(self, max_value): + self.max = max_value + self.pbar = ttyProgressBar(0, max_value, width1=self.width) + + def display(self, value): + if self.pbar: + self.pbar.display(value) + + def close(self): + if self.pbar: + self.pbar.erase() + self.pbar = None diff --git a/scripts/ml-import.py b/scripts/ml-import.py index 3ccbd31..56ff8a6 100755 --- a/scripts/ml-import.py +++ b/scripts/ml-import.py @@ -1,15 +1,23 @@ #! /usr/bin/env python +from __future__ import print_function import argparse +import sys + from m_librarian.config import get_config from m_librarian.db import open_db, init_db, update_counters from m_librarian.glst import import_glst from m_librarian.inp import import_inpx +from m_librarian.pbar import ttyProgressBar +if ttyProgressBar: + from m_librarian.pbar import ml_ttyProgressBar if __name__ == '__main__': parser = argparse.ArgumentParser(description='Import') parser.add_argument('-C', '--config', help='configuration file') parser.add_argument('-D', '--database', help='database URI') + parser.add_argument('-P', '--no-pbar', action='store_true', + help='inhibit progress bar') parser.add_argument('inpx', nargs='+', help='INPX files to import') args = parser.parse_args() @@ -20,5 +28,15 @@ if __name__ == '__main__': init_db() import_glst() for inpx in args.inpx: - import_inpx(inpx) + if ttyProgressBar and not args.no_pbar: + if len(inpx) > 25: + pbar_fname = inpx[:22] + '...' + else: + pbar_fname = inpx + print(pbar_fname.ljust(20), end=': ') + sys.stdout.flush() + pbar = ml_ttyProgressBar() + import_inpx(inpx, pbar_cb=pbar) + else: + import_inpx(inpx) update_counters() diff --git a/setup.py b/setup.py index 0c4c5ae..1d8ebdb 100755 --- a/setup.py +++ b/setup.py @@ -13,6 +13,10 @@ except ImportError: kw = {} if is_setuptools: kw['python_requires'] = '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*' + kw['extras_require'] = { + 'm_lib': ['m_lib>=3.1'], + 'pbar': ['m_lib>=3.1'], + } versionpath = join(abspath(dirname(__file__)), 'm_librarian', '__version__.py') m_librarian_version = load_source('m_librarian_version', versionpath) -- 2.39.5