Commits

Chris Mutel  committed dd974cf

Add validate functions to Method and Database

  • Participants
  • Parent commits 00eb45e

Comments (0)

Files changed (4)

File brightway2/database.py

 import numpy as np
 from query import Query
 from utils import natural_sort, MAX_INT_32
+from validate import db_validator
 try:
     import cPickle as pickle
 except ImportError:
     def copy(self, name):
         # Todo: register copied method
         raise NotImplemented
+
         def relabel_exchanges(obj, keys):
             for e in obj['exchanges']:
                 if e["input"] in data:
     def deregister(self):
         del databases[self.database]
 
+    def validate(self, data):
+        db_validator(data)
+        return True
+
     def write(self, data, name=None):
         if self.database not in databases:
             raise UnknownObject("This database is not yet registered")

File brightway2/method.py

 import os
 import random
 import string
+from validate import ia_validator
 try:
     import cPickle as pickle
 except ImportError:
     def deregister(self):
         del methods[self.method]
 
+    def validate(self, data):
+        ia_validator(data)
+        return True
+
     def write(self, data):
+        if self.method not in methods:
+            raise UnknownObject("This database is not yet registered")
         mapping.add(data.keys())
         filepath = os.path.join(config.dir, "intermediate",
             "%s.pickle" % self.get_abbreviation())

File brightway2/validate.py

 # encoding: utf-8
 try:
-    from voluptuous import Schema, required
+    from voluptuous import Schema, required, Invalid
 except:
     raise ImportError("The voluptuous package is required for validation")
 
-db_validator = Schema({tuple: {
+
+def valid_tuple(o):
+    try:
+        assert isinstance(o, tuple)
+        assert isinstance(o[0], basestring)
+        assert isinstance(o[1], (basestring, int, tuple, list))
+    except:
+        raise Invalid("The key %s is invalid" % o)
+    return o
+
+db_validator = Schema({valid_tuple: {
     required("code"): object,
     "categories": list or tuple,
     "location": object,
         required("input"): object,
         "pedigree matrix": basestring,
         "code": object,
-        required("sigma"): float,
+        "sigma": float,
         required("technosphere"): bool,
         required("uncertainty type"): int
         }]
     }},
     extra=True)
+
+ia_validator = Schema({valid_tuple: float})
   author="Chris Mutel",
   author_email="cmutel@gmail.com",
   license=open('LICENSE.txt').read(),
-  requires=["voluptuous", "nose"],
+  requires=["voluptuous", "nose", "progressbar"],
   url="https://bitbucket.org/cmutel/brightway2",
   long_description=open('README.txt').read(),
 )