Exemple #1
0
    def test_subscriptions_put(self):
        # create subscription, check it. Then modify it and recheck
        pvname = "test:long1"
        mode1 = {"mode": "Monitor", "delta": 1.1}
        mode2 = {"mode": "Scan", "period": 1.1}

        reqbody = json_encode({'mode': mode1})
        response = self.fetch('/subscriptions/'+pvname, method='PUT', body=reqbody)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        response = self.fetch('/subscriptions/'+pvname)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertEqual(pvname, res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(mode1))

        # modify it
        reqbody = json_encode({'mode': mode2})
        response = self.fetch('/subscriptions/'+pvname, method='PUT', body=reqbody)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        response = self.fetch('/subscriptions/'+pvname)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertEqual(pvname, res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(mode2))
Exemple #2
0
def APIResponse(request, code, result, template_name = None, redirect_to = None,
                mimetype=None, status=None, content_type=None, 
                format_param = "format", formatter_params = None):
    if isinstance(result, HttpResponse):
        response = result
    elif redirect_to:   # redirect provided so ignore all else and redirect
        response = HttpResponseRedirect(redirect_to)
    else:
        import request as djrequest
        format = djrequest.get_var(request, format_param, "")
        if code is not None: result = {'code': code, 'value': result}
        if format == "raw":
            return result
        elif format == "json" or not template_name:
            formatter_params = formatter_params or {}
            content_type = content_type or JSON_CONTENT_TYPE
            if type(formatter_params) is list:
                json_str = djjson.json_encode(result, *formatter_params)
            else:
                json_str = djjson.json_encode(result, **formatter_params)
            response = HttpResponse(json_str, content_type = content_type, mimetype = mimetype, status = status)
        else:
            from django.template import RequestContext
            context = RequestContext(request)
            if hasattr(template_name, "__call__"):
                template_name = template_name(request)
            response = render_to_response(template_name, result, context, mimetype = mimetype)
    if content_type: response["Content-Type"] = content_type
    response["Access-Control-Allow-Origin"] = "*"
    return response
    def format_response_method(request, *args, **kwargs):
        result  = func(request, *args, **kwargs)

        if result is not None:
            if isinstance(result, Http404):
                raise result

            import request as djrequest
            content_type = djrequest.get_getvar(request, "content_type", None)
            if isinstance(result, HttpResponse):
                response = result
            elif type(result) is tuple:
                import request as djrequest
                from django.conf import settings
                format = djrequest.get_getvar(request,
                                              settings.FORMAT_PARAM,
                                              djrequest.get_postvar(request, settings.FORMAT_PARAM, ""))
                if format == "json" or len(result) < 2:
                    response = HttpResponse(djjson.json_encode(result[0]), content_type = JSON_CONTENT_TYPE)
                elif isinstance(result[1], HttpResponse):
                    response = result[1]
                else:
                    from django.template import RequestContext
                    response_data = result[0]
                    context = RequestContext(request)
                    response = render_to_response(result[1], response_data, context)
            else:
                # else treat as JSON
                response = HttpResponse(djjson.json_encode(result), content_type = JSON_CONTENT_TYPE)
            response["Access-Control-Allow-Origin"] = "*"
            if content_type: response["Content-Type"] = content_type
            return response
def enviarXML(dir_path, archivo_xml) :
    global LibreDTE
    global verbose
    xml = loadXML(dir_path+'/'+archivo_xml)
    xml_base64 = json_encode(b64encode(bytes(xml, 'iso-8859-1')).decode('iso-8859-1'))
    cargar = LibreDTE.post('/dte/dte_emitidos/cargar_xml', xml_base64)
    if cargar.status_code!=200 :
        print('Error al cargar el XML '+archivo_xml+': '+json_encode(cargar.json()))
        return cargar.status_code
    elif verbose :
        print('Archivo XML '+archivo_xml+' fue cargado con éxito')
Exemple #5
0
def parse_data(csvdata,delimiter,inserts={},overwrite=False,objects=None,lists=None):
  inserts = parse_key_value_args(inserts)
  objects = { k : v.split(',') for k,v in parse_key_value_args(objects).iteritems() }
  lists = { k : v.split(',') for k,v in parse_key_value_args(lists).iteritems() }
  csvreader = csv.DictReader(csvdata,delimiter=delimiter,quotechar='"')
  for row in csvreader:
    if inserts:
      row = add_to_dict(row,inserts,overwrite)
    if objects:
      row = group(row,objects)
    if lists:
      row = group(row,lists,as_list=True)
    
    print json_encode(row)
