Exemplo n.º 1
0
def convert(input_encoding, output_encoding, input_locale, output_locale,
            verify_ssl, order_by, source, destination):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING

    if input_locale is not None:
        with rows.locale_context(input_locale):
            table = _import_table(source, encoding=input_encoding,
                                  verify_ssl=verify_ssl)
    else:
        table = _import_table(source, encoding=input_encoding,
                              verify_ssl=verify_ssl)

    if order_by is not None:
        order_by = _get_field_names(order_by,
                                    table.field_names,
                                    permit_not=True)
        # TODO: use complete list of `order_by` fields
        table.order_by(order_by[0].replace('^', '-'))

    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(table, destination, encoding=output_encoding)
    else:
        export_to_uri(table, destination, encoding=output_encoding)
Exemplo n.º 2
0
def join(input_encoding, output_encoding, input_locale, output_locale,
         verify_ssl, order_by, keys, sources, destination):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING
    keys = [key.strip() for key in keys.split(',')]

    if input_locale is not None:
        with rows.locale_context(input_locale):
            tables = [_import_table(source, encoding=input_encoding,
                                    verify_ssl=verify_ssl)
                     for source in sources]
    else:
        tables = [_import_table(source, encoding=input_encoding,
                                verify_ssl=verify_ssl)
                  for source in sources]

    result = rows.join(keys, tables)

    if order_by is not None:
        order_by = _get_field_names(order_by,
                                    result.field_names,
                                    permit_not=True)
        # TODO: use complete list of `order_by` fields
        result.order_by(order_by[0].replace('^', '-'))

    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(result, destination, encoding=output_encoding)
    else:
        export_to_uri(result, destination, encoding=output_encoding)
Exemplo n.º 3
0
Arquivo: cli.py Projeto: abelthf/rows
def join(input_encoding, output_encoding, input_locale, output_locale,
         verify_ssl, order_by, keys, sources, destination):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING
    keys = [key.strip() for key in keys.split(',')]

    if input_locale is not None:
        with rows.locale_context(input_locale):
            tables = [
                _import_table(source,
                              encoding=input_encoding,
                              verify_ssl=verify_ssl) for source in sources
            ]
    else:
        tables = [
            _import_table(source,
                          encoding=input_encoding,
                          verify_ssl=verify_ssl) for source in sources
        ]

    result = rows.join(keys, tables)

    if order_by is not None:
        order_by = _get_field_names(order_by,
                                    result.fields.keys(),
                                    permit_not=True)
        # TODO: use complete list of `order_by` fields
        result.order_by(order_by[0].replace('^', '-'))

    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(destination, result, encoding=output_encoding)
    else:
        export_to_uri(destination, result, encoding=output_encoding)
Exemplo n.º 4
0
Arquivo: cli.py Projeto: abelthf/rows
def convert(input_encoding, output_encoding, input_locale, output_locale,
            verify_ssl, order_by, source, destination):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING

    if input_locale is not None:
        with rows.locale_context(input_locale):
            table = _import_table(source,
                                  encoding=input_encoding,
                                  verify_ssl=verify_ssl)
    else:
        table = _import_table(source,
                              encoding=input_encoding,
                              verify_ssl=verify_ssl)

    if order_by is not None:
        order_by = _get_field_names(order_by,
                                    table.fields.keys(),
                                    permit_not=True)
        # TODO: use complete list of `order_by` fields
        table.order_by(order_by[0].replace('^', '-'))

    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(destination, table, encoding=output_encoding)
    else:
        export_to_uri(destination, table, encoding=output_encoding)
Exemplo n.º 5
0
def convert(input_encoding, output_encoding, input_locale, output_locale,
            source, destination):

    with rows.locale_context(input_locale):
        table = import_from_uri(source)

    with rows.locale_context(output_locale):
        export_to_uri(destination, table)
