Пример #1
0
def routeToCSV(lat1,lon1,lat2,lon2, transport):
  """Format a route (as list of nodes)"""
  data = LoadOsm(transport)

  node1 = data.findNode(lat1,lon1)
  node2 = data.findNode(lat2,lon2)

  router = Router(data)
  result, route = router.doRoute(node1, node2)
  if result != 'success':
    return("Fail")

  output = ''
  distance = 0
  for i in route:
    try:
      old_node = new_node
      new_node = data.rnodes[i]
      distance+=geopy.distance.vincenty((new_node[0], new_node[1]), (old_node[0], old_node[1])).km
      print(distance)
    except UnboundLocalError:
      new_node = data.rnodes[i]
    """output = output + "%d,%f,%f\n" % ( \
                  i,
                  node[0],
                  node[1])"""
  return(distance)
class RequestHandler(BaseHTTPRequestHandler):
    def __init__(self, request, client_address, server):
        routes = [{
            'regexp': r'^/$',
            'controller': HomeController,
            'action': 'indexAction'
        }, {
            'regexp': r'^/content/',
            'controller': ContentController,
            'action': 'showAction'
        }, {
            'regexp': r'^/api/',
            'controller': APIController,
            'action': 'indexAction'
        }]

        self.__router = Router(self)
        for route in routes:
            self.__router.addRoute(route['regexp'], route['controller'],
                                   route['action'])

        BaseHTTPRequestHandler.__init__(self, request, client_address, server)

    def do_GET(self):
        self.__router.route(self.path)
Пример #3
0
def routeToGpx(lat1,
               lon1,
               lat2,
               lon2,
               transport,
               description="",
               style="track"):
    """Format a route (as list of nodes) into a GPX file"""
    data = LoadOsm(transport)

    node1 = data.findNode(lat1, lon1)
    node2 = data.findNode(lat2, lon2)
    print("Nodes: {}, {}".format(node1, node2))

    router = Router(data)
    result, route = router.doRoute(node1, node2)
    if result != 'success':
        return

    output = ''
    output = output + "<?xml version='1.0'?>\n"

    output = (output + "<gpx version='1.1' creator='pyroute' "
              "xmlns='http://www.topografix.com/GPX/1/1' "
              "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
              "xsi:schemaLocation='http://www.topografix.com/GPX/1/1 "
              "http://www.topografix.com/GPX/1/1/gpx.xsd'>\n")

    if (style == 'track'):
        output = output + " <trk>\n"
        output = output + "  <name>%s</name>\n" % description
        output = output + "  <trkseg>\n"
        count = 0
        for i in route:
            node = data.rnodes[i]
            output = output + "   <trkpt lat='%f' lon='%f'>\n" % (node[0],
                                                                  node[1])
            output = output + "   </trkpt>\n"
            count = count + 1
        output = output + "  </trkseg>\n  </trk>\n</gpx>\n"

    elif (style == 'route'):
        output = output + " <rte>\n"
        output = output + "  <name>%s</name>\n" % description

        count = 0
        for i in route:
            node = data.rnodes[i]
            output = output + "   <rtept lat='%f' lon='%f'>\n" % (node[0],
                                                                  node[1])
            output = output + "    <name>%d</name>\n" % count
            output = output + "   </rtept>\n"
            count = count + 1
        output = output + " </rte>\n</gpx>\n"

    return (output)
Пример #4
0
def routeToGpx(lat1,lon1,lat2,lon2, transport, description="", style="track"):
  """Format a route (as list of nodes) into a GPX file"""
  data = LoadOsm(transport)

  node1 = data.findNode(lat1,lon1)
  node2 = data.findNode(lat2,lon2)

  router = Router(data)
  result, route = router.doRoute(node1, node2)
  if result != 'success':
    return

  output = ''
  output = output + "<?xml version='1.0'?>\n";
  
  output = output + "<gpx version='1.1' creator='pyroute' xmlns='http://www.topografix.com/GPX/1/1' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'>\n"
    
  if(style == 'track'):
    output = output + " <trk>\n"
    output = output + "  <name>%s</name>\n" % description
    output = output + "  <trkseg>\n"
    count = 0;
    for i in route:
      node = data.rnodes[i]
      output = output + "   <trkpt lat='%f' lon='%f'>\n" % ( \
        node[0],
        node[1])
      output = output + "   </trkpt>\n"
      count = count + 1
    output = output + "  </trkseg>\n  </trk>\n</gpx>\n"

  elif(style == 'route'):
    output = output + " <rte>\n"
    output = output + "  <name>%s</name>\n" % description
    
    count = 0;
    for i in route:
      node = data.rnodes[i]
      output = output + "   <rtept lat='%f' lon='%f'>\n" % ( \
        node[0],
        node[1])
      output = output + "    <name>%d</name>\n" % count
      output = output + "   </rtept>\n"
      count = count + 1
    output = output + " </rte>\n</gpx>\n"
  
  return(output)
