Changeset - cc8c03ab626a
[Not reviewed]
0 5 0
Brett Smith - 6 years ago 2017-12-19 14:13:44
brettcsmith@brettcsmith.org
main: Provide template variables about the file being imported.
5 files changed with 55 insertions and 18 deletions:
0 comments (0 inline, 0 general)
README.rst
Show inline comments
...
 
@@ -78,2 +78,9 @@ date               The date of the transaction, in your configured output
 
payee              The name of the transaction payee
 
------------------ ----------------------------------------------------------
 
source_abspath     The absolute path of the file being imported
 
------------------ ----------------------------------------------------------
 
source_name        The filename of the file being imported
 
------------------ ----------------------------------------------------------
 
source_path        The path of the file being imported, as specified on the
 
                   command line
 
================== ==========================================================
import2ledger/__main__.py
Show inline comments
 
import collections
 
import contextlib
...
 
@@ -15,3 +16,5 @@ class FileImporter:
 

	
 
    def import_file(self, in_file):
 
    def import_file(self, in_file, in_path=None):
 
        if in_path is None:
 
            in_path = pathlib.Path(in_file.name)
 
        importers = []
...
 
@@ -33,2 +36,7 @@ class FileImporter:
 
            raise errors.UserInputFileError("no importers available", in_file.name)
 
        source_vars = {
 
            'source_abspath': in_path.absolute().as_posix(),
 
            'source_name': in_path.name,
 
            'source_path': in_path.as_posix(),
 
        }
 
        with contextlib.ExitStack() as exit_stack:
...
 
@@ -50,3 +58,4 @@ class FileImporter:
 
                        del entry_data['_hook_cancel']
 
                        print(template.render(**entry_data), file=out_file, end='')
 
                        render_vars = collections.ChainMap(entry_data, source_vars)
 
                        print(template.render(render_vars), file=out_file, end='')
 

	
...
 
@@ -58,3 +67,3 @@ class FileImporter:
 
                raise errors.UserInputFileError("only seekable files are supported", in_path)
 
            return self.import_file(in_file)
 
            return self.import_file(in_file, in_path)
 

	
tests/data/test_main.ini
Show inline comments
...
 
@@ -10,2 +10,4 @@ template patreon cardfees =
 
template patreon svcfees =
 
 ;SourcePath: {source_abspath}
 
 ;SourceName: {source_name}
 
 Accrued:Accounts Receivable  -{amount}
tests/data/test_main_fees_import.ledger
Show inline comments
...
 
@@ -4,6 +4,2 @@
 

	
 
2017/09/01 Patreon
 
  Accrued:Accounts Receivable  $-61.73
 
  Expenses:Fundraising  $61.73
 

	
 
2017/10/01 Patreon
...
 
@@ -12,3 +8,11 @@
 

	
 
2017/09/01 Patreon
 
  ;SourcePath: {source_abspath}
 
  ;SourceName: {source_name}
 
  Accrued:Accounts Receivable  $-61.73
 
  Expenses:Fundraising  $61.73
 

	
 
2017/10/01 Patreon
 
  ;SourcePath: {source_abspath}
 
  ;SourceName: {source_name}
 
  Accrued:Accounts Receivable  $-117.03
tests/test_main.py
Show inline comments
...
 
@@ -32,6 +32,11 @@ def iter_entries(in_file):
 

	
 
def entries2set(in_file):
 
    return set(normalize_whitespace(e) for e in iter_entries(in_file))
 
def format_entry(entry_s, format_vars):
 
    return normalize_whitespace(entry_s).format_map(format_vars)
 

	
 
def expected_entries(path):
 
def format_entries(source, format_vars=None):
 
    if format_vars is None:
 
        format_vars = {}
 
    return (format_entry(e, format_vars) for e in iter_entries(source))
 

	
 
def expected_entries(path, format_vars=None):
 
    path = pathlib.Path(path)
...
 
@@ -40,8 +45,16 @@ def expected_entries(path):
 
    with path.open() as in_file:
 
        return entries2set(in_file)
 
        return list(format_entries(in_file, format_vars))
 

	
 
def path_vars(path):
 
    return {
 
        'source_abspath': str(path),
 
        'source_name': path.name,
 
        'source_path': str(path),
 
    }
 

	
 
def test_fees_import():
 
    source_path = pathlib.Path(DATA_DIR, 'PatreonEarnings.csv')
 
    arglist = ARGLIST + [
 
        '-c', 'One',
 
        pathlib.Path(DATA_DIR, 'PatreonEarnings.csv').as_posix(),
 
        source_path.as_posix(),
 
    ]
...
 
@@ -49,6 +62,8 @@ def test_fees_import():
 
    assert exitcode == 0
 
    actual = entries2set(stdout)
 
    assert actual == expected_entries('test_main_fees_import.ledger')
 
    actual = list(format_entries(stdout))
 
    expected = expected_entries('test_main_fees_import.ledger', path_vars(source_path))
 
    assert actual == expected
 

	
 
def test_date_range_import():
 
    source_path = pathlib.Path(DATA_DIR, 'PatreonEarnings.csv')
 
    arglist = ARGLIST + [
...
 
@@ -56,3 +71,3 @@ def test_date_range_import():
 
        '--date-range', '2017/10/01-',
 
        pathlib.Path(DATA_DIR, 'PatreonEarnings.csv').as_posix(),
 
        source_path.as_posix(),
 
    ]
...
 
@@ -60,5 +75,5 @@ def test_date_range_import():
 
    assert exitcode == 0
 
    actual = entries2set(stdout)
 
    expected = {entry for entry in expected_entries('test_main_fees_import.ledger')
 
                if entry.startswith('2017/10/')}
 
    actual = list(format_entries(stdout))
 
    valid = expected_entries('test_main_fees_import.ledger', path_vars(source_path))
 
    expected = [entry for entry in valid if entry.startswith('2017/10/')]
 
    assert actual == expected
0 comments (0 inline, 0 general)