Exemple #6
0
    def conv_to_json(obj, fields=None):
        """
        return cdx as json dictionary string
        if ``fields`` is ``None``, output will include all fields
        in order stored, otherwise only specified fields will be
        included

        :param fields: list of field names to output
        """
        if fields is None:
            return json_encode(OrderedDict(((x, obj[x]) for x in obj if not x.startswith('_')))) + '\n'

        result = json_encode(OrderedDict([(x, obj[x]) for x in fields if x in obj])) + '\n'

        return result
    def __init__(
        self,
        private_key,
        public_key,
        recaptcha_options=None,
        verification_timeout=None,
    ):

        """
        :param private_key: The reCAPTCHA API private key
        :type private_key: :class:`str`
        :param public_key: The reCAPTCHA API public key
        :type public_key: :class:`str`
        :param recaptcha_options: Options to customize the challenge
        :type recaptcha_options: :class:`dict` that can be serialized to JSON
        :param verification_timeout: Maximum number of seconds to wait for
            reCAPTCHA to respond to a verification request
        :type verification_timeout: :class:`int`

        When ``verification_timeout`` is ``None``, the default socket timeout
        will be used. See :meth:`is_solution_correct`.

        """
        super(RecaptchaClient, self).__init__()

        self.private_key = private_key
        self.public_key = public_key

        self.recaptcha_options_json = json_encode(recaptcha_options or {})

        self.verification_timeout = verification_timeout
def message_send(service):
    text = request.form.get('message')
    if not text:
        return Error.ARGUMENT_MISSING('message')

    subscribers = Subscription.query.filter_by(service=service).count()
    if subscribers == 0:
        # Pretend we did something even though we didn't
        # Nobody is listening so it doesn't really matter
        return Error.NONE

    level = (request.form.get('level') or '3')[0]
    level = int(level) if level in "12345" else 3
    title = request.form.get('title', '').strip()[:255]
    link = request.form.get('link', '').strip()
    msg = Message(service, text, title, level, link)
    db.session.add(msg)
    db.session.commit()

    if google_api_key or current_app.config['TESTING']:
        Gcm.send_message(msg)

    if zeromq_relay_uri:
        queue_zmq_message(json_encode({"message": msg.as_dict()}))

    service.cleanup()
    db.session.commit()
    return Error.NONE
Exemple #9
0
def add():
    """ Add message to outbox.
    """
    data = {
        'subject': request.params.get('subject', None),
        'from': request.params.get('from', None),
        'to': request.params.get('to', None),
        'text': request.params.get('text', None),
        'html': request.params.get('html', None),
    }

    headers = request.params.get('headers', None)
    if headers:
        try:
            headers = json_decode(headers)
            data['headers'] = headers
        except:
            rejected.next()
            return error_msg('headers contains invalid json data')

    required_fields = ['from', 'to', 'subject', 'text']
    for field in required_fields:
        if not (data.has_key(field) and data[field]):
            rejected.next()
            return error_msg('missing field %s'%field)

    outbox.put(data)
    queued.next()
    
    resp = {'status': 'queued', 'message': 'ok'}
    return json_encode(resp)
    def do_GET(self):

        # Check, if request is coming from same machine
        host, port = self.client_address
        if host != "127.0.0.1":
            self.send_error(403, "Forbidden")
            return None

        # Check, if there has been any persons
        elif not lastPerson:
            idCode = None
            lastSeen = 0
        else:
            # Last person ID code and time passed after card sweep
            idCode, timestamp = lastPerson
            lastSeen = time() - timestamp

        # Return the data to HTTP server
        data = bytes(json_encode({
            "idCode": idCode,
            "lastSeen": int(lastSeen)
        }, ensure_ascii=False, ), "utf-8")

        # Successful request, send HTTP headers
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        # Send data
        self.wfile.write(data)
Exemple #11
0
    def create_new_app(self, request):
        """
        Creates a new app.
        """
        username    = utils.get_reqvar(request, "username")
        appname     = utils.get_reqvar(request, "appname")

        userkey     = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)
        
        appkey      = get_app_key(username, appname)
        if appkey in self.tyrant:
            return errors.json_error_page(request, errors.APP_ALREADY_EXISTS)

        self.tyrant[appkey] = json_encode({'username': username,
                                           'appname': appname,
                                           'dev_pkeyfile': "",
                                           'dev_certfile': "",
                                           'prod_pkeyfile': "",
                                           'prod_certfile': "",
                                           'dev_pkeypasswd': "",
                                           'dev_certpasswd': "",
                                           'prod_pkeypasswd': "",
                                           'prod_certpasswd': "",
                                           'created': datetime.datetime.now()})

        # create the folder for the app so we have 
        # certificates and other things in there
        # <cert_folder>/<alphabet>/<appkey>/
        try:
            self.create_app_cert_folder(username, appname)
        except OSError, e:
            return errors.json_error_page(request, errors.OS_ERROR, str(e))
Exemple #12
0
	def get_json(self, request):
		# Honor the start_empty setting
		if self.start_empty and request.GET.get("_search", "false") == "false":
			return json_encode({
				"page": 1,
				"total": 1,
				"rows": [],
				"records": 0
			})
		# Otherwise return the items
		paginator, page, items = self.get_items(request)
		return json_encode({
			"page": page.number,
			"total": paginator.num_pages,
			"rows": items,
			"records": paginator.count
		})
