Skip to content

Commit

Permalink
Cleanups in definitions.py
Browse files Browse the repository at this point in the history
  • Loading branch information
devcurmudgeon committed May 2, 2016
1 parent 5e04a62 commit 9fb0a8e
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions ybd/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def __init__(self, directory='.'):
self.defaults = Defaults()
app.config['cpu'] = self.defaults.cpus.get(app.config['arch'],
app.config['arch'])
self.parse_files(directory)
self._check_trees()

for path in self._data:
try:
this = self._data[path]
if this.get('ref') and self._trees.get(path):
if this['ref'] == self._trees.get(path)[0]:
this['tree'] = self._trees.get(path)[1]
except:
app.log('DEFINITIONS', 'WARNING: problem with .trees file')
pass

def parse_files(self, directory):
schemas = self.load_schemas()
with app.chdir(directory):
for dirname, dirnames, filenames in os.walk('.'):
Expand All @@ -52,21 +65,9 @@ def __init__(self, directory='.'):
self._fix_keys(data)
self._tidy_and_insert_recursively(data)

caches_are_valid = self._check_trees()
for path in self._data:
try:
this = self._data[path]
if this.get('ref') and self._trees.get(path):
if this['ref'] == self._trees.get(path)[0]:
this['tree'] = self._trees.get(path)[1]
except:
app.log('DEFINITIONS', 'WARNING: problem with .trees file')
pass

if app.config.get('mode') == 'parse-only':
with open(app.config['result-file'], 'w') as f:
f.write(json.dumps(self._data, indent=4,
sort_keys=True))
f.write(json.dumps(self._data, indent=4, sort_keys=True))
app.log('RESULT', 'Parsed definitions data in json format is at',
app.config['result-file'])
os._exit(0)
Expand All @@ -90,15 +91,6 @@ def validate_schema(self, schemas, data):
app.log(data, 'WARNING: schema validation failed:')
print e

def write(self, output):
for path in self._data:
print path
for path in self._data:
filename = self._data[path]['name'] + '.cida'
with open(os.path.join(output, filename), 'w') as f:
f.write(yaml.dump(self._data[path],
default_flow_style=False))

def _load(self, path):
'''Load a single definition file as a dict.
Expand Down Expand Up @@ -143,9 +135,8 @@ def _tidy_and_insert_recursively(self, item):
# 'chunks' field in a stratum .morph file, or the 'strata' field in a
# system .morph file.
item['contents'] = item.get('contents', [])
for subset in ['chunks', 'strata']:
for component in item.get(subset, []):
item['contents'] += [component]
for component in item.get('chunks', []) + item.get('strata', []):
item['contents'] += [component]

lookup = {}
for index, component in enumerate(item.get('contents', [])):
Expand All @@ -165,7 +156,7 @@ def _tidy_and_insert_recursively(self, item):

return self._insert(item)

def _fix_keys(self, item, name='ERROR'):
def _fix_keys(self, item):
'''Normalizes keys for a definition dict and its contents
Some definitions have a 'morph' field which is a relative path. Others
Expand All @@ -176,8 +167,8 @@ def _fix_keys(self, item, name='ERROR'):
the same as 'path' but replacing '/' by '-'
'''
item.setdefault('path', item.pop('morph', item.get('name', name)))
if item['path'] == 'ERROR':
item.setdefault('path', item.pop('morph', item.get('name', None)))
if item['path'] is None:
app.exit(item, 'ERROR: no path, no name?')
item.setdefault('name', item['path'])
item['name'] = item['name'].replace('/', '-')
Expand Down

0 comments on commit 9fb0a8e

Please sign in to comment.