refactor with_absolute_paths
@@ -6,23 +6,25 @@ from .base import Base
base_assets_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'create_project_assets')
-def with_absolute_paths(obj, list_of_keys, target_path):
+def with_absolute_paths(data, to_update, target_path):
def abspath(path): return os.path.abspath(os.path.join(target_path, path))
- for keys in list_of_keys:
- (k1, k2) = keys
- if isinstance(obj[k1][k2], list):
- obj[k1][k2] = [abspath(path) for path in obj[k1][k2]]
+ for key in data.keys():
+ if key in to_update.keys():
+ # update path
+ for k in to_update[key]:
+ if isinstance(data[key][k], list):
+ data[key][k] = [abspath(path) for path in data[key][k]]
else:
- obj[k1][k2] = abspath(obj[k1][k2])
- return obj
+ data[key][k] = abspath(data[key][k])
+ return data
-def update_paths_in_json(target_path, filename, list_of_keys):
+def update_paths_in_json(target_path, filename, to_update):
file_path = os.path.join(target_path, filename)
with open(file_path, 'r') as f:
data = json.load(f)
- data = with_absolute_paths(data, list_of_keys, target_path)
+ data = with_absolute_paths(data, to_update, target_path)
with open(file_path, 'w') as f:
json.dump(data, f, indent=2)
@@ -49,16 +51,13 @@ class CreateProject(Base):
else:
if self.options['isic']:
# update paths in JSON files
- update_paths_in_json(os.path.join(target_path, 'objective'), 'objective.json', [
- ['objective', 'description'],
- ['objective', 'metrics'],
- ['data_manager', 'description'],
- ['data_manager', 'data_opener'],
- ['data_samples', 'paths']
- ])
- update_paths_in_json(os.path.join(target_path, 'dataset'), 'dataset.json', [
- ['data_manager', 'description'],
- ['data_manager', 'data_opener'],
- ['data_samples', 'paths']
- ])
+ update_paths_in_json(os.path.join(target_path, 'objective'), 'objective.json', {
+ 'objective': ['description', 'metrics'],
+ 'data_manager': ['description', 'data_opener'],
+ 'data_samples': ['paths'],
+ })
+ update_paths_in_json(os.path.join(target_path, 'dataset'), 'dataset.json', {
+ 'data_manager': ['description', 'data_opener'],
+ 'data_samples': ['paths'],
+ })
print(f'New project created in {target_path}')