Exemple #13
0
    def get_board(self, query, match_id):
        response = {}

        mm = ManualMatch(match_id)
        mm.initialize(tick_id=-1)

        response['board-state'] = json_encode(mm.match_info())
        return response
Exemple #14
0
    def __str__(self):
        if self.cdxline:
            return self.cdxline

        if not self._from_json:
            return ' '.join(val for n, val in self.iteritems())
        else:
            return json_encode(self)
Exemple #15
0
 def json_response(self, data, success=False, errors=[]):
     """ json response helper """
     self.write(json_encode({
         'data': data,
         'success': success,
         'errors': errors
     }))
     self.finish()
Exemple #16
0
    def __str__(self):
        if self.cdxline:
            return to_native_str(self.cdxline, 'utf-8')

        if not self._from_json:
            return ' '.join(str(val) for val in six.itervalues(self))
        else:
            return json_encode(self)
Exemple #17
0
    def __str__(self):
        if self.cdxline:
            return to_native_str(self.cdxline, 'utf-8')

        if not self._from_json:
            return ' '.join(str(val) for val in six.itervalues(self))
        else:
            return json_encode(self)
Exemple #18
0
    def _subscribe_to_many(self, pvnames, modedefs):
        to_sub = [{'pvname': pvname, 'mode': modedef} \
                for pvname, modedef in zip(pvnames, modedefs)]
        reqbody = json_encode(to_sub)
        response = self.fetch('/subscriptions/', method='POST', body=reqbody)

        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)
Exemple #19
0
    def __str__(self):
        if self.cdxline:
            return self.cdxline

        if not self._from_json:
            return ' '.join(val for n, val in self.iteritems())
        else:
            return json_encode(self)
def find_stacks(token, stack_names):
    method = 'GET'
    headers = {'Authorization': 'Bearer {}'.format(token)}
    filters = json_encode({'SwarmID': getenv('SWARM_ID')})
    path = '/stacks?{}'.format(urlencode({'filters': filters}))
    all_stacks = do_request(method=method, path=path, headers=headers)

    return filter(lambda s: s['Name'] in stack_names, all_stacks)
    def _call(self, cmd, data=None):
        if data is None:
            data = {}
        data['cmd'] = cmd
        data['version'] = API_VERSION
        request = json_encode(data)

        response = None
        for i in range(2):
            if not self.socket and cmd != 'login':
                self._call('login', self.userpwd.copy())
            self.socket_lock.acquire()
            try:
                sock = self.connect()
                response = self._sendrecv(sock, request)
            except IOError as err:
                sys.stderr.write(str(err) + "\n")
                self.close()
            except socket.error as err:
                sys.stderr.write(str(err) + "\n")
                self.close()
                raise IOError('Connection refused')
            else:
                break
            finally:
                self.socket_lock.release()

        if response is None:
            raise IOError('Connection lost timed out during API request')

        try:
            response = json_decode(response)
        except Exception:
            raise RuntimeError('Invalid API response')

        if not response.get('error'):
            return response

        error = response['error']
        if error in ('not-logged-in', 'invalid-credentials'):
            raise AccessDeniedException(
                'Access denied, check your credentials')
        elif 'banned' == error:
            raise AccessDeniedException('Access denied, account is suspended')
        elif 'insufficient-funds' == error:
            raise AccessDeniedException(
                'CAPTCHA was rejected due to low balance')
        elif 'invalid-captcha' == error:
            raise ValueError('CAPTCHA is not a valid image')
        elif 'service-overload' == error:
            raise OverflowError(
                'CAPTCHA was rejected due to service overload, try again later'
            )
        else:
            self.socket_lock.acquire()
            self.close()
            self.socket_lock.release()
            raise RuntimeError('API server error occured: %s' % error)
 def send(self, command, callback=None, awaitInput=False):
     """Sends a command to the Morphology Viewer.
 Parameters:
 command: Command in json-rpc2 format, with a method, params and id.
 callback: Name of the python function that processes the response, accessible in the caller's context. Note: the json module must be available on the system.
 """
     if not 'id' in command: command['id'] = uuid.uuid4().hex
     command = self.encodeRequest(command)
     if awaitInput:
         JS("""{}.then( (rpc) => {{ rpc.send({}).then( async (response) => {{ response = await rpc.packResponse(response); const kernel = IPython.notebook.kernel; kernel.send_input_reply(response) }} ); }} );"""
            .format(self.fetchInterface, json_encode(command)))
     else:
         if callback and hasattr(callback, '__name__'):
             callback = callback.__name__
         JS("""{}.then( (rpc) => {{ rpc.send({}).then( async (response) => {{ response = await rpc.packResponse(response); rpc.jupyterResponseHandler(response,{}) }} ); }} );"""
            .format(self.fetchInterface, json_encode(command),
                    json_encode(callback)))
     return command['id']
