forked from OSeMOSYS/otoole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write out datapackage schema from user-config (closes OSeMOSYS#122)
- Loading branch information
Showing
4 changed files
with
243 additions
and
72 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
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 |
---|---|---|
|
@@ -6,80 +6,98 @@ | |
|
||
import logging | ||
import os | ||
from frictionless import Package | ||
from typing import Dict | ||
|
||
from datapackage import Package | ||
logger = logging.getLogger() | ||
|
||
from otoole.utils import read_packaged_file | ||
|
||
logger = logging.getLogger() | ||
def generate_package(package: Package, config: Dict[str, Dict]) -> Package: | ||
"""Adds schema information to a basic Resource | ||
Arguments | ||
--------- | ||
package: Package | ||
A frictionless Package | ||
config: Dict[str, Dict] | ||
A user-configuration dictionary | ||
def generate_package(path_to_package): | ||
"""Creates a datapackage in folder ``path_to_package`` | ||
Returns | ||
------- | ||
dict | ||
Notes | ||
----- | ||
[{'fields': 'REGION', 'reference': {'resource': 'REGION', 'fields': 'VALUE'}}] | ||
""" | ||
|
||
datapath = os.path.join(path_to_package) | ||
package = Package(base_path=datapath) | ||
|
||
package.infer("data/*.csv") | ||
logger.debug(f"Auto-identified resources {package.resources}") | ||
|
||
package.descriptor["licenses"] = [ | ||
{ | ||
"name": "CC-BY-4.0", | ||
"path": "https://creativecommons.org/licenses/by/4.0/", | ||
"title": "Creative Commons Attribution 4.0", | ||
} | ||
] | ||
# package.licenses = [ | ||
# { | ||
# "name": "CC-BY-4.0", | ||
# "path": "https://creativecommons.org/licenses/by/4.0/", | ||
# "title": "Creative Commons Attribution 4.0", | ||
# } | ||
# ] | ||
|
||
package.descriptor["title"] = "The OSeMOSYS Simplicity Example Model" | ||
# package.title = "The OSeMOSYS Simplicity Example Model" | ||
|
||
package.descriptor["name"] = "osemosys_model_simplicity" | ||
# package.name = "osemosys_model_simplicity" | ||
|
||
package.descriptor["contributors"] = [ | ||
{ | ||
"title": "Will Usher", | ||
"email": "[email protected]", | ||
"path": "http://www.kth.se/wusher", | ||
"role": "author", | ||
} | ||
] | ||
# package.contributors = [ | ||
# { | ||
# "title": "Will Usher", | ||
# "email": "[email protected]", | ||
# "path": "https://www.kth.se/profile/wusher/", | ||
# "role": "author", | ||
# } | ||
# ] | ||
|
||
package.commit() | ||
for resource in package.resources: # typing: Resource | ||
|
||
config = read_packaged_file("config.yaml", "otoole.preprocess") | ||
|
||
new_resources = [] | ||
for resource in package.resources: | ||
name = resource.title # Use the title which preserves case | ||
|
||
descriptor = resource.descriptor | ||
logger.debug(f"Updating resource '{name}'") | ||
|
||
name = resource.name | ||
if config[name]["type"] == "param": | ||
|
||
indices = config[name]["indices"] | ||
logger.debug("Indices of %s are %s", name, indices) | ||
|
||
fields = [] | ||
foreign_keys = [] | ||
for index in indices: | ||
key = { | ||
"fields": index, | ||
"reference": {"resource": index, "fields": "VALUE"}, | ||
"reference": {"resource": index.lower(), "fields": "VALUE"}, | ||
} | ||
foreign_keys.append(key) | ||
field = {"name": index, "type": config[index]["dtype"]} | ||
|
||
fields.append(field) | ||
|
||
value_field = {"name": "VALUE", "type": config[name]["dtype"]} | ||
|
||
fields.append(value_field) | ||
|
||
resource.schema.fields = fields | ||
resource.schema.foreign_keys = foreign_keys | ||
resource.schema.primary_key = indices | ||
resource.schema.missing_values = [""] | ||
|
||
elif config[name]["type"] == "set": | ||
|
||
descriptor["schema"]["foreignKeys"] = foreign_keys | ||
descriptor["schema"]["primaryKey"] = indices | ||
descriptor["schema"]["missingValues"] = [""] | ||
fields = [] | ||
value_field = {"name": "VALUE", "type": config[name]["dtype"]} | ||
|
||
new_resources.append(descriptor) | ||
fields.append(value_field) | ||
resource.schema.fields = fields | ||
resource.schema.missing_values = [""] | ||
|
||
package.descriptor["resources"] = new_resources | ||
package.commit() | ||
logger.debug(f"Schema for resource {name}: {resource.schema}") | ||
|
||
filepath = os.path.join(path_to_package, "datapackage.json") | ||
package.save(filepath) | ||
return package | ||
|
||
|
||
def validate_contents(path_to_package): | ||
|
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
Oops, something went wrong.