From 7dae35e722d672bfc93892403fecee8dc57d5196 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Thu, 9 Aug 2018 20:54:44 +0200 Subject: [PATCH] Adds the statistics command. (Fixes #8) --- pubs/commands/__init__.py | 6 +++--- pubs/commands/statistics_cmd.py | 33 +++++++++++++++++++++++++++++++++ pubs/pubs_cmd.py | 5 +++-- tests/test_usecase.py | 15 +++++++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 pubs/commands/statistics_cmd.py diff --git a/pubs/commands/__init__.py b/pubs/commands/__init__.py index cd17fa3..ed5defa 100644 --- a/pubs/commands/__init__.py +++ b/pubs/commands/__init__.py @@ -6,9 +6,11 @@ from . import add_cmd from . import rename_cmd from . import remove_cmd from . import list_cmd +from . import edit_cmd +from . import tag_cmd +from . import statistics_cmd # doc from . import doc_cmd -from . import tag_cmd from . import note_cmd # bulk from . import export_cmd @@ -16,5 +18,3 @@ from . import import_cmd # bonus from . import websearch_cmd from . import url_cmd - -from . import edit_cmd diff --git a/pubs/commands/statistics_cmd.py b/pubs/commands/statistics_cmd.py new file mode 100644 index 0000000..5132010 --- /dev/null +++ b/pubs/commands/statistics_cmd.py @@ -0,0 +1,33 @@ +from ..repo import Repository +from ..uis import get_ui +from .. import color + + +def parser(subparsers, conf): + parser = subparsers.add_parser( + 'statistics', + help="show statistics on the repository.") + return parser + + +def command(conf, args): + ui = get_ui() + rp = Repository(conf) + papers = list(rp.all_papers()) + + paper_count = len(papers) + doc_count = sum([0 if p.docpath is None else 1 for p in papers]) + tag_count = len(list(rp.get_tags())) + papers_with_tags = sum([0 if p.tags else 1 for p in papers]) + + ui.message(color.dye_out('Repository statistics:', 'bold')) + ui.message('Total papers: {}, {} ({}) have a document attached'.format( + color.dye_out('{:d}'.format(paper_count), 'bgreen'), + color.dye_out('{:d}'.format(doc_count), 'bold'), + '{:.0f}%'.format(100. * doc_count / paper_count), + )) + ui.message('Total tags: {}, {} ({}) of papers have at least one tag'.format( + color.dye_out('{:d}'.format(tag_count), 'bgreen'), + color.dye_out('{:d}'.format(papers_with_tags), 'bold'), + '{:.0f}%'.format(100. * papers_with_tags / paper_count), + )) diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 949fd4a..258cb85 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -20,9 +20,11 @@ CORE_CMDS = collections.OrderedDict([ ('rename', commands.rename_cmd), ('remove', commands.remove_cmd), ('list', commands.list_cmd), + ('edit', commands.edit_cmd), + ('tag', commands.tag_cmd), + ('statistics', commands.statistics_cmd), ('doc', commands.doc_cmd), - ('tag', commands.tag_cmd), ('note', commands.note_cmd), ('export', commands.export_cmd), @@ -30,7 +32,6 @@ CORE_CMDS = collections.OrderedDict([ ('websearch', commands.websearch_cmd), ('url', commands.url_cmd), - ('edit', commands.edit_cmd), ]) diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 43cbf2c..11cce7a 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -925,6 +925,21 @@ class TestUsecase(DataCommandTestCase): self.assertFalse(os.path.isfile(self.default_conf_path)) self.assertTrue(os.path.isfile(alt_conf)) + def test_statistics(self): + cmds = ['pubs init', + 'pubs add data/pagerank.bib', + 'pubs add -d data/turing-mind-1950.pdf data/turing1950.bib', + 'pubs add data/martius.bib', + 'pubs add data/10.1371%2Fjournal.pone.0038236.bib', + 'pubs tag Page99 A+B', + 'pubs tag turing1950computing C', + 'pubs statistics', + ] + out = self.execute_cmds(cmds) + lines = out[-1].splitlines() + self.assertEqual(lines[0], 'Repository statistics:') + self.assertEqual(lines[1], 'Total papers: 4, 1 (25%) have a document attached') + self.assertEqual(lines[2], 'Total tags: 3, 2 (50%) of papers have at least one tag') @ddt.ddt