コード例 #1
0
ファイル: validate.py プロジェクト: yyht/kgtk
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    parser.add_input_file(who="The KGTK file(s) to validate.",
                          dest="input_files",
                          options=["-i", "--input-files"],
                          allow_list=True,
                          positional=True)

    parser.add_argument(
        "--header-only",
        dest="header_only",
        help=
        "Process the only the header of the input file (default=%(default)s).",
        type=optional_bool,
        nargs='?',
        const=True,
        default=False)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    validate_by_default=True,
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #2
0
ファイル: paths.py プロジェクト: usbader/kgtk
def add_arguments(parser: KGTKArgumentParser):
    """
    Parse arguments
    Args:
            parser (argparse.ArgumentParser)
    """
    parser.add_input_file(positional=True)
    parser.add_argument('--directed',
                        action='store_true',
                        dest="directed",
                        help="Is the graph directed or not?")
    parser.add_argument('--max_hops',
                        action="store",
                        type=int,
                        dest="max_hops",
                        help="Maximum number of hops allowed.")
    parser.add_argument('--from',
                        action="store",
                        nargs="*",
                        dest="source_nodes",
                        help="List of source nodes")
    parser.add_argument('--to',
                        action="store",
                        nargs="*",
                        dest="target_nodes",
                        help="List of target nodes")
コード例 #3
0
ファイル: import_atomic.py プロジェクト: usbader/kgtk
def add_arguments(parser: KGTKArgumentParser):
    """
    Parse arguments
    Args:
            parser (argparse.ArgumentParser)
    """
    parser.add_input_file(positional=True)
コード例 #4
0
ファイル: ifempty.py プロジェクト: usbader/kgtk
def run(
        input_file: KGTKFiles,
        output_file: KGTKFiles,
        filter_column_names: typing.List[str],
        all_are: bool = False,
        only_count: bool = False,
        errors_to_stdout: bool = False,
        errors_to_stderr: bool = True,
        show_options: bool = False,
        verbose: bool = False,
        very_verbose: bool = False,
        **kwargs  # Whatever KgtkFileOptions and KgtkValueOptions want.
) -> int:
    # import modules locally
    from kgtk.exceptions import KGTKException

    input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
    output_kgtk_file: Path = KGTKArgumentParser.get_output_file(output_file)

    # Select where to send error messages, defaulting to stderr.
    error_file: typing.TextIO = sys.stdout if errors_to_stdout else sys.stderr

    # Build the option structures.
    reader_options: KgtkReaderOptions = KgtkReaderOptions.from_dict(kwargs)
    value_options: KgtkValueOptions = KgtkValueOptions.from_dict(kwargs)

    # Show the final option structures for debugging and documentation.
    if show_options:
        print("--input-file=%s" % str(input_kgtk_file), file=error_file)
        print("--output-file=%s" % str(output_kgtk_file), file=error_file)
        print("--columns=%s" % " ".join(filter_column_names), file=error_file)
        print("--count=%s" % str(only_count), file=error_file)
        print("--all=%s" % str(all_are), file=error_file)
        reader_options.show(out=error_file)
        value_options.show(out=error_file)
        print("=======", file=error_file, flush=True)

    try:
        ie: KgtkIfEmpty = KgtkIfEmpty(
            input_file_path=input_kgtk_file,
            filter_column_names=filter_column_names,
            output_file_path=output_kgtk_file,
            all_are=all_are,
            notempty=False,
            only_count=only_count,
            reader_options=reader_options,
            value_options=value_options,
            error_file=error_file,
            verbose=verbose,
            very_verbose=very_verbose,
        )

        ie.process()

        return 0

    except SystemExit as e:
        raise KGTKException("Exit requested")
    except Exception as e:
        raise KGTKException(str(e))
