Skip to content

Commit c681235

Browse files
committed
Merge branch 'caps-cli'
2 parents f705cb1 + 68f2d14 commit c681235

15 files changed

+522
-48
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ Help functionality in the CLI
5656
caps-cli --help
5757
```
5858

59+
Use this command to configure username and password for the Model Catalog API
60+
```
61+
caps-cli configure --profile=<NAME_OF_PROFILE>
62+
```
63+
5964
Use this command to know the details of each CLI function (options, arguments)
6065
```
6166
caps-cli <initialize, push, validate> --help
@@ -65,13 +70,13 @@ Run this command to test the template creation functionality (with 2 inputs, 3 o
6570
```
6671
caps-cli initialize -i 2 -o 3 -p 1
6772
```
68-
Running the above command will generate a yaml outline. By default this will create the outline in the current directory, use `-d path/to/location` to speciy a location. By default the yaml will be named `example_yaml.yaml`, user can also use `-d fileName.yaml` to name the output (note the user must specify .yaml after the name. Otherwise the program does not know if it is a directory or filename). By default this will not override an existing file if they share the same name. use flag `-f` to force override.
73+
Running the above command will generate a yaml outline. By default this will create the outline in the current directory, use -d path/to/location to speciy a location. By default the yaml will be named example_yaml.yaml, user can also use -d fileName.yaml to name the output (note the user must specify .yaml after the name. Otherwise the program does not know if it is a directory or filename). By default this will not override an existing file if they share the same name. use flag -f to force override.
6974

7075

7176

7277
Run this command to transform the input YAML into a postable JSON object
7378
```
74-
caps-cli push <path_of_yaml_file_from_root_of_the_project>
79+
caps-cli push --profile=<NAME_OF_PROFILE> <path_of_yaml_file_from_root_of_the_project>
7580
```
7681

7782
Run this command to validate the JSON schema obtained by using the above command
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#This is an example. The model here is a simplification of an existing one.
2+
3+
modelName: EACS #URI: https://w3id.org/okn/i/mint/EACS
4+
#modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
5+
#configuration with no setup ->is modelConfig. Setup is model setup
6+
configuration: EACS_v7_ethiopia
7+
#short_name maps to label. I think it's easier to understand
8+
short_name: Basic configuration of the economic aggregate crop supply model (EACS) v2
9+
description:
10+
- Aggregate crop supply response model for the country of South Sudan (version 2)
11+
author:
12+
- name: Rajiv Mayani #Assume type is person unless a type:Organization is added.
13+
contributor: #optional
14+
- name: Deborah Khider
15+
- name: Zeya Zhang
16+
email: blah@b.com
17+
hasComponentLocation:
18+
- https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v2/economic-v2.zip
19+
hasImplementationScriptLocation: #Optional
20+
- https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v2/run
21+
keywords:
22+
- economy; land use; crop production; fertilizer costs
23+
hasGrid: #optional
24+
- short_name: Grid from the economic crop supply model
25+
description:
26+
- Grid from the economic crop supply model
27+
hasDimension: 0D
28+
hasShape: Point
29+
hasSpatialResolution: Point
30+
hasModelCategory: #optional
31+
- Economy
32+
hasRegion: #Optional
33+
- description:
34+
- Area of the Pongo Basin in South Sudan
35+
short_name: Pongo basin region (South Sudan)
36+
hasSoftwareImage: mintproject/economic:v2
37+
hasSourceCode: #optional
38+
- codeRepository: https://github.com/mintproject/MINT-WorkflowDomain/tree/master/WINGSWorkflowComponents/economic-v2
39+
description:
40+
- Code for creating an encapsulation of EACS for a model configuration (version 2)
41+
short_name: EACS configuration component (version 2 of code)
42+
programmingLanguage:
43+
- shell
44+
hasOutputTimeInterval:
45+
- short_name: Simple economic time interval
46+
description:
47+
- Time interval used in the aggregate crop supply response model for the country
48+
of South Sudan configuration
49+
50+
hasProcess: #each has to be mapped to an object.
51+
- Supply of Crop
52+
- Use of Fertilizer
53+
- Use of land
54+
hasInput:
55+
#input1
56+
- short_name: economic-sim #start always with the name of the input/output/param
57+
description: #should always follow the short name
58+
- Contains the economic data used in the simulation analysis
59+
position: 1
60+
type: #By default these are all datasetSpecifications. The type below is an additional aid
61+
- https://w3id.org/wings/export/MINT#EconomicSimData
62+
hasDimensionality: 0 #If this is not specified, assume 0
63+
hasFormat: csv
64+
#input2
65+
- short_name: economic-subsidies
66+
description:
67+
- File indicating the economic subsidies to use on each iteration of the model
68+
hasDimensionality: 0
69+
hasFormat: txt
70+
position: 2
71+
type:
72+
- https://w3id.org/wings/export/MINT#EconomicSubsidies
73+
74+
hasOutput:
75+
#output 1
76+
- short_name: crop-yield
77+
description:
78+
- Contains the yield elasticities to fertilizer use that are derived from Cycles
79+
output
80+
hasDimensionality: 0
81+
hasFormat: csv
82+
position: 1
83+
type:
84+
- https://w3id.org/wings/export/MINT#CropYield
85+
86+
87+
#output 2
88+
- short_name: economic-data
89+
description:
90+
- Contains data on observed prices for each crop and supply elasticities and the
91+
costs of production for each crop
92+
position: 2
93+
type:
94+
- https://w3id.org/wings/export/MINT#EconomicData
95+
96+
hasParameter:
97+
- short_name: maize-crop-price-adjustment
98+
description:
99+
- Change in market price by crop
100+
usesUnit: "%"
101+
dataType: float #maps to hasDataType
102+
defaultvalue: 0 #maps to hasDefaultValue
103+
maxValue: 50 #optional. Maps to hasMaximumAcceptedValue
104+
minValue: -50 #optional. Maps to hasMinimumAcceptedValue

examples/ModelConfigExample.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#This is an example. The model here is a simplification of an existing one.
22

33
modelName: EACS #URI: https://w3id.org/okn/i/mint/EACS
4-
modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
4+
#modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
55
#configuration with no setup ->is modelConfig. Setup is model setup
66
configuration: EACS_v7_ethiopia
77
#short_name maps to label. I think it's easier to understand
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#This is an example. The model here is a simplification of an existing one.
2+
modelName: EACS #URI: https://w3id.org/okn/i/mint/EACS
3+
#modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
4+
configuration: EACS_v7_ethiopia
5+
setup: EACS_v7_ethiopia_2018
6+
#short_name maps to label. I think it's easier to understand
7+
short_name: Basic configuration of the economic aggregate crop supply model (EACS) v2
8+
description:
9+
- Aggregate crop supply response model for the country of South Sudan (version 2)
10+
author:
11+
- name: Rajiv Mayani #Assume type is person unless a type:Organization is added.
12+
contributor: #optional
13+
- name: Deborah Khider
14+
- name: Zeya Zhang
15+
email: blah@b.com
16+
hasComponentLocation:
17+
- https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v2/economic-v2.zip
18+
hasImplementationScriptLocation: #Optional
19+
- https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v2/run
20+
keywords:
21+
- economy; land use; crop production; fertilizer costs
22+
hasGrid: #optional
23+
- short_name: Grid from the economic crop supply model
24+
description:
25+
- Grid from the economic crop supply model
26+
hasDimension: 0D
27+
hasShape: Point
28+
hasSpatialResolution: Point
29+
hasModelCategory: #optional
30+
- Economy
31+
hasRegion: #Optional
32+
- description:
33+
- Area of the Pongo Basin in South Sudan
34+
short_name: Pongo basin region (South Sudan)
35+
hasSoftwareImage: mintproject/economic:v2
36+
hasSourceCode: #optional
37+
- codeRepository: https://github.com/mintproject/MINT-WorkflowDomain/tree/master/WINGSWorkflowComponents/economic-v2
38+
description:
39+
- Code for creating an encapsulation of EACS for a model configuration (version 2)
40+
short_name: EACS configuration component (version 2 of code)
41+
programmingLanguage:
42+
- shell
43+
hasOutputTimeInterval:
44+
- short_name: Simple economic time interval
45+
description:
46+
- Time interval used in the aggregate crop supply response model for the country
47+
of South Sudan configuration
48+
49+
hasProcess: #each has to be mapped to an object.
50+
- Supply of Crop
51+
- Use of Fertilizer
52+
- Use of land
53+
hasInput:
54+
#input1
55+
- short_name: economic-sim #start always with the name of the input/output/param
56+
description: #should always follow the short name
57+
- Contains the economic data used in the simulation analysis
58+
position: 1
59+
hasValue: #maps to hasFixedResource
60+
- short_name: File for the Oromia region
61+
#below: maps to sd:value
62+
url: https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v7/data/elasticity-cyclesdata.csv
63+
datacatalogId: FFF-e15c1f9e-c7ae-4cda-ba23-2e4f2286a18 #Optional
64+
- short_name: Second file for the Oromia region
65+
#below: maps to sd:value
66+
url: https://github.com/mintproject/MINT-WorkflowDomain/raw/master/WINGSWorkflowComponents/economic-v7/data/some_other_file
67+
type: #By default these are all datasetSpecifications. The type below is an additional aid
68+
- https://w3id.org/wings/export/MINT#EconomicSimData
69+
hasDimensionality: 0 #If this is not specified, assume 0
70+
#input2
71+
- short_name: economic-subsidies
72+
description:
73+
- File indicating the economic subsidies to use on each iteration of the model
74+
hasDimensionality: 0
75+
hasFormat: txt
76+
position: 2
77+
type:
78+
- https://w3id.org/wings/export/MINT#EconomicSubsidies
79+
80+
hasOutput:
81+
#output 1
82+
- short_name: crop-yield
83+
description:
84+
- Contains the yield elasticities to fertilizer use that are derived from Cycles
85+
output
86+
hasDimensionality: 0
87+
hasFormat: csv
88+
position: 1
89+
type:
90+
- https://w3id.org/wings/export/MINT#CropYield
91+
92+
#output 2
93+
- short_name: economic-data
94+
description:
95+
- Contains data on observed prices for each crop and supply elasticities and the
96+
costs of production for each crop
97+
position: 2
98+
type:
99+
- https://w3id.org/wings/export/MINT#EconomicData
100+
hasParameter:
101+
- short_name: maize-crop-price-adjustment
102+
description:
103+
- Change in market price by crop
104+
usesUnit: "%"
105+
value: 0 #maps to hasFixedValue

examples/SetupExample_new.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#This is an example. The model here is a simplification of an existing one.
22
modelName: EACS #URI: https://w3id.org/okn/i/mint/EACS
3-
modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
3+
#modelVersion: v7 #URI: https://w3id.org/okn/i/mint/EACS_v7
44
configuration: EACS_v7_ethiopia
55
setup: EACS_v7_ethiopia_2018
66
#short_name maps to label. I think it's easier to understand
@@ -126,4 +126,4 @@ hasParameter:
126126
description:
127127
- Change in market price by crop
128128
usesUnit: "%"
129-
value: 0 #maps to hasFixedValue
129+
value: 0 #maps to hasFixedValue

insertion_template.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"jsonschema>=3.0.0",
1010
"semver>=2.8.1",
1111
"requests",
12+
"ruamel.yaml",
13+
"modelcatalog-api"
1214
]
1315

