Example #1
0
 def process_record(self, record, dbm):
     import socket
     if self.augment_record and dbm is not None:
         record = self.get_record(record, dbm)
     if self.unit_system is not None:
         record = weewx.units.to_std_system(record, self.unit_system)
     data = self.filter_data(record)
     if weewx.debug >= 2:
         logdbg("data: %s" % data)
     if self.skip_upload:
         loginf("skipping upload")
         return
     url = urlparse(self.server_url)
     for _count in range(self.max_tries):
         try:
             client_id = self.client_id
             if not client_id:
                 pad = "%032x" % random.getrandbits(128)
                 client_id = 'weewx_%s' % pad[:8]
             mc = mqtt.Client(client_id=client_id)
             if url.username is not None and url.password is not None:
                 mc.username_pw_set(url.username, url.password)
             # if we have TLS opts configure TLS on our broker connection
             if len(self.tls_dict) > 0:
                 mc.tls_set(**self.tls_dict)
             mc.connect(url.hostname, url.port)
             mc.loop_start()
             if self.aggregation.find('aggregate') >= 0:
                 tpc = self.topic + '/loop'
                 (res, mid) = mc.publish(tpc,
                                         json.dumps(data),
                                         retain=self.retain,
                                         qos=self.qos)
                 if res != mqtt.MQTT_ERR_SUCCESS:
                     logerr("publish failed for %s: %s" % (tpc, res))
             if self.aggregation.find('individual') >= 0:
                 for key in data:
                     tpc = self.topic + '/' + key
                     (res,
                      mid) = mc.publish(tpc,
                                        json.dumps({
                                            'ts':
                                            datetime.datetime.now(
                                                self.timezone).isoformat(),
                                            'value':
                                            float(data[key])
                                        }),
                                        retain=self.retain)
                     if res != mqtt.MQTT_ERR_SUCCESS:
                         logerr("publish failed for %s: %s" % (tpc, res))
             mc.loop_stop()
             mc.disconnect()
             return
         except (socket.error, socket.timeout, socket.herror) as e:
             logdbg("Failed upload attempt %d: %s" % (_count + 1, e))
         time.sleep(self.retry_wait)
     else:
         raise weewx.restx.FailedPost("Failed upload after %d tries" %
                                      (self.max_tries, ))
Example #2
0
def dumps(params,
          kwargs=None,
          methodname=None,
          methodresponse=None,
          encoding=None,
          allow_none=True):
    """data [,options] -> marshalled data

    Convert an argument tuple or a Fault instance to an JSON-RPC
    request (or response, if the methodresponse option is used).

    In addition to the data object, the following options can be given
    as keyword arguments:

        methodname: the method name for a methodCall packet

        methodresponse: true to create a methodResponse packet.
        If this option is used with a tuple, the tuple must be
        a singleton (i.e. it can contain only one element).

        encoding: the packet encoding (default is UTF-8)

    All 8-bit strings in the data structure are assumed to use the
    packet encoding.  Unicode strings are automatically converted,
    where necessary.
    """

    if not encoding:
        encoding = "utf-8"

    error = None
    if isinstance(params, Fault):
        methodresponse = 1
        error = [params.faultCode, params.faultString]

    kwdata = None
    if error is None:
        data = json.dumps(params, ensure_ascii=False, cls=ExtJSONEncoder)
        if kwargs:
            kwdata = json.dumps(kwargs, ensure_ascii=False, cls=ExtJSONEncoder)
    else:
        data = json.dumps(error, ensure_ascii=False, cls=ExtJSONEncoder)

    # standard JSON-RPC wrappings
    if methodname:
        data = '{"method": %s%s%s}' % (
            json.dumps(methodname, ensure_ascii=False, cls=ExtJSONEncoder),
            ',\n "params": {0}'.format(data) if params else '',
            ',\n "kwargs": {0}\n'.format(kwdata) if kwargs else '')
    elif methodresponse:
        # a method response, or a fault structure
        data = "{%s: %s}" % ('"result"' if error is None else '"error"', data)
    #print(type(data), data)
    return data.encode(encoding)  # return as is
