class RestfulAPI(object): def __init__(self, ip, port): self.__server = HttpClient() self.__url = "http://%s:%s"%(ip, port) self.JSON = JSONDecoder() def __createUrl(self, service): return self.__url + "/" + service def isCutterActive(self): data = self.__server.GET(self.__createUrl("recordings/cut.json")) json = self.JSON.decode(data) return json["active"] def getRecordings(self): data = self.__server.GET(self.__createUrl("recordings.json?marks=true")) json = self.JSON.decode(data) recs = [] for recdata in json["recordings"]: recs.append(VDRRecording(recdata["number"], recdata["name"], recdata["file_name"], recdata["relative_file_name"], recdata["duration"])) return recs def cutRecording(self, recordingnumber): service = self.__createUrl("recordings/cut/" + str(recordingnumber)) return self.__server.POST(service, ' ') def getMarks(self, recordingnumber): data = self.__server.GET(self.__createUrl("recordings/"+str(recordingnumber)+".json?marks=true")) json = self.JSON.decode(data) marks = [] for mark in json["recordings"][0]["marks"]: marks.append(VDRMark(mark)) return marks def saveMarks(self, recordingnumber, marks): url = self.__createUrl("recordings/marks/" + str(recordingnumber)) json = self.__serializeMarks(marks) self.__server.POST(url, json) def deleteMarks(self, recordingnumber): self.__server.DELETE(self.__createUrl("recordings/marks/" + str(recordingnumber))) def refreshRecording(self, recording): if type(recording) == VDRRecording: recs = self.getRecordings() for rec in recs: if rec.FileName == recording.FileName: recording.RecordingId = rec.RecordingId def __serializeMarks(self, marks): json = "{'marks':[" counter = 0 for mark in marks: if type(mark) is VDRMark: json += "'" + mark.toString() +"'" if len(marks)-1 != counter: json += ", " counter += 1 json += "]}" print json return json
def query(self, argument): decoder = JSONDecoder() argument = utility.escape(argument) api_url = u"http://www.imdbapi.com/?t=%(search_term)s&r=json&plot=short" % \ {"search_term": argument} site_search_url = u"http://akas.imdb.com/find?s=all&q=" + argument response = utility.read_url(api_url) if not response: return u"Couldn't connect to the API :( | Manual search: " + site_search_url try: data = decoder.decode(response['data']) except Exception: return u"Couldn't parse the API output :( | Manual search: " + site_search_url if data.get(u"Response") != u"True": return u"No results found! Maybe you should try searching manually: " + \ site_search_url return \ (u"%(title)s (%(year)s) - Rating: %(rating)s out of 10 - Genre: %(genre)s - " + \ u"http://akas.imdb.com/title/%(id)s/ | More results: %(site_search_url)s") % \ {u"title": data.get(u"Title", u"Missing title :S"), u"year": data.get(u"Year", u"Unknown year"), u"rating": data.get(u"Rating", u"N/A"), u"genre": data.get(u"Genre", u"Unknown"), u"id": data.get(u"ID", u"tt0107838"), u"site_search_url": site_search_url}
def __init__(self, client, referrer=None, uri_to_instance=True, default_instance=None, *args, **kwargs): self.client = client self.referrer = referrer self.uri_to_instance = uri_to_instance self.default_instance = default_instance JSONDecoder.__init__(self, *args, **kwargs)
def call(cmd): try: return JSONDecoder().decode(check_output(cmd.split(" "))) except ValueError: raise Exception("invalid json returned by experiment-cli") except Exception: raise Exception("error calling experiment-cli")
def decode(encoded): try: obj = JSONDecoder().decode(encoded) except JSONDecodeError: return None else: return obj
def decode(self, s, *args, **kwargs): o = JSONDecoder.decode(self, s, *args, **kwargs) return schema_resolve_refs( o, partial(self.client.instance, cls=JSONSchemaReference, client=self.client))
def test_passes_result_to_response(self): response = self._callFUT(result=['foo', 'bar']) self.assertEqual(response.content_type, 'application/x-json') from simplejson import JSONDecoder from_json = JSONDecoder().decode(response.body) expected = {'id': None, 'error': None, 'result': ['foo', 'bar']} self.assertEqual(from_json, expected)
def test_no_kargs_builds_a_valid_but_empty_response(self): response = self._callFUT() self.assertEqual(response.content_type, 'application/x-json') from simplejson import JSONDecoder from_json = JSONDecoder().decode(response.body) expected = {'id': None, 'error': None, 'result': []} self.assertEqual(from_json, expected)
def save_node_set(self, name, nodes): name = unquote(name) nodes = unquote(nodes) file_name = "nodes-sets.json" data = JSONDecoder().decode(file(file_name).read()) data[name] = nodes open(file_name, "w").write( JSONEncoder(indent=4,sort_keys=1).encode(data)) return {"name": name}
def decrypt(encrypted, privateKey): try: encrypted = JSONDecoder().decode(encrypted) if "cipher" in encrypted and "AESEncryptedKey" in encrypted: AESkey = PgpHandler.RSAdecrypt(privateKey, encrypted["AESEncryptedKey"]) AESkey = PgpHandler.generateHash(AESkey) plaintext = PgpHandler.AESdecrypt(encrypted["cipher"], AESkey) return Message(plaintext, "") return None except Exception as e: print(e) pass
def _parse_tinymce_request(request): if request.method != 'POST': raise HTTPMethodNotAllowed('Expected POST') try: from_json = JSONDecoder().decode(request.body) methodcall = from_json['method'] lang, words = from_json['params'] if not isinstance(words, list): words = [words] except (KeyError, ValueError): raise HTTPBadRequest('Invalid JSON payload') if methodcall not in ('checkWords', 'getSuggestions'): raise HTTPBadRequest('Unknown RPC method') return methodcall, lang, words
def __init__(self,*args,**kwargs): JSONDecoder.__init__(self,object_hook=self.dict_to_object,*args,**kwargs)
value = value["value"] if "." in value: _time, microsecond = value.split(".") _time = strptime(_time, TIME_FORMAT) _time += timedelta(microseconds=int(microsecond)) return _time.time() else: return strptime(value, TIME_FORMAT).time() return value # The c version treats strings differently. Use py version py_scanstring = simplejson.decoder.py_scanstring scanstring = simplejson.decoder.scanstring decoder = JSONDecoder(object_hook=custom_decoder) decoder.parse_string = py_scanstring decoder.scan_once = simplejson.scanner.py_make_scanner(decoder) def to_json(value): return dumps(value, cls=CustomEncoder) def from_json(json): # Replace the original scanstring simplejson.decoder.scanstring = py_scanstring val = decoder.decode(json) # Put back the original simplejson.decoder.scanstring = scanstring return val
def __init__(self, **kwargs): JSONDecoder.__init__(self, object_hook=self.dict_to_object, **kwargs)
def lookup_direct(self, reference): decoder = JSONDecoder() api_url = self.api_base_url + u"lookup/1/.json?uri=" + reference.URI() response = utility.read_url(api_url) if not response: return None try: data = decoder.decode(response['data']) except StandardError: return None if not data.get(u"info"): return None # Album reference if reference.type == u"album": metadata = data.get(u"album", {}) album = metadata.get(u"name", u"Unknown album") artist = metadata.get(u"artist", u"Unknown artist") year = metadata.get(u"released", u"Unknown year") return u"%s: %s (%s)" % (artist, album, year) # Artist reference elif reference.type == u"artist": metadata = data.get(u"artist", {}) artist = metadata.get(u"name", u"Unknown artist") return u"%s" % artist # Track reference elif reference.type == u"track": # Extract some dicts from the data metadata = data.get(u"track", {}) metadata_album = metadata.get(u"album", {}) metadata_artists = metadata.get(u"artists", [{}]) # Extract info from the dicts album = metadata_album.get(u"name", u"Unknown album") artists = map(lambda artist: artist.get(u"name", u"Unknown artist"), metadata_artists) artist = ", ".join(artists) duration = metadata.get(u"length", u"0.0") popularity = metadata.get(u"popularity", u"0.0") track = metadata.get(u"name", u"Unknown track") year = metadata_album.get(u"released", u"Unknown year") # Convert strings to floats try: duration = float(duration) except ValueError: duration = 0.0; try: popularity = float(popularity) except ValueError: popularity = 0.0; # Construct result return u"%s: %s | %s (%s) | Track popularity %d%%, Track duration %d:%02d" % \ (artist, track, album, year, int(round(popularity*100)), duration / 60, duration % 60) # Unsupported reference else: return None
def lookup_direct(self, reference): decoder = JSONDecoder() api_url = self.api_base_url + u"lookup/1/.json?uri=" + reference.URI() response = utility.read_url(api_url) if not response: return None try: data = decoder.decode(response['data']) except StandardError: return None if not data.get(u"info"): return None # Album reference if reference.type == u"album": metadata = data.get(u"album", {}) album = metadata.get(u"name", u"Unknown album") artist = metadata.get(u"artist", u"Unknown artist") year = metadata.get(u"released", u"Unknown year") return u"%s: %s (%s)" % (artist, album, year) # Artist reference elif reference.type == u"artist": metadata = data.get(u"artist", {}) artist = metadata.get(u"name", u"Unknown artist") return u"%s" % artist # Track reference elif reference.type == u"track": # Extract some dicts from the data metadata = data.get(u"track", {}) metadata_album = metadata.get(u"album", {}) metadata_artists = metadata.get(u"artists", [{}]) # Extract info from the dicts album = metadata_album.get(u"name", u"Unknown album") artists = map( lambda artist: artist.get(u"name", u"Unknown artist"), metadata_artists) artist = ", ".join(artists) duration = metadata.get(u"length", u"0.0") popularity = metadata.get(u"popularity", u"0.0") track = metadata.get(u"name", u"Unknown track") year = metadata_album.get(u"released", u"Unknown year") # Convert strings to floats try: duration = float(duration) except ValueError: duration = 0.0 try: popularity = float(popularity) except ValueError: popularity = 0.0 # Construct result return u"%s: %s | %s (%s) | Track popularity %d%%, Track duration %d:%02d" % \ (artist, track, album, year, int(round(popularity*100)), duration / 60, duration % 60) # Unsupported reference else: return None
def __init__(self, m2m_ep, _, client_id, handle_request_func=None, subscribe_sys_topics=False): """ :param str m2m_ep: :param bool _: :param str client_id: :param call handle_request_func: :param bool subscribe_sys_topics: Whether to subscribe to $SYS topics or not (cf <https://github.com/mqtt/mqtt.github.io/wiki/SYS-Topics>) """ super(OneM2MMQTTClient, self).__init__() parsed_url = urlparse(m2m_ep) self._target_id = parsed_url.fragment self._encode = JSONEncoder().encode self._decode = JSONDecoder().decode self._handle_request_func = handle_request_func self._processed_request_ids = deque([], maxlen=200) self._request_promises = LRUCache(threadsafe=False, max_items=200) if client_id is None: import random import string client_id = ''.join(random.sample(string.letters, 16)) self._client = mqtt.Client( clean_session=False, client_id='::'.join([ 'C' if client_id[0].lower() in ['c', 'm'] else 'A', self._mqtt_mask(client_id), ]), ) @self.attach_callback() def on_connect(client, _, rc): """ :param mqtt.Client client: :param All userdata: :param integer rc: :return void: """ if not rc == mqtt.CONNACK_ACCEPTED: raise ConnectionFailed(mqtt.connack_string(rc)) def request_callback(client, _, message): """ Catch requests and :param mqtt.Client client: :param All _: :param mqtt.MQTTMessage message: :return void: """ originator = message.topic.split('/')[3] try: request = self._decode(message.payload) except JSONDecodeError as e: self.logger.warn('Got rubbish request from client %s: %s' % ( originator, e.message, )) return try: if request['rqi'] in self._processed_request_ids: self.logger.info( 'Request %s already processed; discarding duplicate.' % (request['rqi'], )) return else: rqi = request['rqi'] except KeyError: self.logger.warn( 'Special treatment for special request w/o request id from %s.' % (originator, )) return try: request['pc'] = decode_onem2m_content( self._encode(request['pc']), 'application/json') request['ty'] = type(request['pc']) except KeyError: # No content, eh? request['ty'] = None self.logger.debug('Decoded JSON request: %s' % (request, )) op = OneM2MOperation._member_map_.values()[request['op'] - 1] to = request['to'] del request['op'], request['to'] try: response = self._handle_request_func( OneM2MRequest(op, to, **request)).get() except OneM2MErrorResponse as response: self.logger.error('OneM2MError: %s' % (response.message, )) except CSEError as e: response = OneM2MErrorResponse( status_code=e.response_status_code, rqi=rqi) if not response.rqi: # This really should not happen. No, really, it shouldn't. self.logger.debug( 'FIXUP! FIXUP! FIXUP! Adding missing request identifier to response: %s' % (rqi, )) response.rqi = rqi if response.content: response.content = self._decode( encode_onem2m_content(response.content, 'application/json', path=response.to)[1]) self._publish_message( self._encode({ k: getattr(response, k) for k in self.__response_fields if getattr(response, k) is not None }), self._build_topic(originator, client_id, type='resp'), ) self._processed_request_ids.append(rqi) def response_callback(client, _, message): """ :param mqtt.Client client: :param All _: :param mqtt.MQTTMessage message: :return: """ try: response = self._decode(message.payload) except JSONDecodeError as e: self.logger.error( 'Discarding response w/ damaged payload: %s', (e.message, )) return promise_key = (message.topic.split('/')[4], response['rqi']) try: p = self._request_promises[promise_key] except KeyError: self.logger.debug( 'Response %s could not be mapped to a request. Discarding.' % (response['rqi'], )) return try: response['pc'] = decode_onem2m_content( self._encode(response['pc']), 'application/json') except KeyError: pass except CSEValueError as e: self.logger.error( 'Content of response %s could not be parsed, throwing on the trash heap: %s' % (response['rqi'], e.message)) p.reject(e) status_code = response['rsc'] del response['rsc'] if status_code >= ERROR_MIN: p.reject(OneM2MErrorResponse(status_code, **response)) else: p.fulfill(OneM2MResponse(status_code, **response)) topics = [ self._build_topic(originator=client_id, receiver='#', type='resp'), ] client.message_callback_add(topics[0], response_callback) if self._handle_request_func is not None: topics.append(self._build_topic(receiver=client_id) + '/+') client.message_callback_add(topics[1], request_callback) if subscribe_sys_topics: topics.append('$SYS/#') self.logger.debug('Subscribing to topic(s) %s ...' % (', '.join(topics), )) client.subscribe([(str(topic), MQTT_QOS_LEVEL) for topic in topics]) @self.attach_callback() def on_disconnect(client, userdata, rc): """ :param mqtt.Client client: :param All userdata: :param int rc: :return void: """ if not rc == mqtt.MQTT_ERR_SUCCESS: self.logger.error( 'Involuntary connection loss: %s (code %d). Waiting for reconnect ...' % (mqtt.error_string(rc), rc)) @self.attach_callback() def on_message(client, userdata, message): """ :param mqtt.Client client: :param All userdata: :param mqtt.MQTTMessage message: :return void: """ self.logger.debug('message received on topic %s' % (message.topic, )) @self.attach_callback() def on_log(client, userdata, level, buf): """ :param mqtt.Client client: :param All userdata: :param integer level: :param string buf: :return void: """ self.logger.debug('pahomqtt-%d: %s' % (level, buf)) if parsed_url.username: self._client.username_pw_set(parsed_url.username, parsed_url.password) try: self._client.connect(parsed_url.hostname, parsed_url.port or portmap[parsed_url.scheme]) except SocketError as e: raise ConnectionFailed(e.message) def loop(): try: while self._client.loop( timeout=0.1) != mqtt.mqtt_cs_disconnecting: gevent.sleep() except (KeyboardInterrupt, SystemExit): pass gevent.spawn(loop)
from turbulenz_local.lib.validation import ValidationException from turbulenz_local.lib.exceptions import BadRequest, NotFound from turbulenz_local.lib.tools import create_id from turbulenz_local.models.gamelist import get_game_by_slug from turbulenz_local.models.userlist import get_current_user from turbulenz_local.models.apiv1.gamenotifications import GameNotificationTask, reset_game_notification_settings, \ GameNotificationTaskError, GameNotificationPathError, \ GameNotificationsUnsupportedException, \ GameNotificationTaskListManager, \ GameNotificationSettingsError, \ get_game_notification_settings, GameNotificationKeysList # pylint: disable=C0103 _json_decoder = JSONDecoder(encoding='utf-8') # pylint: enable=C0103 def _get_user_name(): return get_current_user().username def _get_game(slug): game = get_game_by_slug(slug) if not game: raise NotFound('No game with slug %s' % slug) return game
def __init__(self, encoding=None, object_hook=None): if object_hook is None: object_hook = self.json_to_python JSONDecoder.__init__(self, encoding, object_hook)
def __init__(self, db): JSONDecoder.__init__(self, object_hook=self.inflate) self.db = db
def __init__(self, arena=None, encoding=None, object_hook=None): JSONDecoder.__init__(self, encoding, object_hook) if not self.object_hook: self.object_hook = self.json_to_python self.arena = arena
def decode(self, s, *args, **kwargs): o = JSONDecoder.decode(self, s, *args, **kwargs) return self._decode(o)
def __init__(self, m2m_ep, _, client_id, handle_request_func=None, subscribe_sys_topics=False, ca_certs=None, cert_file=None, key_file=None, insecure=False): """ :param str m2m_ep: :param bool _: :param str client_id: :param call handle_request_func: :param bool subscribe_sys_topics: Whether to subscribe to $SYS topics or not (cf <https://github.com/mqtt/mqtt.github.io/wiki/SYS-Topics>) """ super(OneM2MMQTTClient, self).__init__() parsed_url = urlparse(m2m_ep) self._default_target_id = parsed_url.fragment def _default(x): if isinstance(x, datetime): try: isoformat = x.isoformat except AttributeError: raise TypeError("%s (%s)" % (x, type(x))) return isoformat() else: return x self._encode = JSONEncoder(default=_default).encode self._decode = JSONDecoder().decode self._handle_request_func = handle_request_func self._processed_request_ids = deque([], maxlen=200) self._request_promises = LRUCache(threadsafe=False, max_items=200) if client_id is None: import random import string client_id = ''.join(random.sample(string.letters, 16)) else: client_id = self._get_client_id_from_originator(client_id) self._client = mqtt.Client( clean_session=False, client_id='::'.join([ 'C' if client_id[0].lower() in ['c', 'm'] else 'A', self._mqtt_mask(client_id), ]), ) self._client_id = client_id @self.attach_callback() def on_connect(client, userdata, flags_dict, rc): """ :param mqtt.Client client: :param All userdata: :param dict flags_dict: :param integer rc: :return void: """ if not rc == mqtt.CONNACK_ACCEPTED: raise ConnectionFailed(mqtt.connack_string(rc)) topics = [ self._build_topic(originator=client_id, receiver='#', type='resp'), ] client.message_callback_add(topics[0], self._response_callback) if self._handle_request_func is not None: topics.append(self._build_topic(receiver=client_id) + '/+') client.message_callback_add(topics[1], self._request_callback) if subscribe_sys_topics: topics.append('$SYS/#') self.logger.debug('Subscribing to topic(s) %s ...' % (', '.join(topics), )) client.subscribe([(str(topic), MQTT_QOS_LEVEL) for topic in topics]) @self.attach_callback() def on_disconnect(client, userdata, rc): """ :param mqtt.Client client: :param All userdata: :param int rc: :return void: """ if not rc == mqtt.MQTT_ERR_SUCCESS: self.logger.error( 'Involuntary connection loss: %s (code %d). Waiting for reconnect ...' % (mqtt.error_string(rc), rc)) @self.attach_callback() def on_message(client, userdata, message): """ :param mqtt.Client client: :param All userdata: :param mqtt.MQTTMessage message: :return void: """ self.logger.debug('message received on topic %s' % (message.topic, )) @self.attach_callback() def on_log(client, userdata, level, buf): """ :param mqtt.Client client: :param All userdata: :param integer level: :param string buf: :return void: """ self.logger.debug('pahomqtt-%d: %s' % (level, buf)) if parsed_url.username: self._client.username_pw_set(parsed_url.username, parsed_url.password) if parsed_url.scheme != 'mqtt': self._client.tls_set(ca_certs=ca_certs, certfile=cert_file, keyfile=key_file) self._client.tls_insecure_set(insecure) try: self._client.connect(parsed_url.hostname, parsed_url.port or portmap[parsed_url.scheme]) except SocketError as e: raise ConnectionFailed(e.message) def loop(): try: while self._client.loop( timeout=0.1) != mqtt.mqtt_cs_disconnecting: gevent.sleep() except (SystemExit, KeyboardInterrupt, AttributeError): pass gevent.spawn(loop)
def __init__(self, client, referrer=None, *args, **kwargs): self.client = client self.referrer = referrer JSONDecoder.__init__(self, *args, **kwargs)
def decode(self, s, *args, **kwargs): o = JSONDecoder.decode(self, s, *args, **kwargs) return schema_resolve_refs(o, partial(self.client.instance, cls=JSONSchemaReference, client=self.client))
def __init__(self, ip, port): self.__server = HttpClient() self.__url = "http://%s:%s"%(ip, port) self.JSON = JSONDecoder()
from time import time as get_time from simplejson import JSONEncoder, JSONDecoder from turbulenz_local.lib.exceptions import ApiException # pylint: disable=F0401 import yaml from yaml import YAMLError from pylons import config # pylint: enable=F0401 from turbulenz_local.tools import get_absolute_path, create_dir # pylint: disable=C0103 _json_decoder = JSONDecoder() _json_encoder = JSONEncoder() # pylint: enable=C0103 REQUIRED_NOTIFICATION_KEYS = ['key', 'title'] class GameNotificationsUnsupportedException(Exception): pass class GameNotificationPathError(Exception): pass class GameNotificationTaskError(Exception):
def default(self, o): if hasattr(o, "json"): return getattr(o, "json")() if isinstance(o, datetime.datetime): return o.ctime() return JSONEncoder.default(self, o) _encoder = Encoder() def json_encode(o): return _encoder.encode(o) _decoder = JSONDecoder() def json_decode(o): # remove tabs if they exists o = o.replace('\t', '') data = _decoder.decode(o) return data class JsonSerializable(object): def json(self): # TODO: ugly d = {} for k, v in self.__dict__.iteritems(): if isinstance(v, JsonSerializable):