Skip to content

Commit 835c39f

Browse files
committed
Add proper a logging system
1 parent 643b76a commit 835c39f

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

remove_events_from_tt.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import argparse
2+
import logging
3+
import ntpath
24
import sys
35

6+
from fet_api_to_gcal.common.utils import timestamped_filename
7+
from fet_api_to_gcal.common.utils import path_leaf
8+
49

510
def main(filepath, event_names):
611
"""Remove all events inside of event_names list parameter from the FET generated timetable csv file.
@@ -9,28 +14,51 @@ def main(filepath, event_names):
914
filepath (str): path for the FET generated timetable csv file.
1015
event_names (list): a list containing the event names (string values) to be deleted.
1116
"""
12-
f = open(filepath, "r")
13-
data = f.readlines()
1417

15-
# open a file for the resulting filtered file
18+
log_filename = timestamped_filename(filename=path_leaf(filepath))
19+
20+
logging.basicConfig(level=logging.DEBUG,
21+
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
22+
datefmt='%m-%d %H:%M:%S',
23+
filename="./logs/{}.log".format(log_filename),
24+
filemode='w')
25+
26+
console = logging.StreamHandler()
27+
console.setLevel(logging.WARNING)
28+
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
29+
console.setFormatter(formatter)
30+
logging.getLogger('').addHandler(console)
31+
32+
logging.info("[?] Opening file {}".format(path_leaf(filepath)))
33+
try:
34+
f = open(filepath, "r")
35+
data = f.readlines()
36+
except Exception as e:
37+
logging.exception("[!] Exception while opening file {}".format(filepath))
38+
logging.error("[!] Exiting script")
39+
sys.exit(1)
40+
# ? open a file for the resulting filtered file
1641

1742
try:
1843
result_file = open(
1944
"{}_REMOVED_{}".format(filepath, "".join(event_names)), "w")
2045
except Exception as e:
21-
print("Exception while opening result file: {}".format(e))
46+
logging.error("[!] Exception while opening result file: {}".format(e))
2247
sys.exit(1)
2348

2449
# write the first line of the csv containing columns
50+
logging.info("[?] started writing data to result file.")
2551
result_file.write(data[0])
26-
52+
counter = 0
2753
for line in data[1:]:
2854
event__name__ = line.split(",")[4].replace('"', '')
2955
if event__name__ in event_names:
56+
counter += 1
3057
continue
3158
else:
3259
result_file.write(line)
33-
60+
logging.info("[+] Finished writing to result file")
61+
logging.info("Removed {} event records from original file".format(counter))
3462

3563
if __name__ == "__main__":
3664
# create an arguemnt parser
@@ -46,9 +74,11 @@ def main(filepath, event_names):
4674
"--event",
4775
dest="event_names",
4876
action='append',
49-
default=[])
77+
default=[],
78+
help="event name to be appended to the list of event names")
5079

5180
# Parse arguments
81+
5282
args = parser.parse_args()
5383
# call the main function
5484
main(args.file, args.event_names)

0 commit comments

Comments
 (0)