
import os
import json
import uuid
from avro import schema, io

import util
import pkgutil

NAMESPACE = "edu.berkeley.cs.local"
SCHEMA_NAMES = schema.Names(default_namespace=NAMESPACE)
SCHEMAS = [
    # new extension types
    "uuid", 
    "UnitofTime", "Duration", 
    "ReadingType", "ReadingValue",
    
    'InstrumentMetadata', 'LocationMetadata',
    
    # timeseries subobjects
    "Actuator", 
    "Properties", "Metadata", 
    "TimeSeries", "Collection",
    "Reporting"]
SCHEMA_OBJECTS = []

# load all the schemas when we check an object
for sf in SCHEMAS:
    # print "Loading", sf
    obj = json.loads(pkgutil.get_data(__name__, 'schema/' + sf.lower() + ".av"))
    s = schema.make_avsc_object(obj, SCHEMA_NAMES)
    SCHEMA_OBJECTS.append(obj)
        
def validate(schema, obj):
    """Validate an object against its schema.

    Right now, this just checks it against the Avro schema; however in
    the future we will want to impose additional constraints which
    aren't expressable in the schema.
    """
    if schema == 'uuid' and isinstance(obj, uuid.UUID):
        return True
    elif schema == 'Readings':
        return True

    s = SCHEMA_NAMES.get_name(schema, None)
    # swap the uuid for the byte-packed encoding we use with avro
    try:
        id = obj.get('uuid', None)
        if util.is_string(id):
            id = uuid.UUID(id)

        if id: obj['uuid'] = id.bytes
        rv = io.validate(s, obj)
        if id: obj['uuid'] = id
        return rv
    except:
        return False

def filter_fields(schema, obj):
    rv = {}
    schema = util.find(lambda x: x.get("name", "") == schema, SCHEMA_OBJECTS)
    if not schema: 
        return obj

    for field in [x['name'] for x in schema['fields']]:
        if field in obj:
            rv[field] = obj[field]
            
    return rv

# validate("Timeseries", None)

@@ -4,16 +4,27 @@
 port os%0A
+import sys%0A
 import j
@@ -604,54 +604,163 @@
  sf%0A
+%09%0A
     
-obj = json.loads(pkgutil.get_data(__name__
+dirs = %5Bos.path.dirname(sys.modules%5B__name__%5D.__file__), sys.prefix%5D%0A    obj = None%0A    for d in dirs:%0A        try:%0A            path = os.path.join(d
 , 's
@@ -768,12 +768,10 @@
 hema
-/' +
+',
  sf.
@@ -784,15 +784,178 @@
 ) + 
-%22.av%22))
+'.av')%0A            obj = json.load(open(path, 'r'))%0A        except:%0A            pass%0A    if obj == None:%0A        raise Exception('Cannot load schema: ' + sf.lower())%0A    
 %0A   

Check for schema files in multiple locations to support py2exe