|
21 | 21 | import datetime |
22 | 22 | import json |
23 | 23 | import os |
| 24 | +import pathlib as pl |
24 | 25 | import sys |
25 | 26 |
|
26 | 27 | from .download import _request_header |
@@ -341,12 +342,25 @@ def export_json( |
341 | 342 | print( |
342 | 343 | f'writing a json file ("{fpth}") ' |
343 | 344 | + f"of {sel} USGS programs\n" |
344 | | - + f'in the "{program_data_file}" database.' |
| 345 | + + f'in the "{program_data_file}" database.\n' |
345 | 346 | ) |
346 | 347 | if prog_data is not None: |
347 | 348 | for idx, key in enumerate(prog_data.keys()): |
348 | 349 | print(f" {idx + 1:>2d}: {key}") |
349 | | - print("\n") |
| 350 | + |
| 351 | + # process the passed file path into appdir and file_name |
| 352 | + appdir = pl.Path(".") |
| 353 | + file_name = pl.Path(fpth) |
| 354 | + if file_name.parent != str(appdir): |
| 355 | + appdir = file_name.parent |
| 356 | + file_name = file_name.name |
| 357 | + else: |
| 358 | + for idx, argv in enumerate(sys.argv): |
| 359 | + if argv in ("--appdir", "-ad"): |
| 360 | + appdir = pl.Path(sys.argv[idx + 1]) |
| 361 | + |
| 362 | + if str(appdir) != ".": |
| 363 | + appdir.mkdir(parents=True, exist_ok=True) |
350 | 364 |
|
351 | 365 | # get usgs program data |
352 | 366 | udata = usgs_program_data.get_program_dict() |
@@ -392,48 +406,38 @@ def export_json( |
392 | 406 |
|
393 | 407 | # export file |
394 | 408 | try: |
395 | | - with open(fpth, "w") as f: |
396 | | - json.dump(prog_data, f, indent=4) |
| 409 | + with open(file_name, "w") as file_obj: |
| 410 | + json.dump(prog_data, file_obj, indent=4, sort_keys=True) |
397 | 411 | except: |
398 | | - msg = f'could not export json file "{fpth}"' |
| 412 | + msg = f'could not export json file "{file_name}"' |
399 | 413 | raise IOError(msg) |
400 | 414 |
|
401 | | - # export code.json to --appdir directory, if the |
402 | | - # command line argument was specified. Only done if not CI |
403 | | - # command line argument was specified. Only done if not CI |
404 | | - appdir = "." |
405 | | - for idx, argv in enumerate(sys.argv): |
406 | | - if argv in ("--appdir", "-ad"): |
407 | | - appdir = sys.argv[idx + 1] |
408 | | - |
409 | | - # make appdir if it does not already exist |
410 | | - if not os.path.isdir(appdir): |
411 | | - os.makedirs(appdir) |
412 | | - |
413 | 415 | # write code.json |
414 | | - if appdir != ".": |
415 | | - dst = os.path.join(appdir, fpth) |
416 | | - with open(dst, "w") as f: |
417 | | - json.dump(prog_data, f, indent=4) |
| 416 | + if str(appdir) != ".": |
| 417 | + dst = appdir / file_name |
| 418 | + with open(dst, "w") as file_obj: |
| 419 | + json.dump(prog_data, file_obj, indent=4, sort_keys=True) |
418 | 420 |
|
419 | 421 | # write code.md |
420 | 422 | if prog_data is not None and write_markdown: |
421 | | - file_obj = open("code.md", "w") |
422 | | - line = "| Program | Version | UTC Date |" |
423 | | - file_obj.write(line + "\n") |
424 | | - line = "| ------- | ------- | ---- |" |
425 | | - file_obj.write(line + "\n") |
426 | | - for target, target_dict in prog_data.items(): |
427 | | - keys = list(target_dict.keys()) |
428 | | - line = f"| {target} | {target_dict['version']} |" |
429 | | - date_key = "url_download_asset_date" |
430 | | - if date_key in keys: |
431 | | - line += f" {target_dict[date_key]} |" |
432 | | - else: |
433 | | - line += " |" |
434 | | - line += "\n" |
435 | | - file_obj.write(line) |
436 | | - file_obj.close() |
| 423 | + sorted_prog_data = { |
| 424 | + key: prog_data[key] for key in sorted(list(prog_data.keys())) |
| 425 | + } |
| 426 | + with open("code.md", "w") as file_obj: |
| 427 | + line = "| Program | Version | UTC Date |" |
| 428 | + file_obj.write(line + "\n") |
| 429 | + line = "| ------- | ------- | ---- |" |
| 430 | + file_obj.write(line + "\n") |
| 431 | + for target, target_dict in sorted_prog_data.items(): |
| 432 | + keys = list(target_dict.keys()) |
| 433 | + line = f"| {target} | {target_dict['version']} |" |
| 434 | + date_key = "url_download_asset_date" |
| 435 | + if date_key in keys: |
| 436 | + line += f" {target_dict[date_key]} |" |
| 437 | + else: |
| 438 | + line += " |" |
| 439 | + line += "\n" |
| 440 | + file_obj.write(line) |
437 | 441 |
|
438 | 442 | return |
439 | 443 |
|
|
0 commit comments