36
36
import os .path
37
37
import imp
38
38
39
+ from ledgerautosync import config
40
+
39
41
40
42
def find_ledger_file (ledgerrcpath = None ):
41
43
"""Returns main ledger file path or raise exception if it cannot be \
@@ -124,25 +126,25 @@ def make_ofx_converter(account,
124
126
security_list = security_list ,
125
127
date_format = date_format )
126
128
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 )
129
131
for acct in accounts :
130
132
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 )
133
135
if ofx is not None :
134
136
converter = make_ofx_converter (account = ofx .account ,
135
137
name = acct .description ,
136
138
ledger = ledger ,
137
- indent = args .indent ,
139
+ indent = config .indent ,
138
140
fid = None ,
139
- unknownaccount = args .unknownaccount ,
140
- payee_format = args .payee_format ,
141
+ unknownaccount = config .unknownaccount ,
142
+ payee_format = config .payee_format ,
141
143
hardcodeaccount = None ,
142
- shortenaccount = args .shortenaccount ,
144
+ shortenaccount = config .shortenaccount ,
143
145
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 )
146
148
except KeyboardInterrupt :
147
149
raise
148
150
except BaseException :
@@ -151,14 +153,14 @@ def sync(ledger, accounts, args):
151
153
traceback .print_exc (file = sys .stderr )
152
154
153
155
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 )
158
160
txns = sync .filter (
159
161
ofx .account .statement .transactions ,
160
162
ofx .account .account_id )
161
- accountname = args .account
163
+ accountname = config .account
162
164
if accountname is None :
163
165
if ofx .account .institution is not None :
164
166
accountname = "%s:%s" % (ofx .account .institution .organization ,
@@ -172,28 +174,28 @@ def import_ofx(ledger, args):
172
174
converter = make_ofx_converter (account = ofx .account ,
173
175
name = accountname ,
174
176
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 ,
181
183
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 )
184
186
185
187
186
- def import_csv (ledger , args ):
187
- if args .account is None :
188
+ def import_csv (ledger ):
189
+ if config .account is None :
188
190
raise Exception (
189
191
"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 :
194
196
txns = reversed (txns )
195
197
for txn in txns :
196
- print (txn .format (args .indent , args .assertions ))
198
+ print (txn .format (config .indent , config .assertions ))
197
199
198
200
199
201
def load_plugins (config_dir ):
@@ -212,7 +214,7 @@ def load_plugins(config_dir):
212
214
(os .path .splitext (plugin )[0 ]), path )
213
215
214
216
215
- def run (args = None , config = None ):
217
+ def run (args = None , ofx_config = None ):
216
218
if args is None :
217
219
args = sys .argv [1 :]
218
220
@@ -293,15 +295,16 @@ def run(args=None, config=None):
293
295
parser .add_argument ('-y' , '--date-format' , type = str , default = None , dest = "date_format" ,
294
296
help = """Format string to use for printing dates.
295
297
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 )
297
300
if sys .argv [0 ][- 16 :] == "hledger-autosync" :
298
- args .hledger = True
301
+ config .hledger = True
299
302
300
303
ledger_file = None
301
- if args .ledger and args .no_ledger :
304
+ if config .ledger and config .no_ledger :
302
305
raise LedgerAutosyncException (
303
306
'You cannot specify a ledger file and -L' )
304
- elif args .ledger :
307
+ elif config .ledger :
305
308
ledger_file = args .ledger
306
309
else :
307
310
ledger_file = find_ledger_file ()
@@ -341,26 +344,26 @@ def run(args=None, config=None):
341
344
load_plugins (config_dir )
342
345
343
346
if args .PATH is None :
344
- if config is None :
347
+ if ofx_config is None :
345
348
if args .ofxconfig is None :
346
349
config_file = os .path .join (config_dir , 'ofxclient.ini' )
347
350
else :
348
351
config_file = args .ofxconfig
349
352
if (os .path .exists (config_file )):
350
- config = OfxConfig (file_name = config_file )
353
+ ofx_config = OfxConfig (file_name = config_file )
351
354
else :
352
- config = OfxConfig ()
353
- accounts = config .accounts ()
355
+ ofx_config = OfxConfig ()
356
+ accounts = ofx_config .accounts ()
354
357
if args .account :
355
358
accounts = [acct for acct in accounts
356
359
if acct .description == args .account ]
357
- sync (ledger , accounts , args )
360
+ sync (ledger , accounts )
358
361
else :
359
362
_ , file_extension = os .path .splitext (args .PATH .lower ())
360
363
if file_extension == '.csv' :
361
- import_csv (ledger , args )
364
+ import_csv (ledger )
362
365
else :
363
- import_ofx (ledger , args )
366
+ import_ofx (ledger )
364
367
365
368
366
369
if __name__ == '__main__' :
0 commit comments