Skip to content

Commit 4e7bce3

Browse files
committed
Use global config module
1 parent 01f98aa commit 4e7bce3

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

ledgerautosync/cli.py

+45-42
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import os.path
3737
import imp
3838

39+
from ledgerautosync import config
40+
3941

4042
def find_ledger_file(ledgerrcpath=None):
4143
"""Returns main ledger file path or raise exception if it cannot be \
@@ -124,25 +126,25 @@ def make_ofx_converter(account,
124126
security_list=security_list,
125127
date_format=date_format)
126128

127-
def sync(ledger, accounts, args):
128-
sync = OfxSynchronizer(ledger, shortenaccount=args.shortenaccount)
129+
def sync(ledger, accounts):
130+
sync = OfxSynchronizer(ledger, shortenaccount=config.shortenaccount)
129131
for acct in accounts:
130132
try:
131-
(ofx, txns) = sync.get_new_txns(acct, resync=args.resync,
132-
max_days=args.max)
133+
(ofx, txns) = sync.get_new_txns(acct, resync=config.resync,
134+
max_days=config.max)
133135
if ofx is not None:
134136
converter = make_ofx_converter(account=ofx.account,
135137
name=acct.description,
136138
ledger=ledger,
137-
indent=args.indent,
139+
indent=config.indent,
138140
fid=None,
139-
unknownaccount=args.unknownaccount,
140-
payee_format=args.payee_format,
141+
unknownaccount=config.unknownaccount,
142+
payee_format=config.payee_format,
141143
hardcodeaccount=None,
142-
shortenaccount=args.shortenaccount,
144+
shortenaccount=config.shortenaccount,
143145
security_list=SecurityList(ofx),
144-
date_format=args.date_format)
145-
print_results(converter, ofx, ledger, txns, args)
146+
date_format=config.date_format)
147+
print_results(converter, ofx, ledger, txns, config)
146148
except KeyboardInterrupt:
147149
raise
148150
except BaseException:
@@ -151,14 +153,14 @@ def sync(ledger, accounts, args):
151153
traceback.print_exc(file=sys.stderr)
152154

153155

154-
def import_ofx(ledger, args):
155-
sync = OfxSynchronizer(ledger, hardcodeaccount=args.hardcodeaccount,
156-
shortenaccount=args.shortenaccount)
157-
ofx = OfxSynchronizer.parse_file(args.PATH)
156+
def import_ofx(ledger):
157+
sync = OfxSynchronizer(ledger, hardcodeaccount=config.hardcodeaccount,
158+
shortenaccount=config.shortenaccount)
159+
ofx = OfxSynchronizer.parse_file(config.PATH)
158160
txns = sync.filter(
159161
ofx.account.statement.transactions,
160162
ofx.account.account_id)
161-
accountname = args.account
163+
accountname = config.account
162164
if accountname is None:
163165
if ofx.account.institution is not None:
164166
accountname = "%s:%s" % (ofx.account.institution.organization,
@@ -172,28 +174,28 @@ def import_ofx(ledger, args):
172174
converter = make_ofx_converter(account=ofx.account,
173175
name=accountname,
174176
ledger=ledger,
175-
indent=args.indent,
176-
fid=args.fid,
177-
unknownaccount=args.unknownaccount,
178-
payee_format=args.payee_format,
179-
hardcodeaccount=args.hardcodeaccount,
180-
shortenaccount=args.shortenaccount,
177+
indent=config.indent,
178+
fid=config.fid,
179+
unknownaccount=config.unknownaccount,
180+
payee_format=config.payee_format,
181+
hardcodeaccount=config.hardcodeaccount,
182+
shortenaccount=config.shortenaccount,
181183
security_list=security_list,
182-
date_format=args.date_format)
183-
print_results(converter, ofx, ledger, txns, args)
184+
date_format=config.date_format)
185+
print_results(converter, ofx, ledger, txns, config)
184186

185187

186-
def import_csv(ledger, args):
187-
if args.account is None:
188+
def import_csv(ledger):
189+
if config.account is None:
188190
raise Exception(
189191
"When importing a CSV file, you must specify an account name.")
190-
sync = CsvSynchronizer(ledger, payee_format=args.payee_format, date_format=args.date_format)
191-
txns = sync.parse_file(args.PATH, accountname=args.account,
192-
unknownaccount=args.unknownaccount)
193-
if args.reverse:
192+
sync = CsvSynchronizer(ledger, payee_format=config.payee_format, date_format=config.date_format)
193+
txns = sync.parse_file(config.PATH, accountname=config.account,
194+
unknownaccount=config.unknownaccount)
195+
if config.reverse:
194196
txns = reversed(txns)
195197
for txn in txns:
196-
print(txn.format(args.indent, args.assertions))
198+
print(txn.format(config.indent, config.assertions))
197199

198200

199201
def load_plugins(config_dir):
@@ -212,7 +214,7 @@ def load_plugins(config_dir):
212214
(os.path.splitext(plugin)[0]), path)
213215

214216

215-
def run(args=None, config=None):
217+
def run(args=None, ofx_config=None):
216218
if args is None:
217219
args = sys.argv[1:]
218220

@@ -293,15 +295,16 @@ def run(args=None, config=None):
293295
parser.add_argument('-y', '--date-format', type=str, default=None, dest="date_format",
294296
help="""Format string to use for printing dates.
295297
See strftime for details on format string syntax. Default is "%%Y/%%m/%%d".""")
296-
args = parser.parse_args(args)
298+
299+
args = parser.parse_args(args, namespace=config)
297300
if sys.argv[0][-16:] == "hledger-autosync":
298-
args.hledger = True
301+
config.hledger = True
299302

300303
ledger_file = None
301-
if args.ledger and args.no_ledger:
304+
if config.ledger and config.no_ledger:
302305
raise LedgerAutosyncException(
303306
'You cannot specify a ledger file and -L')
304-
elif args.ledger:
307+
elif config.ledger:
305308
ledger_file = args.ledger
306309
else:
307310
ledger_file = find_ledger_file()
@@ -341,26 +344,26 @@ def run(args=None, config=None):
341344
load_plugins(config_dir)
342345

343346
if args.PATH is None:
344-
if config is None:
347+
if ofx_config is None:
345348
if args.ofxconfig is None:
346349
config_file = os.path.join(config_dir, 'ofxclient.ini')
347350
else:
348351
config_file = args.ofxconfig
349352
if (os.path.exists(config_file)):
350-
config = OfxConfig(file_name=config_file)
353+
ofx_config = OfxConfig(file_name=config_file)
351354
else:
352-
config = OfxConfig()
353-
accounts = config.accounts()
355+
ofx_config = OfxConfig()
356+
accounts = ofx_config.accounts()
354357
if args.account:
355358
accounts = [acct for acct in accounts
356359
if acct.description == args.account]
357-
sync(ledger, accounts, args)
360+
sync(ledger, accounts)
358361
else:
359362
_, file_extension = os.path.splitext(args.PATH.lower())
360363
if file_extension == '.csv':
361-
import_csv(ledger, args)
364+
import_csv(ledger)
362365
else:
363-
import_ofx(ledger, args)
366+
import_ofx(ledger)
364367

365368

366369
if __name__ == '__main__':

ledgerautosync/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/test_cli.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# flake8: noqa E501
2020

21-
from ledgerautosync import LedgerAutosyncException
21+
from ledgerautosync import LedgerAutosyncException, config
2222
from ledgerautosync.cli import run, find_ledger_file
2323
from ledgerautosync.ledgerwrap import Ledger, LedgerPython, HLedger
2424
from ofxclient.config import OfxConfig
@@ -94,6 +94,7 @@ def test_no_ledger_arg(self):
9494
'-L'], config)
9595

9696
def test_format_payee(self):
97+
print(dir(config))
9798
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
9899
run([os.path.join('fixtures', 'paypal.csv'), '-a',
99100
'Assets:Foo', '--payee-format', 'GROSS:{Gross}', '-L'])
@@ -129,6 +130,11 @@ def setUp(self):
129130
self.checking_lgr = Ledger(os.path.join('fixtures', 'checking.lgr'),
130131
no_pipe=True)
131132

133+
def tearDown(self):
134+
for attr in dir(config):
135+
if not (attr.startswith("__") or attr == "PATH"):
136+
delattr(config, attr)
137+
132138

133139
@attr('ledger-python')
134140
class TestCliLedgerPython(TestCase, CliTest):

0 commit comments

Comments
 (0)