Exemplo n.º 6
0
def sum_(
    input_encoding,
    output_encoding,
    input_locale,
    output_locale,
    verify_ssl,
    order_by,
    fields,
    fields_exclude,
    sources,
    destination,
):

    import_fields = _get_import_fields(fields, fields_exclude)
    if input_locale is not None:
        with rows.locale_context(input_locale):
            tables = [
                _import_table(
                    source,
                    encoding=input_encoding,
                    verify_ssl=verify_ssl,
                    import_fields=import_fields,
                )
                for source in sources
            ]
    else:
        tables = [
            _import_table(
                source,
                encoding=input_encoding,
                verify_ssl=verify_ssl,
                import_fields=import_fields,
            )
            for source in sources
        ]

    result = sum(tables)
    if order_by is not None:
        order_by = _get_field_names(order_by, result.field_names, permit_not=True)
        # TODO: use complete list of `order_by` fields
        result.order_by(order_by[0].replace("^", "-"))

    export_fields = _get_export_fields(result.field_names, fields_exclude)
    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING
    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(
                result,
                destination,
                encoding=output_encoding,
                export_fields=export_fields,
            )
    else:
        export_to_uri(
            result, destination, encoding=output_encoding, export_fields=export_fields
        )
Exemplo n.º 7
0
def sort(input_encoding, output_encoding, input_locale, output_locale, key,
         source, destination):
    key = key.replace('^', '-')

    with rows.locale_context(input_locale):
        table = import_from_uri(source)
        table.order_by(key)

    with rows.locale_context(output_locale):
        export_to_uri(destination, table)
Exemplo n.º 8
0
def join(input_encoding, output_encoding, input_locale, output_locale, keys,
         sources, destination):
    keys = [key.strip() for key in keys.split(',')]

    with rows.locale_context(input_locale):
        tables = [import_from_uri(source) for source in sources]

    result = rows.join(keys, tables)

    with rows.locale_context(output_locale):
        export_to_uri(destination, result)
Exemplo n.º 9
0
Arquivo: cli.py Projeto: abelthf/rows
def query(input_encoding, output_encoding, input_locale, output_locale,
          verify_ssl, fields, output, query, sources):

    # TODO: may move all 'destination' to '--output'
    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or sys.stdout.encoding or \
                      DEFAULT_OUTPUT_ENCODING
    if not query.lower().startswith('select'):
        field_names = '*' if fields is None else fields
        table_names = ', '.join(
            ['table{}'.format(index) for index in range(1,
                                                        len(sources) + 1)])
        query = 'SELECT {} FROM {} WHERE {}'.format(field_names, table_names,
                                                    query)
    if input_locale is not None:
        with rows.locale_context(input_locale):
            tables = [
                _import_table(source,
                              encoding=input_encoding,
                              verify_ssl=verify_ssl) for source in sources
            ]
    else:
        tables = [
            _import_table(source,
                          encoding=input_encoding,
                          verify_ssl=verify_ssl) for source in sources
        ]

    sqlite_connection = rows.export_to_sqlite(tables[0],
                                              ':memory:',
                                              table_name='table1')
    for index, table in enumerate(tables[1:], start=2):
        rows.export_to_sqlite(table,
                              sqlite_connection,
                              table_name='table{}'.format(index))

    result = rows.import_from_sqlite(sqlite_connection, query=query)

    if output is None:
        fobj = BytesIO()
        if output_locale is not None:
            with rows.locale_context(output_locale):
                rows.export_to_txt(result, fobj, encoding=output_encoding)
        else:
            rows.export_to_txt(result, fobj, encoding=output_encoding)
        fobj.seek(0)
        click.echo(fobj.read())
    else:
        if output_locale is not None:
            with rows.locale_context(output_locale):
                export_to_uri(output, result, encoding=output_encoding)
        else:
            export_to_uri(output, result, encoding=output_encoding)
Exemplo n.º 10
0
def sum(input_encoding, output_encoding, input_locale, output_locale, sources,
        destination):

    with rows.locale_context(input_locale):
        tables = [import_from_uri(source) for source in sources]

    result = tables[0]
    for table in tables[1:]:
        result = result + table

    with rows.locale_context(output_locale):
        export_to_uri(destination, result)
