test
authorMauro Scomparin <scompo@gmail.com>
Mon, 16 Sep 2019 20:20:04 +0000 (22:20 +0200)
committerMauro Scomparin <scompo@gmail.com>
Mon, 16 Sep 2019 20:20:04 +0000 (22:20 +0200)
money/money.py
money/tests/test_money.py
setup.py

index 528be42a58230e4c48faa0466fc8f40ccdba9292..07868309a49bfaa56708e8e48b1807fc6e571fb5 100755 (executable)
@@ -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]}
index f96a6095f19ba336fcf8a8c0605697590d12d96c..e91163dc129ea77c4938355dfe198f25541c901f 100644 (file)
@@ -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 {
index 6ef2dbb1057c19209e59b4dc6d56db16803cd957..ef85cbcb13cfd1ea2a18acb0011d5efe4e878ce7 100644 (file)
--- 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',
         ],
     },
 )