Permalink
Browse files

Luke: Refactors out the submodule

Parameter Contexts are now loaded using a google docs spreadsheet.
  • Loading branch information...
1 parent 730ad68 commit 66a3a67a0e377212aa010815d547ad608858b7ff @lukecampbell lukecampbell committed Oct 30, 2012
View
@@ -11,6 +11,3 @@
[submodule "extern/pyon"]
path = extern/pyon
url = https://github.com/ooici/pyon.git
-[submodule "extern/parameter-definitions"]
- path = extern/parameter-definitions
- url = https://github.com/ooici/parameter-definitions.git
View
@@ -110,7 +110,6 @@ find-links =
[base_config]
initialization =
- sys.path.append('${buildout:directory}/extern/parameter-definitions')
import os
os.environ['PATH'] = os.pathsep.join([os.path.join('${port_agent:path}', 'bin'), os.environ.get('PATH', '')])
# print os.environ.get('PATH')
Submodule parameter-definitions deleted from fb849f
No changes.
View
@@ -0,0 +1,106 @@
+#!/usr/bin/env python
+'''
+@author Luke Campbell <LCampbell@ASAScience.com>
+@file extern/parameter-definitions/parameter_definitions/external/load_csv.py
+@date Tue Oct 30 10:15:55 EDT 2012
+@brief Plugin for loading parameter definitions from a csv file
+'''
+
+from coverage_model.parameter import ParameterContext
+from coverage_model.parameter_types import QuantityType, ArrayType, RecordType
+from coverage_model.basic_types import AxisTypeEnum
+from ion.util.parameter_loader import ParameterPlugin
+import numpy as np
+import csv
+import requests
+
+FILEPATH='extern/parameter-definitions/parameter_definitions/external/example.csv'
+URL='https://docs.google.com/spreadsheet/pub?key=0AgGScp7mjYjydGZVV1JDYUlhWnBHV0JvVXBGWkhsS3c&output=csv'
+
+class NotSupportedError(Exception):
+ pass
+
+class Plugin(ParameterPlugin):
+ name = 'Spreadsheet Plugin'
+ url = URL
+ additional_attrs = {
+ 'Attributes':'attributes',
+ 'Index Key':'index_key',
+ 'Ion Name':'ion_name',
+ 'Standard Name':'standard_name',
+ 'Long Name':'long_name',
+ 'OOI Short Name':'ooi_short_name',
+ 'CDM Data Type':'cdm_data_type',
+ 'Variable Reports':'variable_reports',
+ 'References List':'references_list',
+ 'Comment' : 'comment',
+ 'Code Reports':'code_reports'
+ }
+
+ def __init__(self, config):
+ if 'url' in config and config['url']:
+ self.url = config['url']
+
+
+
+ def load(self):
+ contexts = []
+ for record in self.load_url(self.url):
+ context = self.build_context(record)
+ contexts.append(context)
+ return contexts
+
+
+ def load_url(self,url):
+ csv_doc = requests.get(url).content
+ csv_doc = csv_doc.splitlines()
+ reader = csv.DictReader(csv_doc)
+ return list(reader)
+
+ def load_file(self,path):
+ with open(path,'r+') as csv_doc:
+ reader = csv.DictReader(csv_doc)
+ return list(reader)
+
+ def build_context(self,record):
+ context = ParameterContext(name=record['Name'], param_type=self.param_type(record['Parameter Type']))
+ context.uom = record['Unit of Measure']
+ if record['Fill Value']:
+ context.fill_value = self.fill_value(record['Fill Value'], record['Parameter Type'])
+ if record['Axis']:
+ context.reference_frame = self.ref_frame(record['Axis'])
+ for key in self.additional_attrs.iterkeys():
+ if key in record and record[key]:
+ setattr(context,self.additional_attrs[key],record[key])
+ return context
+
+
+ def param_type(self,s):
+ if s == 'record':
+ return RecordType()
+ elif s == 'array':
+ return ArrayType()
+ else:
+ return QuantityType(value_encoding=np.dtype(s))
+
+ def ref_frame(self,s):
+ s = s.lower()
+ if 'lat' == s:
+ return AxisTypeEnum.LAT
+ elif 'lon' == s:
+ return AxisTypeEnum.LON
+ else:
+ raise NotSupportedError('Unsupported reference frame %s' % s)
+
+ def fill_value(self,v, dtype_str):
+ if 'float' in dtype_str:
+ return float(v)
+ elif 'int' in dtype_str:
+ return int(v)
+ else:
+ raise NotSupportedError('Unsupported fill value %s with %s')
+
+if __name__ == '__main__':
+ import sys
+ sys.stderr.write('This plugin is not meant to be run')
+
@@ -20,7 +20,7 @@ class BootstrapParameterDefinitions(BootstrapPlugin):
def on_initial_bootstrap(self, process, config, **kwargs):
self.dataset_management = DatasetManagementServiceProcessClient(process=process)
self.dict_defs = config.get_safe('process.bootstrap.dict_defs','res/config/param_dict_defs.yml')
- self.context_path = config.get_safe('process.bootstrap.definitions', 'parameter_definitions')
+ self.context_path = config.get_safe('process.bootstrap.definitions', 'ion.core.plugin')
self.loader_config = config.get_safe('process.bootstrap.config', {})
contexts = self.load_contexts()
@@ -28,7 +28,7 @@ def build_contexts(cls, definitions_path, loader_config={}):
'''
Builds a set of parameter definitions by loading the parameter definition plugins in definitions_path
'''
- contexts = __import__(definitions_path)
+ contexts = __import__(definitions_path,fromlist=[''])
package = contexts
contexts = {}

0 comments on commit 66a3a67

Please sign in to comment.