1416

@@ -66,4 +68,4 @@ def find_paths(dirname):
6668
zip_safe=False,
6769
install_requires=install_requires,
6870
python_requires=">=3.5.0",
69-
)
71+
)

src/caps/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.1"
1+
__version__ = "0.3.0"

src/caps/__main__.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from caps import _utils, _metadata_schema, _transform_data, _initialize, _push, _validate
1414

1515
__DEFAULT_CAPS_CLI_CREDENTIALS_FILE__ = "~/.caps-cli/credentials"
16+
__DEFAULT_MINT_API_CREDENTIALS_FILE__ = "~/.mint_api/credentials"
1617

1718

1819
@click.group()
@@ -35,6 +36,42 @@ def version(debug=False):
3536
click.echo(f"{Path(sys.argv[0]).name} v{caps.__version__}")
3637

3738

39+
@cli.command(help="Configure Model Catalog API credentials")
40+
@click.option(
41+
"--profile",
42+
"-p",
43+
envvar="CAPS_PROFILE",
44+
type=str,
45+
default="default",
46+
metavar="<profile-name>",
47+
)
48+
def configure(profile="default"):
49+
api_username = click.prompt("Model Catalog API Username")
50+
api_password = click.prompt("Model Catalog API Password", hide_input=True)
51+
52+
credentials_file = Path(
53+
os.getenv("MINT_API_CREDENTIALS_FILE", __DEFAULT_MINT_API_CREDENTIALS_FILE__)
54+
).expanduser()
55+
os.makedirs(str(credentials_file.parent), exist_ok=True)
56+
57+
credentials = configparser.ConfigParser()
58+
credentials.optionxform = str
59+
60+
if credentials_file.exists():
61+
credentials.read(credentials_file)
62+
63+
credentials[profile] = {
64+
"api_username": api_username,
65+
"api_password": api_password
66+
}
67+
68+
with credentials_file.open("w") as fh:
69+
credentials_file.parent.chmod(0o700)
70+
credentials_file.chmod(0o600)
71+
credentials.write(fh)
72+
click.secho(f"Success", fg="green")
73+
74+
3875
@cli.command(short_help="Create an empty JSON template to fill the data to be inserted in Model Catalog")
3976
@click.option(
4077
"--inputs",
@@ -54,18 +91,40 @@ def version(debug=False):
5491
type=int,
5592
default=0,
5693
)
57-
def initialize(inputs=0, outputs=0, parameters=0):
94+
@click.option(
95+
"--directory",
96+
"-d",
97+
type=str,
98+
default="",
99+
help="Specify folder to generate new component in. Entering a filename will initialize the yaml with that name"
100+
)
101+
@click.option(
102+
"--force",
103+
"-f",
104+
is_flag=True,
105+
help="Force file creation. This will override a file with the same name. Use with caution"
106+
)
107+
def initialize(inputs=0, outputs=0, parameters=0, directory="", force=False):
58108

59109
logging.info("Initializing YAML")
60-
_initialize.initialize(inputs, outputs, parameters)
110+
_initialize.initialize(inputs, outputs, parameters, directory, force)
61111
click.secho(f"Success", fg="green")
62112

63113

64114
@cli.command(short_help="Transform the input YAML into a Valid JSON for posting the file to Model Catalog")
115+
@click.option(
116+
"--profile",
117+
"-m",
118+
envvar="MINT_API_PROFILE",
119+
type=str,
120+
default="default",
121+
metavar="<profile-name>",
122+
)
123+
@click.argument("is_setup", default="false", type=str)
65124
@click.argument("yaml_file_path", default=None, type=str)
66-
def push(yaml_file_path):
125+
def push(is_setup, yaml_file_path, profile="default"):
67126
logging.info("Pushing yaml")
68-
_push.push(yaml_file_path)
127+
_push.push(is_setup, yaml_file_path, profile=profile)
69128
click.secho(f"Success", fg="green")
70129

71130

0 commit comments

Comments
 (0)