Skip to content

Commit 63a26d2

Browse files
committed
refactor output schema support to provide more than XML output (#128)
1 parent 2e70698 commit 63a26d2

File tree

28 files changed

+800
-106
lines changed

28 files changed

+800
-106
lines changed

README.md

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ python setup.py install
3737
### From the command line
3838

3939
```bash
40+
# show all subcommands
41+
pygeometa
42+
43+
# show all supported schemas
44+
pygeometa schemas
45+
4046
# provide a basic sanity check/report on an MCF
4147
pygeometa info --mcf=path/to/file.yml
4248

@@ -64,21 +70,26 @@ Schemas supported by pygeometa:
6470
### Using the API from Python
6571

6672
```python
67-
from pygeometa.core import render_template
73+
from pygeometa.core import read_mcf, render_j2_template
74+
75+
# read from disk
76+
mcf_dict = read_mcf('/path/to/file.yml')
77+
# read from string
78+
mcf_dict = read_mcf(mcf_string)
79+
80+
# choose ISO 19139 output schema
81+
from pygeometa.schemas.iso19139 import ISO19139OutputSchema
82+
iso_os = ISO19139OutputSchema()
83+
6884
# default schema
69-
xml_string = render_template('/path/to/file.yml', schema='iso19139')
85+
xml_string = render_j2_template(mcf_dict, template_dir=iso_os.template_dir)
86+
7087
# user-defined schema
71-
xml_string = render_template('/path/to/file.yml', schema_local='/path/to/new-schema')
72-
# dictionary representation of YAML
73-
xml_string = render_template(yaml_dict, schema='iso19139')
74-
with open('output.xml', 'w') as ff:
88+
xml_string = render_j2_template(mcf_dict, template_dir='/path/to/new-schema')
89+
90+
# write to disk
91+
with open('output.xml', 'wb') as ff:
7592
ff.write(xml_string)
76-
# render from an MCF stored in a string
77-
mcf_string = '...' # some string
78-
xml_string = render_template_string(mcf_string, schema='iso19139')
79-
# render from an MCF as a ConfigParser object
80-
mcf_cp = '...' # some ConfigParser object
81-
xml_string = render_template_string(mcf_cp, schema='iso19139')
8293
```
8394

8495
## Migration
@@ -107,15 +118,40 @@ pip install -r requirements-dev.txt
107118

108119
### Adding a Metadata Schema to the Core
109120

110-
List of supported metadata schemas in `pygeometa/templates/`
121+
Adding an output metadata schemas to pygeometa involves extending
122+
`pygeometa.schemas.base.BaseOutputSchema` and supporting the `write`
123+
function to return a string of exported metadata content. If you are using
124+
Jinja2 templates, see the next section. If you are using another means of
125+
generating metadata (lxml, xml.etree, json, etc.), override the ABS `write`
126+
class to emit a string using your tooling/workflow accordingly. See the
127+
below sections for examples.
128+
129+
Once you have added your metadata schema, you need to register it with
130+
pygeometa's schema registry:
111131

112-
To add support to new metadata schemas:
113132
```bash
114-
cp -r pygeometa/templates/iso19139 pygeometa/templates/new-schema
133+
vi pygeometa/schemas/__init__.py
134+
# edit the SCHEMAS dict with the metadata schema name and dotted path of class
115135
```
116-
Then modify `*.j2` files in the new `pygeometa/templates/new-schema` directory
136+
137+
#### Jinja2 templates
138+
139+
To add support for a new metadata schema using Jinja2 templates:
140+
```bash
141+
cp -r pygeometa/schemas/iso19139 pygeometa/schemas/new-schema
142+
```
143+
Then modify `*.j2` files in the new `pygeometa/schemas/new-schema` directory
117144
to comply to new metadata schema.
118145

146+
#### Custom tooling
147+
148+
To add support for a new metadata schemas using other tooling/workflow:
149+
```bash
150+
mkdir pygeometa/schemas/foo
151+
cp pygeometa/schemas/iso19139/__init__.py pygeometa/schemas/foo
152+
vi pygeometa/schemas/foo/__init__.py
153+
# update class name and super().__init__() function accordingly
154+
119155
### Running Tests
120156

121157
```bash

docs/content/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ Feel free to open an [issue](https://github.com/geopython/pygeometa/issues)!
2323
pygeometa has minimal dependencies (install is less than 50K), and provides
2424
a flexible extension mechanism leveraging the Jinja2 templating system.
2525

26-
pygeoapi is [open source](https://opensource.org) and released under an
27-
[MIT license](https://github.com/geopython/pygeoapi/blob/master/LICENSE.md).
26+
pygeometa is [open source](https://opensource.org) and released under an
27+
[MIT license](https://github.com/geopython/pygeometa/blob/master/LICENSE.md).
2828

2929
## Features
3030
* simple YAML-based configuration
31-
* extensible: template architecture allows for easy addition of new metadata
32-
formats
31+
* extensible: plugin architecture allows for easy addition of new metadata
32+
formats using Jinja2 templates or custom workflow (JSON, YAML, CSV, etc.)
3333
* flexible: use as a command-line tool or integrate as a library
3434

3535
## History

docs/content/reference/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* [Metadata Control File](mcf)
44
* Formats
55
* iso19139, [specification](http://www.iso.org/iso/catalogue_detail.htm?csnumber=32557)
6-
* [iso19139-hnap](formats/hnap), [specification](http://www.gcpedia.gc.ca/wiki/Federal_Geospatial_Platform/Policies_and_Standards/Catalogue/Release/Appendix_B_Guidelines_and_Best_Practices/Guide_to_Harmonized_ISO_19115:2003_NAP)
6+
* [iso19139-hnap](formats/iso19139-hnap), [specification](http://www.gcpedia.gc.ca/wiki/Federal_Geospatial_Platform/Policies_and_Standards/Catalogue/Release/Appendix_B_Guidelines_and_Best_Practices/Guide_to_Harmonized_ISO_19115:2003_NAP)
77
* [wmo-cmp](formats/wmo-cmp), [specification](http://wis.wmo.int/2013/metadata/version_1-3-0/WMO_Core_Metadata_Profile_v1.3_Part_1.pdf)
88
* [wmo-wigos](formats/wmo-wigos.md), [specification](https://library.wmo.int/opac/doc_num.php?explnum_id=3653)

docs/content/tutorial.md

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ The basic pygeometa workflow is:
3535
### Running
3636

3737
```bash
38+
# show all subcommands
39+
pygeometa
40+
41+
# show all supported schemas
42+
pygeometa schemas
43+
3844
# provide a basic sanity check/report on an MCF (Metadata Control File)
3945
pygeometa info --mcf=path/to/file.yml
4046

@@ -95,21 +101,26 @@ python setup.py install
95101
### Using the API from Python
96102

97103
```python
98-
from pygeometa.core import render_template
104+
from pygeometa.core import read_mcf, render_j2_template
105+
106+
# read from disk
107+
mcf_dict = read_mcf('/path/to/file.yml')
108+
# read from string
109+
mcf_dict = read_mcf(mcf_string)
110+
111+
# choose ISO 19139 output schema
112+
from pygeometa.schemas.iso19139 import ISO19139OutputSchema
113+
iso_os = ISO19139OutputSchema()
114+
99115
# default schema
100-
xml_string = render_template('/path/to/file.yml', schema='iso19139')
116+
xml_string = render_j2_template(mcf_dict, template_dir=iso_os.template_dir)
117+
101118
# user-defined schema
102-
xml_string = render_template('/path/to/file.yml', schema_local='/path/to/new-schema')
103-
# dictionary representation of YAML
104-
xml_string = render_template(yaml_dict, schema='iso19139')
105-
with open('output.xml', 'w') as ff:
119+
xml_string = render_j2_template(mcf_dict, schema_local='/path/to/new-schema')
120+
121+
# write to disk
122+
with open('output.xml', 'wb') as ff:
106123
ff.write(xml_string)
107-
# render from an MCF stored in a string
108-
mcf_string = '...' # some string
109-
xml_string = render_template_string(mcf_string, schema='iso19139')
110-
# render from an MCF as a ConfigParser object
111-
mcf_cp = '...' # some ConfigParser object
112-
xml_string = render_template_string(mcf_cp, schema='iso19139')
113124
```
114125

115126
## Development
@@ -125,15 +136,41 @@ pip install -r requirements-dev.txt
125136

126137
### Adding a Metadata Schema to the Core
127138

128-
List of supported metadata schemas in `pygeometa/templates/`
139+
Adding an output metadata schemas to pygeometa involves extending
140+
`pygeometa.schemas.base.BaseOutputSchema` and supporting the `write`
141+
function to return a string of exported metadata content. If you are using
142+
Jinja2 templates, see the next section. If you are using another means of
143+
generating metadata (lxml, xml.etree, json, etc.), override the ABS `write`
144+
class to emit a string using your tooling/workflow accordingly. See the
145+
below sections for examples.
146+
147+
Once you have added your metadata schema, you need to register it with
148+
pygeometa's schema registry:
129149

130-
To add support to new metadata schemas:
131150
```bash
132-
cp -r pygeometa/templates/iso19139 pygeometa/templates/new-schema
151+
vi pygeometa/schemas/__init__.py
152+
# edit the SCHEMAS dict with the metadata schema name and dotted path of class
133153
```
134-
Then modify `*.j2` files in the new `pygeometa/templates/new-schema` directory
154+
155+
#### Jinja2 templates
156+
157+
To add support for a new metadata schema using Jinja2 templates:
158+
```bash
159+
cp -r pygeometa/schemas/iso19139 pygeometa/schemas/new-schema
160+
```
161+
Then modify `*.j2` files in the new `pygeometa/schemas/new-schema` directory
135162
to comply to new metadata schema.
136163

164+
#### Custom tooling
165+
166+
To add support for a new metadata schemas using other tooling/workflow:
167+
```bash
168+
mkdir pygeometa/schemas/foo
169+
cp pygeometa/schemas/iso19139/__init__.py pygeometa/schemas/foo
170+
vi pygeometa/schemas/foo/__init__.py
171+
# update class name and super().__init__() function accordingly
172+
```
173+
137174
### Running Tests
138175

139176
```bash
@@ -169,4 +206,3 @@ metadata within a real-time environment and automated workflows.
169206

170207
In 2015 pygeometa was made publically available in support of the Treasury
171208
Board [Policy on Acceptable Network and Device Use](http://www.tbs-sct.gc.ca/pol/doc-eng.aspx?id=27122).
172-

docs/mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ nav:
1313
- Reference:
1414
- Metadata Control File: reference/mcf.md
1515
- Formats:
16-
- ISO Harmonized North American Profile (HNAP): reference/formats/hnap.md
16+
- ISO Harmonized North American Profile (HNAP): reference/formats/iso19139-hnap.md
1717
- WMO Core Metadata Profile: reference/formats/wmo-cmp.md
1818
- WMO WIGOS Metadata Standard: reference/formats/wmo-wigos.md
1919
# - Index: reference/index.md

pygeometa/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
import click
4848

49-
from pygeometa.core import generate_metadata, info
49+
from pygeometa.core import generate_metadata, info, schemas
5050
from pygeometa.migrations import migrate
5151

5252
__version__ = '0.6.dev0'
@@ -60,4 +60,5 @@ def cli():
6060

6161
cli.add_command(generate_metadata)
6262
cli.add_command(info)
63+
cli.add_command(schemas)
6364
cli.add_command(migrate)

0 commit comments

Comments
 (0)