Exemple #23
0
 def _get_query_uri(self, query_hash: str, first: int, after: Optional[str], **variables: Any) -> str:
     return self.__class__.__URL_QUERY + '?' + urlencode(dict(
         query_hash=query_hash,
         variables=json_encode(dict(
             first=first,
             after=(after or ""),
             **variables
         ))
     ))
Exemple #24
0
    def conv_to_json(obj, fields=None):
        """
        return cdx as json dictionary string
        if ``fields`` is ``None``, output will include all fields
        in order stored, otherwise only specified fields will be
        included

        :param fields: list of field names to output
        """
        if fields is None:
            return json_encode(
                OrderedDict(((x, obj[x])
                             for x in obj if not x.startswith('_')))) + '\n'

        result = json_encode(
            OrderedDict([(x, obj[x]) for x in fields if x in obj])) + '\n'

        return result
Exemple #25
0
    def _call(self, cmd, data=None):
        if data is None:
            data = {}
        data['cmd'] = cmd
        data['version'] = API_VERSION
        request = json_encode(data)

        response = None
        for i in range(2):
            if not self.socket and cmd != 'login':
                self._call('login', self.userpwd.copy())
            self.socket_lock.acquire()
            try:
                sock = self.connect()
                response = self._sendrecv(sock, request)
            except IOError as err:
                sys.stderr.write(str(err) + "\n")
                self.close()
            except socket.error as err:
                sys.stderr.write(str(err) + "\n")
                self.close()
                raise IOError('Connection refused')
            else:
                break
            finally:
                self.socket_lock.release()

        if response is None:
            raise IOError('Connection lost timed out during API request')

        try:
            response = json_decode(response)
        except Exception:
            raise RuntimeError('Invalid API response')

        if not response.get('error'):
            return response

        error = response['error']
        if error in ('not-logged-in', 'invalid-credentials'):
            raise AccessDeniedException('Access denied, check your credentials')
        elif 'banned' == error:
            raise AccessDeniedException('Access denied, account is suspended')
        elif 'insufficient-funds' == error:
            raise AccessDeniedException(
                'CAPTCHA was rejected due to low balance')
        elif 'invalid-captcha' == error:
            raise ValueError('CAPTCHA is not a valid image')
        elif 'service-overload' == error:
            raise OverflowError(
                'CAPTCHA was rejected due to service overload, try again later')
        else:
            self.socket_lock.acquire()
            self.close()
            self.socket_lock.release()
            raise RuntimeError('API server error occured: %s' % error)
Exemple #26
0
	def get_config(self, as_json=True):
		config = self.get_default_config()
		config.update({
			"url": self.get_url(),
			"caption": self.get_caption(),
			"colModel": self.get_colmodels(),
		})
		if as_json:
			config = json_encode(config)
		return config
Exemple #27
0
def api_make_post( ):
	print(request.form.get("content"))
	data = request.values.to_dict( )
	JSON = {
		"status":"",
		"message":""
	}
	#Check that they have submitted the correct data
	try:
		title = data["title"]
		content = data["content"]
	except KeyError:
		return json_encode({"error":"Incorrect POST data submitted"})

	post = Post(title=data['title'], content=data["content"])
	db.session.add(post)
	db.session.commit( )
	
	return json_encode({"message":"Post created successfully"})
Exemple #28
0
def alexa_voices():
	params = {}
	voices = []

	# get AWS Polly voices
	query = polly.describe_voices(**params)
	voices.extend(query.get("Voices", []))

	response = make_response( json_encode(voices) )
	response.headers['Content-type'] = 'application/json'
	return response
Exemple #29
0
def moulinette_error_to_http_response(error):

    content = error.content()
    if isinstance(content, dict):
        return HTTPResponse(
            json_encode(content),
            error.http_code,
            headers={"Content-type": "application/json"},
        )
    else:
        return HTTPResponse(content, error.http_code)
Exemple #30
0
 def process_payload(cls, payload=None) -> bytes:
     """
     Processes the JWT payload. Adds "exp" and "ttp" keys to the payload
     :param payload: The payload to process.
     :return: The processed payload.
     """
     if payload is None:
         payload = {}
     payload["exp"] = str(datetime.utcnow() + cls.get_lifetime())
     payload["ttp"] = cls.get_type()
     return string_as_base64(json_encode(payload))
 def __new__(self, had_errors=False, content=None,
             error_code=0, error_desc=""):
     rDict = {}
     rDict["had_errors"] = had_errors
     rDict["content"] = content
     rDict["error_code"] = error_code
     rDict["error_desc"] = error_desc
     self.result = rDict
     self.json = json_encode(rDict, sort_keys=True, indent=4,
                             cls=ObjectJsonEncoder)
     return self.json
