The Assimilation Project  based on Assimilation version 1.1.7.1474836767
hello.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # vim: smartindent tabstop=4 shiftwidth=4 expandtab number
3 #
4 # This file is part of the Assimilation Project.
5 #
6 # Author: Alan Robertson <alanr@unix.sh>
7 # Copyright (C) 2013 - Assimilation Systems Limited
8 #
9 # Free support is available from the Assimilation Project community - http://assimproj.org
10 # Paid support is available from Assimilation Systems Limited - http://assimilationsystems.com
11 #
12 # The Assimilation software is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # The Assimilation software is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with the Assimilation Project software. If not, see http://www.gnu.org/licenses/
24 #
25 #
26 '''
27 Prototype code for providing a REST interface for the Assimilation project.
28 '''
29 import sys
30 sys.path.append('..')
31 from flask import Flask, request, Response
32 from py2neo import neo4j
33 from store import Store
34 from graphnodes import GraphNode
35 from query import ClientQuery
36 import cmainit
37 from AssimCtypes import QUERYINSTALL_DIR
38 # These next two imports are actually needed because they register
39 # some types. But pylint doesn't know that.
40 # pylint: disable=W0611
41 from droneinfo import Drone
42 from hbring import HbRing
43 allqueries = {}
44 
45 app = Flask(__name__)
46 
47 @app.route('/')
49  'Dummy code for printing hello world on the root (/) page'
50  return 'Hello World! %s' % str(request.args)
51 
52 @app.route('/querymeta/<queryname>')
53 def query_meta(queryname):
54  '''Dummy code for returning the metadata for a particular query
55  - that doesn't do anything yet.'''
56  return 'Hello Query Metadata "%s"!' % queryname
57 
58 @app.route('/doquery/<queryname>')
59 def doquery(queryname):
60  '''Prototype code for executing a particular query.
61  The error cases are detected, but not handled correctly yet.
62  They all return apparent success, just no JSON.
63  '''
64  if queryname not in allqueries:
65  return 'No such query: %s' % queryname
66  query = allqueries[queryname]
67  try:
68  req = {}
69  argdict = dict(request.args)
70  for arg in argdict:
71  req[arg] = str(argdict[arg][0])
72  query.validate_parameters(req)
73  except ValueError, e:
74  return 'Invalid Parameters to %s [%s]' % (queryname, str(e))
75  return Response(query.execute(None, idsonly=False, expandJSON=True, maxJSON=1024, **req)
76  , mimetype='application/javascript')
77 
78 if __name__ == '__main__':
79  def setup(dbhost='localhost', dbport=7474, dburl=None, querypath=None):
80  '''
81  Program to set up for running our REST server.
82  We do these things:
83  - Attach to the database
84  - Initialize our type objects so things like ClientQuery will work...
85  - Load the queries into the database from flat files
86  Not sure if doing this here makes the best sense, but it
87  works, and currently we're the only one who cares about them
88  so it seems reasonable -- at the moment ;-)
89  Also we're most likely to iterate changing on those relating to the
90  REST server, so fixing them just by restarting the REST server seems
91  to make a lot of sense (at the moment)
92  - Remember the set of queries in the 'allqueries' hash table
93  '''
94  if dburl is None:
95  dburl = ('http://%s:%d/db/data/' % (dbhost, dbport))
96  print >> sys.stderr, 'CREATING Graph("%s")' % dburl
97  neodb = neo4j.Graph(dburl)
98  qstore = Store(neodb, None, None)
99  print GraphNode.classmap
100  for classname in GraphNode.classmap:
101  GraphNode.initclasstypeobj(qstore, classname)
102  print "LOADING TREE!"
103  if querypath is None:
104  querypath = "/home/alanr/monitor/src/queries"
105  queries = ClientQuery.load_tree(qstore, querypath)
106  for q in queries:
107  allqueries[q.queryname] = q
108  qstore.commit()
109  for q in allqueries:
110  allqueries[q].bind_store(qstore)
111  #queryquery = 'START q1=node:ClientQuery({queryname}) RETURN q1 LIMIT 1'
112  #print 'Neodb =', neodb
113  #print 'qstore =', qstore
114 
115  cmainit.CMAinit(io=None, readonly=True, use_network=False)
116  setup(querypath=QUERYINSTALL_DIR)
117  app.debug = True
118  app.run()
def setup(dbhost='localhost', dbport=7474, dburl=None, querypath=None)
Definition: hello.py:79
def query_meta(queryname)
Definition: hello.py:53
def hello_world()
Definition: hello.py:48
def doquery(queryname)
Definition: hello.py:59