<commit_before>from __future__ import print_function
import argparse
from flattentool import create_template, unflatten, flatten
from six import text_type

"""
This file does most of the work of the flatten-tool commandline command.

It takes any commandline arguments, and passes them to a function in
``__init__.py``.

It is callable via the ``flatten-tool`` executable in the directory below, or
using ``python -m flattentool.cli``.

"""


def create_parser():
    """
    Create an argparse ArgumentParser for our commandline arguments

    Defaults are not set here, but rather given in the appropriate function.

    (This is split out as it's own function primarily so it can be tested.)

    """

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subparser_name')

    parser_create_template = subparsers.add_parser(
        'create-template',
        help='Create a template from the given schema')
    parser_create_template.add_argument(
        "-s", "--schema",
        help="Path to the schema file you want to use to create the template",
        required=True)
    parser_create_template.add_argument(
        "-f", "--output-format",
        help="Type of template you want to create. Defaults to all available options")
    parser_create_template.add_argument(
        "-m", "--main-sheet-name",
        help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")
    parser_create_template.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended if format is all.")
    parser_create_template.add_argument(
        "--rollup",
        action='store_true',
        help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
    parser_create_template.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
    parser_create_template.add_argument(
        "--use-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    parser_flatten = subparsers.add_parser(
        'flatten',
        help='Flatten a JSON file')
    parser_flatten.add_argument(
        'input_name',
        help="Name of the input JSON file.")
    parser_flatten.add_argument(
        "-s", "--schema",
        help="Path to a relevant schema.")
    parser_flatten.add_argument(
        "-f", "--output-format",
        help="Type of template you want to create. Defaults to all available options")
    parser_flatten.add_argument(
        "-m", "--main-sheet-name",
        help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")
    parser_flatten.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended if format is all.")
    parser_flatten.add_argument(
        "--root-list-path",
        help="Path of the root list, defaults to main")
    parser_flatten.add_argument(
        "--rollup",
        action='store_true',
        help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
    parser_flatten.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS")
    parser_flatten.add_argument(
        "--use-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    parser_unflatten = subparsers.add_parser(
        'unflatten',
        help='Unflatten a spreadsheet')
    parser_unflatten.add_argument(
        'input_name',
        help="Name of the input file or directory.")
    parser_unflatten.add_argument(
        "-f", "--input-format",
        help="File format of input file or directory.",
        required=True)
    parser_unflatten.add_argument(
        "-b", "--base-json",
        help="A base json file to populate with the unflattened data.")
    parser_unflatten.add_argument(
        "-m", "--root-list-path",
        help="The path in the JSON that will contain the unflattened list. Defaults to main.")
    parser_unflatten.add_argument(
        "-e", "--encoding",
        help="Encoding of the input file(s) (only relevant for CSV). Defaults to utf8.")
    parser_unflatten.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended as appropriate. Defaults to unflattened.json")
    parser_unflatten.add_argument(
        "-c", "--cell-source-map",
        help="Path to write a cell source map to. Will have an extension appended as appropriate.")
    parser_unflatten.add_argument(
        "-a", "--heading-source-map",
        help="Path to write a heading source map to. Will have an extension appended as appropriate.")
    parser_unflatten.add_argument(
        "--timezone-name",
        help="Name of the timezone, defaults to UTC. Should be in tzdata format, e.g. Europe/London")
    parser_unflatten.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS and blank for 360Giving (use --root-id=''). Defaults to ocid.")
    parser_unflatten.add_argument(
        "-s", "--schema",
        help="Path to a relevant schema.")
    parser_unflatten.add_argument(
        "--convert-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    return parser


def kwargs_from_parsed_args(args):
    """
    Transforms argparse's parsed args object into a dictionary to be passed  as
    kwargs.<commit_msg>[#40] Remove some out of date root-id documentation<commit_after>from __future__ import print_function
import argparse
from flattentool import create_template, unflatten, flatten
from six import text_type

"""
This file does most of the work of the flatten-tool commandline command.

It takes any commandline arguments, and passes them to a function in
``__init__.py``.

It is callable via the ``flatten-tool`` executable in the directory below, or
using ``python -m flattentool.cli``.

"""


def create_parser():
    """
    Create an argparse ArgumentParser for our commandline arguments

    Defaults are not set here, but rather given in the appropriate function.

    (This is split out as it's own function primarily so it can be tested.)

    """

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='subparser_name')

    parser_create_template = subparsers.add_parser(
        'create-template',
        help='Create a template from the given schema')
    parser_create_template.add_argument(
        "-s", "--schema",
        help="Path to the schema file you want to use to create the template",
        required=True)
    parser_create_template.add_argument(
        "-f", "--output-format",
        help="Type of template you want to create. Defaults to all available options")
    parser_create_template.add_argument(
        "-m", "--main-sheet-name",
        help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")
    parser_create_template.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended if format is all.")
    parser_create_template.add_argument(
        "--rollup",
        action='store_true',
        help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
    parser_create_template.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS")
    parser_create_template.add_argument(
        "--use-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    parser_flatten = subparsers.add_parser(
        'flatten',
        help='Flatten a JSON file')
    parser_flatten.add_argument(
        'input_name',
        help="Name of the input JSON file.")
    parser_flatten.add_argument(
        "-s", "--schema",
        help="Path to a relevant schema.")
    parser_flatten.add_argument(
        "-f", "--output-format",
        help="Type of template you want to create. Defaults to all available options")
    parser_flatten.add_argument(
        "-m", "--main-sheet-name",
        help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")
    parser_flatten.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended if format is all.")
    parser_flatten.add_argument(
        "--root-list-path",
        help="Path of the root list, defaults to main")
    parser_flatten.add_argument(
        "--rollup",
        action='store_true',
        help="\"Roll up\" columns from subsheets into the main sheet if they are specified in a rollUp attribute in the schema.")
    parser_flatten.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS")
    parser_flatten.add_argument(
        "--use-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    parser_unflatten = subparsers.add_parser(
        'unflatten',
        help='Unflatten a spreadsheet')
    parser_unflatten.add_argument(
        'input_name',
        help="Name of the input file or directory.")
    parser_unflatten.add_argument(
        "-f", "--input-format",
        help="File format of input file or directory.",
        required=True)
    parser_unflatten.add_argument(
        "-b", "--base-json",
        help="A base json file to populate with the unflattened data.")
    parser_unflatten.add_argument(
        "-m", "--root-list-path",
        help="The path in the JSON that will contain the unflattened list. Defaults to main.")
    parser_unflatten.add_argument(
        "-e", "--encoding",
        help="Encoding of the input file(s) (only relevant for CSV). Defaults to utf8.")
    parser_unflatten.add_argument(
        "-o", "--output-name",
        help="Name of the outputted file. Will have an extension appended as appropriate. Defaults to unflattened.json")
    parser_unflatten.add_argument(
        "-c", "--cell-source-map",
        help="Path to write a cell source map to. Will have an extension appended as appropriate.")
    parser_unflatten.add_argument(
        "-a", "--heading-source-map",
        help="Path to write a heading source map to. Will have an extension appended as appropriate.")
    parser_unflatten.add_argument(
        "--timezone-name",
        help="Name of the timezone, defaults to UTC. Should be in tzdata format, e.g. Europe/London")
    parser_unflatten.add_argument(
        "-r", "--root-id",
        help="Root ID of the data format, e.g. ocid for OCDS")
    parser_unflatten.add_argument(
        "-s", "--schema",
        help="Path to a relevant schema.")
    parser_unflatten.add_argument(
        "--convert-titles",
        action='store_true',
        help="Convert titles. Requires a schema to be specified.")

    return parser


def kwargs_from_parsed_args(args):
    """
    Transforms argparse's parsed args object into a dictionary to be passed  as
    kwargs.
{'fraction_non_alphanumeric': 0.10584518167456557, 'fraction_numerical': 0.000877654906090925, 'mean_word_length': 2.786972416085078, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1955288', 'n_tokens_mistral': 3223, 'n_tokens_neox': 3158, 'n_words': 1126}