From: Mauro Scomparin Date: Mon, 16 Sep 2019 20:20:04 +0000 (+0200) Subject: test X-Git-Url: https://git.scompo.it/?a=commitdiff_plain;h=2a0ad0530765b888d933f6a9c51fac52f0714b1c;p=money.git test --- diff --git a/money/money.py b/money/money.py index 528be42..0786830 100755 --- a/money/money.py +++ b/money/money.py @@ -4,6 +4,7 @@ from pprint import pprint from decimal import * from pkg_resources import resource_filename import json +import re import gettext @@ -214,6 +215,34 @@ def latest_movements(): data_file = get_data_file(resulting_config) write_latest_movements(data_file, resulting_config) +def mov_filter(movs, filter): + return list(filter(lambda m: re.match(filter, m[DESCRIPTION_FIELD_NAME]))) + +def filter_regex_movements(data_file, conf, filter): + movements = load_file(data_file) + ordered_movements = sort_movements(movements) + filtered_movements = mov_filter(ordered_movements, filter) + for movement in filtered_movements: + formatted_movement = format_movement(ordered_movements[i]) + print(formatted_movement) + +def filter_movements(): + """Writes the latest movements that match a filter formatted. + + Called by money-filter + + """ + filter = read_filter() + default_config = default_configuration() + resulting_config = None + if file_exists(CONFIG_FILE_LOCATION): + config_from_file = configuration_from_file(CONFIG_FILE_LOCATION) + resulting_config = override_default_configuration( + default_config, config_from_file) + else: + resulting_config = default_config + data_file = get_data_file(resulting_config) + filter_regex_movements(data_file, resulting_config, filter) def override_default_configuration(default_conf, other_conf): result_conf = {DATA_FILE_NAME: default_conf[DATA_FILE_NAME]} diff --git a/money/tests/test_money.py b/money/tests/test_money.py index f96a609..e91163d 100644 --- a/money/tests/test_money.py +++ b/money/tests/test_money.py @@ -100,6 +100,18 @@ class MoneyTest(unittest.TestCase): self.assertEqual(res[2]['prog'], 3) self.assertEqual(res[3]['prog'], 4) + def test_mov_filter(self): + m = { + money.TYPE_FIELD_NAME: '-', + money.VALUE_FIELD_NAME: '100', + money.DATE_FIELD_NAME: '18/04/2017', + money.HOUR_FIELD_NAME: '00:01', + money.DESCRIPTION_FIELD_NAME: 'test', + } + ms = [m] + res = money.mov_filter(ms, 't') + self.assertEqual(len(res), 1) + def create_test_movement(date, hour, prog): return { diff --git a/setup.py b/setup.py index 6ef2dbb..ef85cbc 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ setup( 'console_scripts': [ 'money-insert=money.money:read_new_movement', 'money-latest=money.money:latest_movements', + 'money-filter=money.money:filter_movements', ], }, )