コード例 #5
0
def run(
        input_file: KGTKFiles,
        output_file: KGTKFiles,
        key_column_names: typing.List[str],
        errors_to_stdout: bool = False,
        errors_to_stderr: bool = True,
        show_options: bool = False,
        verbose: bool = False,
        very_verbose: bool = False,
        **kwargs  # Whatever KgtkFileOptions and KgtkValueOptions want.
) -> int:
    # import modules locally
    from pathlib import Path
    import sys

    from kgtk.exceptions import KGTKException
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.io.kgtkwriter import KgtkWriter
    from kgtk.reshape.kgtkexpand import KgtkExpand
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
    output_kgtk_file: Path = KGTKArgumentParser.get_output_file(output_file)

    # Select where to send error messages, defaulting to stderr.
    error_file: typing.TextIO = sys.stdout if errors_to_stdout else sys.stderr

    # Build the option structures.
    reader_options: KgtkReaderOptions = KgtkReaderOptions.from_dict(kwargs)
    value_options: KgtkValueOptions = KgtkValueOptions.from_dict(kwargs)

    # Show the final option structures for debugging and documentation.
    if show_options:
        print("--input-file=%s" % str(input_kgtk_file), file=error_file)
        print("--output-file=%s" % str(output_kgtk_file), file=error_file)
        print("--columns=%s" % " ".join(key_column_names), file=error_file)
        reader_options.show(out=error_file)
        value_options.show(out=error_file)
        print("=======", file=error_file, flush=True)

    try:
        ex: KgtkExpand = KgtkExpand(
            input_file_path=input_kgtk_file,
            key_column_names=key_column_names,
            output_file_path=output_kgtk_file,
            reader_options=reader_options,
            value_options=value_options,
            error_file=error_file,
            verbose=verbose,
            very_verbose=very_verbose,
        )

        ex.process()

        return 0

    except SystemExit as e:
        raise KGTKException("Exit requested")
    except Exception as e:
        raise KGTKException(str(e))
コード例 #6
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    # import modules locally
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.reshape.kgtkidbuilder import KgtkIdBuilder, KgtkIdBuilderOptions
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(positional=True)
    parser.add_output_file()

    KgtkIdBuilderOptions.add_arguments(parser,
                                       expert=True)  # Show all the options.
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #7
0
ファイル: paths.py プロジェクト: yyht/kgtk
def add_arguments(parser: KGTKArgumentParser):
    """
    Parse arguments
    Args:
            parser (argparse.ArgumentParser)
    """
    parser.add_input_file(positional=True)
    parser.add_input_file(who="KGTK file with path start and end nodes.",
                          options=["--path_file"],
                          dest="path_file",
                          metavar="PATH_FILE")
    parser.add_argument(
        '--statistics-only',
        action='store_true',
        dest='output_stats',
        help=
        'If this flag is set, output only the statistics edges. Else, append the statistics to the original graph.'
    )

    parser.add_argument('--directed',
                        action='store_true',
                        dest="directed",
                        help="Is the graph directed or not?")
    parser.add_argument('--max_hops',
                        action="store",
                        type=int,
                        dest="max_hops",
                        help="Maximum number of hops allowed.")
