def test_write(self): url = 'http://google.com' body = '<!doctype html><html itemtype="http://schema.org/page">' content = StringIO('Iñtërnâtiônàližætiøn') nt.assert_equal(20, io.write(StringIO(), content)) content.seek(0) nt.assert_equal(28, io.write(TemporaryFile(), content)) content = io.IterStringIO(iter('Hello World')) nt.assert_equal(12, io.write(TemporaryFile(), content, chunksize=2)) responses.add(responses.GET, url=url, body=body) r = requests.get(url, stream=True) nt.assert_equal(55, io.write(TemporaryFile(), r.iter_content))
def test_write(self): # pylint: disable=R0201 """Test for writing to a file""" url = 'http://google.com' body = '<!doctype html><html itemtype="http://schema.org/page">' content1 = StringIO('Iñtërnâtiônàližætiøn') nt.assert_equal(20, io.write(StringIO(), content1)) content1.seek(0) nt.assert_equal(20, io.write(TemporaryFile(), content1)) content2 = io.IterStringIO(iter('Hello World')) nt.assert_equal(12, io.write(TemporaryFile(), content2, chunksize=2)) # pylint: disable=E1101 responses.add(responses.GET, url=url, body=body) r = requests.get(url, stream=True) # pylint: disable=C0103 nt.assert_equal(55, io.write(TemporaryFile(), r.iter_content))
def test_write(self): # pylint: disable=R0201 """Test for writing to a file""" url = "http://google.com" body = '<!doctype html><html itemtype="http://schema.org/page">' content1 = StringIO("Iñtërnâtiônàližætiøn") nt.assert_equal(20, io.write(StringIO(), content1)) content1.seek(0) nt.assert_equal(20, io.write(TemporaryFile(), content1)) content2 = io.IterStringIO(iter("Hello World")) nt.assert_equal(12, io.write(TemporaryFile(), content2, chunksize=2)) # pylint: disable=E1101 responses.add(responses.GET, url=url, body=body) r = requests.get(url, stream=True) # pylint: disable=C0103 nt.assert_equal(55, io.write(TemporaryFile(), r.iter_content))
def run(): # noqa: C901 """Parses the CLI options and runs the main program """ if args.debug: pprint(dict(args._get_kwargs())) # pylint: disable=W0212 exit(0) if args.version: from . import __version__ as version print('v%s' % version) exit(0) if args.list_mappings: print(', '.join(MODULES)) exit(0) if args.custom: name = p.splitext(p.split(args.custom)[1])[0] found = find_module(name, [p.dirname(p.abspath(args.custom))]) module = load_module(name, *found) else: module = import_module('csv2ofx.mappings.%s' % args.mapping) mapping = module.mapping okwargs = { 'def_type': args.account_type or 'Bank' if args.qif else 'CHECKING', 'start': parse(args.start) if args.start else None, 'end': parse(args.end) if args.end else None } cont = QIF(mapping, **okwargs) if args.qif else OFX(mapping, **okwargs) source = open(args.source, encoding=args.encoding) if args.source else stdin delimiter = mapping.get('delimiter', ',') try: records = read_csv(source, has_header=cont.has_header, delimiter=delimiter) groups = cont.gen_groups(records, args.chunksize) trxns = cont.gen_trxns(groups, args.collapse) cleaned_trxns = cont.clean_trxns(trxns) data = utils.gen_data(cleaned_trxns) body = cont.gen_body(data) if args.server_date: server_date = parse(args.server_date) else: try: mtime = p.getmtime(source.name) except (AttributeError, FileNotFoundError): mtime = time.time() server_date = dt.fromtimestamp(mtime) header = cont.header(date=server_date, language=args.language) footer = cont.footer(date=server_date) filtered = filter(None, [header, body, footer]) content = it.chain.from_iterable(filtered) kwargs = { 'overwrite': args.overwrite, 'chunksize': args.chunksize, 'encoding': args.encoding } except Exception as err: # pylint: disable=broad-except source.close() exit(err) dest = open(args.dest, 'w', encoding=args.encoding) if args.dest else stdout try: res = write(dest, IterStringIO(content), **kwargs) except KeyError as err: msg = 'Field %s is missing from file. Check `mapping` option.' % err except TypeError as err: msg = 'No data to write. %s. ' % str(err) if args.collapse: msg += 'Check `start` and `end` options.' else: msg += 'Try again with `-c` option.' except ValueError as err: # csv2ofx called with no arguments or broken mapping msg = 'Possible mapping problem: %s. ' % str(err) parser.print_help() except Exception as err: # pylint: disable=broad-except msg = 1 traceback.print_exc() else: msg = 0 if res else 'No data to write. Check `start` and `end` options.' finally: exit(msg) source.close() if args.source else None dest.close() if args.dest else None
def run(): # noqa: C901 """Parses the CLI options and runs the main program """ if args.debug: pprint(dict(args._get_kwargs())) # pylint: disable=W0212 exit(0) if args.version: from . import __version__ as version print("v%s" % version) exit(0) if args.list_mappings: print(", ".join(MODULES)) exit(0) if args.custom: name = p.splitext(p.split(args.custom)[1])[0] found = find_module(name, [p.dirname(p.abspath(args.custom))]) module = load_module(name, *found) else: module = import_module("csv2ofx.mappings.%s" % args.mapping) mapping = module.mapping okwargs = { "def_type": args.account_type or "Bank" if args.qif else "CHECKING", "split_header": args.split, "start": parse(args.start) if args.start else None, "end": parse(args.end) if args.end else None, } cont = QIF(mapping, **okwargs) if args.qif else OFX(mapping, **okwargs) source = open(args.source, encoding=args.encoding) if args.source else stdin try: records = read_csv(source, has_header=cont.has_header) groups = cont.gen_groups(records, args.chunksize) trxns = cont.gen_trxns(groups, args.collapse) cleaned_trxns = cont.clean_trxns(trxns) data = utils.gen_data(cleaned_trxns) body = cont.gen_body(data) if args.server_date: server_date = parse(args.server_date) else: try: mtime = p.getmtime(source.name) except AttributeError: mtime = time.time() server_date = dt.fromtimestamp(mtime) header = cont.header(date=server_date, language=args.language) footer = cont.footer(date=server_date) filtered = filter(None, [header, body, footer]) content = it.chain.from_iterable(filtered) kwargs = {"overwrite": args.overwrite, "chunksize": args.chunksize, "encoding": args.encoding} except: source.close() raise dest = open(args.dest, "w", encoding=args.encoding) if args.dest else stdout try: res = write(dest, IterStringIO(content), **kwargs) except KeyError as err: msg = "Field %s is missing from file. Check `mapping` option." % err except TypeError as err: msg = "No data to write. %s. " % str(err) if args.collapse: msg += "Check `start` and `end` options." else: msg += "Try again with `-c` option." except Exception as err: # pylint: disable=broad-except msg = 1 traceback.print_exc() else: msg = 0 if res else "No data to write. Check `start` and `end` options." finally: exit(msg) source.close() if args.source else None dest.close() if args.dest else None