Пример #5
0
Файл: mole.py Проект: ruter/Mole
    def __init__(self, catchall=True, autojson=True, config=None):
        """ Create a new mole instance.
            You usually don't do that. Use `mole.app.push()` instead.
        """
        self.routes = []  # List of installed routes including metadata.
        self.callbacks = {}  # Cache for wrapped callbacks.
        self.router = Router()  # Maps to self.routes indices.

        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and json_dumps:
            self.add_filter(dict, dict2json)
        self.hooks = {'before_request': [], 'after_request': []}
Пример #6
0
    def __init__(self, url, data):
        self.ws = websocket.WebSocketApp(url)
        # 存储所有cases
        self.cases = CasesStorage()

        self.route = Router()
        self.data = data
        self.url_map = self.route.url_map
        self.last_message = None
Пример #7
0
  def doRoute(self, from_lat, from_lon, to_lat, to_lon):
    """Route from one point to another, and set that as the active route"""
    data = LoadOsm(self.get('transport', 'cycle'))

    node1 = data.findNode(from_lat, from_lon)
    node2 = data.findNode(to_lat, to_lon)
    print "Routing from node %d to %d..." %(node1,node2)

    router = Router(data)
    result, route = router.doRoute(node1, node2)
    
    if result == 'success':
      self.route = []
      for node_id in route:
        node = data.rnodes[node_id]
        self.route.append((node[0], node[1]))
      print "Route discovered"
    else:
      print "Error in routing: " + result
    def __init__(self, *args, **kwargs):
        super(JsonProxyRestHandler, self).__init__(*args, **kwargs)

        self.router = Router()

        self.router.add(
            Route('/search/jobs/<sid>/control', {"POST": self.job_control},
                  'job_control'))
        self.router.add(
            Route('/search/jobs/<sid>/<data_source>', {"GET": self.job_data},
                  'job_data'))
        self.router.add(
            Route('/search/jobs/<sid>', {
                "GET": self.eai,
                "DELETE": self.delete_job
            }, 'job_info'))
        self.router.add(
            Route('/search/jobs', {
                "GET": self.eai,
                "POST": self.create_job
            }, 'jobs'))
        self.router.add(
            Route('/search/parser', self.parse_query, 'parse_query'))
        self.router.add(Route('/search/typeahead', self.typeahead,
                              'typeahead'))
        self.router.add(
            Route(
                '/search/tags/<name>', {
                    "GET": self.eai,
                    "DELETE": self.modify_or_delete_tag,
                    "POST": self.modify_or_delete_tag
                }, 'tag_info'))
        self.router.add(
            Route('/properties/<file>/<stanza>', self.properties_stanza,
                  'properties_stanza_info'))
        self.router.add(
            Route('/properties/<file>/<stanza>/<key>',
                  self.properties_stanza_key, 'properties_stanza_key'))
        self.router.add(
            Route('/receivers/simple', self.http_simple_input,
                  'http_simple_input'))
        self.router.add(Route('/auth/login', {"POST": self.auth}, 'auth'))
        self.router.add(Route('/<:.*>', self.eai, 'eai'))
Пример #9
0
def routeToCSV(lat1,lon1,lat2,lon2, transport):
  """Format a route (as list of nodes)"""
  data = LoadOsm(transport)

  node1 = data.findNode(lat1,lon1)
  node2 = data.findNode(lat2,lon2)

  router = Router(data)
  result, route = router.doRoute(node1, node2)
  if result != 'success':
    return("Fail")

  output = ''
  for i in route:
    node = data.rnodes[i]
    output = output + "%d,%f,%f\n" % ( \
      i,
      node[0],
      node[1])
  return(output)
Пример #10
0
def routeToCSV(lat1, lon1, lat2, lon2, transport):
    """Format a route (as list of nodes)"""
    data = LoadOsm(transport)

    node1 = data.findNode(lat1, lon1)
    node2 = data.findNode(lat2, lon2)

    router = Router(data)
    result, route = router.doRoute(node1, node2)
    if result != 'success':
        return ("Fail")

    output = ''
    for i in route:
        node = data.rnodes[i]
        output = output + "%d,%f,%f\n" % ( \
          i,
          node[0],
          node[1])
    return (output)
Пример #11
0
    def __init__(self, catchall=True, autojson=True, config=None):
        """ Create a new mole instance.
            You usually don't do that. Use `mole.app.push()` instead.
        """
        self.routes = [] # List of installed routes including metadata.
        self.callbacks = {} # Cache for wrapped callbacks.
        self.router = Router() # Maps to self.routes indices.

        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and json_dumps:
            self.add_filter(dict, dict2json)
        self.hooks = {'before_request': [], 'after_request': []}
 def __init__(self, *args, **kwargs):
     super(JsonProxyRestHandler, self).__init__(*args, **kwargs)
     
     self.router = Router()
     
     self.router.add(Route('/search/jobs/<sid>/control', {"POST": self.job_control}, 'job_control'))
     self.router.add(Route('/search/jobs/<sid>/<data_source>', {"GET": self.job_data}, 'job_data'))
     self.router.add(Route('/search/jobs/<sid>', {"GET": self.eai, "DELETE": self.delete_job}, 'job_info'))
     self.router.add(Route('/search/jobs', {"GET": self.eai, "POST": self.create_job}, 'jobs'))
     self.router.add(Route('/search/parser', self.parse_query, 'parse_query'))
     self.router.add(Route('/search/typeahead', self.typeahead, 'typeahead'))
     self.router.add(Route('/search/tags/<name>', 
         {
             "GET": self.eai, 
             "DELETE": self.modify_or_delete_tag, 
             "POST": self.modify_or_delete_tag
         }, 
         'tag_info'
     ))
     self.router.add(Route('/properties/<file>/<stanza>', self.properties_stanza, 'properties_stanza_info'))
     self.router.add(Route('/properties/<file>/<stanza>/<key>', self.properties_stanza_key, 'properties_stanza_key'))
     self.router.add(Route('/receivers/simple', self.http_simple_input, 'http_simple_input'))
     self.router.add(Route('/auth/login', {"POST": self.auth}, 'auth'))
     self.router.add(Route('/<:.*>', self.eai, 'eai'))
