Skip to content

Commit 9abc223

Browse files
committed
Migrate Program.set_cfg and Program.get_cfg to separate Cfg class.
Introduce Global class which allows global configuration.
1 parent f18482d commit 9abc223

File tree

1 file changed

+98
-44
lines changed

1 file changed

+98
-44
lines changed

mbed/mbed.py

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ class Repo(object):
754754
rev = None
755755
scm = None
756756
libs = []
757-
cache = '/tmp/repo_cache'
757+
cache = None
758758

759759
@classmethod
760760
def fromurl(cls, url, path=None):
@@ -781,6 +781,8 @@ def fromurl(cls, url, path=None):
781781
repo.rev = m_repo_url.group(3)
782782
else:
783783
error('Invalid repository (%s)' % url.strip(), -1)
784+
repo.cache = Global().get_cfg('CACHE')
785+
784786
return repo
785787

786788
@classmethod
@@ -807,6 +809,7 @@ def fromrepo(cls, path=None):
807809

808810
repo.path = os.path.abspath(path)
809811
repo.name = os.path.basename(repo.path)
812+
repo.cache = Global().get_cfg('CACHE')
810813

811814
repo.sync()
812815

@@ -830,7 +833,7 @@ def findparent(cls, path=None):
830833
path = os.path.abspath(path or os.getcwd())
831834

832835
while cd(path):
833-
if os.path.isfile(os.path.join(path, Program.config_file)) or Repo.isrepo(path):
836+
if os.path.isfile(os.path.join(path, Cfg.file)) or Repo.isrepo(path):
834837
return path
835838

836839
tpath = path
@@ -1070,9 +1073,8 @@ def check_repo(self, show_warning=None):
10701073
return True
10711074

10721075

1073-
# Program object, used to indicate the root of the code base
1076+
# Program class, acts code base root
10741077
class Program(object):
1075-
config_file = ".mbed"
10761078
path = None
10771079
name = None
10781080
is_cwd = False
@@ -1085,7 +1087,7 @@ def __init__(self, path=None, print_warning=False):
10851087

10861088
while cd(path):
10871089
tpath = path
1088-
if os.path.isfile(os.path.join(path, Program.config_file)):
1090+
if os.path.isfile(os.path.join(path, Cfg.file)):
10891091
self.path = path
10901092
self.is_cwd = False
10911093
break
@@ -1102,45 +1104,17 @@ def __init__(self, path=None, print_warning=False):
11021104
"Could not find mbed program in current path \"%s\".\n"
11031105
"You can fix this by calling \"mbed new .\" or \"mbed default root .\" in the root of your program." % self.path)
11041106

1105-
# Sets config value
1106-
def set_cfg(self, var, val):
1107-
fl = os.path.join(self.path, self.config_file)
1108-
try:
1109-
with open(fl) as f:
1110-
lines = f.read().splitlines()
1111-
except IOError:
1112-
lines = []
1113-
1114-
for line in lines:
1115-
m = re.match(r'^([\w+-]+)\=(.*)?$', line)
1116-
if m and m.group(1) == var:
1117-
lines.remove(line)
1118-
1119-
lines += [var+"="+val]
1120-
1121-
with open(fl, 'w') as f:
1122-
f.write('\n'.join(lines) + '\n')
1123-
1124-
# Gets config value
1125-
def get_cfg(self, var, default_val=None):
1126-
fl = os.path.join(self.path, self.config_file)
1127-
try:
1128-
with open(fl) as f:
1129-
lines = f.read().splitlines()
1130-
except IOError:
1131-
lines = []
1132-
1133-
for line in lines:
1134-
m = re.match(r'^([\w+-]+)\=(.*)?$', line)
1135-
if m and m.group(1) == var:
1136-
return m.group(2)
1137-
return default_val
1107+
def get_cfg(self, *args, **kwargs):
1108+
return Cfg(self.path).get(*args, **kwargs) or Global().get_cfg(*args, **kwargs)
1109+
1110+
def set_cfg(self, *args, **kwargs):
1111+
return Cfg(self.path).set(*args, **kwargs)
11381112

11391113
def set_root(self):
11401114
return self.set_cfg('ROOT', '.')
11411115

11421116
def unset_root(self, path=None):
1143-
fl = os.path.join(path or self.path, self.config_file)
1117+
fl = os.path.join(path or self.path, Cfg.file)
11441118
if os.path.isfile(fl):
11451119
os.remove(fl)
11461120

@@ -1257,6 +1231,63 @@ def get_macros(self):
12571231
return macros
12581232