Example #3
0
 def RenderJson(self, pretty=False):
     """
     Render a Tropo object into a Json string.
     """
     steps = self._steps
     topdict = {}
     topdict['tropo'] = steps
     if pretty:
         try:
             json = jsonlib.dumps(topdict, indent=4, sort_keys=False)
         except TypeError:
             json = jsonlib.dumps(topdict)
     else:
         json = jsonlib.dumps(topdict)
     return json
Example #4
0
    def _do_post(self, url, data, limit=None):
        """
        Send a JSON payload with a string for ranking or sorting.
        """
        payload = {}
        response = {}
        http = httplib2.Http(".cache")

        # Data can't be an empty string.
        if not data:
            raise LingAPIException("Empty required parameter: %s" % arg)

        payload['string'] = data
        if limit:
            payload['limit'] = limit

        resp, content = http.request(
            url,
            "PUT",
            body=json.dumps(payload),
            headers={'content-type': 'application/json'})
        if resp.status == 200:
            response["response"] = resp
            response["content"] = json.loads(content)
        else:
            print url
            raise LingAPIException("Response failed with status code: %s" %
                                   resp["status"])

        return response
 def default(self, *args, **kwargs):
     """
     Nimmt die JSON-RPC-Anfrage entgegen und übergibt sie an die entsprechende
     JSON-RPC-Methode.
     """
     
     responses = []
     
     # Response content type -> JSON
     set_content_type_json()
     
     # Get data
     if cherrypy.request.method == "GET":
         data = kwargs
         if "params" in data:
             if self.debug:
                 cherrypy.log("")
                 cherrypy.log(u"params (raw): " + repr(data["params"]))
                 cherrypy.log("")
             try:
                 data["params"] = json.loads(data["params"])
             except _ParseError, err:
                 traceback_info = "".join(traceback.format_exception(*sys.exc_info())) 
                 cherrypy.log(traceback_info)
                 return json.dumps(
                     responses.ParseErrorResponse(
                         data = unicode(err)
                     ).to_dict()
                 )
         requests = [data]
Example #6
0
    def _do_post(self, url, data, limit=None):
        """
        Send a JSON payload with a string for ranking or sorting.
        """
        payload = {}
        response = {} 
        http = httplib2.Http(".cache")

        # Data can't be an empty string.
        if not data:
            raise LingAPIException("Empty required parameter: %s" % arg) 

        payload['string'] = data
        if limit:
            payload['limit'] = limit

        resp, content = http.request(url, "PUT", body=json.dumps(payload),
                                     headers={'content-type': 'application/json'})
        if resp.status == 200:
            response["response"]  = resp
            response["content"] = json.loads(content)
        else:
            print url
            raise LingAPIException("Response failed with status code: %s" % resp["status"])

        return response
Example #7
0
def fix_line(line):
    try:
        rec = loads(line)
        rec['time'] = rec['time'][:len("2017-02-07T19:11:04")]
        rec['day'] = rec['time'][:rec['time'].index("T")]
        return dumps(rec)
    except:
        return
Example #8
0
	def on_queue_bound(self, frame):
		self.boundQueue -= 1
		print "BOUND",self.boundQueue
		if self.boundQueue == 0:
			print "Start consume!!"
			self.channel.basic_consume(consumer_callback=self.on_message, queue=self.queue, no_ack=True)
		else:
			return

		for user in self.users:
			self.channel.basic_publish(exchange='dealer_exchange_1',
								routing_key="dealer",
								body=json.dumps({'method':'enter','source':'IAMGOD', "room_id":1, "user_id":user.user_id}))

		self.channel.basic_publish(exchange='dealer_exchange_1',
								routing_key="dealer",
								body=json.dumps({'method':'sit','source':'IAMGOD','user_id':1, "room_id":1, "seat":1,"private_key":self.users[0].private_key, "stake":100}))