Пример #13
0
class JsonProxyRestHandler(splunk.rest.BaseRestHandler):
    def __init__(self, *args, **kwargs):
        super(JsonProxyRestHandler, self).__init__(*args, **kwargs)

        self.router = Router()

        self.router.add(
            Route('/search/jobs/<sid>/control', {"POST": self.job_control},
                  'job_control'))
        self.router.add(
            Route('/search/jobs/<sid>/<data_source>', {"GET": self.job_data},
                  'job_data'))
        self.router.add(
            Route('/search/jobs/<sid>', {
                "GET": self.eai,
                "DELETE": self.delete_job
            }, 'job_info'))
        self.router.add(
            Route('/search/jobs', {
                "GET": self.eai,
                "POST": self.create_job
            }, 'jobs'))
        self.router.add(
            Route('/search/parser', self.parse_query, 'parse_query'))
        self.router.add(Route('/search/typeahead', self.typeahead,
                              'typeahead'))
        self.router.add(
            Route(
                '/search/tags/<name>', {
                    "GET": self.eai,
                    "DELETE": self.modify_or_delete_tag,
                    "POST": self.modify_or_delete_tag
                }, 'tag_info'))
        self.router.add(
            Route('/properties/<file>/<stanza>', self.properties_stanza,
                  'properties_stanza_info'))
        self.router.add(
            Route('/properties/<file>/<stanza>/<key>',
                  self.properties_stanza_key, 'properties_stanza_key'))
        self.router.add(
            Route('/receivers/simple', self.http_simple_input,
                  'http_simple_input'))
        self.router.add(Route('/auth/login', {"POST": self.auth}, 'auth'))
        self.router.add(Route('/<:.*>', self.eai, 'eai'))

    # UNDONE
    # This allows us to use basic auth, but it's not the ideal way to do this.
    # The problem is that we want to be able to reuse the code in splunk.rest.simpleRequest,
    # but that code does not allow us to set headers. As such, we have to create this wrapper
    # class.
    def wrap_http(self):
        is_basicauth = self.is_basicauth()
        basicauth = self.get_authorization()

        class Http(httplib2.Http):
            def request(self, *args, **kwargs):
                if is_basicauth and kwargs.has_key("headers"):
                    kwargs["headers"]["Authorization"] = basicauth

                return super(Http, self).request(*args, **kwargs)

        return Http

    def extract_path(self):
        self.scrubbed_path = self.request['path'].replace(
            "/services/json/v1", "")
        if re.match(r"^/servicesNS/[^/]*/[^/]*", self.scrubbed_path):
            self.scrubbed_path = re.sub(r"^(/servicesNS/[^/]*/[^/]*)(/.*)",
                                        r"\2", self.scrubbed_path)
        elif re.match(r"^/services/.*", self.scrubbed_path):
            self.scrubbed_path = re.sub(r"^(/services)(/.*)", r"\2",
                                        self.scrubbed_path)

        if self.scrubbed_path.endswith("/"):
            self.scrubbed_path = self.scrubbed_path[:-1]

        if self.scrubbed_path.endswith("?"):
            self.scrubbed_path = self.scrubbed_path[:-1]

    def extract_sessionKey(self):
        self.sessionKey = self.request["headers"].get(
            "authorization", "").replace("Splunk", "").strip() or None

    def extract_origin(self):
        if self.request["headers"].has_key(REMOTEORIGIN_HEADER):
            parsed = urlparse(self.request["headers"][REMOTEORIGIN_HEADER])
            self.remote_origin = parsed.netloc.replace(":" + str(parsed.port),
                                                       "")
        else:
            self.remote_origin = self.request["remoteAddr"]

    def extract_allowed_domains(self):
        self.allowed_domains = None

        self.settings = splunk.clilib.cli_common.getConfStanza(
            CONF_FILE, SETTINGS_STANZA)
        self.allowed_domains = map(
            lambda s: s.strip(),
            self.settings.get(ALLOWED_DOMAINS_KEY).split(","))

    def is_basicauth(self):
        return self.request["headers"].get("authorization",
                                           "").startswith("Basic ")

    def get_authorization(self):
        return self.request["headers"].get("authorization", "")

    def get_origin_error(self):
        output = ODataEntity()
        output.messages.append({
            "type":
            "HTTP",
            "text":
            "Origin '%s' is not allowed. Please check json.conf" %
            self.remote_origin
        })

        return 403, self.render_odata(output)

    def handle(self):
        output = ODataEntity()
        status = 500

        try:
            self.extract_path()
            self.extract_origin()
            self.extract_sessionKey()
            self.extract_allowed_domains()

            # Get the appropriate handler
            handler, args, kwargs = self.router.match(self.scrubbed_path)

            # Check to see if we are in the list of allowed domains
            if not self.remote_origin in self.allowed_domains:
                status, content = self.get_origin_error()
            else:
                if isinstance(handler, dict):
                    if handler.has_key(self.method):
                        handler = handler[self.method]
                    else:
                        self.set_response(404, "")
                        return

                status, content = handler(*args, **kwargs)
        except splunk.RESTException, e:
            responseCode = e.statusCode
            output.messages.append({
                'type': 'HTTP',
                'text': '%s %s' % (e.statusCode, e.msg)
            })
            if hasattr(e, 'extendedMessages') and e.extendedMessages:
                for message in e.extendedMessages:
                    output.messages.append(message)

            content = self.render_odata(output)
        except Exception, e:
            status = 500
            output.messages.append({'type': 'ERROR', 'text': '%s' % e})

            content = self.render_odata(output)
            raise