Exemplo n.º 11
0
def join(input_encoding, output_encoding, input_locale, output_locale,
         verify_ssl, order_by, fields, fields_exclude, keys, sources,
         destination):

    export_fields = _get_import_fields(fields, fields_exclude)
    keys = make_header(keys.split(','), permit_not=False)

    if input_locale is not None:
        with rows.locale_context(input_locale):
            tables = [
                _import_table(source,
                              encoding=input_encoding,
                              verify_ssl=verify_ssl) for source in sources
            ]
    else:
        tables = [
            _import_table(source,
                          encoding=input_encoding,
                          verify_ssl=verify_ssl) for source in sources
        ]

    result = rows.join(keys, tables)
    if order_by is not None:
        order_by = _get_field_names(order_by,
                                    result.field_names,
                                    permit_not=True)
        # TODO: use complete list of `order_by` fields
        result.order_by(order_by[0].replace('^', '-'))

    if export_fields is None:
        export_fields = _get_export_fields(result.field_names, fields_exclude)
    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or DEFAULT_OUTPUT_ENCODING
    if output_locale is not None:
        with rows.locale_context(output_locale):
            export_to_uri(result,
                          destination,
                          encoding=output_encoding,
                          export_fields=export_fields)
    else:
        export_to_uri(result,
                      destination,
                      encoding=output_encoding,
                      export_fields=export_fields)
Exemplo n.º 12
0
def query(input_encoding, output_encoding, input_locale, output_locale,
          verify_ssl, fields, output, query, sources):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or sys.stdout.encoding or \
                      DEFAULT_OUTPUT_ENCODING

    if not query.lower().startswith('select'):
        field_names = '*' if fields is None else fields
        table_names = ', '.join(['table{}'.format(index)
                                 for index in range(1, len(sources) + 1)])
        query = 'SELECT {} FROM {} WHERE {}'.format(field_names, table_names,
                                                    query)

    if len(sources) == 1:
        source = detect_source(sources[0], verify_ssl=verify_ssl)

        if source.plugin_name != 'sqlite':
            if input_locale is not None:
                with rows.locale_context(input_locale):
                    table = import_from_source(source, DEFAULT_INPUT_ENCODING)
            else:
                table = import_from_source(source, DEFAULT_INPUT_ENCODING)

            sqlite_connection = sqlite3.Connection(':memory:')
            rows.export_to_sqlite(table,
                                  sqlite_connection,
                                  table_name='table1')
            result = rows.import_from_sqlite(sqlite_connection, query=query)

        else:
            # Optimization: query the SQLite database directly
            result = import_from_source(source,
                                        DEFAULT_INPUT_ENCODING,
                                        query=query)

    else:
        if input_locale is not None:
            with rows.locale_context(input_locale):
                tables = [_import_table(source, encoding=input_encoding,
                                        verify_ssl=verify_ssl)
                          for source in sources]
        else:
            tables = [_import_table(source, encoding=input_encoding,
                                    verify_ssl=verify_ssl)
                      for source in sources]

        sqlite_connection = sqlite3.Connection(':memory:')
        for index, table in enumerate(tables, start=1):
            rows.export_to_sqlite(table,
                                  sqlite_connection,
                                  table_name='table{}'.format(index))

        result = rows.import_from_sqlite(sqlite_connection, query=query)

    if output is None:
        fobj = BytesIO()
        if output_locale is not None:
            with rows.locale_context(output_locale):
                rows.export_to_txt(result, fobj, encoding=output_encoding)
        else:
            rows.export_to_txt(result, fobj, encoding=output_encoding)
        fobj.seek(0)
        click.echo(fobj.read())
    else:
        if output_locale is not None:
            with rows.locale_context(output_locale):
                export_to_uri(result, output, encoding=output_encoding)
        else:
            export_to_uri(result, output, encoding=output_encoding)