コード例 #8
0
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """

    _expert: bool = parsed_shared_args._expert

    parser.add_input_file(positional=True)
    parser.add_output_file()

    parser.add_argument(      "--columns", dest="filter_column_names",
                              help="The columns in the file being filtered (Required).", nargs='+', required=True)

    parser.add_argument(      "--count", dest="only_count", metavar="True|False",
                              help="Only count the records, do not copy them. (default=%(default)s).",
                              type=optional_bool, nargs='?', const=True, default=False)

    parser.add_argument(      "--all", dest="all_are", metavar="True|False",
                              help="False: Test if any are not empty, True: test if all are not empty (default=%(default)s).",
                              type=optional_bool, nargs='?', const=True, default=False)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #9
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    _expert: bool = parsed_shared_args._expert

    parser.add_argument(
        "kgtk_files",
        nargs="*",
        help="The KGTK file(s) to validate. May be omitted or '-' for stdin.",
        type=Path)

    parser.add_argument(
        "--header-only",
        dest="header_only",
        help=
        "Process the only the header of the input file (default=%(default)s).",
        type=optional_bool,
        nargs='?',
        const=True,
        default=False)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    validate_by_default=True,
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #10
0
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str)->str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_argument(      "input_kgtk_file", nargs="?", type=Path, default="-",
                              help="The KGTK file to filter. May be omitted or '-' for stdin (default=%(default)s).")

    parser.add_argument("-o", "--output-file", dest="output_kgtk_file", help="The KGTK file to write (default=%(default)s).", type=Path, default="-")

    KgtkIdBuilderOptions.add_arguments(parser, expert=True) # Show all the options.
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #11
0
def run(input_file: KGTKFiles,
        output_file: KGTKFiles,
        no_header: bool = False,
        properties: str = '',
        undirected: bool = False,
        strong: bool = False,
        **kwargs  # Whatever KgtkFileOptions and KgtkValueOptions want.
        ) -> int:
    from kgtk.gt.connected_components import ConnectedComponents
    from kgtk.exceptions import KGTKException

    input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
    output_kgtk_file: Path = KGTKArgumentParser.get_output_file(output_file)

    cc: ConnectedComponents = ConnectedComponents(input_file_path=input_kgtk_file,
                                                  output_file_path=output_kgtk_file,
                                                  no_header=no_header,
                                                  properties=properties,
                                                  undirected=undirected,
                                                  strong=strong)

    try:
        cc.process()
        return 0
    except Exception as e:
        raise KGTKException(str(e))
コード例 #12
0
ファイル: reorder_columns.py プロジェクト: vishalbelsare/kgtk
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    # import modules locally
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert
    _command: str = parsed_shared_args._command

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file()
    parser.add_output_file()

    parser.add_argument("--output-format",
                        dest="output_format",
                        help=h("The file format (default=kgtk)"),
                        type=str)

    parser.add_argument(
        '-c',
        "--columns",
        dest="column_names",
        required=True,
        nargs='+',
        metavar="COLUMN_NAME",
        help=
        "The list of reordered column names, optionally containing '...' for column names not explicitly mentioned."
    )

    parser.add_argument(
        "--trim",
        dest="omit_remaining_columns",
        help="If true, omit unmentioned columns. (default=%(default)s).",
        metavar="True|False",
        type=optional_bool,
        nargs='?',
        const=True,
        default=(_command == SELECT_COLUMNS_COMMAND))

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #13
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.imports.kgtkntriples import KgtkNtriples
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions, KgtkReaderMode
    from kgtk.reshape.kgtkidbuilder import KgtkIdBuilder, KgtkIdBuilderOptions
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(who="The ntriples file(s) to import.",
                          allow_list=True)
    parser.add_output_file()

    parser.add_output_file(
        who="The ntriples output file for records that are rejected.",
        dest="reject_file",
        options=["--reject-file"],
        metavar="REJECT_FILE",
        optional=True)

    parser.add_input_file(who="The KGTK input file with known namespaces.",
                          dest="namespace_file",
                          options=["--namespace-file"],
                          metavar="NAMESPACE_FILE",
                          optional=True)

    parser.add_output_file(who="The KGTK output file with updated namespaces.",
                           dest="updated_namespace_file",
                           options=["--updated-namespace-file"],
                           metavar="NAMESPACE_FILE",
                           optional=True)

    KgtkNtriples.add_arguments(parser)
    KgtkIdBuilderOptions.add_arguments(parser)
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(
        parser,
        mode_options=True,
        default_mode=KgtkReaderMode[parsed_shared_args._mode],
        expert=_expert)
    KgtkValueOptions.add_arguments(parser)
コード例 #14
0
ファイル: md.py プロジェクト: usbader/kgtk
def run(
        input_file: KGTKFiles,
        output_file: KGTKFiles,
        output_format: str,
        errors_to_stdout: bool = False,
        errors_to_stderr: bool = True,
        show_options: bool = False,
        verbose: bool = False,
        very_verbose: bool = False,
        **kwargs  # Whatever KgtkFileOptions and KgtkValueOptions want.
) -> int:
    # import modules locally
    from kgtk.exceptions import KGTKException

    input_file_path: Path = KGTKArgumentParser.get_input_file(input_file)
    output_file_path: Path = KGTKArgumentParser.get_output_file(output_file)

    # Select where to send error messages, defaulting to stderr.
    error_file: typing.TextIO = sys.stdout if errors_to_stdout else sys.stderr

    # TODO: check that at most one input file is stdin?

    # Build the option structures.
    reader_options: KgtkReaderOptions = KgtkReaderOptions.from_dict(kwargs)
    value_options: KgtkValueOptions = KgtkValueOptions.from_dict(kwargs)

    # Show the final option structures for debugging and documentation.
    if show_options:
        print("--input-file=%s" % str(input_file_path),
              file=error_file,
              flush=True)
        print("--output-file=%s" % str(output_file_path),
              file=error_file,
              flush=True)
        reader_options.show(out=error_file)
        value_options.show(out=error_file)
        print("=======", file=error_file, flush=True)

    try:
        kc: KgtkCat = KgtkCat(input_file_paths=[input_file_path],
                              output_path=output_file_path,
                              output_format=output_format,
                              reader_options=reader_options,
                              value_options=value_options,
                              error_file=error_file,
                              verbose=verbose,
                              very_verbose=very_verbose)

        kc.process()

        return 0

    except SystemExit as e:
        raise KGTKException("Exit requested")
    except Exception as e:
        raise KGTKException(str(e))
コード例 #15
0
ファイル: import_conceptnet.py プロジェクト: yyht/kgtk
def add_arguments(parser: KGTKArgumentParser):
    """
    Parse arguments
    Args:
            parser (argparse.ArgumentParser)
    """
    parser.add_input_file(positional=True)
    parser.add_argument('--english_only',
                        action="store_true",
                        help="Only english conceptnet?")
コード例 #16
0
def run(input_file,
        output_file,
        columns='1',
        reverse=False,
        space=False,
        speed=False,
        extra='',
        tsv=False,
        csv=False,
        _dt=None,
        naptime=1):
    import time

    import kgtk.cli.zconcat as zcat

    time.sleep(int(naptime))
    # print("Sort running.", file=sys.stderr, flush=True) # ***

    input = str(KGTKArgumentParser.get_input_file(input_file))
    output = str(KGTKArgumentParser.get_output_file(output_file))
    if output == "-":
        output = None

    # logging.basicConfig(level=logging.INFO)
    """Run sort according to the provided command-line arguments.
    """
    try:
        colsep = '\t'
        if not tsv and (csv or _dt == 'csv'):
            colsep = ','

        options = extra
        if reverse:
            options += ' -r'
        if space:
            options += ' ' + space_config
        elif speed:
            options += ' ' + speed_config

        pipe = build_command(input=input,
                             output=output,
                             columns=columns,
                             colsep=colsep,
                             options=options)
        # print("pipe: %s" % str(pipe), file=sys.stderr, flush=True) # ***
        return zcat.run_sh_commands(pipe).exit_code
    except sh.SignalException_SIGPIPE:
        # hack to work around Python3 issue when stdout is gone when we try to report an exception;
        # without this we get an ugly 'Exception ignored...' msg when we quit with head or a pager:
        sys.stdout = os.fdopen(1)
    except Exception as e:
        #import traceback
        #traceback.print_tb(sys.exc_info()[2], 10)
        raise KGTKException('INTERNAL ERROR: ' + type(e).__module__ + '.' +
                            str(e) + '\n')
コード例 #17
0
ファイル: unreify_values.py プロジェクト: nicklein/kgtk
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions, KgtkReaderMode
    from kgtk.unreify.kgtkunreifyvalues import KgtkUnreifyValues
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str)->str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(who="The KGTK input file with the reified data. " +
                        "It must have node1, label, and node2 columns, or their aliases. " +
                        "It may have an ID column;  if it does not, one will be appended to the output file. " +
                        "It may not have any additional columns. ")

    parser.add_output_file(who="The KGTK file to write output records with unreified data. " +
                        "This file may differ in shape from the input file by the addition of an ID column. " +
                        "The records in the output file will not, generally, be in the same order as they appeared in the input file. ")
    
    parser.add_output_file(who="A KGTK output file that will contain only the reified RDF statements.",
                           dest="reified_file",
                           options=["--reified-file"],
                           metavar="REIFIED_FILE",
                           optional=True)
    parser.add_output_file(who="A KGTK output file that will contain only the unreified RDF statements.",
                           dest="unreified_file",
                           options=["--unreified-file"],
                           metavar="UNREIFIED_FILE",
                           optional=True)
    parser.add_output_file(who="A KGTK output file that will contain only the uninvolved input.",
                           dest="uninvolved_file",
                           options=["--uninvolved-file"],
                           metavar="UNINVOLVED_FILE",
                           optional=True)
    
    KgtkUnreifyValues.add_arguments(parser)
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    default_mode=KgtkReaderMode[parsed_shared_args._mode],
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser)
コード例 #18
0
def add_arguments(parser: KGTKArgumentParser):
    """
    Parse arguments
    Args:
            parser (argparse.ArgumentParser)
    """
    parser.add_input_file(positional=True,
                          who="Visual Genome scene graph file")
    parser.add_input_file(who="Visual Genome file with attribute synsets.",
                          options=["--attr-synsets"],
                          dest="attr_syn_file",
                          metavar="ATTR_SYN_FILE")
コード例 #19
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """

    _expert: bool = parsed_shared_args._expert

    parser.add_argument(
        "input_kgtk_file",
        nargs="?",
        help="The KGTK file to filter. May be omitted or '-' for stdin.",
        type=Path)

    parser.add_argument(
        "--columns",
        dest="filter_column_names",
        help="The columns in the file being filtered (Required).",
        nargs='+',
        required=True)

    parser.add_argument(
        "--count",
        dest="only_count",
        help="Only count the records, do not copy them. (default=%(default)s).",
        type=optional_bool,
        nargs='?',
        const=True,
        default=False)

    parser.add_argument("-o",
                        "--output-file",
                        dest="output_kgtk_file",
                        help="The KGTK file to write (default=%(default)s).",
                        type=Path,
                        default="-")

    parser.add_argument(
        "--all",
        dest="all_are",
        help=
        "False: Test if any are, True: test if all are (default=%(default)s).",
        type=optional_bool,
        nargs='?',
        const=True,
        default=False)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #20
