import gettext
loc_dir = resource_filename('money', 'translations')
-t = gettext.translation('money', localedir = loc_dir, fallback=True)
+t = gettext.translation('money', localedir=loc_dir, fallback=True)
_ = t.gettext
t.install()
HOUR_FIELD_NAME = _('hour')
DESCRIPTION_FIELD_NAME = _('description')
+
def create_input_message(field_name, field_format, field_default_value):
return field_name + " (" + field_format + ") [" + field_default_value + "]: "
+
TYPE_MESSAGE = create_input_message(TYPE_FIELD_NAME, '+/-', '-')
DATE_MESSAGE = create_input_message(DATE_FIELD_NAME, 'DD/MM/YYYY', _('today'))
HOUR_MESSAGE = create_input_message(HOUR_FIELD_NAME, 'HH:MM', _('now'))
VALUE_MESSAGE = create_input_message(VALUE_FIELD_NAME, '(#####.##)', '')
DESCRIPTION_MESSAGE = create_input_message(DESCRIPTION_FIELD_NAME, '', '')
-def write_movement(path, m):
+
+def write_movement_to_file(path, m):
with open(path, 'a') as f:
- f.write(m[TYPE_FIELD_NAME] + m[VALUE_FIELD_NAME])
- f.write(';')
- f.write(m[DATE_FIELD_NAME])
- f.write(';')
- f.write(m[HOUR_FIELD_NAME])
- f.write(';')
- f.write(m[DESCRIPTION_FIELD_NAME])
- f.write('\n')
+ write_movement_to_stream(f, m)
+ return
+
+
+def write_movement_to_stream(fileStream, movement):
+ fileStream.write(movement[TYPE_FIELD_NAME] + movement[VALUE_FIELD_NAME])
+ fileStream.write(';')
+ fileStream.write(movement[DATE_FIELD_NAME])
+ fileStream.write(';')
+ fileStream.write(movement[HOUR_FIELD_NAME])
+ fileStream.write(';')
+ fileStream.write(movement[DESCRIPTION_FIELD_NAME])
+ fileStream.write('\n')
return
return data_file
-def load_file(file):
+def load_movements_from_stream(data_file):
movements = []
- with open(file, "r") as data_file:
- for line in data_file:
- split_line = l.split(';')
- movement = {
- VALUE_FIELD_NAME: split_line[0],
- DATE_FIELD_NAME: split_line[1],
- HOUR_FIELD_NAME: split_line[2],
- DESCRIPTION_FIELD_NAME: split_line[3]
- }
- movements.append(movement)
+ for line in data_file:
+ split_line = line.split(';')
+ movement = {
+ VALUE_FIELD_NAME: split_line[0],
+ DATE_FIELD_NAME: split_line[1],
+ HOUR_FIELD_NAME: split_line[2],
+ DESCRIPTION_FIELD_NAME: split_line[3].rstrip()
+ }
+ movements.append(movement)
return movements
+def load_file(file):
+ res = []
+ with open(file, "r") as data_file:
+ res = load_movements_from_stream(data_file)
+ return res
+
+
def insert_new_movement(data_file):
movement = read_movement()
- write_movement(data_file, movement)
+ write_movement_to_file(data_file, movement)
def read_new_movement():
from decimal import *
from os.path import expanduser
from money import money
+import io
class MoneyTest(unittest.TestCase):
self.assertEqual(money.last_movement_number_message(
10), "latest 10 movements:")
+ def test_write_movement_to_stream(self):
+ test_str = io.StringIO()
+ 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: 'testings',
+ }
+ money.write_movement_to_stream(test_str, m)
+ test_str.seek(0)
+ self.assertEqual('-100;18/04/2017;00:01;testings\n', test_str.readline(), 'wrong string read')
+
+ def test_load_movements_from_stream(self):
+ test_str = io.StringIO()
+ test_str.write('-100;18/04/2017;00:01;testings\n')
+ test_str.seek(0)
+ movements = money.load_movements_from_stream(test_str)
+ self.assertEqual(1, len(movements), 'should read only one movement')
+ m = movements[0]
+ self.assertEqual('-100', m[money.VALUE_FIELD_NAME])
+ self.assertEqual('18/04/2017', m[money.DATE_FIELD_NAME])
+ self.assertEqual('00:01', m[money.HOUR_FIELD_NAME])
+ self.assertEqual('testings', m[money.DESCRIPTION_FIELD_NAME])
+
def test_value_message(self):
self.assertEqual(money.value_message('blah: {}',
Decimal(10.00)), "blah: 10")