Skip to content

Commit

Permalink
Update yaml output to preserve order of dict keys from models (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
JrGoodle committed Oct 5, 2020
1 parent 45129bc commit 86e445c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion clowder/cli/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from clowder.clowder_controller import CLOWDER_CONTROLLER
from clowder.data.util import validate_project_statuses
from clowder.util.decorators import valid_clowder_yaml_required
from clowder.util.yaml import print_clowder_yaml, yaml_string
from clowder.util.yaml import yaml_string

from .util import add_parser_arguments

Expand Down
9 changes: 5 additions & 4 deletions clowder/data/model/clowder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ def get_yaml(self, resolved: bool = False) -> dict:
"""

yaml = {
"name": self.name,
"clowder": self.clowder.get_yaml(resolved=resolved)
"name": self.name
}

if self.protocol is not None:
yaml['protocol'] = self.protocol
if self.defaults is not None:
yaml['defaults'] = self.defaults.get_yaml()
if self.sources is not None:
yaml['sources'] = {s.name.get_yaml(): s.get_yaml() for s in self.sources}
if self.defaults is not None:
yaml['defaults'] = self.defaults.get_yaml()

yaml["clowder"] = self.clowder.get_yaml(resolved=resolved)

return yaml
13 changes: 7 additions & 6 deletions clowder/data/model/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ def get_yaml(self) -> dict:

yaml = {}

if self.source is not None:
yaml['source'] = self.source.get_yaml()
if self.remote is not None:
yaml['remote'] = self.remote
if self.git_settings is not None:
yaml['git'] = self.git_settings.get_yaml()
if self.branch is not None:
yaml['branch'] = self.branch
elif self.tag is not None:
yaml['tag'] = self.tag
elif self.commit is not None:
yaml['commit'] = self.commit

if self.remote is not None:
yaml['remote'] = self.remote
if self.source is not None:
yaml['source'] = self.source.get_yaml()
if self.git_settings is not None:
yaml['git'] = self.git_settings.get_yaml()
if self.upstream_defaults is not None:
yaml['upstream'] = self.upstream_defaults.get_yaml()

Expand Down
8 changes: 5 additions & 3 deletions clowder/data/model/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ def get_yaml(self, resolved: bool = False) -> Union[dict, list]:
if not self._has_projects_key:
return [p.get_yaml(resolved=resolved) for p in self.projects]

yaml = {"projects": [p.get_yaml(resolved=resolved) for p in self.projects]}
yaml = {}

if self.path is not None:
yaml['path'] = str(self.path)
if self.protocol is not None:
yaml['protocol'] = self.protocol
if self.groups is not None:
yaml['groups'] = str(self.groups)
if self.defaults is not None:
yaml['defaults'] = self.defaults.get_yaml()
if self.protocol is not None:
yaml['protocol'] = self.protocol

yaml["projects"] = [p.get_yaml(resolved=resolved) for p in self.projects]

return yaml
4 changes: 2 additions & 2 deletions clowder/data/model/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ def get_yaml(self, resolved: bool = False) -> Union[dict, str]:

yaml = {"name": self.name}

if self.path is not None:
yaml['path'] = str(self.path)
if resolved:
yaml['commit'] = CLOWDER_CONTROLLER.get_project_sha(self.resolved_project_id)
else:
Expand All @@ -141,8 +143,6 @@ def get_yaml(self, resolved: bool = False) -> Union[dict, str]:
yaml['groups'] = self.groups
if self.remote is not None:
yaml['remote'] = self.remote
if self.path is not None:
yaml['path'] = str(self.path)
if self.source is not None:
yaml['source'] = self.source.get_yaml()
if self.git_settings is not None:
Expand Down
4 changes: 2 additions & 2 deletions clowder/data/model/upstream_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def get_yaml(self) -> dict:

yaml = {}

if self.source is not None:
yaml['source'] = self.source.get_yaml()
if self.remote is not None:
yaml['remote'] = self.remote
if self.source is not None:
yaml['source'] = self.source.get_yaml()

return yaml
4 changes: 2 additions & 2 deletions clowder/util/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def save_yaml_file(yaml_output: dict, yaml_file: Path) -> None:
print(f" - Save yaml to file at {fmt.path_string(str(yaml_file))}")
try:
with yaml_file.open(mode="w") as raw_file:
pyyaml.safe_dump(yaml_output, raw_file, default_flow_style=False, indent=2)
pyyaml.safe_dump(yaml_output, raw_file, default_flow_style=False, indent=2, sort_keys=False)
except pyyaml.YAMLError as err:
LOG_DEBUG('Failed to save yaml file', err)
raise ClowderError(ClowderErrorType.FAILED_SAVE_FILE,
Expand Down Expand Up @@ -201,7 +201,7 @@ def yaml_string(yaml_output: dict) -> str:
"""

try:
return pyyaml.safe_dump(yaml_output, default_flow_style=False, indent=2)
return pyyaml.safe_dump(yaml_output, default_flow_style=False, indent=2, sort_keys=False)
except pyyaml.YAMLError as err:
LOG_DEBUG('Failed to dump yaml file contents', err)
raise ClowderError(ClowderErrorType.FAILED_YAML_DUMP,
Expand Down
14 changes: 7 additions & 7 deletions tests/functional/data/yaml/command_output/clowder.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

name: cats-default-test
protocol: https
clowder:
cats:
- name: JrGoodle/mu
branch: knead
- name: JrGoodle/duke
branch: purr
black-cats:
path: black-cats
projects:
- JrGoodle/kit
- JrGoodle/kishka
- JrGoodle/june
- JrGoodle/sasha
cats:
- branch: knead
name: JrGoodle/mu
- branch: purr
name: JrGoodle/duke
name: cats-default-test
protocol: https
30 changes: 15 additions & 15 deletions tests/functional/data/yaml/command_output/resolved.clowder.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

name: cats-default-test
protocol: https
clowder:
cats:
- name: JrGoodle/mu
commit: 60044a0c5489872c1b793d9dbd8b3e4b19f66fb3
- name: JrGoodle/duke
commit: 43347045441d05c2dee745acf2a96f90643b8210
black-cats:
path: black-cats
projects:
- commit: f2e20031ddce5cb097105f4d8ccbc77f4ac20709
name: JrGoodle/kit
- commit: 9777b550e4b586b71b33d5f703d8c37d2b477b1a
name: JrGoodle/kishka
- commit: 858cbea9bdef3809382681cf2b7c01817ca50b2d
name: JrGoodle/june
- commit: b2c8e63d65aba4375b8ea5b84ad2a0e8487ec196
name: JrGoodle/sasha
cats:
- commit: 60044a0c5489872c1b793d9dbd8b3e4b19f66fb3
name: JrGoodle/mu
- commit: 43347045441d05c2dee745acf2a96f90643b8210
name: JrGoodle/duke
name: cats-default-test
protocol: https
- name: JrGoodle/kit
commit: f2e20031ddce5cb097105f4d8ccbc77f4ac20709
- name: JrGoodle/kishka
commit: 9777b550e4b586b71b33d5f703d8c37d2b477b1a
- name: JrGoodle/june
commit: 858cbea9bdef3809382681cf2b7c01817ca50b2d
- name: JrGoodle/sasha
commit: b2c8e63d65aba4375b8ea5b84ad2a0e8487ec196

0 comments on commit 86e445c

Please sign in to comment.