Skip to content

Commit

Permalink
add vocab application
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Sep 27, 2016
1 parent e202a48 commit d2a11e9
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 0 deletions.
20 changes: 20 additions & 0 deletions vocab/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Open Source Geospatial Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions vocab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OSGeo Vocabulary Resolver


1 change: 1 addition & 0 deletions vocab/VERSION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1-dev
84 changes: 84 additions & 0 deletions vocab/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: ISO-8859-15 -*-
###################################################################
#
# Authors: Tom Kralidis <[email protected]>
#
# Copyright (c) 2014 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
###################################################################

from flask import abort, Flask, redirect, Response

from model import Vocabulary

APP = Flask(__name__)

VOCAB = Vocabulary()


@APP.route('/')
@APP.route('/vocab/')
def index():
"""return json of entire vocabulary"""

return Response(VOCAB.dumps(), mimetype='application/json')


@APP.route('/vocab/<resource_type>/')
def resolve_resource_type(resource_type):
"""resolve a given vocabulary resource type"""

if resource_type not in ['service', 'data']:
abort(404)

url = 'http://osgeo.org/vocab/%s' % resource_type
members = VOCAB.filter(url)

return Response(VOCAB.dumps(data=members), mimetype='application/json')


@APP.route('/vocab/<resource_type>/<provider>/')
def resolve_resource_type_provider(resource_type, provider):
"""resolve a given vocabulary resource type and provider"""

url = 'http://osgeo.org/vocab/%s/%s' % (resource_type, provider)
members = VOCAB.filter(url)

return Response(VOCAB.dumps(data=members), mimetype='application/json')


@APP.route('/vocab/<resource_type>/<provider>/<identifier>/<version>')
def resolve_vocab_member(provider, resource_type, identifier, version):
"""resolve a given vocabulary member"""

url = 'http://osgeo.org/vocab/%s' % '/'.join([resource_type, provider,
identifier, version])

# check if the link_type exists:
for link in VOCAB.json:
if link['link_type'] == url:
return redirect(link['redirect'])

if __name__ == '__main__': # run locally, for fun
APP.run(host='0.0.0.0', port=8000, use_reloader=True, debug=True)
65 changes: 65 additions & 0 deletions vocab/app/build/LinkPropertyLookupTable.csv

Large diffs are not rendered by default.

Empty file.
65 changes: 65 additions & 0 deletions vocab/app/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: ISO-8859-15 -*-
###################################################################
#
# Authors: Tom Kralidis <[email protected]>
#
# Copyright (c) 2014 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
###################################################################

import csv
import json


class Vocabulary(object):
"""Vocabulary object"""
def __init__(self):
"""constructor"""

with open('build/LinkPropertyLookupTable.csv') as vocab:
# serialize to JSON
csvobj = csv.reader(vocab)
# skip CSV header
csvobj.next()
self.json = []
for row in csvobj:
self.json.append({'link_type': row[0],
'description': row[2],
'redirect': row[3]})

def filter(self, startswith):
"""rudimentary filtering mechanism"""

members = []
for link in self.json:
if link['link_type'].startswith(startswith):
members.append(link)
return members

def dumps(self, data=None):
"""return json as string"""

if data is not None:
return json.dumps(data)
return json.dumps(self.json)
Binary file added vocab/app/model.pyc
Binary file not shown.
105 changes: 105 additions & 0 deletions vocab/pavement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: ISO-8859-15 -*-
# =================================================================
#
# Authors: Tom Kralidis <[email protected]>
#
# Copyright (c) 2014 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================

import os
import shutil
from urllib2 import urlopen

from paver.easy import Bunch, cmdopts, options, path, task


options(
app=Bunch(
home=path('app'),
voc='https://raw.githubusercontent.com/OSGeo/Cat-Interop/%s/LinkPropertyLookupTable.csv',
build=path('app/build'),
version=open('VERSION.txt').read().strip()
)
)


@task
def setup():
"""setup install"""

if not os.path.exists(options.app.build):
options.app.build.mkdir()
if not os.path.exists(options.app.build / 'LinkPropertyLookupTable.csv'):
if options.app.version.endswith('-dev'): # master
url = options.app.voc % 'master'
else:
url = options.app.voc % options.app.version
with open(options.app.build / 'LinkPropertyLookupTable.csv', 'w') as fileobj:
fileobj.write(urlopen(url).read())


@task
def clean():
"""return to pristine state"""

if os.path.exists(options.app.build):
shutil.rmtree(options.app.build)


@task
@cmdopts([
('venv=', 'v', 'path to virtual environment'),
])
def deploy():
"""deploy install"""

venv = options.get('venv', None)
if venv is None:
raise ValueError('path to virtual environment required')
venv = os.path.abspath(venv)

loc = [
'import sys',
'sys.path.insert(0, "%s/app")' % venv,
'activate = "%s/bin/activate_this.py"' % venv,
'execfile(activate_this, {__file__: activate})',
'from app import APP as application'
]

with open(options.app.build / 'vocab.wsgi', 'w') as fileobj:
fileobj.write('\n'.join(loc))

loc = [
'WSGIDaemonProcess vocab user=user1 group=group1 threads=5',
'WSGIScriptAlias /vocab %s' % options.app.home,
'<Directory %s>' % path(venv),
' WSGIProcessGroup vocab',
' WSGIApplicationGroup %{GLOBAL}',
' Order deny,allow',
' Allow from all',
'</Directory>']

with open(options.app.build / 'osgeo.vocab.conf', 'w') as fileobj:
fileobj.write('\n'.join(loc))
2 changes: 2 additions & 0 deletions vocab/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask
Paver

0 comments on commit d2a11e9

Please sign in to comment.