12591233

1234+
# Global class, used for global config
1235+
class Global(object):
1236+
def __init__(self):
1237+
self.path = os.path.join(os.path.expanduser("~"), '.mbed')
1238+
if not os.path.exists(self.path):
1239+
os.mkdir(self.path)
1240+
1241+
def get_cfg(self, *args, **kwargs):
1242+
return Cfg(self.path).get(*args, **kwargs)
1243+
1244+
def set_cfg(self, *args, **kwargs):
1245+
return Cfg(self.path).set(*args, **kwargs)
1246+
1247+
1248+
class Cfg(object):
1249+
path = None
1250+
file = ".mbed"
1251+
1252+
def __init__(self, path):
1253+
self.path = path
1254+
1255+
# Sets config value
1256+
def set(self, var, val):
1257+
fl = os.path.join(self.path, self.file)
1258+
try:
1259+
with open(fl) as f:
1260+
lines = f.read().splitlines()
1261+
except IOError:
1262+
lines = []
1263+
1264+
for line in lines:
1265+
m = re.match(r'^([\w+-]+)\=(.*)?$', line)
1266+
if m and m.group(1) == var:
1267+
lines.remove(line)
1268+
1269+
if not val is None:
1270+
lines += [var+"="+val]
1271+
1272+
with open(fl, 'w') as f:
1273+
f.write('\n'.join(lines) + '\n')
1274+
1275+
# Gets config value
1276+
def get(self, var, default_val=None):
1277+
fl = os.path.join(self.path, self.file)
1278+
try:
1279+
with open(fl) as f:
1280+
lines = f.read().splitlines()
1281+
except IOError:
1282+
lines = []
1283+
1284+
for line in lines:
1285+
m = re.match(r'^([\w+-]+)\=(.*)?$', line)
1286+
if m and m.group(1) == var:
1287+
return m.group(2)
1288+
return default_val
1289+
1290+
12601291
def formaturl(url, format="default"):
12611292
url = "%s" % url
12621293
m = re.match(regex_mbed_url, url)
@@ -1915,19 +1946,42 @@ def toolchain_(name=None):
19151946
@subcommand('default',
19161947
dict(name='name', help='Variable name. E.g. "target", "toolchain", "protocol"'),
19171948
dict(name='value', nargs='?', help='Value. Will show the currently set default value for a variable if not specified.'),
1949+
dict(name=['-u', '--unset'], dest='unset', action='store_true', help='Unset the specified variable.'),
19181950
help='Set or get program default options.')
1919-
def default_(name, value=None):
1951+
def default_(name, value=None, unset=False):
19201952
# Find the root of the program
19211953
program = Program(os.getcwd())
19221954
# Change current dir to program root
19231955
with cd(program.path):
19241956
var = str(name).upper()
1925-
if value is None:
1926-
value = program.get_cfg(var)
1927-
action(('%s' % value) if value else 'No default %s set in program "%s"' % (name, program.name))
1928-
else:
1957+
if unset:
1958+
program.set_cfg(var, None)
1959+
action('Unset default %s in program "%s"' % (name, program.name))
1960+
elif value:
19291961
program.set_cfg(var, value)
19301962
action('%s now set as default %s in program "%s"' % (value, name, program.name))
1963+
else:
1964+
value = program.get_cfg(var)
1965+
action(('%s' % value) if value else 'No default %s set in program "%s"' % (name, program.name))
1966+
1967+
# Generic config command
1968+
@subcommand('global',
1969+
dict(name='name', help='Variable name. E.g. "target", "toolchain", "protocol"'),
1970+
dict(name='value', nargs='?', help='Value. Will show the currently set default value for a variable if not specified.'),
1971+
dict(name=['-u', '--unset'], dest='unset', action='store_true', help='Unset the specified variable.'),
1972+
help='Set or get global options for all programs. Global options may be overridden by program defaults.')
1973+
def default_(name, value=None, unset=False):
1974+
g = Global()
1975+
var = str(name).upper()
1976+
if unset:
1977+
g.set_cfg(var, None)
1978+
action('Unset global %s' % name)
1979+
elif value:
1980+
g.set_cfg(var, value)
1981+
action('%s now set as global %s' % (value, name))
1982+
else:
1983+
value = g.get_cfg(var)
1984+
action(('%s' % value) if value else 'No global %s set' % (name))
19311985

19321986
# 'config' command (calls into tools/get_config.py)
19331987
@subcommand('config',

0 commit comments

Comments
 (0)