0
def add_arguments(parser: KGTKArgumentParser):
    parser.add_argument('-o', '--out', default=None, dest='output',
                        help='output file to write to, otherwise output goes to stdout')
    parser.add_argument('--gz', '--gzip', action='store_true', dest='gz',
                        help='compress result with gzip')
    parser.add_argument('--bz2', '--bzip2', action='store_true', dest='bz2',
                        help='compress result with bzip2')
    parser.add_argument('--xz', action='store_true', dest='xz',
                        help='compress result with xz')

    # parser.add_argument("inputs", metavar="INPUT", nargs="*", action="store",
    #                    help="input files to process, if empty or `-' read from stdin")
    parser.add_input_file(positional=True, allow_list=True, dest="input_files")
コード例 #21
0
ファイル: connected-components.py プロジェクト: nicklein/kgtk
def run(
        input_file: KGTKFiles,
        output_file: KGTKFiles,
        properties: str = '',
        undirected: bool = False,
        strong: bool = False,

        # The following have been modified to postpone importing gtaph_tools.
        # ClusterComponents cann't be referenced here.
        cluster_name_method: typing.Optional[typing.Any] = None,
        cluster_name_separator: typing.Optional[str] = None,
        cluster_name_prefix: typing.Optional[str] = None,
        cluster_name_zfill: typing.Optional[int] = None,
        minimum_cluster_size: typing.Optional[int] = None,
        **kwargs  # Whatever KgtkFileOptions and KgtkValueOptions want.
) -> int:
    from pathlib import Path

    from kgtk.exceptions import KGTKException
    from kgtk.gt.connected_components import ConnectedComponents
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions

    input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
    output_kgtk_file: Path = KGTKArgumentParser.get_output_file(output_file)

    # It's OK to mention ClusterComponents here.
    cluster_name_method_x: ConnectedComponents.Method = \
        cluster_name_method if cluster_name_method is not None else ConnectedComponents.DEFAULT_CLUSTER_NAME_METHOD
    cluster_name_separator = ConnectedComponents.DEFAULT_CLUSTER_NAME_SEPARATOR if cluster_name_separator is None else cluster_name_separator
    cluster_name_prefix = ConnectedComponents.DEFAULT_CLUSTER_NAME_PREFIX if cluster_name_prefix is None else cluster_name_prefix
    cluster_name_zfill = ConnectedComponents.DEFAULT_CLUSTER_NAME_ZFILL if cluster_name_zfill is None else cluster_name_zfill
    minimum_cluster_size = ConnectedComponents.DEFAULT_MINIMUM_CLUSTER_SIZE if minimum_cluster_size is None else minimum_cluster_size

    cc: ConnectedComponents = ConnectedComponents(
        input_file_path=input_kgtk_file,
        output_file_path=output_kgtk_file,
        properties=properties,
        undirected=undirected,
        strong=strong,
        cluster_name_method=cluster_name_method_x,
        cluster_name_separator=cluster_name_separator,
        cluster_name_prefix=cluster_name_prefix,
        cluster_name_zfill=cluster_name_zfill,
        minimum_cluster_size=minimum_cluster_size,
    )

    try:
        cc.process()
        return 0
    except Exception as e:
        raise KGTKException(str(e))
