def process_transaction(t, options): """Assign a category to a transaction. """ cat, ruler = t['category'], None extras = {} if not t['category']: # Grab category from json cache cat, ruler, _ = tags.find_tag_for(t) if cat: t['category'] = cat extras = {'category': '+ Category'} print('---\n' + TERM.clear_eol, end='') print_transaction(t, extras=extras) edit = False audit = options.get('audit', False) if t['category']: if audit: msg = "\nEdit '%s' category" % TERM.green(t['category']) edit = quick_input(msg, 'yN', vanish=True) == 'Y' if not edit: return t['category'], ruler # Query for category and overwrite category on screen if (not cat or edit) and not options.get('batch', False): t['category'] = query_cat(cat) extras = {'category': '✔ Category'} if not t['category'] else {} print(TERM.clear_last, end='') print_field(t, 'category', extras=extras) # Query ruler if category entered or edit if t['category']: ruler = query_ruler(t) return t['category'], ruler
def process_transaction(t, options): """Assign a category to a transaction. """ cat, ruler = t['category'], None extras = {} if not t['category']: # Grab category from json cache cat, ruler, _ = tags.find_tag_for(t) if cat: t['category'] = cat extras = {'category': '+ Category'} print('---\n' + TERM.clear_eol, end='') print_transaction(t, extras=extras) edit = options['force'] > 1 or (options['force'] and t['category'] not in tags.TAGS) audit = options['audit'] if t['category']: if audit: msg = "\nEdit '%s' category" % TERM.green(t['category']) edit = quick_input(msg, 'yN', vanish=True) == 'Y' if not edit: return t['category'], ruler # Query for category and overwrite category on screen if (not cat or edit) and not options['batch']: t['category'] = query_cat(cat) # Query ruler if category entered or edit if t['category']: ruler = query_ruler(t) extras = {'category': TERM.OK + ' Category'} if t['category'] else {} print(TERM.clear_last, end='') print_field(t, 'category', extras=extras) return t['category'], ruler
def test_update_config(self): dest = os.path.join(tempfile.mkdtemp(), os.path.basename(CONFIG_FILE)) shutil.copy2(CONFIG_FILE, dest) # Replace 'Sully' match tag from 'Bars' to 'Drink' tags.edit('Bars', 'Sully', 'Drink', 'Sully', {'config': dest}) tags.load(dest) self.assertEqual(tags.find_tag_for('Sully')[0], 'Drink')
def process_transaction(t, options): """Assign a category to a transaction. """ cat, ruler = t["category"], None extras = {} if not t["category"]: # Grab category from json cache cat, ruler, _ = tags.find_tag_for(t) if cat: t["category"] = cat extras = {"category": "+ Category"} print("---\n" + TERM.clear_eol, end="") print_transaction(t, extras=extras) edit = options["force"] > 1 or (options["force"] and t["category"] not in tags.TAGS) audit = options["audit"] if t["category"]: if audit: msg = "\nEdit '%s' category" % TERM.green(t["category"]) edit = quick_input(msg, "yN", clear=True) == "Y" if not edit: return t["category"], ruler # Query for category and overwrite category on screen if (not cat or edit) and not options["batch"]: t["category"] = query_cat(cat) # Query ruler if category entered or edit if t["category"]: ruler = query_ruler(t) extras = {"category": TERM.OK + " Category"} if t["category"] else {} print(TERM.clear_last, end="") print_field(t, "category", extras=extras) return t["category"], ruler
def process_file(transactions, options): """Process file's transactions. Operate in a dedicated edit screen.""" tag = None with TERM.fullscreen(): try: for (i, t) in enumerate(transactions): cached_tag, cached_match = tags.find_tag_for(t['payee']) tag, match = process_transaction(t, cached_tag or t['category'], cached_match, options) if tag: tags.edit(cached_tag, cached_match, tag, match, options) t['category'] = tag if not t['payee']: print('Skip transaction: no payee') separator = '-' * 3 print(separator) i = i + 1 if not options.get('batch', False): quick_input('Press any key to continue (Ctrl+D to discard ' 'edits)') except KeyboardInterrupt: return transactions[:i] return transactions[:i]
def print_transaction(t, short=True, extras=None): """Print transaction fields values and indicators about matchings status. If short is True, a limited set of fields is printed. extras dict can be used to add leading character to fields lines: - '✖' when the field don't match the prompted rule - '✔' when the field match the prompted rule - '+' when the category is fetched from .json matches file - ' ' when the category is present in input file """ keys = ('date', 'amount', 'payee', 'category') if short else t.keys() _, _, matches = tags.find_tag_for(t) for field in keys: if t[field] and not field.isdigit(): print_field(t, field, matches, extras) print (TERM.clear_eos, end='')
def print_transaction(t, short=True, extras=None): """Print transaction fields values and indicators about matchings status. If short is True, a limited set of fields is printed. extras dict can be used to add leading character to fields lines: - '✖' when the field don't match the prompted rule - '✔' when the field match the prompted rule - '+' when the category is fetched from .json matches file - ' ' when the category is present in input file """ keys = ('date', 'amount', 'payee', 'category') if short else list(t.keys()) _, _, matches = tags.find_tag_for(t) for field in keys: if t[field] and not field.isdigit(): print_field(t, field, matches, extras) print(TERM.clear_eos, end='')
def test_find_tag_for__best_ruler(self): tag, ruler, _ = tags.find_tag_for({"payee": "Foo Art Brut Shop"}) self.assertEqual(tag, "Clothes")
def test_find_tag_for__basic_ruler(self): tag, ruler, _ = tags.find_tag_for({"payee": "SuLLy"}) self.assertEqual(tag, "Bars") tag, ruler, _ = tags.find_tag_for({"payee": "Sullyz Sull"}) self.assertEqual(tag, None)
def test_find_tag_for__best_ruler(self): tag, ruler, _ = tags.find_tag_for({'payee': 'Foo Art Brut Shop'}) self.assertEqual(tag, 'Clothes')
def test_find_tag_for__basic_ruler(self): tag, ruler, _ = tags.find_tag_for({'payee': 'SuLLy'}) self.assertEqual(tag, 'Bars') tag, ruler, _ = tags.find_tag_for({'payee': 'Sullyz Sull'}) self.assertEqual(tag, None)
def test_find_tag(self): self.assertEqual(tags.find_tag_for('Sully')[0], 'Bars') self.assertEqual(tags.find_tag_for('Sullyz')[0], None) self.assertEqual(tags.find_tag_for('Shop Art Brut Denim')[0], 'Clothes')