def get_auth_token():
    method = 'POST'
    path = '/auth'
    body = json_encode({
        'Username': getenv('API_USERNAME'),
        'Password': getenv('API_PASSWORD')
    })
    headers = {'Content-Type': 'application/json'}
    payload = do_request(method='POST', path=path, body=body, headers=headers)

    return payload['jwt']
Exemple #33
0
    def to_json(self, fields=None):
        """
        return cdx as json dictionary string
        if ``fields`` is ``None``, output will include all fields
        in order stored, otherwise only specified fields will be
        included

        :param fields: list of field names to output
        """
        if fields is None:
            return json_encode(self) + '\n'

        try:
            result = json_encode(OrderedDict((x, self[x]) for x in fields)) + '\n'
        except KeyError as ke:
            msg = 'Invalid field "{0}" found in fields= argument'
            msg = msg.format(ke.message)
            raise CDXException(msg)

        return result
Exemple #34
0
 def post(self, url, data, cookies=None):
     self._url = url
     self._data = None
     self._bytes_to_read = -1
     data = json_encode(data)
     self._buf = bytes(
         "POST {} HTTP/1.1\r\nHost: {}\r\n{}{}Accept: */*\r\nUser-Agent: m3-collector/1.0\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{{\"data\":{}}}\r\n\r\n"
         .format(url, self._host,
                 "Connection: keep-alive\r\n" if self._keepalive else '',
                 "Cookie: {}\r\n".format(cookies) if cookies else '',
                 len(data) + 9, data), 'ascii')
Exemple #35
0
    def render(self, request):
        """
        We handle GET or POST requests to this handler.  This will be of
        the form:
        /apps/<appname>/<device_token>/?args
        """
        parts = request.path.split("/")[2:]
        if len(parts) < 2:
            return errors.no_resource_error(request)

        appname     = parts[0]
        dev_token   = parts[1]
        username    = utils.get_reqvar(request, "username")
        content     = request.content.read()
        badge       = utils.get_reqvar(request, "badge")
        sound       = utils.get_reqvar(request, "sound")
        alert       = utils.get_reqvar(request, "alert")
        env         = utils.get_reqvar(request, "env")
        identifier  = utils.get_reqvar(request, "identifier")
        expiry      = utils.get_reqvar(request, "expiry")

        if content:
            payload = json_decode(content)
            if 'aps' not in payload:
                payload['aps'] = {}
        else:
            payload = {'aps': {}}

        if badge:
            payload['aps']['badge'] = int(badge)
        if sound:
            payload['aps']['sound'] = sound
        if alert:
            payload['aps']['alert'] = alert

        logging.debug("Payload: " + json_encode(payload))

        appkey = "%s$%s$%s" % (env, username, appname)
        self.apns_daemon.sendMessage(appname, dev_token, json_encode(payload), identifier, expiry)

        return json_response(request, 0, "OK")
Exemple #36
0
    def render(self, request):
        """
        We handle GET or POST requests to this handler.  This will be of
        the form:
        /apps/<appname>/<device_token>/?args
        """
        parts = request.path.split("/")[2:]
        if len(parts) < 2:
            return errors.no_resource_error(request)

        appname = parts[0]
        dev_token = parts[1]
        username = utils.get_reqvar(request, "username")
        content = request.content.read()
        badge = utils.get_reqvar(request, "badge")
        sound = utils.get_reqvar(request, "sound")
        alert = utils.get_reqvar(request, "alert")
        env = utils.get_reqvar(request, "env")
        identifier = utils.get_reqvar(request, "identifier")
        expiry = utils.get_reqvar(request, "expiry")

        if content:
            payload = json_decode(content)
            if "aps" not in payload:
                payload["aps"] = {}
        else:
            payload = {"aps": {}}

        if badge:
            payload["aps"]["badge"] = int(badge)
        if sound:
            payload["aps"]["sound"] = sound
        if alert:
            payload["aps"]["alert"] = alert

        logging.debug("Payload: " + json_encode(payload))

        appkey = "%s$%s$%s" % (env, username, appname)
        self.apns_daemon.sendMessage(appname, dev_token, json_encode(payload), identifier, expiry)

        return json_response(request, 0, "OK")
Exemple #37
0
 def generate_header(cls) -> bytes:
     """
     Generates the JWT header. This is a dictionary which contains two keys:
     "typ" (the token type, this is always "jwt") and "alg" (the hashing
     algorithm).
     :return: Generated header as base64-encoded bytes
     """
     return string_as_base64(
         json_encode({
             "typ": "jwt",
             "alg": cls.get_algorithm()
         }))
