示例#1
0
def expand_object( parents, obj, index ):
    obj_parents = obj.get( '@parents', [] )
    obj_parents.extend( parents )
    parents = obj_parents
    
    context = '{0} [{1}]'.format( '.'.join( [o['name'] for o in parents[::-1]] ), index )

    obj_type = obj.get( '@type', False )
    if not obj_type:
        tool.INFO( 'object at {0} is missing @type', context )
        return ( None, None )

    if obj_type.rfind('/') >= 0:
        obj_type = obj_type[ obj_type.rfind('/')+1 : ]

    context = '[{0}: {1}]'.format( obj_type, context )

    template_param = { k:obj[ k ] for k in obj if k[ 0 ] != '@' and k != 'id' }
    template_param[ 'parents' ] = parents

    try:
        cwm_cls = getattr( cwm, obj_type )
        argspec = inspect.getargspec( cwm_cls.__init__ )
        required_args = argspec.args[ 1:( len(argspec.args) - len(argspec.defaults or []) ) ] 
        
        cwm_obj = cwm_cls( **template_param )
        # print cwm_obj.data
        return ( parents, cwm_obj )

    except TypeError as e:
        # print 'object at ' + str(context) + ' is missing property.\n  Provided: ' + str(template_param.keys()) + '\n  Needed: ' + str(required_args)
        tool.INFO( 'object {0} has wrong properties\n  Provided: {1}\n  Needed: {2}', context, sorted(template_param.keys()), sorted(required_args) )
        tool.VERBOSE( traceback.format_exc() )
        return ( None, None )

    except Exception as e:
        tool.INFO( 'object {0} failed: {1}', context, e )
        tool.VERBOSE( traceback.format_exc() )
        return ( None, None )
示例#2
0
def create_style( layer ):
    url = '{0}/rest/styles'.format( env.gsHost() )

    template = '''
    <style>
     <name>{0}</name>
     <filename>{1}.sld</filename>
    </style>    
    '''

    data = template.format( layer['name'], layer['styleName'] )
    r,_ = make_request( 'post', url, headers={ 'Content-type': 'text/xml' }, data=data )

    if not r:
        return False

    url = '{0}/rest/styles/{1}'.format( env.gsHost(), layer['styleName'] )

    fh = None
    try:
        fn = '{0}/{1}.SLD'.format( args.styleDir, layer['styleName'] )
        fh = open( fn, 'rb' )
        tool.VERBOSE( 'using SLD {0}', fn )
    except:
        tool.INFO( '{0} missing', fn )
        return False

    template = fh.read()
    fh.close()

    data = template.format( layer['name'], layer['title'] )
    r,_ = make_request( 'put', url, headers={ 'Content-type': 'application/vnd.ogc.sld+xml' }, data=data )

    if not r:
        return False

    url = '{0}/rest/layers/{1}'.format( env.gsHost(), layer['name'] )

    template = '''
    <layer>
     <defaultStyle>
      <name>{0}</name>
     </defaultStyle>
    </layer>
    '''

    data = template.format( layer['styleName'] )
    r,_ = make_request( 'put', url, headers={ 'Content-type': 'text/xml' }, data=data )

    return r
示例#3
0
    def get_headers( self, **extra ):
        if not self.headers:
            query = urllib.urlencode( env.oauthParams( self.scope ) )
            requestUrl = env.oauthBase() + 'oauth/token?' + query 
            tool.VERBOSE( 'GET ' + requestUrl )
            r = requests.get( requestUrl, auth=( self.user, self.password ) )
            if not r.ok:
                tool.VERBOSE( 'response: ' + str(r.status_code) )
                tool.VERBOSE( r.text )
                tool.INFO( 'Failed to get token for {0}', self.user )
                exit(1)

            self.headers = {
                'Authorization': 'Bearer ' + json.loads( r.text )[ 'access_token' ],
                'Content-Type': 'application/json'
            }

        if extra:
            h = self.headers.copy()
            h.update( extra )
            return h

        return self.headers