Exemplo n.º 13
0
def query(
    input_encoding,
    output_encoding,
    input_locale,
    output_locale,
    verify_ssl,
    samples,
    output,
    frame_style,
    query,
    sources,
):

    samples = samples if samples > 0 else None

    if not query.lower().startswith("select"):
        table_names = ", ".join(
            ["table{}".format(index) for index in range(1, len(sources) + 1)]
        )
        query = "SELECT * FROM {} WHERE {}".format(table_names, query)

    if len(sources) == 1:
        source = detect_source(sources[0], verify_ssl=verify_ssl, progress=True)

        if source.plugin_name in ("sqlite", "postgresql"):
            # Optimization: query the db directly
            result = import_from_source(
                source, DEFAULT_INPUT_ENCODING, query=query, samples=samples
            )
        else:
            if input_locale is not None:
                with rows.locale_context(input_locale):
                    table = import_from_source(
                        source, DEFAULT_INPUT_ENCODING, samples=samples
                    )
            else:
                table = import_from_source(
                    source, DEFAULT_INPUT_ENCODING, samples=samples
                )

            sqlite_connection = sqlite3.Connection(":memory:")
            rows.export_to_sqlite(table, sqlite_connection, table_name="table1")
            result = rows.import_from_sqlite(sqlite_connection, query=query)

    else:
        # TODO: if all sources are SQLite we can also optimize the import
        if input_locale is not None:
            with rows.locale_context(input_locale):
                tables = [
                    _import_table(
                        source,
                        encoding=input_encoding,
                        verify_ssl=verify_ssl,
                        samples=samples,
                    )
                    for source in sources
                ]
        else:
            tables = [
                _import_table(
                    source,
                    encoding=input_encoding,
                    verify_ssl=verify_ssl,
                    samples=samples,
                )
                for source in sources
            ]

        sqlite_connection = sqlite3.Connection(":memory:")
        for index, table in enumerate(tables, start=1):
            rows.export_to_sqlite(
                table, sqlite_connection, table_name="table{}".format(index)
            )

        result = rows.import_from_sqlite(sqlite_connection, query=query)

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or sys.stdout.encoding or DEFAULT_OUTPUT_ENCODING
    if output is None:
        fobj = BytesIO()
        if output_locale is not None:
            with rows.locale_context(output_locale):
                rows.export_to_txt(
                    result, fobj, encoding=output_encoding, frame_style=frame_style
                )
        else:
            rows.export_to_txt(
                result, fobj, encoding=output_encoding, frame_style=frame_style
            )
        fobj.seek(0)
        click.echo(fobj.read())
    else:
        if output_locale is not None:
            with rows.locale_context(output_locale):
                export_to_uri(result, output, encoding=output_encoding)
        else:
            export_to_uri(result, output, encoding=output_encoding)
Exemplo n.º 14
0
Arquivo: cli.py Projeto: wnlima/rows
def query(input_encoding, output_encoding, input_locale, output_locale,
          verify_ssl, fields, output, query, sources):

    # TODO: may use sys.stdout.encoding if output_file = '-'
    output_encoding = output_encoding or sys.stdout.encoding or \
                      DEFAULT_OUTPUT_ENCODING

    if not query.lower().startswith('select'):
        field_names = '*' if fields is None else fields
        table_names = ', '.join(
            ['table{}'.format(index) for index in range(1,
                                                        len(sources) + 1)])
        query = 'SELECT {} FROM {} WHERE {}'.format(field_names, table_names,
                                                    query)

    if len(sources) == 1:
        source = detect_source(sources[0], verify_ssl=verify_ssl)

        if source.plugin_name != 'sqlite':
            if input_locale is not None:
                with rows.locale_context(input_locale):
                    table = import_from_source(source, DEFAULT_INPUT_ENCODING)
            else:
                table = import_from_source(source, DEFAULT_INPUT_ENCODING)

            sqlite_connection = sqlite3.Connection(':memory:')
            rows.export_to_sqlite(table,
                                  sqlite_connection,
                                  table_name='table1')
            result = rows.import_from_sqlite(sqlite_connection, query=query)

        else:
            # Optimization: query the SQLite database directly
            result = import_from_source(source,
                                        DEFAULT_INPUT_ENCODING,
                                        query=query)

    else:
        if input_locale is not None:
            with rows.locale_context(input_locale):
                tables = [
                    _import_table(source,
                                  encoding=input_encoding,
                                  verify_ssl=verify_ssl) for source in sources
                ]
        else:
            tables = [
                _import_table(source,
                              encoding=input_encoding,
                              verify_ssl=verify_ssl) for source in sources
            ]

        sqlite_connection = sqlite3.Connection(':memory:')
        for index, table in enumerate(tables, start=1):
            rows.export_to_sqlite(table,
                                  sqlite_connection,
                                  table_name='table{}'.format(index))

        result = rows.import_from_sqlite(sqlite_connection, query=query)

    if output is None:
        fobj = BytesIO()
        if output_locale is not None:
            with rows.locale_context(output_locale):
                rows.export_to_txt(result, fobj, encoding=output_encoding)
        else:
            rows.export_to_txt(result, fobj, encoding=output_encoding)
        fobj.seek(0)
        click.echo(fobj.read())
    else:
        if output_locale is not None:
            with rows.locale_context(output_locale):
                export_to_uri(result, output, encoding=output_encoding)
        else:
            export_to_uri(result, output, encoding=output_encoding)