Skip to content

Commit fb29e35

Browse files
committed
Made reviw changes
1 parent dc59b8c commit fb29e35

File tree

4 files changed

+16
-31
lines changed

4 files changed

+16
-31
lines changed

scripts/1-fetch/gcs_fetch.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,14 @@ def get_search_service():
9999
)
100100

101101

102-
def initialize_data_file(file_path, header):
103-
if not os.path.isfile(file_path):
104-
with open(file_path, "w", encoding="utf-8", newline="\n") as file_obj:
105-
writer = csv.DictWriter(
106-
file_obj, fieldnames=header, dialect="unix"
107-
)
108-
writer.writeheader()
109-
110-
111102
def initialize_all_data_files(args):
112-
if not args.enable_save:
113-
return
114-
115-
# Create data directory for this phase
116-
os.makedirs(PATHS["data_phase"], exist_ok=True)
117-
118-
initialize_data_file(FILE1_COUNT, HEADER1_COUNT)
119-
initialize_data_file(FILE2_LANGUAGE, HEADER2_LANGUAGE)
120-
initialize_data_file(FILE3_COUNTRY, HEADER3_COUNTRY)
103+
for file_path, header in [
104+
(FILE1_COUNT, HEADER1_COUNT),
105+
(FILE2_LANGUAGE, HEADER2_LANGUAGE),
106+
(FILE3_COUNTRY, HEADER3_COUNTRY),
107+
]:
108+
if not os.path.isfile(file_path):
109+
shared.rows_to_csv(args, file_path, header, [])
121110

122111

123112
def get_last_completed_plan_index():
@@ -150,8 +139,6 @@ def load_plan():
150139

151140

152141
def append_data(args, plan_row, index, count):
153-
if not args.enable_save:
154-
return
155142
if plan_row["COUNTRY"]:
156143
file_path = FILE3_COUNTRY
157144
fieldnames = HEADER3_COUNTRY
@@ -178,11 +165,7 @@ def append_data(args, plan_row, index, count):
178165
"TOOL_IDENTIFIER": plan_row["TOOL_IDENTIFIER"],
179166
"COUNT": count,
180167
}
181-
with open(file_path, "a", encoding="utf-8", newline="\n") as file_obj:
182-
writer = csv.DictWriter(
183-
file_obj, fieldnames=fieldnames, dialect="unix"
184-
)
185-
writer.writerow(row)
168+
shared.rows_to_csv(args, file_path, fieldnames, [row], append=True)
186169

187170

188171
def query_gcs(args, service, last_completed_plan_index, plan):

scripts/1-fetch/github_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def parse_arguments():
6868

6969
def check_for_completion():
7070
try:
71-
with open(FILE_COUNT, "r", newline="") as file_obj:
71+
with open(FILE_COUNT, "r", encoding="utf-8") as file_obj:
7272
reader = csv.DictReader(file_obj, dialect="unix")
7373
if len(list(reader)) == len(GITHUB_TOOLS):
7474
raise shared.QuantifyingException(

scripts/1-fetch/smithsonian_fetch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ def check_for_completion():
7474
completed_units = False
7575

7676
try:
77-
with open(FILE_1_METRICS, "r", newline="") as file_obj:
77+
with open(FILE_1_METRICS, "r", encoding="utf-8") as file_obj:
7878
reader = csv.DictReader(file_obj, dialect="unix")
7979
if len(list(reader)) > 0:
8080
completed_metrics = True
8181
except FileNotFoundError:
8282
pass # File may not be found without --enable-save, etc.
8383

8484
try:
85-
with open(FILE_2_UNITS, "r", newline="") as file_obj:
85+
with open(FILE_2_UNITS, "r", encoding="utf-8") as file_obj:
8686
reader = csv.DictReader(file_obj, dialect="unix")
8787
if len(list(reader)) > 30:
8888
completed_units = True

scripts/shared.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,22 @@ def paths_list_update(logger, paths_list, old_quarter, new_quarter):
233233
return paths_list
234234

235235

236-
def rows_to_csv(args, file_path, fieldnames, rows):
236+
def rows_to_csv(args, file_path, fieldnames, rows, append=False):
237237
"""Write rows to a CSV file if saving is enabled."""
238238
if not args.enable_save:
239239
return
240240

241241
os.makedirs(os.path.dirname(file_path), exist_ok=True)
242242

243-
with open(file_path, "w", encoding="utf-8", newline="\n") as file_obj:
243+
mode = "a" if append else "w"
244+
with open(file_path, mode, encoding="utf-8", newline="\n") as file_obj:
244245
writer = csv.DictWriter(
245246
file_obj,
246247
fieldnames=fieldnames,
247248
dialect="unix",
248249
)
249-
writer.writeheader()
250+
if not append:
251+
writer.writeheader()
250252
for row in rows:
251253
writer.writerow(row)
252254

0 commit comments

Comments
 (0)