示例#4
0
def process_object( obj_parents, obj_param, status, index ):   
    parents, cwm_obj = expand_object( obj_parents, obj_param, index )
    if not cwm_obj:
        return ( 0, False )

    obj = OrderedDict()
    detail = OrderedDict()

    preDelete = obj_param.get( '@preDelete', [] )
    obj['mode'] = args.mode or obj_param.get( '@mode', args.defaultMode )
    mode = object_mode.get( obj['mode'], 0 )

    data = cwm_obj.data

    detail['type'] = cwm_obj.type_name
    detail['name'] = data['name']
    detail['parents'] = [ o['name'] for o in parents[::-1] ]

    obj['obj'] = "{0}: {1}".format( detail['type'], '.'.join( detail['parents'] + [detail['name']] ) )

    if not mode:
        tool.INFO( "mode {0} not valid for {1}", obj['mode'], obj['obj'] )
        return ( 0, False )

    if not obj_param.get( '@enabled', True ):
        tool.INFO( "{0} not enabled, skipping", obj['obj'] )
        return ( 0, False )
       
    status.append( obj )

    svc = service[ cwm_obj.service ]

    detail['exists'], detail['ver'], detail['id'] = svc.element_exists( cwm_obj.access )

    if mode == TEST:
        if detail['exists']:
            tool.VERBOSE( obj['obj'] + ' exists' )
        elif detail['exists'] == False:
            tool.VERBOSE( obj['obj'] + ' missing' )
        else:
            tool.VERBOSE( obj['obj'] + ' unknown' )

        obj['successful'] = detail['exists'] == True

    if preDelete and ( mode == DELETE or mode == RECREATE ):
        detail['preDeleted'] = {}
        for pd in preDelete:
            s, u = pd
            pd_exists, pd_ver, pd_id = service[s].element_exists( u )
            if pd_exists != False or args.force:
                detail['preDeleted'][u],_,_ = service[s].delete_element( u, pd_ver )

                # if detail['preDeleted'][pd]:
                #     tool.VERBOSE( obj['obj'] +' pre-deleted '+pd )

                status.append( OrderedDict([
                    ( 'mode',       'predelete' ),
                    ( 'obj',        u          ),
                    ( 'successful', detail['preDeleted'][u] )
                ]))

    if mode == DELETE:
        if args.force or detail['exists'] != False:
            detail['deleted'],_,_ = svc.delete_element( cwm_obj.access, detail['ver'] )

            # if detail['deleted']:
            #     tool.VERBOSE( obj['obj'] + ' deleted' )

            obj['successful'] = detail['deleted']               
        else:
            obj['successful'] = True               

    if mode == RECREATE:
        if args.force or detail['exists'] != False:
            detail['deleted'],_,_ = svc.delete_element( cwm_obj.access, detail['ver'] )

            # if detail['deleted']:
            #     tool.VERBOSE( obj['obj'] + ' deleted' )

        if detail['exists'] == False or detail['deleted'] or args.force:
            detail['created'], detail['newVer'], detail['id'] = svc.load_element( cwm_obj.create, data )
            
            # if detail['created']:
            #     tool.VERBOSE( obj['obj'] + ' created' )

            obj['successful'] = detail['created']
        else:
            obj['successful'] = False

    if mode == CREATE:
        if detail['exists'] == False:
            detail['created'], detail['newVer'], detail['id'] = svc.load_element( cwm_obj.create, data )
            
            # if detail['created']:
            #     tool.VERBOSE( obj['obj'] + ' created' )

            obj['successful'] = detail['created']
        else:
            obj['successful'] = detail['exists'] == True 

    if mode == UPDATE:
        if detail['exists']:
            detail['updated'], detail['newVer'], detail['id'] = svc.update_element( cwm_obj.access, detail['ver'], detail['id'], data )
            
            # if detail['updated']:
            #     tool.VERBOSE( obj['obj'] + ' updated' )

            obj['successful'] = detail['updated']
        elif detail['exists'] == False:
            detail['created'], detail['newVer'], detail['id'] = svc.load_element( cwm_obj.create, data )
            
            # if detail['created']:
            #     tool.VERBOSE( obj['obj'] + ' created' )

            obj['successful'] = detail['created']
        else:
            obj['successful'] = False

    tool.STATUS( '({1}) {0}', obj['obj'], 'OK' if obj['successful'] else 'FAIL' )
    tool.VERBOSE()

    if args.stop and not obj['successful']: 
        return ( 1, False )

    count = 0
    if obj_param.get( '@children' ):
        parent = {
            'name': detail[ 'name' ],
            'id': detail[ 'id' ]
        }
        c, ok = process_child_objects( [ parent ] + parents, obj_param['@children'], status )
        count += c

        if args.stop and not ok:
            return ( 1 + count, False )

    return ( 1 + count, True )
