1
+ #!/usr/bin/env python
2
+ """
3
+ This file is dedicated to obtain a .csv record report for Github Data.
4
+ """
5
+
1
6
# Standard library
2
7
import os
3
- import os .path
4
8
import sys
5
9
import traceback
6
10
13
17
# First-party/Local
14
18
import quantify # noqa: E402
15
19
16
- PATH_REPO_ROOT , PATH_WORK_DIR , PATH_DOTENV , DATETIME_TODAY = quantify .setup (
20
+ # Setup paths, Date and LOGGER using quantify.setup()
21
+ PATH_REPO_ROOT , PATH_WORK_DIR , _ , DATETIME_TODAY , LOGGER = quantify .setup (
17
22
__file__
18
23
)
19
24
25
+ # Set up file path for CSV report
20
26
DATA_WRITE_FILE = os .path .join (
21
27
PATH_WORK_DIR ,
22
- f "data_github_"
28
+ "data_github_"
23
29
f"{ DATETIME_TODAY .year } _{ DATETIME_TODAY .month } _{ DATETIME_TODAY .day } .csv" ,
24
30
)
25
31
32
+ # Log the start of the script execution
33
+ LOGGER .info ("Script execution started." )
34
+
26
35
27
36
def set_up_data_file ():
28
37
"""Writes the header row of the data file."""
@@ -43,6 +52,7 @@ def get_response_elems(license):
43
52
dict: A dictionary mapping metadata to its value provided from the API
44
53
query of specified parameters.
45
54
"""
55
+ LOGGER .info ("Providing the metadata for query of specified License" )
46
56
try :
47
57
base_url = "https://api.github.com/search/repositories?q=license:"
48
58
request_url = f"{ base_url } { license } "
@@ -57,8 +67,15 @@ def get_response_elems(license):
57
67
response .raise_for_status ()
58
68
search_data = response .json ()
59
69
return {"totalResults" : search_data ["total_count" ]}
60
- except Exception as e :
61
- raise e
70
+ except requests .HTTPError as e :
71
+ LOGGER .error (f"HTTP Error: { e } " )
72
+ raise
73
+ except requests .RequestException as e :
74
+ LOGGER .error (f"Request Exception: { e } " )
75
+ raise
76
+ except KeyError as e :
77
+ LOGGER .error (f"KeyError: { e } . Search data is: { search_data } " )
78
+ raise
62
79
63
80
64
81
def record_license_data (license_type ):
@@ -96,11 +113,11 @@ def main():
96
113
try :
97
114
main ()
98
115
except SystemExit as e :
116
+ LOGGER .error (f"System exit with code: { e .code } " )
99
117
sys .exit (e .code )
100
118
except KeyboardInterrupt :
101
- print ( "INFO (130) Halted via KeyboardInterrupt.", file = sys . stderr )
119
+ LOGGER . info ( " (130) Halted via KeyboardInterrupt." )
102
120
sys .exit (130 )
103
121
except Exception :
104
- print ("ERROR (1) Unhandled exception:" , file = sys .stderr )
105
- print (traceback .print_exc (), file = sys .stderr )
122
+ LOGGER .exception (f"(1) Unhandled exception: { traceback .format_exc ()} " )
106
123
sys .exit (1 )
0 commit comments