Example #1
0
def _json_handler(obj):
    if isinstance(obj, datetime):
        return 'new Date("%s")' % obj.ctime()
    if isinstance(obj, Model):
        return dumps(dict(obj))
    if isinstance(obj, ObjectId):
        return json_dumps(str(obj))
    return json_dumps(obj)
Example #2
0
def json_request(method, url, **kwargs):
    """Takes a request in json parse it and return in json"""
    kwargs.setdefault("headers", {})
    if "body" in kwargs:
        kwargs["headers"]["Content-Type"] = "application/json"
        kwargs["body"] = json_dumps(kwargs["body"])
    parsed, conn = http_connection(url)
    conn.request(method, parsed.path, **kwargs)
    resp = conn.getresponse()
    body = resp.read()
    http_log((url, method), kwargs, resp, body)
    if body:
        try:
            body = json_loads(body)
        except ValueError:
            body = None
    if not body or resp.status < 200 or resp.status >= 300:
        raise ClientException(
            "Auth GET failed",
            http_scheme=parsed.scheme,
            http_host=conn.host,
            http_port=conn.port,
            http_path=parsed.path,
            http_status=resp.status,
            http_reason=resp.reason,
        )
    return resp, body
Example #3
0
def elem2json(elem, strip=True, indent=0, convert_types=False):
    """Convert an ElementTree or Element into a JSON string."""
    if hasattr(elem, 'getroot'):
        elem = elem.getroot()

    internal = elem2internal(elem, strip=strip, convert_types=convert_types)

    # Module 'simplejson' has no 'encoder' member
    # pylint: disable=E1101
    json_encoder.FLOAT_REPR = float_to_string
    # pylint: enable=E1101
    if indent > 0:
        output = json_dumps(internal, sort_keys=True, indent=indent)
    else:
        output = json_dumps(internal, sort_keys=True, separators=(',', ':'))

    return output
def api_get_disk_info(disk='/'):

    if not disk.startswith('/'):
        disk = '/' + disk

    ctx = api.get_disk_info(disk=disk)

    return Response(response=json_dumps(ctx), mimetype="application/json")
def api_get_all_disks_info():
    disks_data = {}
    for disk in DISKS:
        data = api.get_disk_info(disk)
        if data:
            disks_data[disk] = data

    return Response(response=json_dumps(disks_data), mimetype="application/json")
Example #6
0
def tweets_to_json(tweets, tweetfile, append=False, pretty=False):
    """
    Exports a collection of tweets (any iterable of tweet objects) to given
    file in JSON, line-separated format.
    To append to given filename, pass append=True
    To print in pretty (line- and entity-separated) format, pass pretty=True
    """
    if append:
        handle = open(tweetfile, "a")
    else:
        handle = open(tweetfile, "w")
    if pretty:
        for tweet in tweets:
            handle.write(json_dumps(tweet, indent=4, separators=(',', ': ')) + "\n")
    else:
        for tweet in tweets:
            handle.write(json_dumps(tweet) + "\n")
    handle.close()
Example #7
0
 def set_configuration(self, user, configuration):
     """
     Should be replaced by intelligent proxy object.
     """
     try:
         user.configuration = json_dumps(configuration)
         self.db.flush()
     except:
         pass
Example #8
0
def fake_auth_request_v2(*args, **kwargs):
    s_url = "http://127.0.0.1:8080/v1.0/AUTH_fakeuser"
    resp = {
        "access": {
            "token": {"id": "12" * 10},
            "serviceCatalog": [{"type": "object-store", "endpoints": [{"region": "test", "internalURL": s_url}]}],
        }
    }
    ret = Response(status=200, content=json_dumps(resp))
    return ret
Example #9
0
    def write_json(self, value, seconds):
	if not isinstance(value, (basestring, )):
	    value = json_dumps(value, indent=4)
	self.response.headers['Content-Type'] = 'application/x-javascript'
	self.response.headers['Expires'] =  (datetime.now() + timedelta(hours=1)).ctime()
	self.response.headers['Cache-Control'] = 'max-age=' + str(seconds)
	cb = parse_qs(self.request.query_string).get('callback', (None, ))[0]
	if cb:
	    value = '%s(%s)' % (cb, value)
	self.response.out.write(value)
