From e3a47d74252c8dfd5e20317b6782c2b72b7cf313 Mon Sep 17 00:00:00 2001 From: Dmytro Yeroshkin Date: Fri, 22 Oct 2021 14:28:13 +0200 Subject: [PATCH] Commented my changes + swapped to tempfile library --- novel_stats/novel_stats.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/novel_stats/novel_stats.py b/novel_stats/novel_stats.py index 9bae732..326df3c 100755 --- a/novel_stats/novel_stats.py +++ b/novel_stats/novel_stats.py @@ -3,13 +3,12 @@ import collections import sys -import os CHAPTER_MARKER = '## ' STATUS_MARKER = '[status]: # ' ACT_MARKER = '[act]: # ' -COMMENT_MARKER = '[//]: # ' +COMMENT_MARKER = '[//]: # ' # Strandard markdown comment marker, supported by pandoc and calibre's ebook-convert def count_words(line): @@ -27,13 +26,17 @@ def count_words(line): def main(): arguments = sys.argv[1:] filename = arguments[0] - tmpfilename = None + mdfile = None if '-pp' in arguments: - import MarkdownPP - tmpfilename = f'.tmp-{os.getpid}.md' - MarkdownPP.MarkdownPP(input=open(filename), output=open(tmpfilename,'w'), modules=list(MarkdownPP.modules)) - filename = tmpfilename + # -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, tempfile + mdfile = tempfile.TemporaryFile(mode='w+') + MarkdownPP.MarkdownPP(input=open(filename), output=mdfile, modules=list(MarkdownPP.modules)) + mdfile.seek(0) + else: + mdfile = open(filename) chapter_heading = None act_heading = None @@ -44,7 +47,7 @@ def main(): status_by_chapter = {} current_status = None - for line in open(filename).readlines(): + for line in mdfile.readlines(): if line.startswith(CHAPTER_MARKER): word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading] total_word_count += word_count_by_chapter[chapter_heading] @@ -55,7 +58,7 @@ def main(): status_by_chapter[chapter_heading] = collections.defaultdict(int) current_status = None - elif line.startswith(STATUS_MARKER): + elif line.startswith(STATUS_MARKER): # Modified to allow multiple statuses in a single chapter, can swap back and forth. if current_status == None: current_status = line[len(STATUS_MARKER):].strip('()\n') status_by_chapter[chapter_heading][current_status] = count_words(chapter_heading) @@ -65,7 +68,7 @@ def main(): elif line.startswith(ACT_MARKER): act_heading = line[len(ACT_MARKER):].strip('()\n') word_count_by_act[act_heading] = count_words(act_heading) - elif line.startswith(COMMENT_MARKER): + elif line.startswith(COMMENT_MARKER): # don't count the words in a comment pass else: line_word_count = count_words(line) @@ -79,8 +82,7 @@ def main(): word_count_by_act[act_heading] += word_count_by_chapter[chapter_heading] total_word_count += word_count_by_chapter[chapter_heading] - if '-c' in arguments or '--chapter' in arguments: - # Print out word counts. + if '-c' in arguments or '--chapter' in arguments: # -c or --chapter to give a chapter-by-chapter word count summary for chapter_heading, chapter_word_count in word_count_by_chapter.items(): if chapter_heading is None: continue @@ -99,7 +101,7 @@ def main(): print() - if '-a' in arguments or '--act' in arguments: + if '-a' in arguments or '--act' in arguments: # -a or --act to give an act-by-act word count summary for act_heading, act_word_count in word_count_by_act.items(): if act_heading is None: continue @@ -109,12 +111,11 @@ def main(): print() for status, status_word_count in word_count_by_status.items(): - print('{}: {:,} words (~{}%)'.format(status, status_word_count, status_word_count * 100 // total_word_count)) + print(f'{status}: {status_word_count:,} words (~{status_word_count * 100 // total_word_count}%)') - print('total: {:,} words'.format(total_word_count)) + print(f'total: {total_word_count:,} words') - if tmpfilename: - os.remove(tmpfilename) + mdfile.close() if __name__ == '__main__':