Пример #14
0
 def __init__(self, data):
     Router.__init__(self,data)
     Grower.__init__(self,data)
     self.route = {'valid':False}
     self.mode = 'direct';
Пример #15
0
router = Router([
    {
        'name': 'udpate_svn_and_import'
    },
    {
        'name': 'move_new_data'
    },
    {
        'name': 'make_reduced_data'
    },
    {
        'name': 'pack_txt'
    },
    {
        'name': 'sa_sweeps_tables'
    },
    {
        'name': 'sa_time_tables_cpu',
        'spin_flip_rate': 6.5e9
    },
    {
        'name': 'dwave_anneal_time_table'
    },
    {
        'name': 'scaling_ctq'
    },
    {
        'name': 'scaling_dtq'
    },
    {
        'name': 'scaling_dwave'
    },
    {
        'name': 'make_backup_to_archive'
    },
    {
        'name': 'make_backup_to_server'
    },
])
Пример #16
0
Файл: mole.py Проект: ruter/Mole
class Mole(object):
    """ WSGI application or Handler """
    def __init__(self, catchall=True, autojson=True, config=None):
        """ Create a new mole instance.
            You usually don't do that. Use `mole.app.push()` instead.
        """
        self.routes = []  # List of installed routes including metadata.
        self.callbacks = {}  # Cache for wrapped callbacks.
        self.router = Router()  # Maps to self.routes indices.

        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and json_dumps:
            self.add_filter(dict, dict2json)
        self.hooks = {'before_request': [], 'after_request': []}

    def optimize(self, *a, **ka):
        utils.depr("Mole.optimize() is obsolete.")

    def mount(self, app, script_path):
        ''' Mount a Mole application to a specific URL prefix '''
        if not isinstance(app, Mole):
            raise TypeError('Only Mole instances are supported for now.')
        script_path = '/'.join(filter(None, script_path.split('/')))
        path_depth = script_path.count('/') + 1
        if not script_path:
            raise TypeError('Empty script_path. Perhaps you want a merge()?')
        for other in self.mounts:
            if other.startswith(script_path):
                raise TypeError('Conflict with existing mount: %s' % other)

        @self.route('/%s/:#.*#' % script_path, method="ANY")
        def mountpoint():
            request.path_shift(path_depth)
            return app.handle(request.environ)

        self.mounts[script_path] = app

    def add_filter(self, ftype, func):
        ''' Register a new output filter. Whenever mole hits a handler output
            matching `ftype`, `func` is applied to it. '''
        if not isinstance(ftype, type):
            raise TypeError("Expected type object, got %s" % type(ftype))
        self.castfilter = [(t, f) for (t, f) in self.castfilter if t != ftype]
        self.castfilter.append((ftype, func))
        self.castfilter.sort()

    def match_url(self, path, method='GET'):
        return self.match({'PATH_INFO': path, 'REQUEST_METHOD': method})

    def match(self, environ):
        """ Return a (callback, url-args) tuple or raise HTTPError. """
        target, args = self.router.match(environ)
        try:
            return self.callbacks[target], args
        except KeyError:
            callback, decorators = self.routes[target]
            wrapped = callback
            for wrapper in decorators[::-1]:
                wrapped = wrapper(wrapped)
            #for plugin in self.plugins or []:
            #    wrapped = plugin.apply(wrapped, rule)
            functools.update_wrapper(wrapped, callback)
            self.callbacks[target] = wrapped
            return wrapped, args

    def get_url(self, routename, **kargs):
        """ Return a string that matches a named route """
        scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
        location = self.router.build(routename, **kargs).lstrip('/')
        return urljoin(urljoin('/', scriptname), location)

    def route(self,
              path=None,
              method='GET',
              no_hooks=False,
              decorate=None,
              template=None,
              template_opts={},
              callback=None,
              name=None,
              static=False):
        """ Decorator: Bind a callback function to a request path.

            :param path: The request path or a list of paths to listen to. See 
              :class:`Router` for syntax details. If no path is specified, it
              is automatically generated from the callback signature. See
              :func:`yieldroutes` for details.
            :param method: The HTTP method (POST, GET, ...) or a list of
              methods to listen to. (default: GET)
            :param decorate: A decorator or a list of decorators. These are
              applied to the callback in reverse order (on demand only).
            :param no_hooks: If true, application hooks are not triggered
              by this route. (default: False)
            :param template: The template to use for this callback.
              (default: no template)
            :param template_opts: A dict with additional template parameters.
            :param name: The name for this route. (default: None)
            :param callback: If set, the route decorator is directly applied
              to the callback and the callback is returned instead. This
              equals ``Mole.route(...)(callback)``.
        """
        # @route can be used without any parameters
        if callable(path): path, callback = None, path
        # Build up the list of decorators
        decorators = makelist(decorate)
        if template: decorators.insert(0, view(template, **template_opts))
        if not no_hooks: decorators.append(self._add_hook_wrapper)

        #decorators.append(partial(self.apply_plugins, skiplist))
        def wrapper(func):
            for rule in makelist(path) or yieldroutes(func):
                for verb in makelist(method):
                    if static:
                        rule = rule.replace(':', '\\:')
                        utils.depr("Use backslash to escape ':' in routes.")
                    #TODO: Prepare this for plugins
                    self.router.add(rule, verb, len(self.routes), name=name)
                    self.routes.append((func, decorators))
            return func

        return wrapper(callback) if callback else wrapper

    def _add_hook_wrapper(self, func):
        ''' Add hooks to a callable. See #84 '''
        @functools.wraps(func)
        def wrapper(*a, **ka):
            for hook in self.hooks['before_request']:
                hook()
            response.output = func(*a, **ka)
            for hook in self.hooks['after_request']:
                hook()
            return response.output

        return wrapper

    def get(self, path=None, method='GET', **kargs):
        """ Decorator: Bind a function to a GET request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def post(self, path=None, method='POST', **kargs):
        """ Decorator: Bind a function to a POST request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def put(self, path=None, method='PUT', **kargs):
        """ Decorator: Bind a function to a PUT request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def delete(self, path=None, method='DELETE', **kargs):
        """ Decorator: Bind a function to a DELETE request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def error(self, code=500):
        """ Decorator: Register an output handler for a HTTP error code"""
        def wrapper(handler):
            self.error_handler[int(code)] = handler
            return handler

        return wrapper

    def hook(self, name):
        """ Return a decorator that adds a callback to the specified hook. """
        def wrapper(func):
            self.add_hook(name, func)
            return func

        return wrapper

    def add_hook(self, name, func):
        ''' Add a callback from a hook. '''
        if name not in self.hooks:
            raise ValueError("Unknown hook name %s" % name)
        if name in ('after_request'):
            self.hooks[name].insert(0, func)
        else:
            self.hooks[name].append(func)

    def remove_hook(self, name, func):
        ''' Remove a callback from a hook. '''
        if name not in self.hooks:
            raise ValueError("Unknown hook name %s" % name)
        self.hooks[name].remove(func)

    def handle(self, environ):
        """ Execute the handler bound to the specified url and method and return
        its output. If catchall is true, exceptions are catched and returned as
        HTTPError(500) objects. """
        if not self.serve:
            return HTTPError(503, "Server stopped")
        try:
            handler, args = self.match(environ)
            return handler(**args)
        except HTTPResponse, e:
            return e
        except Exception, e:
            import traceback
            traceback.print_exc()
            if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\
            or not self.catchall:
                raise
            return HTTPError(500, 'Unhandled exception', e, format_exc(10))
