from time import localtime, gmtime, strftime, strptime
-from os.path import expanduser, join
+from os.path import expanduser, join, exists, abspath
from pprint import pprint
from decimal import *
+import json
+
+DATA_FILE_NAME = 'dataFile'
+CONFIG_FILE_NAME = 'money-config.json'
+CONFIG_FILE_LOCATION = expanduser('~') + CONFIG_FILE_NAME
+
def scrivi_movimento(path, m):
with open(path, 'a') as f:
f.write('\n')
return
+
def leggi_tipo():
t = 'n'
while not (t == '' or t == '+' or t == '-'):
t = input('tipo (+/-) [-]: ')
if t == '':
- t='-'
+ t = '-'
elif t == '+':
- t=''
+ t = ''
return t
+
def leggi_valore():
v = ''
while v == '':
v = input('valore (#####.##) []: ')
return v
+
def leggi_data():
d = input('data (DD/MM/YYYY) [oggi]: ')
if d == '':
d = strftime("%d/%m/%Y", localtime())
return d
+
def leggi_ora():
o = input('ora (HH:MM) [adesso]: ')
if o == '':
o = strftime('%H:%M', localtime())
return o
+
def leggi_descrizione():
d = input('descrizione () []: ')
return d
+
def leggi_movimento():
tipo = leggi_tipo()
valore = leggi_valore()
ora = leggi_ora()
descrizione = leggi_descrizione()
m = {
- 'tipo' : tipo,
- 'valore' : valore,
- 'data' : data,
- 'ora' : ora,
+ 'tipo': tipo,
+ 'valore': valore,
+ 'data': data,
+ 'ora': ora,
'descrizione': descrizione
}
return m
-def get_file_dati():
- home = expanduser('~')
- nome_file_dati = 'movimenti.dat'
- file_dati = join(home, 'dati', nome_file_dati)
+
+def get_file_dati(config):
+ file_dati = abspath(config[DATA_FILE_NAME])
print('file dati:', file_dati)
return file_dati
+
def carica_file(f):
dati = []
with open(f, "r") as df:
for l in df:
spl = l.split(';')
d = {
- 'valore' : spl[0],
- 'data' : spl[1],
- 'ora' : spl[2],
- 'descrizione' : spl[3]
+ 'valore': spl[0],
+ 'data': spl[1],
+ 'ora': spl[2],
+ 'descrizione': spl[3]
}
dati.append(d)
return dati
+
def inserimento(file_dati):
m = leggi_movimento()
scrivi_movimento(file_dati, m)
+
def inserimento_dati():
- file_dati = get_file_dati()
+ 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
+ file_dati = get_file_dati(resulting_config)
inserimento(file_dati)
+
def riassunto_dati():
- file_dati = get_file_dati()
+ 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
+ file_dati = get_file_dati(resulting_config)
riassunto(file_dati)
+
+def override_default_configuration(default_conf, other_conf):
+ result_conf = {DATA_FILE_NAME: default_conf[DATA_FILE_NAME]}
+ if other_conf:
+ if DATA_FILE_NAME in other_conf:
+ result_conf[DATA_FILE_NAME] = other_conf[DATA_FILE_NAME]
+ return result_conf
+
+
+def file_exists(file_path):
+ return exists(file_path)
+
+
+def configuration_from_file(file_path):
+ with open(file_path, 'r') as fp:
+ return json.load(fp)
+
+
def data_default(data):
try:
return strptime(data, '%d/%m/%Y')
except ValueError:
return gmtime(0)
+
def ora_default(ora):
try:
return strptime(ora, '%H:%M')
except ValueError:
return gmtime(0)
+
def ordina(dati):
return sorted(
- dati,
- key = lambda x: (
- data_default(x['data']),
+ dati,
+ key=lambda x: (
+ data_default(x['data']),
ora_default(x['ora'])
),
- reverse = True
+ reverse=True
)
+
+def default_configuration():
+ return {DATA_FILE_NAME: 'movimenti.dat'}
+
+
def riassunto(file_dati):
dati = carica_file(file_dati)
dati_ordinati = ordina(dati)
import unittest
+import tempfile
+import json
+from os.path import expanduser
from money import money
-class UselessTest(unittest.TestCase):
+
+class MoneyTest(unittest.TestCase):
def test_passa_sempre(self):
self.assertTrue(True, msg='se fallisce son problemi')
-
+
+ def test_config_file_name(self):
+ self.assertEqual(money.CONFIG_FILE_NAME, 'money-config.json')
+
+ def test_config_file_path(self):
+ self.assertEqual(money.CONFIG_FILE_LOCATION,
+ expanduser('~') + 'money-config.json')
+
+ def test_default_configuration(self):
+ conf = money.default_configuration()
+ self.assertTrue(money.DATA_FILE_NAME in conf)
+ self.assertEqual(conf[money.DATA_FILE_NAME], 'movimenti.dat')
+
+ def test_file_exists(self):
+ with tempfile.TemporaryFile() as fp:
+ self.assertTrue(money.file_exists(fp.name))
+
+ def test_override_default_configuration(self):
+ def_conf = money.default_configuration()
+ new_conf = {money.DATA_FILE_NAME: 'new-file.dat'}
+ res = money.override_default_configuration(def_conf, new_conf)
+ self.assertTrue(money.DATA_FILE_NAME in res)
+ self.assertEqual(res[money.DATA_FILE_NAME], 'new-file.dat')
+
def test_ordina_ore(self):
date = [
{
- 'data' : '18/04/2017',
- 'ora' : '00:01',
- 'prog' : 1,
+ 'data': '18/04/2017',
+ 'ora': '00:01',
+ 'prog': 1,
},
{
- 'data' : '19/04/2017',
- 'ora' : '00:01',
- 'prog' : 2,
+ 'data': '19/04/2017',
+ 'ora': '00:01',
+ 'prog': 2,
},
{
- 'data' : '17/04/2017',
- 'ora' : '',
- 'prog' : 3,
+ 'data': '17/04/2017',
+ 'ora': '',
+ 'prog': 3,
},
{
- 'data' : '',
- 'ora' : '00:01',
- 'prog' : 4,
+ 'data': '',
+ 'ora': '00:01',
+ 'prog': 4,
},
]
res = money.ordina(date)
self.assertEqual(res[2]['prog'], 3)
self.assertEqual(res[3]['prog'], 4)
+
def main():
unittest.main()
+
if __name__ == '__main__':
- main()
\ No newline at end of file
+ main()