|
14 | 14 |
|
15 | 15 | from ._version import __version__ |
16 | 16 | from .attributes import _LONG_NAME_WIDTH, _SHORT_NAME_WIDTH, ATTRIBUTES |
| 17 | +from .classes import _AttributeList, _AttributeTagsList |
17 | 18 | from .constants import _TAGS_NAMES |
18 | 19 | from .utils import validate_attribute_value |
19 | 20 |
|
@@ -106,9 +107,8 @@ def get_help(self, ctx): |
106 | 107 | "-j", |
107 | 108 | "json_", |
108 | 109 | is_flag=True, |
109 | | - help="Print output in JSON format, for use with --list.", |
| 110 | + help="Print output in JSON format, for use with --list and --get.", |
110 | 111 | default=False, |
111 | | - hidden=True, # not yet implemented |
112 | 112 | ) |
113 | 113 | DEBUG_OPTION = click.option( |
114 | 114 | "--debug", required=False, is_flag=True, default=False, hidden=True |
@@ -218,7 +218,14 @@ def cli( |
218 | 218 | if invalid_attr: |
219 | 219 | click.echo("") # add a new line before rest of help text |
220 | 220 | click.echo(ctx.get_help()) |
221 | | - ctx.exit() |
| 221 | + ctx.exit(2) |
| 222 | + |
| 223 | + # check that json_ only used with get or list_ |
| 224 | + if json_ and not any([get, list_]): |
| 225 | + click.echo("--json can only be used with --get or --list", err=True) |
| 226 | + click.echo("") # add a new line before rest of help text |
| 227 | + click.echo(ctx.get_help()) |
| 228 | + ctx.exit(2) |
222 | 229 |
|
223 | 230 | for f in files: |
224 | 231 | if walk and os.path.isdir(f): |
@@ -319,59 +326,69 @@ def process_file(fpath, json_, set_, append, update, remove, clear, get, list_): |
319 | 326 |
|
320 | 327 | if get: |
321 | 328 | logging.debug(f"get: {get}") |
| 329 | + if json_: |
| 330 | + data = {} |
| 331 | + data["_version"] = __version__ |
| 332 | + data["_filepath"] = str(fpath) |
| 333 | + data["_filename"] = fpath.name |
322 | 334 | for attr in get: |
323 | 335 | attribute = ATTRIBUTES[attr] |
324 | 336 | logging.debug(f"getting {attr}") |
325 | | - value = md.get_attribute(attribute.name) |
326 | | - click.echo( |
327 | | - f"{attribute.name:{_SHORT_NAME_WIDTH}}{attribute.constant:{_LONG_NAME_WIDTH}} = {value}" |
328 | | - ) |
| 337 | + if json_: |
| 338 | + if attribute.type_ == datetime.datetime: |
| 339 | + # need to convert datetime.datetime to string to serialize |
| 340 | + value = md.get_attribute(attribute.name) |
| 341 | + if type(value) == list: |
| 342 | + value = [v.isoformat() for v in value] |
| 343 | + else: |
| 344 | + value = value.isoformat() |
| 345 | + data[attribute.constant] = value |
| 346 | + else: |
| 347 | + # get raw value |
| 348 | + data[attribute.constant] = md.get_attribute(attribute.name) |
| 349 | + else: |
| 350 | + value = md.get_attribute_str(attribute.name) |
| 351 | + click.echo( |
| 352 | + f"{attribute.name:{_SHORT_NAME_WIDTH}}{attribute.constant:{_LONG_NAME_WIDTH}} = {value}" |
| 353 | + ) |
| 354 | + if json_: |
| 355 | + json_str = json.dumps(data) |
| 356 | + click.echo(json_str) |
329 | 357 |
|
330 | 358 | if list_: |
331 | 359 | attribute_list = md.list_metadata() |
| 360 | + if json_: |
| 361 | + data = {} |
| 362 | + data["_version"] = __version__ |
| 363 | + data["_filepath"] = str(fpath) |
| 364 | + data["_filename"] = fpath.name |
332 | 365 | for attr in attribute_list: |
333 | 366 | try: |
334 | 367 | attribute = ATTRIBUTES[attr] |
335 | | - value = md.get_attribute_str(attribute.name) |
336 | | - click.echo( |
337 | | - f"{attribute.name:{_SHORT_NAME_WIDTH}}{attribute.constant:{_LONG_NAME_WIDTH}} = {value}" |
338 | | - ) |
| 368 | + if json_: |
| 369 | + if attribute.type_ == datetime.datetime: |
| 370 | + # need to convert datetime.datetime to string to serialize |
| 371 | + value = md.get_attribute(attribute.name) |
| 372 | + if type(value) == list: |
| 373 | + value = [v.isoformat() for v in value] |
| 374 | + else: |
| 375 | + value = value.isoformat() |
| 376 | + data[attribute.constant] = value |
| 377 | + else: |
| 378 | + # get raw value |
| 379 | + data[attribute.constant] = md.get_attribute(attribute.name) |
| 380 | + else: |
| 381 | + value = md.get_attribute_str(attribute.name) |
| 382 | + click.echo( |
| 383 | + f"{attribute.name:{_SHORT_NAME_WIDTH}}{attribute.constant:{_LONG_NAME_WIDTH}} = {value}" |
| 384 | + ) |
339 | 385 | except KeyError: |
340 | 386 | click.echo( |
341 | 387 | f"{'UNKNOWN':{_SHORT_NAME_WIDTH}}{attr:{_LONG_NAME_WIDTH}} = THIS ATTRIBUTE NOT HANDLED" |
342 | 388 | ) |
343 | | - |
344 | | - |
345 | | -# def write_json_data(fp, data): |
346 | | -# json.dump(data, fp) |
347 | | -# fp.write("\n") |
348 | | - |
349 | | - |
350 | | -# def write_text_data(fp, data): |
351 | | -# file = data["file"] |
352 | | - |
353 | | -# fc = data["fc"] |
354 | | -# fc = fc if fc is not None else "" |
355 | | - |
356 | | -# dldate = data["dldate"] |
357 | | -# dldate = dldate if dldate is not None else "" |
358 | | - |
359 | | -# desc = data["description"] |
360 | | -# desc = desc if desc is not None else "" |
361 | | - |
362 | | -# where_from = data["where_from"] |
363 | | -# where_from = where_from if where_from is not None else "" |
364 | | - |
365 | | -# tags = data["tags"] |
366 | | -# tags = tags if len(tags) != 0 else "" |
367 | | - |
368 | | -# print(f"file: {file}", file=fp) |
369 | | -# print(f"description: {desc}", file=fp) |
370 | | -# print(f"tags: {tags}", file=fp) |
371 | | -# print(f"Finder comment: {fc}", file=fp) |
372 | | -# print(f"Download date: {dldate}", file=fp) |
373 | | -# print(f"Where from: {where_from}", file=fp) |
374 | | -# print("\n", file=fp) |
| 389 | + if json_: |
| 390 | + json_str = json.dumps(data) |
| 391 | + click.echo(json_str) |
375 | 392 |
|
376 | 393 |
|
377 | 394 | # def restore_from_json(json_file, quiet=False): |
|
0 commit comments