Exemple #38
0
    def to_json(self, fields=None):
        """
        return cdx as json dictionary string
        if ``fields`` is ``None``, output will include all fields
        in order stored, otherwise only specified fields will be
        included

        :param fields: list of field names to output
        """
        if fields is None:
            return json_encode(self) + '\n'

        try:
            result = json_encode(OrderedDict(
                (x, self[x]) for x in fields)) + '\n'
        except KeyError as ke:
            msg = 'Invalid field "{0}" found in fields= argument'
            msg = msg.format(ke.message)
            raise CDXException(msg)

        return result
Exemple #39
0
def jsify(what, json=False):
    if what is None: return "null"
    elif isinstance(what, int) or isinstance(what, long): return str(what)
    else:
        what = str(what)
        try: what = what.decode("utf-8")
        except: what = what.decode("latin-1")
        result = json_encode(what)
        if not json:
            quote = result[0]
            return result.replace("</", "<%s+%s/" % (quote, quote)).replace("<!", "<%s+%s!" % (quote, quote))
        else:
            return result
Exemple #40
0
def status():
    """ Return how much emails are queued, sent, errors and 
    how much messages are in outbox.
    """
    getvalue = lambda x: str(x.__reduce__()[1][0])
    resp = {
        'sent': getvalue(sent),
        'error': getvalue(error),
        'queued': getvalue(queued),
        'rejected': getvalue(rejected),
        'outbox': outbox.qsize(),
    }
    return json_encode(resp)
def subscription_post(client, service):
    exists = Subscription.query.filter_by(device=client).filter_by(service=service).first() is not None
    if exists:
        return Error.DUPLICATE_LISTEN

    subscription_new = Subscription(client, service)
    db.session.add(subscription_new)
    db.session.commit()

    if zeromq_relay_uri:
        queue_zmq_message(json_encode({'subscription': subscription_new.as_dict()}))

    return jsonify({'service': service.as_dict()})
Exemple #42
0
def hadoop_feature_line(id, properties, geometry):
    ''' Convert portions of a GeoJSON feature to a single line of text.
    
        Allows Hadoop to stream features from the mapper to the reducer.
        See also skeletron-hadoop-mapper.py and skeletron-hadoop-reducer.py.
    '''
    line = [
        json_encode(id), ' ',
        b64encode(pickle(sorted(list(properties.items())))), '\t',
        b64encode(geometry.wkb)
    ]

    return ''.join(line)
Exemple #43
0
    def new_tick(self, query, match_id):
        response = {}

        mm = ManualMatch(match_id)
        mm.initialize(tick_id=-1)
        mm.begin_simulation()
        mm.create_tick()
        # TODO: Move shared functionality somewhere else;
        # a simulation method should do specifically what is requested -
        # create a new tick but not start a loop (called in loop though)

        response['tick-log'] = json_encode(mm.last_tick_info())
        return response
 def append(self, prop_name, value):
     """
     append element to list
     :param prop_name: str
     :param value:
     :return: FixtureBuilder
     """
     if prop_name not in self._data:
         raise KeyError('attribute {} does not exist in {}'.format(
             prop_name, json_encode(self.data)))
     data = self._deepcopy(self._data)
     data[prop_name].append(self._wrap(value))
     return FixtureBuilder(data, self.parent, self.location)
Exemple #45
0
    def process(self, _route, arguments={}):
        """Process the relevant action for the route

        Call the actions map in order to process the relevant action for
        the route with the given arguments and process the returned
        value.

        Keyword arguments:
            - _route -- The action route as a 2-tuple (method, path)
            - arguments -- A dict of arguments for the route

        """

        try:
            ret = self.actionsmap.process(arguments, timeout=30, route=_route)
        except MoulinetteError as e:
            raise moulinette_error_to_http_response(e)
        except Exception as e:
            if isinstance(e, HTTPResponse):
                raise e
            import traceback

            tb = traceback.format_exc()
            logs = {"route": _route, "arguments": arguments, "traceback": tb}
            return HTTPResponse(json_encode(logs), 500)
        else:
            return format_for_response(ret)
        finally:

            # Clean upload directory
            # FIXME do that in a better way
            global UPLOAD_DIR
            if UPLOAD_DIR is not None:
                rmtree(UPLOAD_DIR, True)
                UPLOAD_DIR = None

            # Close opened WebSocket by putting StopIteration in the queue
            profile = request.params.get(
                "profile", self.actionsmap.default_authentication
            )
            authenticator = self.actionsmap.get_authenticator(profile)
            try:
                s_id = authenticator.get_session_cookie()["id"]
                queue = self.log_queues[s_id]
            except MoulinetteAuthenticationError:
                pass
            except KeyError:
                pass
            else:
                queue.put(StopIteration)
Exemple #46
0
def hadoop_feature_line(id, properties, geometry):
    ''' Convert portions of a GeoJSON feature to a single line of text.
    
        Allows Hadoop to stream features from the mapper to the reducer.
        See also skeletron-hadoop-mapper.py and skeletron-hadoop-reducer.py.
    '''
    line = [
        json_encode(id),
        ' ',
        b64encode(pickle(sorted(list(properties.items())))),
        '\t',
        b64encode(geometry.wkb)
        ]
    
    return ''.join(line)