コード例 #22
0
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    _expert: bool = parsed_shared_args._expert

    parser.add_argument(      "input_file", nargs="?", help="The KGTK file to read.  May be omitted or '-' for stdin.", type=Path)
    parser.add_argument(      "output_file", nargs="?", help="The KGTK file to write.  May be omitted or '-' for stdout.", type=Path)
    
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, validate_by_default=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #23
0
def run(
    input_file: KGTKFiles,
    prop_file: KGTKFiles,
    labels: str,
    aliases: str,
    descriptions: str,
    prop_declaration: bool,
    output_prefix: str,
    n: int,
    log_path: str,
    warning: bool,
    has_rank: bool,
    error_action: str,
    property_declaration_label: str,
    ignore_property_declarations_in_file: bool,
    filter_prop_file: bool,
    verbose: bool,
):
    # import modules locally
    from pathlib import Path
    from kgtk.generator import JsonGenerator
    import sys
    import gzip
    from kgtk.exceptions import KGTKException

    input_kgtk_file: Path = KGTKArgumentParser.get_input_file(input_file)
    prop_kgtk_file: typing.Optional[
        Path] = KGTKArgumentParser.get_optional_input_file(
            prop_file, who="KGTK prop file")

    generator = JsonGenerator(
        input_file=input_kgtk_file,
        prop_file=prop_kgtk_file,
        label_set=labels,
        alias_set=aliases,
        description_set=descriptions,
        output_prefix=output_prefix,
        n=n,
        log_path=log_path,
        warning=warning,
        prop_declaration=prop_declaration,
        has_rank=has_rank,
        error_action=error_action,
        property_declaration_label=property_declaration_label,
        ignore_property_declarations_in_file=
        ignore_property_declarations_in_file,
        filter_prop_file=filter_prop_file,
        verbose=verbose,
    )
    generator.process()