def api_get_filtered_processes(process_filter=None):

    if not process_filter:
        process_filter = PROCESSES if PROCESSES else ''

    process_filter = process_filter.split(',')

    ctx = api.get_filtered_processes(process_filter)

    return Response(response=json_dumps(ctx), mimetype="application/json")
Example #11
0
 def do_request(cls, endpoint, data):
     try:
         r = requests.post(cls.YGGDRASIL_BASE + endpoint, data=json_dumps(data))
         if not r.ok:
             try:
                 error = r.json()['errorMessage']
             except:
                 error = "unknown error"
             raise SessionException("%d: %s" % (r.status_code, error))
         json = r.json()
         return json
     except Exception as e:
         print str(e)
Example #12
0
File: swift.py Project: audip/lunr
def get_auth(url, user, key, region, snet=False):
    """
    Get authentication/authorization credentials.

    The snet parameter is used for Rackspace's ServiceNet internal network
    implementation. In this function, it simply adds *snet-* to the beginning
    of the host name for the returned storage URL. With Rackspace Cloud Files,
    use of this network path causes no bandwidth charges but requires the
    client to be running on Rackspace's ServiceNet network.

    :param url: authentication/authorization URL
    :param user: user to authenticate as
    :param key: key or password for authorization
    :param region: service region [dfw, ord, syd, iad, etc]
    :param snet: use SERVICENET internal network (see above), default is False
    :returns: tuple of (storage URL, auth token)
    :raises ClientException: HTTP GET request to auth URL failed
    """
    swift_service = 'object-store'
    parsed, conn = http_connection(url)
    params = json_dumps({"auth": {"RAX-KSKEY:apiKeyCredentials":
                                  {"username": user, "apiKey": key}}})
    conn.request('POST', parsed.path, params,
                 {'Accept': 'application/json',
                  'Content-Type': 'application/json'})
    resp = conn.getresponse()
    data = json_loads(resp.read())
    if resp.status < 200 or resp.status >= 300:
        raise ClientException(
            'Auth POST failed', http_scheme=parsed.scheme,
            http_host=conn.host, http_port=conn.port,
            http_path=parsed.path, http_status=resp.status,
            http_reason=resp.reason)

    try:
        token = data['access']['token']['id']
        for service in data['access']['serviceCatalog']:
            if service['type'] == swift_service:
                for points in service['endpoints']:
                    if points['region'] == region:
                        if snet:
                            storage_url = points['internalURL']
                        else:
                            storage_url = points['publicURL']
                        return storage_url, token
                raise ClientException('Region %s not found' % region)
        raise ClientException('Service Type %s not found' % swift_service)
    except KeyError:
        raise ClientException(
            'Inconsistent Service Catalog back from auth: %s' % data)
Example #13
0
 def do_request(cls, endpoint, data):
     try:
         log.debug("sending %s" % (data,))
         r = requests.post(cls.YGGDRASIL_BASE + endpoint, data=json_dumps(data))
         if not r.ok:
             try:
                 error = r.json()['errorMessage']
             except:
                 error = "unknown error"
             raise SessionException("%d: %s" % (r.status_code, error))
         json = r.json()
         log.debug("received %s" % (json,))
         return json
     except requests.exceptions.RequestException, err:
         raise SessionException(err.message)
Example #14
0
def fake_auth_request_v2(*args, **kwargs):
    s_url = 'http://127.0.0.1:8080/v1.0/AUTH_fakeuser'
    resp = {'access': {'token': {'id': '12' * 10},
                       'serviceCatalog':
                       [
                           {'type': 'object-store',
                            'endpoints': [{'region': 'test',
                                          'internalURL': s_url,
                                           },
                                          ]
                            },
                       ]
                       }
            }
    ret = Response(status=200, content=json_dumps(resp))
    return ret