def subscription_post(client, service):
    exists = Subscription.query.filter_by(device=client).filter_by(
        service=service).first() is not None
    if exists:
        return jsonify(Error.DUPLICATE_LISTEN)

    subscription_new = Subscription(client, service)
    db.session.add(subscription_new)
    db.session.commit()

    if zeromq_relay_uri:
        queue_zmq_message(
            json_encode({'subscription': subscription_new.as_dict()}))

    return jsonify({'service': service.as_dict()})
Exemple #48
0
def _get_upaiPolicy_Signature():

    upai_policy = {}
    upai_policy['bucket'] = 'o2bratest'
    upai_policy['expiration'] = int(time.time()) + 60 * 5
    # expired after 5 minutes
    upai_policy['save-key'] = '/{year}/{mon}/{day}/{random32}.jpg'
    #upai_policy['image-height-range'] = '0,1024'
    upai_policy = json_encode(upai_policy)
    upai_policy = base64.b64encode(upai_policy)
    signature = hashlib.md5(
        upai_policy + '&' +
        'yRjQ/MCwscHIKQO9icfrZXNycVY=').hexdigest().upper()

    return (upai_policy, signature)
Exemple #49
0
def lookup(click_ctx, ip, frmt):
    geoip = click_ctx.obj['conf'].load('geoip')
    if frmt == 'json':
        from json import dumps as json_encode
        print(json_encode(geoip[ip], indent=4).strip())
        return
    from xml.dom.minidom import getDOMImplementation
    impl = getDOMImplementation()
    doc = impl.createDocument(None, 'location', None)
    root = doc.documentElement
    for k, v in geoip[ip].items():
        el = doc.createElement(str(k))
        el.appendChild(doc.createTextNode(str(v)))
        root.appendChild(el)
    doc.appendChild(root)
    print(doc.toprettyxml(indent=' '*4, newl='\n').strip())
Exemple #50
0
def format_for_response(content):
    """Format the resulted content of a request for the HTTP response."""
    if request.method == 'POST':
        response.status = 201  # Created
    elif request.method == 'GET':
        response.status = 200  # Ok
    else:
        # Return empty string if no content
        if content is None or len(content) == 0:
            response.status = 204  # No Content
            return ''
        response.status = 200

    # Return JSON-style response
    response.content_type = 'application/json'
    return json_encode(content, cls=JSONExtendedEncoder)
Exemple #51
0
def api_encode(value):
    """
    Encodes a native Python value in a way that the API expects. Encodes lists
    and dicts to JSON and boolean values to 'true' or 'false'.

    Args:
        value - The Python value to encode.

    Returns:
        The value encoded for the Janrain API.
    """
    if type(value) in (dict, list):
        return json_encode(value)
    elif type(value) == bool:
        return str(value).lower()
    return value
Exemple #52
0
def format_for_response(content):
    """Format the resulted content of a request for the HTTP response."""
    if request.method == 'POST':
        response.status = 201  # Created
    elif request.method == 'GET':
        response.status = 200  # Ok
    else:
        # Return empty string if no content
        if content is None or len(content) == 0:
            response.status = 204  # No Content
            return ''
        response.status = 200

    # Return JSON-style response
    response.content_type = 'application/json'
    return json_encode(content)
Exemple #53
0
def jsify(what, json=False):
    if what is None: return "null"
    elif isinstance(what, int) or isinstance(what, long): return str(what)
    else:
        what = str(what)
        try:
            what = what.decode("utf-8")
        except:
            what = what.decode("latin-1")
        result = json_encode(what)
        if not json:
            quote = result[0]
            return result.replace("</", "<%s+%s/" % (quote, quote)).replace(
                "<!", "<%s+%s!" % (quote, quote))
        else:
            return result
Exemple #54
0
def query_nominatim(request, format, squery):
    """Nominatim query gateway."""

    format = format or request.GET.get('format', 'json')
    if format not in ['json']:
        return HttpResponseBadRequest("ERROR: Invalid format")

    squery = squery or request.GET.get('q', '')

    try:
        contents = nominatim.query(squery, with_polygons=False)
    except:
        contents = []

    if format == 'json':
        return HttpResponse(content=json_encode(contents),
                            mimetype='text/json')
Exemple #55
0
    def change_user_password(self, request):
        username = utils.get_reqvar(request, "username")
        newpassword = utils.get_reqvar(request, "newpassword")

        userkey = get_user_key(username)
        if userkey not in self.tyrant:
            return errors.json_error_page(request, errors.USER_DOES_NOT_EXIST)

        if not is_password_valid(newpassword):
            return errors.json_error_page(request, errors.PASSWORD_INVALID)

        userdata = json_decode(self.tyrant[userkey])
        userdata['pwdreset'] = False
        userdata['password'] = newpassword
        self.tyrant[userkey] = json_encode(userdata)

        return json_response(request, 0, "OK")
Exemple #56
0
 def __init__(
     self,
     response: Optional[Any] = None,
     status: Optional[Union[str, int]] = None,
     headers: Optional[Union[Headers, Mapping[str, str], Sequence[Tuple[str, str]]]] = None,
     mimetype: Optional[str] = 'application/json',
     content_type: Optional[str] = 'application/json',
     direct_passthrough: bool = False,
 ) -> None:
     super().__init__(
         response=json_encode(response),
         status=status,
         headers=headers,
         mimetype=mimetype,
         content_type=content_type,
         direct_passthrough=direct_passthrough
     )
def send_data(client, data):
    if not isinstance(data, basestring):
        data = json_encode(data)

    client.send(chr(129))
    length = len(data)

    if length <= 125:
        client.send(chr(length))
    elif length >= 126 and length <= 65535:
        client.send(chr(126))
        client.send(struct.pack(">H", length))
    else:
        client.send(chr(127))
        client.send(struct.pack(">Q", length))

    return client.send(data)
Exemple #58
0
def api_papersize(request):
    """API handler to get the compatible paper sizes for the provided layout
    and bounding box."""

    if request.method != 'POST':
        return HttpResponseBadRequest("ERROR: Bad request")

    f = forms.MapPaperSizeForm(request.POST)
    if not f.is_valid():
       return HttpResponseBadRequest("ERROR: Invalid arguments")

    renderer = ocitysmap.OCitySMap(www.settings.OCITYSMAP_CFG_PATH)
    osmid = f.cleaned_data.get('osmid')
    layout = f.cleaned_data.get('layout')
    stylesheet = renderer.get_stylesheet_by_name(
        f.cleaned_data.get('stylesheet'))

    # Determine geographic area
    if osmid is not None:
        try:
            bbox_wkt, area_wkt = renderer.get_geographic_info(osmid)
        except ValueError:
            LOG.exception("Error determining compatible paper sizes")
            raise
        bbox = ocitysmap.coords.BoundingBox.parse_wkt(bbox_wkt)
    else:
        lat_upper_left = f.cleaned_data.get("lat_upper_left")
        lon_upper_left = f.cleaned_data.get("lon_upper_left")
        lat_bottom_right = f.cleaned_data.get("lat_bottom_right")
        lon_bottom_right = f.cleaned_data.get("lon_bottom_right")

        # Check we have correct floats
        if (lat_upper_left == None or lon_upper_left == None
            or lat_bottom_right == None or lon_bottom_right == None):
           return HttpResponseBadRequest("ERROR: Invalid arguments")

        bbox = ocitysmap.coords.BoundingBox(
            lat_upper_left, lon_upper_left,
            lat_bottom_right, lon_bottom_right)

    renderer_cls = ocitysmap.renderers.get_renderer_class_by_name(layout)
    paper_sizes = sorted(renderer_cls.get_compatible_paper_sizes(bbox),
                         key = lambda p: p[1])

    return HttpResponse(content=json_encode(paper_sizes),
                        mimetype='text/json')
Exemple #59
0
def show_view(rmap, opts):
    if opts.label not in rmap['views']:
        raise ViewNotFoundError(label=opts.label)

    with open(rmap['views'][opts.label]) as y:
        spec = yaml_load(y)

    if opts.detail:
        query = spec['detail'].strip().strip(';')
    else:
        query = spec['list'].strip().strip(';')

    columns = dict(map(lambda x: (x['name'], x), spec['columns']))

    if opts.filter:
        sql, vals = parse_filter_expr(opts.filter)
        unbind = text("SELECT * FROM (%s) as ______ WHERE %s" % (query, sql))
        query = unbind.bindparams(**vals)

    if opts.json:
        with open_datasource(rmap, spec['source']) as db:
            norm = lambda x: dict(map(lambda x: (x[0], unicode(x[1])),
                                      x.items()))
            out(json_encode(map(lambda row: dict(norm(row)),
                                db.execute(query).fetchall()),
                            indent=4))
    else:
        with open_datasource(rmap, spec['source']) as db:
            result = db.execute(query)
            title = lambda x: columns[x].get('title', x)
            rows = result.fetchall()
            if opts.detail:
                for row in rows:
                    t = PrettyTable(['key', 'value'])
                    t.align['key'] = 'r'
                    t.align['value'] = 'l'
                    map(lambda x: t.add_row([title(x) if x in columns else x,
                                             row[x]]), result.keys())
                    out("%s\n" % t)
            else:
                headers = map(lambda x: title(x) if x in columns else x,
                              result.keys())
                t = PrettyTable(headers)
                map(lambda row: t.add_row(row), rows)
                out(t)