Example #9
0
 def delimited_write(self, obj):
     """
     prefix json object with a comma
     """
     try:
         self.obj.write("," + json.dumps(obj))
     except:
         self.bad_obj(obj)
Example #10
0
 def write(self, obj):
     """
     writes the first row, then overloads self with delimited_write
     """
     try:
         self.obj.write(json.dumps(obj))
         setattr(self, "write", self.delimited_write)
     except:
         self.bad_obj(obj)
Example #11
0
 def post(self, site):
     if site == "wb":
         content = self.get_argument("content")
         access_token = self.session.get("oauth_access_token")
         auth = OAuthHandler(options.SINA_APP_KEY, options.SINA_APP_SECRET)
         auth.set_access_token(access_token.key, access_token.secret)
         api = API(auth)
         api.update_status(content)
         self.finish(json.dumps("success"))
Example #12
0
def write_to_file(events, f):
	for event in events:
		event = event.copy()
		timestamp = event["time"]
		del event["time"] # Don't want this in the JSON output
		if json.__name__ == "cjson":
			json_output = json.encode(event)
		else:
			json_output = json.dumps(event)
		f.write("%s %s\n" % (timestamp, json_output))
Example #13
0
def _failed(msg_to_log, error_code, msg, rpcid=None):
    print(msg_to_log, file=stderr)
    if rpcid is not NOTIFICATION:
        return dumps({
            'jsonrpc': '2.0',
            'error': {
                'code': error_code,
                'message': msg
            },
            'id': rpcid
        })
Example #14
0
	def on_message(self,ch, method, properties, body):
		print " [x] %r:%r" % (method.routing_key, json.loads(body),)
		msg = json.loads(body)
#		if 'Cards in hand' in msg:
		self.pKeys[method.routing_key] = 1
#
		if len(self.pKeys) == 1:
			self.pKeys = {}
			self.channel.basic_publish(exchange='dealer_exchange_1',
					routing_key="dealer",
					body=json.dumps({'method':'action','action':8,'user_id':1,
						"room_id":1, "private_key":self.users[0].private_key, "amount":20}))
Example #15
0
    def get_post_body(self, in_record):
        """Override, then supply the body and MIME type of the POST."""

        # put everything into the right units
        record = weewx.units.to_METRIC(in_record)

        # put data into expected scaling, structure, and format
        values = dict()
        values['station_id'] = self.station_id
        for _key in self._DATA_MAP:
            rkey = self._DATA_MAP[_key][0]
            if rkey in record and record[rkey] is not None:
                values[_key] = record[rkey] * self._DATA_MAP[_key][1] + self._DATA_MAP[_key][2]
        data = json.dumps([values])
        return data, 'application/json'
