-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_content.py
92 lines (70 loc) · 2.43 KB
/
count_content.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from os import listdir
from pathlib import Path
import click
from sw_utils import get_chapter_number_list, clrScrn
@click.group()
def cli():
"""A command line utility to work with text in a file."""
pass
BASE_DIR = (
Path(__file__).resolve().parent
) # BASE_DIR is the directory in which this python script lies
present_directory_filename_list = listdir()
NOVEL_FILE_NAME = ""
# This is here because we assume this file to be in the same folder as the
# novel folder
for filename in present_directory_filename_list:
if not filename.endswith(".py"):
NOVEL_FILE_NAME = filename
child_directory_filename_list = listdir(f"{BASE_DIR}/{NOVEL_FILE_NAME}/")
def count_words(fileNames: list[str], novelPath: str) -> int:
"""
Return word count of all the files within a directory
NOTE input is list of filenames(str)"""
word_count: int = 0
for filename in fileNames:
with open(f"{novelPath}/{filename}", "r") as file_object:
chapter_content = file_object.read()
word_count += len(chapter_content.split())
return word_count
@cli.command()
@click.option(
"--latest-chapter",
is_flag=True,
flag_value=True,
help="This will show the latest chapter number as well.",
)
@click.option(
"--list-all-chapters",
is_flag=True,
flag_value=True,
help="This will return the list of all the chapters.",
)
def check(latest_chapter: bool = False, list_all_chapters: bool = False):
"""Check wether any chapters are missing."""
chapter_number_list: list[int] = get_chapter_number_list(
child_directory_filename_list
)
n = len(chapter_number_list)
flag: bool = False
possible_missing_chapters: list[str] = []
for i in range(0, n - 1):
if chapter_number_list[i + 1] != chapter_number_list[i] + 1:
flag = True
possible_missing_chapters.append(chapter_number_list[i] + 1)
if not flag:
click.echo("Everythings fine chap.")
else:
click.echo(
f"There seems to be a problem, please check if the following chapters exist:\n{possible_missing_chapters}"
)
if latest_chapter:
click.echo(f"This is the latest chapter number: {chapter_number_list[-1]}")
if list_all_chapters:
click.echo(f"Here is the list of all the chapters:\n{chapter_number_list}")
@cli.command()
def cls():
"""clear screen"""
clrScrn(click.clear)
if __name__ == "__main__":
cli()