-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation for code metadata (#78)
And prepare way for transition to YAML format for code metadata.
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Schema and validation of simulation code metadata.""" | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
from typing import Dict, Optional, List | ||
from pathlib import Path | ||
from serde import serialize, deserialize, json, yaml | ||
|
||
DATA_DIR = Path(__file__).parent.parent / 'src' / 'data' | ||
|
||
class QueryMethods(str, Enum): | ||
"""Methods for querying Google Scholar.""" | ||
SEARCH = 'search term' | ||
PUBLICATION = 'publication' | ||
|
||
|
||
@deserialize | ||
@serialize | ||
@dataclass | ||
class Code: # pylint: disable=too-many-instance-attributes | ||
"""Class representing a code. | ||
Optional fields are no longer used by the web application. | ||
Fields with default values are used by the web application. | ||
""" | ||
name: str | ||
benchmarks: List[str] | ||
homepage: str | ||
author_name: Optional[str] | ||
query_method: str | ||
query_string: str | ||
types: List[str] | ||
license: str | ||
tags: List | ||
nomad_tags: list = None | ||
query_publication_id: int = None | ||
notes: str = None | ||
license_annotation: str = None | ||
|
||
def read_codes_json(path: Path = DATA_DIR / 'codes.json') -> Dict[str, Code]: | ||
"""Read codes.json file and return dictionary.""" | ||
|
||
with open(path) as handle: | ||
return json.from_json(Dict[str, Code], handle.read()) | ||
|
||
def write_codes_yaml(path: Path = DATA_DIR / 'codes.yaml', codes: Dict[str, Code] = read_codes_json()) -> None: | ||
"""Write codes.yaml file. | ||
May use if switch to yaml format is made. | ||
""" | ||
|
||
with open(path, 'w') as handle: | ||
handle.write(yaml.to_yaml(codes)) | ||
|
||
def read_codes_yaml(path: Path = DATA_DIR / 'codes.yaml') -> Dict[str, Code]: | ||
"""Read codes.yaml file and return dictionary. | ||
May use if switch to yaml format is made. | ||
""" | ||
|
||
with open(path) as handle: | ||
return yaml.from_yaml(Dict[str, Code], handle.read()) | ||
|
||
print(read_codes_json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyserde[yaml]~=0.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters