From fc6ffac5a94671770c5cfe78083c6494913e58e5 Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Sat, 23 Oct 2021 13:49:23 +0200 Subject: [PATCH 1/4] Switch argument parsing to argparse --- novel_stats/novel_stats.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index 6f66893..0d8395b 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -4,6 +4,7 @@ import collections import sys import tempfile +import argparse CHAPTER_MARKER = '## ' STATUS_MARKER = '[status]: # ' @@ -25,11 +26,18 @@ def count_words(line): def main(): - arguments = sys.argv[1:] - filename = arguments[0] + # Better argument parsing + parser = argparse.ArgumentParser() + parser.add_argument('-c', '--chapter', action='store_true') + parser.add_argument('-a', '--act', action='store_true') + parser.add_argument('-pp', action='store_true') + parser.add_argument('filename') + arguments = parser.parse_args() + + filename = arguments.filename mdfile = None - if '-pp' in arguments: + if arguments.pp: # -pp flag to allow Markdown Preprocessing primarily to allow multi-file novel formatting # this is implemented using a temporary file created using python's buit-in tempfile library import MarkdownPP @@ -88,7 +96,7 @@ def main(): total_word_count += word_count_by_chapter[chapter_heading] # -c or --chapter to give a chapter-by-chapter word count summary. - if '-c' in arguments or '--chapter' in arguments: + if arguments.chapter: for chapter_heading, chapter_word_count in word_count_by_chapter.items(): if chapter_heading is None: continue @@ -108,15 +116,13 @@ def main(): print() # -a or --act to give an act-by-act word count summary. - if '-a' in arguments or '--act' in arguments: + if arguments.act: for act_heading, act_word_count in word_count_by_act.items(): if act_heading is None: continue print( - 'act {}: {:,} words (~{}%)'.format( - act_heading, act_word_count, act_word_count * 100 // total_word_count - ) + f'act {act_heading}: {act_word_count:,} words (~{act_word_count * 100// total_word_count}%)' ) print() From 5df3a48f35ba39d0a8f4f740e6b8eb457be7bd04 Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Sat, 23 Oct 2021 14:08:12 +0200 Subject: [PATCH 2/4] Cleaned up argument parsing --- novel_stats/novel_stats.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index 0d8395b..5181d06 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -1,10 +1,9 @@ #!/usr/bin/python -import collections -import sys -import tempfile import argparse +import collections +import tempfile CHAPTER_MARKER = '## ' STATUS_MARKER = '[status]: # ' @@ -28,13 +27,30 @@ def count_words(line): def main(): # Better argument parsing parser = argparse.ArgumentParser() - parser.add_argument('-c', '--chapter', action='store_true') - parser.add_argument('-a', '--act', action='store_true') - parser.add_argument('-pp', action='store_true') - parser.add_argument('filename') + parser.add_argument( + '-c', + '--chapter', + action='store_true', + help='output chapter-by-chapter breakdown of word counts, including how many words in each chapter are tagged with which status', + ) + parser.add_argument( + '-a', + '--act', + action='store_true', + help='output act-by-act breakdown of word counts (total only)', + ) + parser.add_argument( + '-pp', + action='store_true', + help='run markdown pre-processor, this allows for a multi-file input (e.g. each chapter in its own file), but requires the MarkdownPP python library', + ) + parser.add_argument( + 'markdown_file', + type=argparse.FileType('r'), + help='The markdown file for the novel, main file if a multi-file novel', + ) arguments = parser.parse_args() - filename = arguments.filename mdfile = None if arguments.pp: @@ -43,10 +59,12 @@ def main(): import MarkdownPP mdfile = tempfile.TemporaryFile(mode='w+') - MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules)) + MarkdownPP.MarkdownPP( + input=arguments.markdown_file, output=mdfile, modules=list(MarkdownPP.modules) + ) mdfile.seek(0) else: - mdfile = open(filename) + mdfile = arguments.markdown_file chapter_heading = None act_heading = None From 1c28b519165cc660eb214b8ddd085d940adbb9af Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Sat, 23 Oct 2021 14:15:45 +0200 Subject: [PATCH 3/4] Cleaned up readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 238a570..4925097 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ novel-stats takes a single argument: The path to your novel file in markdown format. For instance: ```bash -novel-stats /path/to/your/novel.md[pp] [-c/--chapter] [-a/--act] [-pp] +novel-stats [-c/--chapter] [-a/--act] [-pp] /path/to/your/novel.md[pp] ``` ### Optional flags @@ -78,7 +78,7 @@ novel-stats /path/to/your/novel.md[pp] [-c/--chapter] [-a/--act] [-pp] * `-c` or `--chapter` — output chapter-by-chapter breakdown of word counts, including how many words in each chapter are tagged with which status * `-a` or `--act` — output act-by-act breakdown of word counts (total only) -* `--pp` — run markdown pre-processor, this allows for a multi-file input +* `-pp` — run markdown pre-processor, this allows for a multi-file input (e.g. each chapter in its own file), but requires the MarkdownPP python library. From e2935dbddfc9ff91097fd992afed8a07ec226181 Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Sat, 23 Oct 2021 14:22:21 +0200 Subject: [PATCH 4/4] Readme cleanup order of arguments --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4925097..db82bf7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ total: 539 words Example output with chapter data: ```bash -$ novel-stats example.md -c +$ novel-stats -c example.md chapter 1: 103 (drafted) chapter 2: 83 (dev edited) chapter 3: 115 @@ -36,7 +36,7 @@ total: 539 words Example with multi-file markdown: ```bash -$ novel-stats multi_file.mdpp -pp -c -a +$ novel-stats -pp -c -a multi_file.mdpp chapter 1 Lorem: 203 (drafted) 303 (dev edited)