-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
80 lines (57 loc) · 2.04 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
75
76
77
78
79
80
# python
import os
import json
# modules
import utils
import repository
class Config:
def __init__(self):
'''init config'''
self._remotes = {
'origin': ''
}
self.load()
def save(self):
if repository.exists():
try:
with open(self.getPath(), 'w') as outfile:
json.dump(self.toDict(), outfile, sort_keys = True, indent = 2)
except Exception as e:
raise Exception('could not save config. reason: {reason}'.format(reason = e.message))
def load(self):
if self.exists():
try:
with open(self.getPath()) as json_data:
data = json.load(json_data)
if 'remotes' in data and data['remotes']:
self.setRemotes(data['remotes'])
except Exception as e:
raise Exception('could not load config. reason: {reason}'.format(reason = e.message))
def exists(self):
return os.path.isfile(self.getPath())
def getPath(self):
return os.path.join(repository.getPath(), 'config.json')
def setRemotes(self, remotes = {}):
'''set remote repositories'''
assert isinstance(remotes, dict), 'setRemotes: remotes must be an instance of dict'
self._remotes = remotes
return self
def getRemotes(self):
'''get remote origins'''
return self._remotes
def setRemote(self, remote = '', url = ''):
'''set remote'''
assert isinstance(remote, basestring), 'setRemote: remote must be an instance of basestring'
assert isinstance(url, basestring), 'setRemote: url must be an instance of basestring'
self._remotes[remote] = url
return self
def getRemote(self, remote = ''):
'''get remote by name'''
if remote in self.getRemotes():
return self.getRemotes()[remote]
return ''
def toDict(self):
return {
'remotes': self.getRemotes()
}
config = Config()