Пример #17
0
from client import Client
from route import Router

cli = Client(__name__)
sk = Router()
storage = cli.storage

if __name__ == '__main__':
    cli.run()
Пример #18
0
class Mole(object):
    """ WSGI application or Handler """

    def __init__(self, catchall=True, autojson=True, config=None):
        """ Create a new mole instance.
            You usually don't do that. Use `mole.app.push()` instead.
        """
        self.routes = [] # List of installed routes including metadata.
        self.callbacks = {} # Cache for wrapped callbacks.
        self.router = Router() # Maps to self.routes indices.

        self.mounts = {}
        self.error_handler = {}
        self.catchall = catchall
        self.config = config or {}
        self.serve = True
        self.castfilter = []
        if autojson and json_dumps:
            self.add_filter(dict, dict2json)
        self.hooks = {'before_request': [], 'after_request': []}

    def optimize(self, *a, **ka):
        utils.depr("Mole.optimize() is obsolete.")

    def mount(self, app, script_path):
        ''' Mount a Mole application to a specific URL prefix '''
        if not isinstance(app, Mole):
            raise TypeError('Only Mole instances are supported for now.')
        script_path = '/'.join(filter(None, script_path.split('/')))
        path_depth = script_path.count('/') + 1
        if not script_path:
            raise TypeError('Empty script_path. Perhaps you want a merge()?')
        for other in self.mounts:
            if other.startswith(script_path):
                raise TypeError('Conflict with existing mount: %s' % other)
        @self.route('/%s/:#.*#' % script_path, method="ANY")
        def mountpoint():
            request.path_shift(path_depth)
            return app.handle(request.environ)
        self.mounts[script_path] = app

    def add_filter(self, ftype, func):
        ''' Register a new output filter. Whenever mole hits a handler output
            matching `ftype`, `func` is applied to it. '''
        if not isinstance(ftype, type):
            raise TypeError("Expected type object, got %s" % type(ftype))
        self.castfilter = [(t, f) for (t, f) in self.castfilter if t != ftype]
        self.castfilter.append((ftype, func))
        self.castfilter.sort()

    def match_url(self, path, method='GET'):
        return self.match({'PATH_INFO': path, 'REQUEST_METHOD': method})
        
    def match(self, environ):
        """ Return a (callback, url-args) tuple or raise HTTPError. """
        target, args = self.router.match(environ)
        try:
            return self.callbacks[target], args
        except KeyError:
            callback, decorators = self.routes[target]
            wrapped = callback
            for wrapper in decorators[::-1]:
                wrapped = wrapper(wrapped)
            #for plugin in self.plugins or []:
            #    wrapped = plugin.apply(wrapped, rule)
            functools.update_wrapper(wrapped, callback)
            self.callbacks[target] = wrapped
            return wrapped, args

    def get_url(self, routename, **kargs):
        """ Return a string that matches a named route """
        scriptname = request.environ.get('SCRIPT_NAME', '').strip('/') + '/'
        location = self.router.build(routename, **kargs).lstrip('/')
        return urljoin(urljoin('/', scriptname), location)

    def route(self, path=None, method='GET', no_hooks=False, decorate=None,
              template=None, template_opts={}, callback=None, name=None,
              static=False):
        """ Decorator: Bind a callback function to a request path.

            :param path: The request path or a list of paths to listen to. See 
              :class:`Router` for syntax details. If no path is specified, it
              is automatically generated from the callback signature. See
              :func:`yieldroutes` for details.
            :param method: The HTTP method (POST, GET, ...) or a list of
              methods to listen to. (default: GET)
            :param decorate: A decorator or a list of decorators. These are
              applied to the callback in reverse order (on demand only).
            :param no_hooks: If true, application hooks are not triggered
              by this route. (default: False)
            :param template: The template to use for this callback.
              (default: no template)
            :param template_opts: A dict with additional template parameters.
            :param name: The name for this route. (default: None)
            :param callback: If set, the route decorator is directly applied
              to the callback and the callback is returned instead. This
              equals ``Mole.route(...)(callback)``.
        """
        # @route can be used without any parameters
        if callable(path): path, callback = None, path
        # Build up the list of decorators
        decorators = makelist(decorate)
        if template:     decorators.insert(0, view(template, **template_opts))
        if not no_hooks: decorators.append(self._add_hook_wrapper)
        #decorators.append(partial(self.apply_plugins, skiplist))
        def wrapper(func):
            for rule in makelist(path) or yieldroutes(func):
                for verb in makelist(method):
                    if static:
                        rule = rule.replace(':','\\:')
                        utils.depr("Use backslash to escape ':' in routes.")
                    #TODO: Prepare this for plugins
                    self.router.add(rule, verb, len(self.routes), name=name)
                    self.routes.append((func, decorators))
            return func
        return wrapper(callback) if callback else wrapper

    def _add_hook_wrapper(self, func):
        ''' Add hooks to a callable. See #84 '''
        @functools.wraps(func)
        def wrapper(*a, **ka):
            for hook in self.hooks['before_request']: hook()
            response.output = func(*a, **ka)
            for hook in self.hooks['after_request']: hook()
            return response.output
        return wrapper

    def get(self, path=None, method='GET', **kargs):
        """ Decorator: Bind a function to a GET request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def post(self, path=None, method='POST', **kargs):
        """ Decorator: Bind a function to a POST request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def put(self, path=None, method='PUT', **kargs):
        """ Decorator: Bind a function to a PUT request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def delete(self, path=None, method='DELETE', **kargs):
        """ Decorator: Bind a function to a DELETE request path.
            See :meth:'route' for details. """
        return self.route(path, method, **kargs)

    def error(self, code=500):
        """ Decorator: Register an output handler for a HTTP error code"""
        def wrapper(handler):
            self.error_handler[int(code)] = handler
            return handler
        return wrapper

    def hook(self, name):
        """ Return a decorator that adds a callback to the specified hook. """
        def wrapper(func):
            self.add_hook(name, func)
            return func
        return wrapper

    def add_hook(self, name, func):
        ''' Add a callback from a hook. '''
        if name not in self.hooks:
            raise ValueError("Unknown hook name %s" % name)
        if name in ('after_request'):
            self.hooks[name].insert(0, func)
        else:
            self.hooks[name].append(func)

    def remove_hook(self, name, func):
        ''' Remove a callback from a hook. '''
        if name not in self.hooks:
            raise ValueError("Unknown hook name %s" % name)
        self.hooks[name].remove(func)

    def handle(self, environ):
        """ Execute the handler bound to the specified url and method and return
        its output. If catchall is true, exceptions are catched and returned as
        HTTPError(500) objects. """
        if not self.serve:
            return HTTPError(503, "Server stopped")
        try:
            handler, args = self.match(environ)
            return handler(**args)
        except HTTPResponse, e:
            return e
        except Exception, e:
            import traceback;traceback.print_exc()
            if isinstance(e, (KeyboardInterrupt, SystemExit, MemoryError))\
            or not self.catchall:
                raise
            return HTTPError(500, 'Unhandled exception', e, format_exc(10))
Пример #19
0
# these have to be loaded in later, because route depends on loadOsm
#	which depends on the modified crimeweights dictionary
from route import Router
from loadOsm import *

# load in the given OSM file
data = LoadOsm(sys.argv[1])

start_node = data.findNode(start_lat, start_lon, 'foot')
end_node = data.findNode(end_lat, end_lon, 'foot')

print 'Node IDs: ', start_node, 'to', end_node
print

# do the routing
router = Router(data)
result, route = router.doRouteAsLL(start_node, end_node, 'foot')

abs_dist = dist_miles(router.coords(start_node), router.coords(end_node))

print "Your destination is {0:.3f} miles away.".format(abs_dist)
dist_input = raw_input(
    'Please enter, in miles, the maximum distance you want to traverse (leave blank if unnecessary): '
)
max_dist = None
if dist_input == '':
    print 'Not accounting for distance.'
elif abs_dist > float(dist_input):
    print 'Input distance less than absolute distance between nodes. Ignoring distance input.'
else:
    max_dist = float(dist_input)
Пример #20
0
 def __init__(self, data):
     Router.__init__(self, data)
     Grower.__init__(self, data)
     self.route = {'valid': False}
     self.mode = 'direct'
Пример #21
0
# these have to be loaded in later, because route depends on loadOsm 
#	which depends on the modified crimeweights dictionary
from route import Router
from loadOsm import *

# load in the given OSM file
data = LoadOsm(sys.argv[1])

start_node = data.findNode(start_lat, start_lon, 'foot')
end_node = data.findNode(end_lat, end_lon, 'foot')

print 'Node IDs: ',start_node,'to',end_node
print

# do the routing
router = Router(data)
result, route = router.doRouteAsLL(start_node, end_node, 'foot')

abs_dist = dist_miles(router.coords(start_node),router.coords(end_node))

print "Your destination is {0:.3f} miles away.".format(abs_dist)
dist_input = raw_input('Please enter, in miles, the maximum distance you want to traverse (leave blank if unnecessary): ')
max_dist = None
if dist_input == '':
	print 'Not accounting for distance.'
elif abs_dist > float(dist_input):
	print 'Input distance less than absolute distance between nodes. Ignoring distance input.'
else:
	max_dist = float(dist_input)

# print out the results
Пример #22
0
def route_geojson(input_f, output_f, mode='foot', local_planet=None):
    osmdata = LoadOsm(mode)

    if local_planet is not None:
        osmdata.getArea = lambda lat, lon: None
        osmdata.api = None
        print('loading osm data (this may take a while)...')
        osmdata.loadOsm(local_planet)

    print('starting router...')
    router = Router(osmdata)

    print('processing shapes...')
    # First load up the shapes
    layer = geojson.load(input_f)
    non_linestring = 0
    not_two_points = 0
    unsuccessful = 0
    successful = 0
    very_long = 0
    first = True

    output_f.write('{"crs": {"type": "name", "properties": '
                   '{"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}, '
                   '"type": "FeatureCollection", "features": [\n')

    for feature in layer.features:
        if feature.geometry.type != 'LineString':
            # Not a LineString, skip!
            non_linestring += 1
            continue

        geometry = list(feature.geometry.coordinates)
        if len(geometry) != 2:
            # LineString with other than two points, skip!
            not_two_points += 1
            continue

        if pythagoras(*geometry[0] + geometry[1]) > 1.0:
            very_long += 1
            continue

        # Now find a route. Data has x,y coordinates, but function is y,x, so
        # reverse the parameters.
        start = osmdata.findNode(*geometry[0][::-1])
        end = osmdata.findNode(*geometry[1][::-1])

        result, route = router.doRoute(start, end)
        if result != 'success':
            unsuccessful += 1
            continue

        routed_geometry = []
        for node_id in route:
            node = osmdata.rnodes[node_id]
            routed_geometry.append((node[1], node[0]))

        new_feature = geojson.Feature(
            geometry=geojson.LineString(coordinates=routed_geometry),
            properties=feature.properties,
            id=feature.id,
        )

        if not first:
            output_f.write(',\n')
        first = False

        geojson.dump(new_feature, output_f)
        output_f.flush()
        successful += 1
    output_f.write('\n]}\n')
    output_f.close()

    print(
        '%d LineStrings routed. Errors: %d non-linestring(s), '
        '%d linestring(s) with !=2 points, %d very long, '
        '%d unsuccessful routings' %
        (successful, non_linestring, not_two_points, very_long, unsuccessful))
class JsonProxyRestHandler(splunk.rest.BaseRestHandler):
    def __init__(self, *args, **kwargs):
        super(JsonProxyRestHandler, self).__init__(*args, **kwargs)
        
        self.router = Router()
        
        self.router.add(Route('/search/jobs/<sid>/control', {"POST": self.job_control}, 'job_control'))
        self.router.add(Route('/search/jobs/<sid>/<data_source>', {"GET": self.job_data}, 'job_data'))
        self.router.add(Route('/search/jobs/<sid>', {"GET": self.eai, "DELETE": self.delete_job}, 'job_info'))
        self.router.add(Route('/search/jobs', {"GET": self.eai, "POST": self.create_job}, 'jobs'))
        self.router.add(Route('/search/parser', self.parse_query, 'parse_query'))
        self.router.add(Route('/search/typeahead', self.typeahead, 'typeahead'))
        self.router.add(Route('/search/tags/<name>', 
            {
                "GET": self.eai, 
                "DELETE": self.modify_or_delete_tag, 
                "POST": self.modify_or_delete_tag
            }, 
            'tag_info'
        ))
        self.router.add(Route('/properties/<file>/<stanza>', self.properties_stanza, 'properties_stanza_info'))
        self.router.add(Route('/properties/<file>/<stanza>/<key>', self.properties_stanza_key, 'properties_stanza_key'))
        self.router.add(Route('/receivers/simple', self.http_simple_input, 'http_simple_input'))
        self.router.add(Route('/auth/login', {"POST": self.auth}, 'auth'))
        self.router.add(Route('/<:.*>', self.eai, 'eai'))
        
    # UNDONE
    # This allows us to use basic auth, but it's not the ideal way to do this.
    # The problem is that we want to be able to reuse the code in splunk.rest.simpleRequest,
    # but that code does not allow us to set headers. As such, we have to create this wrapper
    # class.
    def wrap_http(self):   
        is_basicauth = self.is_basicauth()
        basicauth = self.get_authorization()
        
        class Http(httplib2.Http):            
            def request(self, *args, **kwargs):
                if is_basicauth and kwargs.has_key("headers"):
                    kwargs["headers"]["Authorization"] = basicauth
                    
                return super(Http,self).request(*args, **kwargs)
                
        return Http
    
    def extract_path(self):
        self.scrubbed_path = self.request['path'].replace("/services/json/v1", "")
        if re.match(r"^/servicesNS/[^/]*/[^/]*", self.scrubbed_path):
            self.scrubbed_path = re.sub(r"^(/servicesNS/[^/]*/[^/]*)(/.*)", r"\2", self.scrubbed_path)
        elif re.match(r"^/services/.*", self.scrubbed_path):
            self.scrubbed_path = re.sub(r"^(/services)(/.*)", r"\2", self.scrubbed_path)
            
        if self.scrubbed_path.endswith("/"):
            self.scrubbed_path = self.scrubbed_path[:-1]
            
        if self.scrubbed_path.endswith("?"):
            self.scrubbed_path = self.scrubbed_path[:-1]
    
    def extract_sessionKey(self):        
        self.sessionKey = self.request["headers"].get("authorization", "").replace("Splunk", "").strip() or None
        
    def extract_origin(self):
        if self.request["headers"].has_key(REMOTEORIGIN_HEADER):
            parsed = urlparse(self.request["headers"][REMOTEORIGIN_HEADER])
            self.remote_origin = parsed.netloc.replace(":" + str(parsed.port), "")
        else:
            self.remote_origin = self.request["remoteAddr"]
            
    def extract_allowed_domains(self):
        self.allowed_domains = None
        
        self.settings = splunk.clilib.cli_common.getConfStanza(CONF_FILE, SETTINGS_STANZA)
        self.allowed_domains = map(lambda s: s.strip(), self.settings.get(ALLOWED_DOMAINS_KEY).split(","))
        
    def is_basicauth(self):
        return self.request["headers"].get("authorization", "").startswith("Basic ")
        
    def get_authorization(self):
        return self.request["headers"].get("authorization", "")
    
    def get_origin_error(self):
        output = ODataEntity()
        output.messages.append({
            "type": "HTTP",
            "text": "Origin '%s' is not allowed. Please check json.conf" % self.remote_origin
        })
        
        return 403, self.render_odata(output)
    
    def handle(self):
        output = ODataEntity()
        status = 500
        
        try:
            self.extract_path()
            self.extract_origin()
            self.extract_sessionKey()
            self.extract_allowed_domains()
            
            # Get the appropriate handler
            handler, args, kwargs = self.router.match(self.scrubbed_path)
                
            # Check to see if we are in the list of allowed domains
            if not self.remote_origin in self.allowed_domains: 
                status, content = self.get_origin_error()        
            else:
                if isinstance(handler, dict):
                    if handler.has_key(self.method):
                        handler = handler[self.method]
                    else:
                        self.set_response(404, "")
                        return
                
                status, content = handler(*args, **kwargs)
        except splunk.RESTException, e:
            responseCode = e.statusCode
            output.messages.append({
                'type': 'HTTP',
                'text': '%s %s' % (e.statusCode, e.msg)
            })
            if hasattr(e, 'extendedMessages') and e.extendedMessages:
                for message in e.extendedMessages:
                    output.messages.append(message)
                  
            content = self.render_odata(output)
        except Exception, e:
            status = 500
            output.messages.append({
                'type': 'ERROR',
                'text': '%s' % e
            })
          
            content = self.render_odata(output)
            raise