Example #15
0
def pack_info_create(pack_data, pack_index):
    pack = Pack.from_objects(pack_data, pack_index)
    info = {}
    for obj in pack.iterobjects():
        # Commit
        if obj.type_num == Commit.type_num:
            info[obj.id] = (obj.type_num, obj.parents, obj.tree)
        # Tree
        elif obj.type_num == Tree.type_num:
            shas = [(s, n, not stat.S_ISDIR(m)) for
                    n, m, s in obj.iteritems() if not S_ISGITLINK(m)]
            info[obj.id] = (obj.type_num, shas)
        # Blob
        elif obj.type_num == Blob.type_num:
            info[obj.id] = None
        # Tag
        elif obj.type_num == Tag.type_num:
            info[obj.id] = (obj.type_num, obj.object[1])
    return zlib.compress(json_dumps(info))
Example #16
0
    def swift_auth_v2(self):
        self.tenant, self.user = self.user.split(';')
        auth_dict = {}
        auth_dict['auth'] = {'passwordCredentials':
                             {
                                 'username': self.user,
                                 'password': self.password,
                             },
                             'tenantName': self.tenant}
        auth_json = json_dumps(auth_dict)
        headers = {'Content-Type': 'application/json'}
        auth_httpclient = HTTPClient.from_url(
            self.auth_url,
            connection_timeout=self.http_timeout,
            network_timeout=self.http_timeout,
            )
        path = urlparse(self.auth_url).path
        if not path.endswith('tokens'):
            path = posixpath.join(path, 'tokens')
        ret = auth_httpclient.request('POST', path,
                                      body=auth_json,
                                      headers=headers)

        if ret.status_code < 200 or ret.status_code >= 300:
            raise SwiftException('AUTH v2.0 request failed on ' +
                                 '%s with error code %s (%s)'
                                 % (str(auth_httpclient.get_base_url()) +
                                    path, ret.status_code,
                                    str(ret.items())))
        auth_ret_json = json_loads(ret.read())
        token = auth_ret_json['access']['token']['id']
        catalogs = auth_ret_json['access']['serviceCatalog']
        object_store = [o_store for o_store in catalogs if
                        o_store['type'] == 'object-store'][0]
        endpoints = object_store['endpoints']
        endpoint = [endp for endp in endpoints if
                    endp["region"] == self.region_name][0]
        return endpoint[self.endpoint_type], token
def json_request(method, url, **kwargs):
    """Takes a request in json parse it and return in json"""
    kwargs.setdefault('headers', {})
    if 'body' in kwargs:
        kwargs['headers']['Content-Type'] = 'application/json'
        kwargs['body'] = json_dumps(kwargs['body'])
    parsed, conn = http_connection(url)
    conn.request(method, parsed.path, **kwargs)
    resp = conn.getresponse()
    body = resp.read()
    if body:
        try:
            body = json_loads(body)
        except ValueError:
            body = None
    if not body or resp.status < 200 or resp.status >= 300:
        raise ClientException('Auth GET failed', http_scheme=parsed.scheme,
                              http_host=conn.host,
                              http_port=conn.port,
                              http_path=parsed.path,
                              http_status=resp.status,
                              http_reason=resp.reason)
    return resp, body
Example #18
0
 def json_to_string(self, sort=True, indent=1):
     """Convert the asset to JSON and return it as a string."""
     json_encoder.FLOAT_REPR = float_to_string
     return json_dumps(self.asset, sort_keys=sort, indent=indent)
Example #19
0
def JsonHttpResponse(data, **kw):
    return HttpResponse(json_dumps(data, encoding='utf-8', **kw))
Example #20
0
 def test_get_container_objects(self):
     with patch('geventhttpclient.HTTPClient.request',
                lambda *args: Response(content=json_dumps(
                    (({'name': 'a'}, {'name': 'b'}))))):
         self.assertEqual(len(self.conn.get_container_objects()), 2)
def api_get_uptime():

    ctx = api.get_uptime()

    return Response(response=json_dumps(ctx), mimetype="application/json")
def api_get_network_info():

    ctx = api.get_network_info()

    return Response(response=json_dumps(ctx), mimetype="application/json")
Example #23
0
import csv
from json import JSONEncoder;
try:
   from simplejson import dumps as json_dumps;
except Exception,e:
   from json import dumps as json_dumps;

from sys import argv;

