Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mapper aliases configurable #175

Merged
merged 5 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions optimade/server/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ index_base_url = http://localhost:5001/optimade
[structures]
band_gap :
_mp_chemsys :

[structures.aliases]
id = task_id
chemical_formula_descriptive = pretty_formula
chemical_formula_reduced = pretty_formula
chemical_formula_anonymous = formula_anonymous
13 changes: 13 additions & 0 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def load_from_ini(self):
else:
self.provider = self._DEFAULTS("provider")

self.aliases = {}
for endpoint in {"links", "references", "structures"}:
if f"{endpoint}.aliases" in config:
section = config[f"{endpoint}.aliases"]
self.aliases[endpoint] = tuple(section.items())

self.provider_fields = {}
for endpoint in {"links", "references", "structures"}:
self.provider_fields[endpoint] = (
Expand Down Expand Up @@ -233,5 +239,12 @@ def load_from_json(self):
set(config.get("provider_fields", {}).get(endpoint, []))
)

self.aliases = {}
if "aliases" in config:
for endpoint in {"links", "references", "structures"}:
if endpoint in config["aliases"]:
section = config["aliases"][endpoint]
self.aliases[endpoint] = tuple(section.items())


CONFIG = ServerConfig()
1 change: 1 addition & 0 deletions optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def all_aliases(cls) -> Tuple[Tuple[str, str]]:
(CONFIG.provider["prefix"] + field, field)
for field in CONFIG.provider_fields.get(cls.ENDPOINT, [])
)
+ CONFIG.aliases.get(cls.ENDPOINT, ())
+ cls.ALIASES
)

Expand Down
6 changes: 0 additions & 6 deletions optimade/server/mappers/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,3 @@
class StructureMapper(ResourceMapper):

ENDPOINT = "structures"
ALIASES = (
("id", "task_id"),
("chemical_formula_descriptive", "pretty_formula"),
("chemical_formula_reduced", "pretty_formula"),
("chemical_formula_anonymous", "formula_anonymous"),
)
6 changes: 6 additions & 0 deletions tests/server/config_test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ index_base_url = http://localhost:5001/optimade
[structures]
band_gap :
_mp_chemsys :

[structures.aliases]
id = task_id
chemical_formula_descriptive = pretty_formula
chemical_formula_reduced = pretty_formula
chemical_formula_anonymous = formula_anonymous
8 changes: 8 additions & 0 deletions tests/server/config_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@
"band_gap",
"_mp_chemsys"
]
},
"aliases": {
"structures": {
"id": "task_id",
"chemical_formula_descriptive": "pretty_formula",
"chemical_formula_reduced": "pretty_formula",
"chemical_formula_anonymous": "formula_anonymous"
}
}
}