def parse_file(self, path, accountname=None, unknownaccount=None): with open(path) as f: has_bom = f.read(3) == codecs.BOM_UTF8 if not(has_bom): f.seek(0) else: f.seek(3) dialect = csv.Sniffer().sniff(f.read(1024)) if not(has_bom): f.seek(0) else: f.seek(3) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter( set(reader.fieldnames), dialect, name=accountname, unknownaccount=unknownaccount, payee_format=self.payee_format) # Create a new reader in case the converter modified the dialect if not(has_bom): f.seek(0) else: f.seek(3) reader = csv.DictReader(f, dialect=dialect) return [ converter.convert(row) for row in reader if not( self.is_row_synced( converter, row))]
def make_converter(self, f, name=None): dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter(set(reader.fieldnames), dialect, name) f.seek(0) reader = csv.DictReader(f, dialect=dialect) return (reader, converter)
def make_converter(self, f, name=None): dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter( set(reader.fieldnames), dialect, name) f.seek(0) reader = csv.DictReader(f, dialect=dialect) return (reader, converter)
def parse_file(self, path, accountname=None, unknownaccount=None): with open(path, 'rb') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter( reader, name=accountname, unknownaccount=unknownaccount, payee_format=self.payee_format) return [ converter.convert(row) for row in reader if not (self.is_row_synced(converter, row)) ]
def test_mint_format(reader): converter = CsvConverter.make_converter(set(reader.fieldnames), reader.dialect) assert type(converter) == MintConverter assert (converter.convert(next(reader)).format() == """2016/08/02 Amazon 1234 $29.99 ; csvid: mint.a7c028a73d76956453dab634e8e5bdc1 Expenses:Shopping -$29.99 """) assert (converter.convert( next(reader)).format() == """2016/06/02 Autopay Rautopay Auto 1234 -$123.45 ; csvid: mint.a404e70594502dd62bfc6f15d80b7cd7 Credit Card Payment $123.45 """)
def test_format(self): with open('fixtures/amazon.csv', 'rb') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter(reader, name='Foo') self.assertEqual(type(converter), AmazonConverter) self.assertEqual( converter.convert(reader.next()).format(), """2016/01/29 Best Soap Ever ; url: https://www.amazon.com/gp/css/summary/print.html/ref=od_aui_print_invoice?ie=UTF8&orderID=123-4567890-1234567 ; csvid: amazon.123-4567890-1234567 Foo $21.90 Expenses:Misc -$21.90 """)
def parse_file(self, path, accountname=None, unknownaccount=None): with open(path, 'rb') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter( reader, name=accountname, ledger=self.lgr, unknownaccount=unknownaccount) return [ converter.convert(row) for row in reader if not (self.lgr.check_transaction_by_id( "csvid", converter.get_csv_id(row))) ]
def parse_file(self, path, accountname=None, unknownaccount=None): with codecs.open(path, 'r', encoding='utf-8-sig') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter( set(reader.fieldnames), dialect, name=accountname, unknownaccount=unknownaccount, payee_format=self.payee_format) # Create a new reader in case the converter modified the dialect f.seek(0) reader = csv.DictReader(f, dialect=dialect) return [ converter.convert(row) for row in reader if not (self.is_row_synced(converter, row)) ]
def test_format(self): with open('fixtures/mint.csv', 'rb') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter(reader) self.assertEqual(type(converter), MintConverter) self.assertEqual( converter.convert(reader.next()).format(), """2016/08/02 Amazon ; csvid: mint.a7c028a73d76956453dab634e8e5bdc1 1234 $29.99 Expenses:Shopping -$29.99 """) self.assertEqual( converter.convert(reader.next()).format(), """2016/06/02 Autopay Rautopay Auto ; csvid: mint.a404e70594502dd62bfc6f15d80b7cd7 1234 -$123.45 Credit Card Payment $123.45 """)
def test_format(self): with open('fixtures/paypal.csv', 'rb') as f: dialect = csv.Sniffer().sniff(f.read(1024)) f.seek(0) dialect.skipinitialspace = True reader = csv.DictReader(f, dialect=dialect) converter = CsvConverter.make_converter(reader, name='Foo') self.assertEqual(type(converter), PaypalConverter) self.assertEqual( converter.convert(reader.next()).format(), """2016/06/04 Jane Doe [email protected] My Friend ID: XYZ1, Recurring Payment Sent ; csvid: paypal.XYZ1 Foo -20.00 USD Expenses:Misc 20.00 USD """) self.assertEqual( converter.convert(reader.next()).format(), """2016/06/04 Debit Card ID: XYZ2, Charge From Debit Card ; csvid: paypal.XYZ2 Foo 20.00 USD Transfer:Paypal -20.00 USD """)
def test_get_csv_id(self): converter = CsvConverter(None) h = {'foo': 'bar', 'bar': 'foo'} self.assertEqual(converter.get_csv_id(h), hashlib.md5("bar=foo\nfoo=bar\n").hexdigest())
def converter(reader): return CsvConverter.make_converter(set(reader.fieldnames), reader.dialect, "Foo")
def test_get_csv_id(): converter = CsvConverter(None) h = {"foo": "bar", "bar": "foo"} assert (converter.get_csv_id(h) == hashlib.md5( "bar=foo\nfoo=bar\n".encode("utf-8")).hexdigest())
def test_get_csv_id(self): converter = CsvConverter(None) h = {'foo': 'bar', 'bar': 'foo'} self.assertEqual(converter.get_csv_id(h), hashlib.md5( "bar=foo\nfoo=bar\n".encode('utf-8')).hexdigest())