if(len(argv)<3):
   raise Exception("Need two arguments: input filename and output filename");

data = open(argv[1])
reader = csv.DictReader(data, delimiter="\t", quotechar='"')
list_out=[]
f = open(argv[2],"w");
for r in reader:
   row_obj={};
   for k,v in r.items():
      
      if 'q' in k:
         v = float(v)
      if 'p' in k:
         v = float(v)
      row_obj[k]=v;
   list_out.append(row_obj);
data.close();

f.write(json_dumps(list_out,indent=4))
f.close();
Example #24
0
    def __init__(self, data = None, status = 200):
        if not data:
            data = {}

        content = json_dumps(data, cls=JsonEncoder, ensure_ascii=False, indent=4)
        HttpResponse.__init__(self, content, mimetype = "application/json", status = status)
Example #25
0
 def json_to_string(self, sort=True, indent=1):
     """Convert the asset to JSON and return it as a string."""
     json_encoder.FLOAT_REPR = float_to_string
     return json_dumps(self.asset, sort_keys=sort, indent=indent)
Example #26
0
def dumps(*args, **kwargs):
    kwargs['default'] = _json_handler
    return json_dumps(*args, **kwargs)
Example #27
0
def JsonHttpResponse(data, **kw):
	return HttpResponse(json_dumps(data, encoding='utf-8', **kw))
Example #28
0
		## this assumes that the value has already been
		## parsed and cooked at least one time.
		data = self.load(zdecompress(storage.payload.encode('ISO-8859-1')))
		self.cache_set(data)
	    else:
		## 1b. total failure
		data = self.fail_value
	else:
	    ## 2a.  cook the new value before storing and sending
	    data = self.cook(data)
	    ## 2b.  store in cache and history
	    storage = History.all().filter('url =', url).get()
	    if storage is None:
		storage = History(url=url)
	    # reparse because it may have changed
	    storage.payload = unicode(zcompress(json_dumps(data, indent=4)), 'ISO-8859-1')
	    storage.put()
	    self.cache_set(data, self.cache_key)
	return data

    @property
    def request_lang(self):
	lang = 'en'
	try:
	    values = parse_qs(self.request.query_string).get('lang', (lang, ))
	    lang = values[0].lower()
	except:
	    pass
	return lang

    def write_json(self, value, seconds):
Example #29
0
 def test_get_container_objects(self):
     with patch(
         "geventhttpclient.HTTPClient.request",
         lambda *args: Response(content=json_dumps((({"name": "a"}, {"name": "b"})))),
     ):
         self.assertEqual(len(self.conn.get_container_objects()), 2)
Example #30
0
        
        qs = qs[start: start+length]
        
        # Get data    
        result.aaData = dt_request.dump_queryset_data(qs, pks_as_ids = True)
    
    except Exception, e:
        
        result.update(
                      bSuccess = False,
                      sError = unicode(e).encode('utf-8'),
                      )
        if settings.DEBUG:
            result.update(sTraceback = traceback.format_exc())
    #print "La respuesta es: ", pformat(result)
    return HttpResponse(json_dumps(result))


def get_from(request):
    # TODO: Better HTML
    ''' Gets a form from request '''
    form_name = request.REQUEST.get('form', None)
    if not form_name:
        return HttpResponse("Falta el form")
    try:
        from_path, form_class = form_name.rsplit('.', 1)
        m = __import__(from_path, {}, {}, '*')
        Form = getattr(m, form_class)
        form = Form()
        
    except (ImportError, AttributeError), e:
Example #31
0
def join_server(session, server_hash):
    r = requests.post('https://sessionserver.mojang.com/session/minecraft/join', data=json_dumps({
        'accessToken': session.access_token,
        'selectedProfile': session.uuid_hex,
        'serverId': server_hash,
    }), headers = {
        'Content-Type': 'application/json', #; charset=utf-8',
        'User-Agent': None,
    })
    return r.status_code in (200, 204)
Example #32
0
 def invalidate(self):
     r = requests.post(self.YGGDRASIL_BASE + "/invalidate", data=json_dumps({
         'accessToken': self._access_token
     }))
     return r.status_code in (200, 204)