This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.py
74 lines (53 loc) · 2.13 KB
/
Config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#/usr/bin/env python3
from Messages import INFO
from xdg.BaseDirectory import xdg_config_home
from os.path import join as pjoin, exists, sep, expanduser, normpath
from os import mkdir
# Defaults
__config_dir = pjoin( xdg_config_home, 'foodlogger')
user_foodlist = pjoin( __config_dir, 'list.txt' )
user_foodlog = pjoin( __config_dir, 'log.txt' )
user_weightlog= pjoin( __config_dir, 'weight.txt')
# Custom user
user_config_file = pjoin( expanduser('~'), '.pyflogrc' )
if exists( user_config_file ):
# ConfigParser is a bit much for this, just use simple read
with open(user_config_file, 'r') as conf:
for line in conf:
tokens = line.splitlines()[0].split('=')
if tokens[0].startswith('user_foodlist'):
user_foodlist = normpath( tokens[1].strip() )
elif tokens[0].startswith('user_foodlog'):
user_foodlog = normpath( tokens[1].strip() )
elif tokens[0].startswith('user_weightlog'):
user_weightlog = normpath( tokens[1].strip() )
# if not all found, defaults are used
conf.close()
else:
with open(user_config_file, 'w') as conf:
conf.write('user_foodlist = %s\n' % user_foodlist)
conf.write('user_foodlog = %s\n' % user_foodlog)
conf.write('user_weightlog = %s\n' % user_weightlog)
conf.close()
INFO("default list, log, weight file locations have been set at: %s" % user_config_file)
class Config:
@staticmethod
def resolveAllPaths(): # called from main
Config.resolvePath(user_foodlist)
Config.resolvePath(user_foodlog)
Config.resolvePath(user_weightlog)
@staticmethod
def resolvePath(filepath):
Config.__touch(filepath)
@staticmethod
def __touch(filename):
if not exists(filename):
info(filename, "does not exist -- creating new")
direc = sep.join(filename.split(sep)[:-1])
if not exists(direc):
mkdir( direc )
# Open file and write blank
with open(filename,'w') as f:
f.write("\n")
f.close()
return filename