Example #16
0
File: bot.py Project: s3/spiffy
    def _sqlitelog(self, prefix, command, params, text):  
        nick, user, host = sourcesplit(prefix)
        if not user:
            return # Don't logg server messages.
        timestamp = datetime.datetime.now()
        
        if command in ["PRIVMSG", "PART", "KICK", "TOPIC", "MODE", "NOTICE"]:
            channels = [params[0].lower()]
        elif command in ["NICK", "QUIT"]:
            channels = self.bot.chanlist.chans(nick)
        elif command in ["332", "333"]:
            channels = [params[1].lower()]
        elif command in ["JOIN"]:
            channels = [text or params[0].lower()]
        else:
            channels = ["#debug"] # fixme

        conn = sqlite3.connect(self.logdir, detect_types = sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
        conn.row_factory = sqlite3.Row
        c = conn.cursor()

        for channel in channels:            
            if channel.startswith("#"):
                tablename = self.bot.config["network"] + u"." + channel
            else:
                tablename = self.bot.config["network"] + u"." + nick.lower()

            # SQLite won't accept table names that start in 0-9, so we add spiffy_ in front
            hashname = "spiffy_" + hashlib.md5(tablename.encode("utf-8")).hexdigest()

            # check if a table exists for the current channel. sqlite_master contains
            # info about all the tables in a particular SQLite database
            if nick.lower() == self.bot.nickname or not channel.startswith('#'):
                c.execute("select tbl_name from sqlite_master where tbl_name = ?", (hashname,))
                if not c.fetchone():
                    c.execute("CREATE TABLE %s (ts TIMESTAMP, prefix TEXT, nick TEXT, command TEXT, params TEXT, text TEXT)" % hashname)
                    c.execute("CREATE INDEX idx_%s ON %s (command DESC, nick DESC, prefix DESC)" % (hashname, hashname))
                    self.bot._print("Created table %s (%s) in SQLite database" % (hashname, tablename))
                    c.execute("INSERT INTO spiffy_channels (hash, plaintext) VALUES (?,?)", (hashname, tablename))

            # table has been created if it didn't already exist, so we can do our insertions
            query = "INSERT INTO %s (ts, prefix, nick, command, params, text) VALUES (?,?,?,?,?,?)"
            c.execute(query % hashname, (timestamp, prefix, nick, command, json.dumps(params), text))


        c.close()
        conn.commit()
        conn.close()
	def _make_call(self, url, timestamp, magnitude):
		data = {
			'updates': [
				{
					'timestamp': timestamp,
					'power': {
						'magnitude':	int(magnitude), # truncated by WattzOn API, anyway
						'unit':			'W',
					}
				},
			]
		}

		req = RequestPostWithContentType('application/json', url, json.dumps(data))
		f = urllib2.urlopen(req)

		return f.read()
Example #18
0
 def process_record(self, record, dbm):
     import socket
     if self.augment_record and dbm is not None:
         record = self.get_record(record, dbm)
     if self.unit_system is not None:
         record = weewx.units.to_std_system(record, self.unit_system)
     data = self.filter_data(record)
     if weewx.debug >= 2:
         logdbg("data: %s" % data)
     if self.skip_upload:
         loginf("skipping upload")
         return
     url = urlparse.urlparse(self.server_url)
     for _count in range(self.max_tries):
         try:
             mc = mqtt.Client()
             if url.username is not None and url.password is not None:
                 mc.username_pw_set(url.username, url.password)
             # if we have TLS opts configure TLS on our broker connection
             if len(self.tls_dict) > 0:
                 mc.tls_set(**self.tls_dict)
             mc.connect(url.hostname, url.port)
             mc.loop_start()
             if self.aggregation.find('aggregate') >= 0:
                 tpc = self.topic + '/loop'
                 (res, mid) = mc.publish(tpc,
                                         json.dumps(data),
                                         retain=self.retain)
                 if res != mqtt.MQTT_ERR_SUCCESS:
                     logerr("publish failed for %s: %s" % (tpc, res))
             if self.aggregation.find('individual') >= 0:
                 for key in data:
                     tpc = self.topic + '/' + key
                     (res, mid) = mc.publish(tpc,
                                             data[key],
                                             retain=self.retain)
                     if res != mqtt.MQTT_ERR_SUCCESS:
                         logerr("publish failed for %s: %s" % (tpc, res))
             mc.loop_stop()
             mc.disconnect()
             return
         except (socket.error, socket.timeout, socket.herror), e:
             logdbg("Failed upload attempt %d: %s" % (_count + 1, e))
         time.sleep(self.retry_wait)
 def _prep_data(self, client, data, topic):
     if self.topics[topic]['type'] == 'json':
         self._publish_data(client, topic, json.dumps(data),
                            self.topics[topic]['qos'],
                            self.topics[topic]['retain'])
     if self.topics[topic]['type'] == 'keyword':
         payload = ', '.join("%s=%s" % (key, val)
                             for (key, val) in data.items())
         self._publish_data(client, topic, payload,
                            self.topics[topic]['qos'],
                            self.topics[topic]['retain'])
     if self.topics[topic]['type'] == 'individual':
         for key in data:
             tpc = topic + '/' + key
             self._publish_data(client,
                                tpc,
                                data[key],
                                retain=self.topics[topic]['retain'],
                                qos=self.topics[topic]['qos'])
                cherrypy.log(traceback_info)
                if hasattr(err, "data"):
                    error_data = err.data
                else:
                    error_data = None
                responses.append(
                    responses.InternalErrorResponse(
                        jsonrpc = jsonrpc, 
                        id = id,
                        data = error_data or unicode(err)
                    ).to_dict()
                )
        
        # Return as JSON-String (batch or normal)
        if len(requests) == 1:
            return json.dumps(responses[0])
        elif len(requests) > 1:
            return json.dumps(responses)
        else:
            return None
    
    default.exposed = True








Example #21
0
 def insert(cls, mutator, key, columns):
     updates = dict((key, json.dumps(value))
                    for key, value in columns.iteritems())
     mutator.insert(cls._cf, key, updates)
Example #22
0
def serialize(datastruct, filepath, encoding="utf-8"):
    _file = codecs.open(filepath, 'w', encoding=encoding)
    _file.write(dumps(datastruct))
    _file.close()
    return
Example #23
0
File: bot.py Project: s3/spiffy
    def _mysqllog(self, prefix, command, params, text):  
        nick, user, host = sourcesplit(prefix)
        if not user:
            return # Don't logg server messages.
        timestamp = datetime.datetime.now()
        
        if command in ["PRIVMSG", "PART", "KICK", "TOPIC", "MODE", "NOTICE"]:
            channels = [params[0].lower()]
        elif command in ["NICK", "QUIT"]:
            channels = self.bot.chanlist.chans(nick)
        elif command in ["332", "333"]:
            channels = [params[1].lower()]
        elif command in ["JOIN"]:
            channels = [text or params[0].lower()]
        else:
            channels = ["#debug"] # fixme

        """conn = MySQLdb.connect(host = self.mysql_host, user = self.mysql_user,
                             passwd = self.mysql_pass, db = self.mysql_db,
                             port = self.mysql_port or 3306, charset = 'utf8')
        c = conn.cursor()"""
        conn = self.mysql_conn
        c = self.mysql_curs

        for channel in channels:            
            if channel.startswith("#"):
                tablename = self.bot.config["network"] + u"." + channel
            else:
                tablename = self.bot.config["network"] + u"." + nick.lower()

            hashname = "spiffy_" + hashlib.md5(tablename.encode("utf-8")).hexdigest()

            # check if a table exists for the current channel. sqlite_master contains
            # info about all the tables in a particular SQLite database
            if nick.lower() == self.bot.nickname or not channel.startswith('#'):
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")                    
                    c.execute("""CREATE TABLE IF NOT EXISTS %s (
                                    `ts` datetime,
                                    `prefix` varchar(512) CHARSET utf8 COLLATE utf8_general_ci,
                                    `nick` varchar(50) CHARSET utf8 COLLATE utf8_general_ci,
                                    `command` varchar(7) CHARSET utf8 COLLATE utf8_general_ci,
                                    `params` varchar(512) CHARSET utf8 COLLATE utf8_general_ci,
                                    `text` varchar(512) CHARSET utf8 COLLATE utf8_general_ci,
                                    KEY `nick` (`nick`,`command`) 
                                    ) charset=utf8 collate=utf8_general_ci;""" % hashname)
                c.execute("INSERT INTO spiffy_channels (hash, plaintext) VALUES (%s, %s) ON DUPLICATE KEY UPDATE hash=hash", (hashname, tablename))

            # table has been created if it didn't already exist, so we can do our insertions
            c.execute("INSERT INTO %s" % hashname + " (ts, prefix, nick, command, params, text) VALUES (%s, %s, %s, %s, %s, %s)", (timestamp, prefix, nick, command, json.dumps(params), text))


        #c.close()
        conn.commit()
Example #24
0
def pack(result, rpcid):
    if rpcid is not NOTIFICATION:
        return dumps({'jsonrpc': '2.0', 'result': result, 'id': rpcid})
Example #25
0
def serialize(datastruct, filepath, encoding="utf-8"):
    _file = codecs.open( filepath, 'w', encoding=encoding )
    _file.write( dumps( datastruct ) )
    _file.close()
    return