コード例 #24
0
ファイル: normalize_nodes.py プロジェクト: nicklein/kgtk
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """

    _expert: bool = parsed_shared_args._expert
    _mode: str = parsed_shared_args._mode

    parser.add_input_file(positional=True)
    parser.add_output_file()

    parser.add_argument(
        '-c',
        "--columns",
        action="store",
        type=str,
        dest="columns",
        nargs='+',
        help=
        "Columns to remove as a space-separated list. (default=all columns except id)"
    )

    parser.add_argument(
        "--labels",
        action="store",
        type=str,
        dest="labels",
        nargs='+',
        help=
        "Label names to use as a space-separated list. (default=column names)")

    parser.add_argument(
        "--id-column",
        action="store",
        type=str,
        dest="id_column_name",
        help="The name of the ID column. (default=id or alias)")

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    default_mode=KgtkReaderMode.NONE if _mode
                                    == "NONE" else KgtkReaderMode.NODE,
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #25
0
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions, KgtkReaderMode
    from kgtk.unreify.kgtkunreifyrdfstatements import KgtkUnreifyRdfStatements
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str)->str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(who="The KGTK input file with the reified data.")
    parser.add_output_file()
    parser.add_output_file(who="A KGTK output file that will contain only the reified RDF statements.",
                           dest="reified_file",
                           options=["--reified-file"],
                           metavar="REIFIED_FILE",
                           optional=True)
    parser.add_output_file(who="A KGTK output file that will contain only the unreified RDF statements.",
                           dest="unreified_file",
                           options=["--unreified-file"],
                           metavar="UNREIFIED_FILE",
                           optional=True)
    parser.add_output_file(who="A KGTK output file that will contain only the uninvolved input.",
                           dest="uninvolved_file",
                           options=["--uninvolved-file"],
                           metavar="UNINVOLVED_FILE",
                           optional=True)

    KgtkUnreifyRdfStatements.add_arguments(parser)
    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    default_mode=KgtkReaderMode[parsed_shared_args._mode],
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser)
コード例 #26
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    _expert: bool = parsed_shared_args._expert

    parser.add_input_file(positional=True)
    parser.add_output_file(positional=True)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    validate_by_default=True,
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #27
0
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """                                                                                                                                                               
    Parse arguments                                                                                                                                                   
    Args:                                                                                                                                                             
        parser (argparse.ArgumentParser)                                                                                                                              
    """
    from kgtk.exceptions import KGTKException
    from kgtk.lift.kgtklift import KgtkLift
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert
    _command: str = parsed_shared_args._command

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file()
    parser.add_output_file()

    # The default value for this option depends upon the command used.
    parser.add_argument(
        '-l',
        '--lines',
        dest="count_records",
        metavar="True/False",
        help="If true, count records and print a single number to stdout. " +
        "If false, count non-empty values per column and produce a simple KGTK output file. (default=%(default)s).",
        type=optional_bool,
        nargs='?',
        const=True,
        default=DEFAULT_COUNT_RECORDS_WC
        if _command == WC_COMMAND else DEFAULT_COUNT_RECORDS)

    # This is an expert option.  It will not show up on `--help` without `--expert`:
    parser.add_argument(
        "--count-property",
        dest="count_property",
        help=
        h("The property used for column count output edges. (default=%(default)s)."
          ),
        default=DEFAULT_COUNT_PROPERTY)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #28
0
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.utils.argparsehelpers import optional_bool

    # These special shared aruments inticate whether the `--expert` option
    # was supplied and the command name that was used.
    _expert: bool = parsed_shared_args._expert
    _command: str = parsed_shared_args._command

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    # KGTK Browser hostname
    parser.add_argument(
        '--host',
        dest="kgtk_browser_host",
        help="Hostname used to launch flask server, defaults to localhost",
        default="localhost",
    )

    # KGTK Browser port number
    parser.add_argument(
        '-p', '--port',
        dest="kgtk_browser_port",
        help="Port number used to launch flask server, defaults to 5000",
        default="5000",
    )

    # KGTK Browser configuration file
    parser.add_argument(
        '-c', '--config',
        dest="kgtk_browser_config",
        help="KGTK Browser configuration file, defaults to `kgtk_browser_config.py`",
        default="kgtk_browser_config.py",
    )

    # KGTK Browser application file
    parser.add_argument(
        '-a', '--app',
        dest="kgtk_browser_app",
        help="KGTK Browser flask application file, defaults to `kgtk_browser_app.py`",
        default="kgtk_browser_app.py",
    )
コード例 #29
0
ファイル: html.py プロジェクト: nicklein/kgtk
def add_arguments_extended(parser: KGTKArgumentParser,
                           parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions, KgtkReaderMode
    from kgtk.io.kgtkwriter import KgtkWriter
    from kgtk.utils.argparsehelpers import optional_bool
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str) -> str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(who="The KGTK file to convert to an HTML table.",
                          positional=True)
    parser.add_output_file(who="The GitHub markdown file to write.")

    parser.add_argument(
        "--pp",
        "--readable",
        dest="readable",
        help=
        "If true, use a human-readable output format. (default=%(default)s).",
        metavar="True/False",
        type=optional_bool,
        nargs='?',
        const=True,
        default=False)

    parser.add_argument("--output-format",
                        dest="output_format",
                        type=str,
                        help=h("The file format (default=%(default)s)"),
                        default=KgtkWriter.OUTPUT_FORMAT_HTML_COMPACT)

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser,
                                    mode_options=True,
                                    default_mode=KgtkReaderMode.NONE,
                                    expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)
コード例 #30
0
ファイル: table.py プロジェクト: vishalbelsare/kgtk
def add_arguments_extended(parser: KGTKArgumentParser, parsed_shared_args: Namespace):
    """
    Parse arguments
    Args:
        parser (argparse.ArgumentParser)
    """
    from kgtk.io.kgtkreader import KgtkReader, KgtkReaderOptions, KgtkReaderMode
    from kgtk.value.kgtkvalueoptions import KgtkValueOptions

    _expert: bool = parsed_shared_args._expert

    # This helper function makes it easy to suppress options from
    # The help message.  The options are still there, and initialize
    # what they need to initialize.
    def h(msg: str)->str:
        if _expert:
            return msg
        else:
            return SUPPRESS

    parser.add_input_file(who="The KGTK file to convert to an HTML table.", positional=True)
    parser.add_output_file(who="The GitHub markdown file to write.")

    parser.add_argument(      "--output-format", dest="output_format", help=h("The file format (default=%(default)s)"), type=str, default="table")

    KgtkReader.add_debug_arguments(parser, expert=_expert)
    KgtkReaderOptions.add_arguments(parser, mode_options=True, default_mode=KgtkReaderMode.NONE, expert=_expert)
    KgtkValueOptions.add_arguments(parser, expert=_expert)