-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsources.py
85 lines (75 loc) · 2.71 KB
/
sources.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
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""
#
# locations, names for image / iso files
# feel free to add some more at etc/sources.toml
#
# also methods for parsing shortcut config files (toml / yaml)
"""
from common import *
import toml
import yaml
class sources(object):
default_configs = ['sources', 'etc/sources']
default_settings = ['default', 'etc/default']
default_types = ['.toml', '.yaml']
# provides an option to provide etc/sources.toml or add directly to dictionary
@classmethod
def get_source(cls):
for econfig in cls.default_configs:
for etype in cls.default_types:
try:
if os.path.isfile(econfig + etype):
if 'toml' in etype:
source = toml.load(econfig + etype)
else:
source = yaml.load(open(econfig + etype), Loader=yaml.Loader)
return source
except:
pass
else:
print("couldn't find default source toml or yaml, FYI")
# catch all if sources.toml doesn't exist:
source = {
'stretch_lite': 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-11-15/2018-11-13'
'-raspbian-stretch-lite.zip',
'stretch_desktop': 'http://downloads.raspberrypi.org/raspbian/images/raspbian-2019-04-09/2019-04-08-raspbian'
'-stretch.zip',
'octoprint': 'https://octopi.octoprint.org/latest',
}
return source
@staticmethod
def has_conf():
# soften argument / no argument
try:
if '.toml' in sys.argv[1]:
return True
if '.yaml' in sys.argv[1]:
return True
except:
return False
@classmethod
def load_args(cls):
if sources.has_conf():
source = toml.load(sys.argv[1])
return source
else:
for econfig in cls.default_settings:
for etype in cls.default_types:
try:
if os.path.isfile(econfig + etype):
if 'toml' in etype:
source = toml.load(econfig + etype)
else:
source = yaml.load(open(econfig + etype), Loader=yaml.Loader)
return source
except:
pass
@staticmethod
def do_arg(arg, default):
xargs = sources.load_args()
try:
k = xargs[arg]
return k
except:
return default