示例#5
0
        i += 1

    return ( count, True )

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

status = []
for name in tool.get_filenames():
    file_status = OrderedDict()
    file_status[ 'filename' ] = name
    file_status[ 'objects' ] = []
    status.append( file_status )

    objects = []
    try:
        tool.INFO( 'reading {0}... \\', name )
        with open( name, 'rb') as file:
            objects = json.loads( file.read() )

    except Exception as e:
        tool.INFO( 'failed' )
        tool.VERBOSE( traceback.format_exc() )
        tool.INFO()

        file_status[ 'error' ] = str(e)
        continue

    tool.VERBOSE()
    file_status[ 'count' ], ok = process_child_objects( [], objects, file_status[ 'objects' ] )
    tool.STATUS( '' )
        # ( "defaultStyleId",                f["name"] + '_STYLE'               ),
        ("associatedFeatureViewId", f["name"]),
        ("associatedManagedWMSServiceId", f["associatedManagedWMSServiceId"]),
        ("metadataUrl", f["metadataUrl"])
    ])


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


def json_out(data):
    return json.dumps(data, sort_keys=False, indent=2, separators=(',', ': '))


if not os.path.isdir(arg.base + '/' + arg.dir):
    os.makedirs(arg.base + '/' + arg.dir)

with open(arg.base + '/' + arg.manifest, 'w') as man:
    for fname in feature:
        ifn = arg.dir + '/' + fname + '.json'
        fn = arg.base + '/' + ifn
        with open(fn, 'w') as out:
            fc = createFeatureClass(feature[fname], featureAttr.get(fname, {}))
            fv = createFeatureView(feature[fname], featureAttr.get(fname, {}))
            # sl = createServiceLayer( feature[ fname ] )

            # out.write( json_out( [ fc, fv, sl ] ) )
            out.write(json_out([fc, fv]))
            man.write(ifn + '\n')
            tool.INFO('wrote {}', fn)
