Merge pull request #160 from pubs/feat/stats
Adds the statistics command. (Fixes #8)
This commit is contained in:
commit
de7644f90a
@ -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
|
||||
|
33
pubs/commands/statistics_cmd.py
Normal file
33
pubs/commands/statistics_cmd.py
Normal file
@ -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),
|
||||
))
|
@ -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),
|
||||
])
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user