Merge branch 'feature/partition_table_expand_line' into 'master'

partition_table: Expand vars before splitting fields (github #841)

See merge request !1174
pull/783/merge
Angus Gratton 2017-09-18 09:18:25 +08:00
commit 9a1ba5985b
1 zmienionych plików z 12 dodań i 10 usunięć

Wyświetl plik

@ -51,9 +51,17 @@ class PartitionTable(list):
@classmethod @classmethod
def from_csv(cls, csv_contents): def from_csv(cls, csv_contents):
res = PartitionTable() res = PartitionTable()
lines = csv_contents.split("\n") lines = csv_contents.splitlines()
def expand_vars(f):
f = os.path.expandvars(f)
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
if m:
raise InputError("unknown variable '%s'" % m.group(1))
return f
for line_no in range(len(lines)): for line_no in range(len(lines)):
line = lines[line_no].strip() line = expand_vars(lines[line_no]).strip()
if line.startswith("#") or len(line) == 0: if line.startswith("#") or len(line) == 0:
continue continue
try: try:
@ -134,7 +142,7 @@ class PartitionDefinition(object):
"app" : APP_TYPE, "app" : APP_TYPE,
"data" : DATA_TYPE, "data" : DATA_TYPE,
} }
# Keep this map in sync with esp_partition_subtype_t enum in esp_partition.h # Keep this map in sync with esp_partition_subtype_t enum in esp_partition.h
SUBTYPES = { SUBTYPES = {
APP_TYPE : { APP_TYPE : {
@ -181,13 +189,7 @@ class PartitionDefinition(object):
def from_csv(cls, line): def from_csv(cls, line):
""" Parse a line from the CSV """ """ Parse a line from the CSV """
line_w_defaults = line + ",,,," # lazy way to support default fields line_w_defaults = line + ",,,," # lazy way to support default fields
def expand_vars(f): fields = [ f.strip() for f in line_w_defaults.split(",") ]
f = os.path.expandvars(f)
m = re.match(r'(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)', f)
if m:
raise InputError("unknown variable '%s'" % m.group(1))
return f
fields = [ expand_vars(f.strip()) for f in line_w_defaults.split(",") ]
res = PartitionDefinition() res = PartitionDefinition()
res.name = fields[0] res.name = fields[0]