OS FDP Adapters Service
3.3K
Convert various data formats into FDP
This repository contains a collection of converters from various data formats and sources into FDP. These adapters are used by OpenSpending when importing data into the OpenSpending Data Store. The goal is for OpenSpending importer to handle only FDP sources, and support all other sources through the use of this adapter collection.
Each converter is used for a single data source. When used, it will convert the data source into a Fiscal Data Package with all its resources.
Common use case:
datapackage.json file
Each datasource code resides in a separate directory.
In the directory there must be a adapter.yml file which contains the adapter definitions.
adapter.yml has a root element named adapters, which is a list of adapter definitions. Each adapter has the following properties:
urlmatch: a list of regular expressions for matching potential URLsrun: name of a python script (without the .py) which performs the conversion. The script is resolved from the path containing the adapter definition file.parameters: an optional list of string parameters which will be passed to the adapter scriptkind: either json or csvEach adapter script will receive parameters through the command line.
First argument will always be the matched URL. After that, parameters that were specified in the adapter configuration.
Output file should be printed to stdout
Each adapter directory should have a subdirectory named tests. In this directory a testcases.yml file must be present. It must have a testcases root element which is a list of test cases.
Each test case is a list of two elements - a URL and an expected result. The URL must match in one of the rules of the adapter; when running the adapter with the specified URL it must return a file identical to the expected result.
URL can be an external URL or the name of a local file in the tests directory.
Directory structure:
/sample-excel-adapter/
adapter.yml
excel-adapter.py
tests/
testcases.yml
sample.xls
datapackage.json
sample.csv
adapter.yml
adapters:
- urlmatch:
- "\.xls$"
- "\.xlsx$"
run: excel-adapter
kind: json
- urlmatch:
- "\.xls#[0-9]+$"
- "\.xlsx#[0-9]+$"
run: excel-adapter
kind: csv
excel-adapter.py (pseudo-code)
import sys
import filefetcher
from xls2csv import xls2csv
url = sys.args[1]
if '#' not in url:
print """{
"resources": [
{
"path": {0}
}
]
}""".format(url+"#0")
else:
csv = xls2csv(filefetcher.get(url.split('#')[0]), sheet_index=0)
print csv
tests/testcases.yml
testcases:
-
- sample.xls
- datapackage.json
-
- sample.xls#0
- sample.csv
Content type
Image
Digest
Size
153.6 MB
Last updated
over 7 years ago
docker pull openspending/os-fdp-adapters