-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
138 lines (117 loc) · 4.11 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import datetime
import os.path
from argparse import ArgumentParser
from log_table import LogTable
from log_parser import LogParser
# TODO Put all the text parameters in another file (help customisation and
# later internationnalization).
VERBOSE = False
class ErrorFormatDate(Exception):
pass
def get_date_parser_loaded():
try:
import dateparser
global dateparser
except ImportError:
print(
"# INFO : Module dateparser is not installed, run : 'pip install dateparser', "
"or pipenv|conda install dateparser if this is what you are using. \n"
f"A log.{get_args().type} with the date of today ({datetime.date.today()}) "
"will still be created in case this is what you are looking for.")
return False
return True
def get_args():
parser = ArgumentParser()
parser.add_argument(
"date",
nargs='?',
default="",
help='Set your own starting date : 22/12/2019, yesterday, or\
"20 of december 2019" in any language. Default is today')
parser.add_argument(
"-o",
"--overwrite",
help='overwrite the existing log.md file',
action="store_true")
parser.add_argument(
"-f",
"--filename",
help='choose your own filename output',
type=str,
default="log")
parser.add_argument(
"-l",
"--list",
help='append this log to an existing list of logs',
action="store_true")
parser.add_argument(
"-d",
"--duration",
help='set a custom duration for your challenge',
default=100,
type=int)
parser.add_argument(
"-t",
"--type",
help='generate your log in html format',
default="md",
type=str)
parser.add_argument("-v", "--verbose", help='print process infos',
action="store_true")
parser.add_argument("-u", "--update", help='update an existing log file',
action="store_true")
args = parser.parse_args()
return args
def get_start_day(args, isDateparserLoaded):
if args.date and isDateparserLoaded:
start_day = dateparser.parse(args.date)
if not start_day:
raise(ErrorFormatDate("Date format not recognized..."))
else:
start_day = datetime.date.today()
return start_day
def get_create_string(args, filename):
if os.path.isfile(filename) and args.overwrite:
create_string = "w"
else:
create_string = "x"
return create_string
def get_filename(args):
if len(args.filename) > 3 and args.filename[-3:] == args.type:
filename = args.filename
else:
filename = args.filename + "." + args.type
return filename
def get_verbosity(args):
return args.verbose
def print_message(args, start_day, duration, filename, create_string, filetype):
print(f"main.py called with args: {args}")
print(f"Writing log with start_day: {start_day} and duration: {duration} days "\
+ f"to filename '{filename}' with file writing options '{create_string}''")
def write_log_table(filename, create_string, start_day, duration, filetype):
with open(filename, create_string) as f:
log_table = LogTable(start_day, days=duration, filetype=filetype)
f.write(log_table.get_intro())
f.write(log_table.get_string_table())
f.write(log_table.get_blank_diary())
f.write(f"\n\n\n# Conclusion\n\n")
if VERBOSE:
print(f"Successfully wrote log file to {filename}")
def main():
args = get_args()
VERBOSE = get_verbosity(args)
DPLoaded = get_date_parser_loaded()
start_day = get_start_day(args, DPLoaded)
duration = args.duration
filename = get_filename(args)
create_string = get_create_string(args, filename)
filetype = args.type
if VERBOSE:
print_message(args, start_day, duration, filename, create_string, filetype)
if args.update:
parser = LogParser(filename)
parser.update_log()
else:
write_log_table(filename, create_string, start_day, duration, filetype)
if __name__ == '__main__':
main()