示例#7
0
service = {
    'cwms':
    Requester(env.cwmsUser(), env.cwmsPassword(), env.cwmsScope(),
              env.cwmsBase()),
    'sos':
    Requester(env.sosUser(), env.sosPassword(), env.sosScope(), env.sosBase())
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# out = sys.stdout
# if not args.filename == '-':
#     out = open( args.filename, 'w' )

if not os.path.isdir(args.base):
    tool.INFO('create {0}', args.base)
    os.makedirs(args.base)

svc = service['cwms']

ok0, ver0, data0 = svc.get_element('/dataSources')

if not ok0:
    tool.INFO('ds failed')
    exit(1)

dataSourceName = {}
for ds in data0['dataSources']:
    dataSourceName[ds['id']] = ds['name']

kw = {}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


def json_out(data):
    return json.dumps(data, sort_keys=False, indent=2, separators=(',', ': '))


if not os.path.isdir(arg.base + '/' + arg.dir):
    os.makedirs(arg.base + '/' + arg.dir)

for mapN in mapObj:
    mOv = overlay.get(mapN)
    if not mOv:
        continue

    fn = arg.base + '/' + arg.dir + '/' + mapN + '.json'
    with open(fn, 'w') as out:
        try:
            mLy = layer.get(mapN, {})
            mAt = attribute.get(mapN, {})
            ovs = [
                createOverlay(mOv[ovId], mLy.get(ovId, {}), mAt.get(ovId, {}))
                for ovId in mOv
            ]  # sorted( mOv, key = lambda k: mOv[k]['title'] ) ]
            out.write(json_out({'overlays': ovs}))
            tool.INFO('{} written', fn)
        except:
            tool.INFO('Map {} failed', mapN)
            print traceback.format_exc()
示例#9
0
tool.add_argument( '--style-dir', '-s',  
    help    = 'directory containing SLDs',
    dest    = 'styleDir',
    default = 'styles' )

tool.add_argument( '--test', '-t',  
    help    = 'test existance of layers',
    action  = 'store_true' )

args = tool.args()

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

env = tool.import_environment()
if not env:
    tool.INFO( "Could not load environment {0}", environment_name() )
    exit(1)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def make_request( method, url, **kwargs ):
    tool.VERBOSE( method.upper() + ' ' + url )
    # tool.VERBOSE( json.dumps( kwargs['headers'], sort_keys=True, indent=2, separators=(',', ': ') ) ) 
    if 'data' in kwargs:
        tool.VERBOSE( kwargs['data'] ) 

    if not 'auth' in kwargs:
        kwargs['auth'] = ( env.gsUser(), env.gsPassword() )

    r = getattr( requests, method.lower() )( url, **kwargs )
示例#10
0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


def createLayer(f):
    return OrderedDict([("workspace", f['workspace']),
                        ("datastore", f['dataStore']), ("name", f['name']),
                        ("title", f.get('title', f['name'])),
                        ("styleName", f.get('styleName', f['name']))])


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


def json_out(data):
    return json.dumps(data, sort_keys=False, indent=2, separators=(',', ': '))


if not os.path.isdir(arg.base + '/' + arg.dir):
    os.makedirs(arg.base + '/' + arg.dir)

with open(arg.base + '/' + arg.manifest, 'w') as mout:
    for fname in feature:
        ifn = arg.dir + '/' + fname + '.json'
        fn = arg.base + '/' + ifn
        with open(fn, 'w') as out:
            out.write(json_out([createLayer(feature[fname])]))
            tool.INFO('{} written', fn)
            mout.write(ifn + '\n')

tool.INFO('{} written', arg.manifest)
示例#11
0
    kw['params'] = {'pageRowCount': args.pageRowCount}

ok, ver, data = svc.get_element(args.path, **kw)

if ok:
    pc = args.path.split('/')
    if pc[-1] in data:
        for obj in data[pc[-1]]:
            if not args.links and 'links' in obj:
                del obj['links']

        objs = [
            d for d in data[pc[-1]]
            if re.search(args.filter, d.get('name', d.get('id')))
        ]
        out.write(
            json.dumps(objs, sort_keys=True, indent=2, separators=(',', ': ')))
        tool.INFO('{0}: {1}', args.path, len(objs))
    else:
        if not args.links and not data['@type'].endswith(
                'resources') and 'links' in data:
            del data['links']

        out.write(
            json.dumps(data, sort_keys=True, indent=2, separators=(',', ': ')))
        tool.INFO('{0}: {1}', args.path, 1)

    out.write('\n')
else:
    tool.INFO('{0} failed', args.path)
示例#12
0
service = {
    'cwms':
    Requester(env.cwmsUser(), env.cwmsPassword(), env.cwmsScope(),
              env.cwmsBase()),
    'sos':
    Requester(env.sosUser(), env.sosPassword(), env.sosScope(), env.sosBase())
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# out = sys.stdout
# if not args.filename == '-':
#     out = open( args.filename, 'w' )

if not os.path.isdir(args.base):
    tool.INFO('create {0}', args.base)
    os.makedirs(args.base)

svc = service['sos']

# ok0, ver0, data0 = svc.get_element( '/dataSources' )

# if not ok0:
#     tool.INFO( 'ds failed' )
#     exit(1)

# dataSourceName={}
# for ds in data0['dataSources']:
#     dataSourceName[ ds['id'] ] = ds['name']

qts = ["All", "FishWildlife", "Forest", "Land", "Mining", "OilGas", "Water"]