|
9 | 9 | import subprocess |
10 | 10 | import sys |
11 | 11 | import time |
12 | | -import warnings |
13 | 12 | import webbrowser |
14 | 13 | from collections.abc import Iterable, Sequence |
15 | 14 | from typing import Any, Literal |
@@ -461,139 +460,6 @@ def build_arg_list( # noqa: PLR0912 |
461 | 460 | return gmt_args |
462 | 461 |
|
463 | 462 |
|
464 | | -def build_arg_string(kwdict, confdict=None, infile=None, outfile=None): |
465 | | - r""" |
466 | | - Convert keyword dictionaries and input/output files into a GMT argument string. |
467 | | -
|
468 | | - Make sure all values in ``kwdict`` have been previously converted to a |
469 | | - string representation using the ``kwargs_to_strings`` decorator. The only |
470 | | - exceptions are True, False and None. |
471 | | -
|
472 | | - Any lists or tuples left will be interpreted as multiple entries for the |
473 | | - same command line option. For example, the kwargs entry ``'B': ['xa', |
474 | | - 'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string. |
475 | | -
|
476 | | - Note that spaces `` `` in arguments are converted to the equivalent octal |
477 | | - code ``\040``, except in the case of -J (projection) arguments where PROJ4 |
478 | | - strings (e.g. "+proj=longlat +datum=WGS84") will have their spaces removed. |
479 | | - See https://github.com/GenericMappingTools/pygmt/pull/1487 for more info. |
480 | | -
|
481 | | - .. deprecated:: 0.12.0 |
482 | | -
|
483 | | - Use :func:`build_arg_list` instead. |
484 | | -
|
485 | | - Parameters |
486 | | - ---------- |
487 | | - kwdict : dict |
488 | | - A dictionary containing parsed keyword arguments. |
489 | | - confdict : dict |
490 | | - A dictionary containing configurable GMT parameters. |
491 | | - infile : str or pathlib.Path |
492 | | - The input file. |
493 | | - outfile : str or pathlib.Path |
494 | | - The output file. |
495 | | -
|
496 | | - Returns |
497 | | - ------- |
498 | | - args : str |
499 | | - The space-delimited argument string with '-' inserted before each |
500 | | - keyword, or '--' inserted before GMT configuration key-value pairs. |
501 | | - The keyword arguments are sorted alphabetically, followed by GMT |
502 | | - configuration key-value pairs, with optional input file at the |
503 | | - beginning and optional output file at the end. |
504 | | -
|
505 | | - Examples |
506 | | - -------- |
507 | | -
|
508 | | - >>> print( |
509 | | - ... build_arg_string( |
510 | | - ... dict( |
511 | | - ... A=True, |
512 | | - ... B=False, |
513 | | - ... E=200, |
514 | | - ... J="+proj=longlat +datum=WGS84", |
515 | | - ... P="", |
516 | | - ... R="1/2/3/4", |
517 | | - ... X=None, |
518 | | - ... Y=None, |
519 | | - ... Z=0, |
520 | | - ... ) |
521 | | - ... ) |
522 | | - ... ) |
523 | | - -A -E200 -J+proj=longlat+datum=WGS84 -P -R1/2/3/4 -Z0 |
524 | | - >>> print( |
525 | | - ... build_arg_string( |
526 | | - ... dict( |
527 | | - ... R="1/2/3/4", |
528 | | - ... J="X4i", |
529 | | - ... B=["xaf", "yaf", "WSen"], |
530 | | - ... I=("1/1p,blue", "2/0.25p,blue"), |
531 | | - ... ) |
532 | | - ... ) |
533 | | - ... ) |
534 | | - -BWSen -Bxaf -Byaf -I1/1p,blue -I2/0.25p,blue -JX4i -R1/2/3/4 |
535 | | - >>> print(build_arg_string(dict(R="1/2/3/4", J="X4i", watre=True))) |
536 | | - Traceback (most recent call last): |
537 | | - ... |
538 | | - pygmt.exceptions.GMTInvalidInput: Unrecognized parameter 'watre'. |
539 | | - >>> print( |
540 | | - ... build_arg_string( |
541 | | - ... dict( |
542 | | - ... B=["af", "WSne+tBlank Space"], |
543 | | - ... F='+t"Empty Spaces"', |
544 | | - ... l="'Void Space'", |
545 | | - ... ), |
546 | | - ... ) |
547 | | - ... ) |
548 | | - -BWSne+tBlank\040Space -Baf -F+t"Empty\040\040Spaces" -l'Void\040Space' |
549 | | - >>> print( |
550 | | - ... build_arg_string( |
551 | | - ... dict(A="0", B=True, C="rainbow"), |
552 | | - ... confdict=dict(FORMAT_DATE_MAP="o dd"), |
553 | | - ... infile="input.txt", |
554 | | - ... outfile="output.txt", |
555 | | - ... ) |
556 | | - ... ) |
557 | | - input.txt -A0 -B -Crainbow --FORMAT_DATE_MAP="o dd" ->output.txt |
558 | | - """ |
559 | | - msg = ( |
560 | | - "Utility function 'build_arg_string()' is deprecated in v0.12.0 and will be " |
561 | | - "removed in v0.14.0. Use 'build_arg_list()' instead." |
562 | | - ) |
563 | | - warnings.warn(msg, category=FutureWarning, stacklevel=2) |
564 | | - |
565 | | - gmt_args = [] |
566 | | - for key in kwdict: |
567 | | - if len(key) > 2: # raise an exception for unrecognized options |
568 | | - raise GMTInvalidInput(f"Unrecognized parameter '{key}'.") |
569 | | - if kwdict[key] is None or kwdict[key] is False: |
570 | | - pass # Exclude arguments that are None and False |
571 | | - elif is_nonstr_iter(kwdict[key]): |
572 | | - for value in kwdict[key]: |
573 | | - _value = str(value).replace(" ", r"\040") |
574 | | - gmt_args.append(rf"-{key}{_value}") |
575 | | - elif kwdict[key] is True: |
576 | | - gmt_args.append(f"-{key}") |
577 | | - else: |
578 | | - if key != "J": # non-projection parameters |
579 | | - _value = str(kwdict[key]).replace(" ", r"\040") |
580 | | - else: |
581 | | - # special handling if key == "J" (projection) |
582 | | - # remove any spaces in PROJ4 string |
583 | | - _value = str(kwdict[key]).replace(" ", "") |
584 | | - gmt_args.append(rf"-{key}{_value}") |
585 | | - gmt_args = sorted(gmt_args) |
586 | | - |
587 | | - if confdict: |
588 | | - gmt_args.extend(f'--{key}="{value}"' for key, value in confdict.items()) |
589 | | - |
590 | | - if infile: |
591 | | - gmt_args = [str(infile), *gmt_args] |
592 | | - if outfile: |
593 | | - gmt_args.append("->" + str(outfile)) |
594 | | - return non_ascii_to_octal(" ".join(gmt_args)) |
595 | | - |
596 | | - |
597 | 463 | def is_nonstr_iter(value): |
598 | 464 | """ |
599 | 465 | Check if the value is not a string but is iterable (list, tuple, array) |
|
0 commit comments