From b58199a563f292e476ee15d6d3be5736444d95b3 Mon Sep 17 00:00:00 2001 From: Phondanai Khanti Date: Fri, 12 Oct 2018 11:22:06 +0700 Subject: [PATCH] hacktoberfest: reformatted code for cleaner, use isinstance instead of compare string with type function --- microgear/cache.py | 3 +- microgear/client.py | 346 +++++++++++++++++++++++++++++--------------- 2 files changed, 229 insertions(+), 120 deletions(-) diff --git a/microgear/cache.py b/microgear/cache.py index 9f1d6a9..d4411e7 100755 --- a/microgear/cache.py +++ b/microgear/cache.py @@ -16,7 +16,7 @@ def get_item(key): return None -def set_item(key,value): +def set_item(key, value): """Write JSON content from value argument to cached file and return""" CACHED_KEY_FILE = os.path.join(CURRENT_DIR, key) @@ -31,4 +31,3 @@ def delete_item(key): if os.path.isfile(CACHED_KEY_FILE): os.remove(CACHED_KEY_FILE) - diff --git a/microgear/client.py b/microgear/client.py index 5ec8daf..37c77c0 100755 --- a/microgear/client.py +++ b/microgear/client.py @@ -11,9 +11,11 @@ import requests import certifi + def do_nothing(arg1=None, arg2=None): pass + subscribe_list = [] current_subscribe_list = [] current_id = None @@ -30,20 +32,23 @@ def do_nothing(arg1=None, arg2=None): on_disconnect = do_nothing config_list = {} -def create(gearkey,gearsecret, appid="", args = {}): + +def create(gearkey, gearsecret, appid="", args={}): if 'debugmode' in args: - logging.basicConfig(level=logging.INFO, - format='%(asctime)s %(levelname)-8s %(message)s', - datefmt='%d/%m/%Y %I:%M:%S %p', - ) + logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(levelname)-8s %(message)s', + datefmt='%d/%m/%Y %I:%M:%S %p', + ) else: - logging.basicConfig(level=logging.WARNING, - format='%(asctime)s %(levelname)-8s %(message)s', - datefmt='%d/%m/%Y %I:%M:%S %p', - ) - microgear.gearalias = args.get('alias',"")[0:16] + logging.basicConfig( + level=logging.WARNING, + format='%(asctime)s %(levelname)-8s %(message)s', + datefmt='%d/%m/%Y %I:%M:%S %p', + ) + microgear.gearalias = args.get('alias', "")[0:16] if 'scope' in args: - matchScope = re.match( r'^(\w+:[a-zA-Z\/]+,*)+$', args['scope']) + matchScope = re.match(r'^(\w+:[a-zA-Z\/]+,*)+$', args['scope']) if matchScope: microgear.scope = args["scope"] else: @@ -54,36 +59,39 @@ def create(gearkey,gearsecret, appid="", args = {}): microgear.gearsecret = gearsecret microgear.appid = appid + def client_on_connect(client, userdata, rc): global block microgear.state = True - logging.info("Connected with result code "+str(rc)) - if rc == 0 : + logging.info("Connected with result code " + str(rc)) + if rc == 0: on_connect() auto_subscribeAndpublish() - elif rc == 1 : + elif rc == 1: logging.warning("Unable to connect: Incorrect protocol version.") - elif rc == 2 : + elif rc == 2: logging.warning("Unable to connect: Invalid client identifier.") - elif rc == 3 : + elif rc == 3: logging.warning("Unable to connect: Server unavailable.") - elif rc == 4 : + elif rc == 4: unsubscribe(current_id) microgear.mqtt_client.disconnect() on_info("Invalid credential.") logging.info("Unable to connect: Invalid credential, requesting new one") resettoken() connect(block_loop) - elif rc == 5 : + elif rc == 5: on_warning("Not authorised.") logging.warning("Unable to connect: Not authorised.") else: logging.warning("Unable to connect: Unknown reason") + def client_on_publish(client, userdata, mid): - #Publish callback + # Publish callback pass + def client_on_message(client, userdata, msg): topics = msg.topic.split("/") if msg.topic != "@info" and msg.topic != "@error": @@ -92,24 +100,27 @@ def client_on_message(client, userdata, msg): elif topics[2] == "&absent": on_absent(str(msg.payload)) elif '&id' in topics[2]: - #controll message + # controll message pass else: - on_message(msg.topic,str(msg.payload)) + on_message(msg.topic, str(msg.payload)) elif msg.topic == "@info": on_info(str(msg.payload)) elif msg.topic == "@error": on_error(str(msg.payload)) + def client_on_subscribe(client, userdata, mid, granted_qos): ## TODO: Check subscribe fail pass + def client_on_disconnect(client, userdata, rc): - if rc!=0: + if rc != 0: microgear.state = False on_disconnect() - logging.info("Diconnected with result code "+str(rc)) + logging.info("Diconnected with result code " + str(rc)) + def connect(block=False): global block_loop @@ -120,26 +131,29 @@ def connect(block=False): while not microgear.accesstoken: get_token() time.sleep(times) - times = times+10 + times = times + 10 microgear.mqtt_client = mqtt.Client(microgear.accesstoken["token"]) - current_id = '/&id/'+str(microgear.accesstoken["token"])+'/#' - current_subscribe_list.append('/&id/'+str(microgear.accesstoken["token"])+'/#') + current_id = '/&id/' + str(microgear.accesstoken["token"]) + '/#' + current_subscribe_list.append('/&id/' + str(microgear.accesstoken["token"]) + '/#') endpoint = microgear.accesstoken["endpoint"].split("//")[1].split(":") - username = microgear.gearkey+"%"+str(int(time.time())) - password = hmac(microgear.accesstoken["secret"]+"&"+microgear.gearsecret,microgear.accesstoken["token"]+"%"+username) - microgear.mqtt_client.username_pw_set(username,password) + username = microgear.gearkey + "%" + str(int(time.time())) + password = hmac( + microgear.accesstoken["secret"] + "&" + microgear.gearsecret, + microgear.accesstoken["token"] + "%" + username, + ) + microgear.mqtt_client.username_pw_set(username, password) if microgear.securemode: microgear.mqtt_client.tls_set(certifi.where()) - microgear.mqtt_client.connect(endpoint[0],int(microgear.gbsport), 60) + microgear.mqtt_client.connect(endpoint[0], int(microgear.gbsport), 60) else: - microgear.mqtt_client.connect(endpoint[0],int(microgear.gbport), 60) + microgear.mqtt_client.connect(endpoint[0], int(microgear.gbport), 60) microgear.mqtt_client.on_connect = client_on_connect microgear.mqtt_client.on_message = client_on_message microgear.mqtt_client.on_publish = client_on_publish microgear.mqtt_client.on_subscribe = client_on_subscribe microgear.mqtt_client.on_disconnect = client_on_disconnect - if(block): + if block: microgear.mqtt_client.loop_forever() else: microgear.mqtt_client.loop_start() @@ -147,48 +161,56 @@ def connect(block=False): time.sleep(2) break + def auto_subscribeAndpublish(): global publish_list global current_subscribe_list if microgear.mqtt_client: - microgear.mqtt_client.subscribe("/"+microgear.appid+"/&present") - microgear.mqtt_client.subscribe("/"+microgear.appid+"/&absent") - for topic in current_subscribe_list : + microgear.mqtt_client.subscribe("/" + microgear.appid + "/&present") + microgear.mqtt_client.subscribe("/" + microgear.appid + "/&absent") + for topic in current_subscribe_list: microgear.mqtt_client.subscribe(topic) - logging.info("Auto subscribe "+topic) + logging.info("Auto subscribe " + topic) else: on_error("Microgear currently is not available.") logging.error("Microgear currently is not available.") - if microgear.mqtt_client : - for topic in publish_list : - microgear.mqtt_client.publish(topic[0],topic[1],retain=topic[2].get('retain',False)) + if microgear.mqtt_client: + for topic in publish_list: + microgear.mqtt_client.publish( + topic[0], topic[1], retain=topic[2].get('retain', False) + ) publish_list = [] else: on_error("Microgear currently is not available.") logging.error("Microgear currently is not available.") -def subscribe_thread(topic,qos=0): - if microgear.mqtt_client : - logging.info("Auto subscribe "+topic) - microgear.mqtt_client.subscribe(topic,qos) + +def subscribe_thread(topic, qos=0): + if microgear.mqtt_client: + logging.info("Auto subscribe " + topic) + microgear.mqtt_client.subscribe(topic, qos) else: on_error("Microgear currently is not available.") logging.error("Microgear currently is not available.") -def subscribe(topic,qos=0): + +def subscribe(topic, qos=0): global subscribe_list global current_subscribe_list threads = [] - if "/"+microgear.appid+topic not in current_subscribe_list: - current_subscribe_list.append("/"+microgear.appid+topic) + if "/" + microgear.appid + topic not in current_subscribe_list: + current_subscribe_list.append("/" + microgear.appid + topic) if microgear.mqtt_client: - t = threading.Thread(target=subscribe_thread, args=("/"+microgear.appid+topic,qos)) + t = threading.Thread( + target=subscribe_thread, args=("/" + microgear.appid + topic, qos) + ) threads.append(t) t.start() else: - subscribe_list.append("/"+microgear.appid+topic) + subscribe_list.append("/" + microgear.appid + topic) + def unsubscribe(topic): global current_subscribe_list @@ -197,15 +219,16 @@ def unsubscribe(topic): if topic == current_id: current_subscribe_list.remove(current_id) microgear.mqtt_client.unsubscribe(current_id) - if "/"+microgear.appid+topic in current_subscribe_list: - current_subscribe_list.remove("/"+microgear.appid+topic) - microgear.mqtt_client.unsubscribe("/"+microgear.appid+topic) - logging.info("Auto unsubscribe "+topic) + if "/" + microgear.appid + topic in current_subscribe_list: + current_subscribe_list.remove("/" + microgear.appid + topic) + microgear.mqtt_client.unsubscribe("/" + microgear.appid + topic) + logging.info("Auto unsubscribe " + topic) else: on_error("Microgear currently is not available.") logging.error("Microgear currently is not available.") -def publish_thread(topic,message,args = {}): + +def publish_thread(topic, message, args={}): qos = 0 retain = False @@ -215,61 +238,71 @@ def publish_thread(topic,message,args = {}): if 'retain' in args: retain = args['retain'] - if microgear.mqtt_client : - microgear.mqtt_client.publish(topic,message,qos=qos,retain=retain) + if microgear.mqtt_client: + microgear.mqtt_client.publish(topic, message, qos=qos, retain=retain) else: on_error("Microgear currently is not available.") logging.error("Microgear currently is not available.") -def publish(topic,message,args = {}): + +def publish(topic, message, args={}): global publish_list threads = [] if microgear.mqtt_client: - t = threading.Thread(target=publish_thread, args=("/"+microgear.appid+topic,message,args)) + t = threading.Thread( + target=publish_thread, args=("/" + microgear.appid + topic, message, args) + ) threads.append(t) t.start() else: - publish_list.append(["/"+microgear.appid+topic,message,args]) + publish_list.append(["/" + microgear.appid + topic, message, args]) + def setname(topic): logging.warning("Deprecated soon: Please consider using setalias()") microgear.gearname = topic - subscribe("/gearname/"+topic) + subscribe("/gearname/" + topic) + def setalias(alias): - publish("/@setalias/"+alias,"") + publish("/@setalias/" + alias, "") + + +def chat(topic, message): + publish("/gearname/" + topic, message) -def chat(topic,message): - publish("/gearname/"+topic,message) def readstream(stream, filter): - publish('/@readstream/'+stream,'{"filter":"'+filter+'"}') + publish('/@readstream/' + stream, '{"filter":"' + filter + '"}') + + +def writestream(stream, data): + publish('/@writestream/' + stream, '{"data":' + data + '}') -def writestream(stream,data): - publish('/@writestream/'+stream,'{"data":'+data+'}') def pushOwner(message): - if type(message) is dict: + if isinstance(message, dict): logging.info("push notification json") json = "{" for key in message: - json += "\""+str(key)+"\""+":"+str(message[key])+"," - json = json[:len(json)-1] + "}" - publish("/@push/owner",json) + json += "\"" + str(key) + "\"" + ":" + str(message[key]) + "," + json = json[: len(json) - 1] + "}" + publish("/@push/owner", json) else: logging.info("push notification message") - publish("/@push/owner",message) + publish("/@push/owner", message) + def get_token(): logging.info("Check stored token.") - cached = cache.get_item("microgear-"+microgear.gearkey+".cache") + cached = cache.get_item("microgear-" + microgear.gearkey + ".cache") if cached: if cached.get("accesstoken"): if not cached.get("key"): cached["key"] = microgear.gearkey - cache.set_item("microgear-"+microgear.gearkey+".cache", cached) + cache.set_item("microgear-" + microgear.gearkey + ".cache", cached) microgear.accesstoken = cached.get("accesstoken") - for name,value in microgear.accesstoken.items(): + for name, value in microgear.accesstoken.items(): microgear.accesstoken[name] = str(value) endpoint = microgear.accesstoken.get("endpoint").split("//")[1].split(":") microgear.gearexaddress = endpoint[0] @@ -279,33 +312,68 @@ def get_token(): else: get_requesttoken(cached) else: - cached = cache.set_item("microgear-"+microgear.gearkey+".cache", {"key": microgear.gearkey}) + cached = cache.set_item( + "microgear-" + microgear.gearkey + ".cache", {"key": microgear.gearkey} + ) + def get_requesttoken(cached): logging.info("Requesting a request token.") - path="" - url="" + path = "" + url = "" if len(microgear.gearalias): verifier = microgear.gearalias - path = '/oauth2/authorize?'+"response_type=code&client_id=" + microgear.gearkey + "&scope=appid:" + microgear.appid +" "+"alias:" +microgear.gearalias+ "&state=mgrev:" + microgear.mgrev; + path = ( + '/oauth2/authorize?' + + "response_type=code&client_id=" + + microgear.gearkey + + "&scope=appid:" + + microgear.appid + + " " + + "alias:" + + microgear.gearalias + + "&state=mgrev:" + + microgear.mgrev + ) else: verifier = microgear.mgrev - path = '/oauth2/authorize?'+"response_type=code&client_id=" + microgear.gearkey + "&scope=appid:" + microgear.appid +"&state=mgrev:" + microgear.mgrev; - + path = ( + '/oauth2/authorize?' + + "response_type=code&client_id=" + + microgear.gearkey + + "&scope=appid:" + + microgear.appid + + "&state=mgrev:" + + microgear.mgrev + ) + if microgear.securemode: - url = "https://"+microgear.gearauthsite+":"+microgear.gearapisecureport+path; + url = ( + "https://" + + microgear.gearauthsite + + ":" + + microgear.gearapisecureport + + path + ) else: - url = "http://"+microgear.gearauthsite+":"+microgear.gearapiport+path; + url = "http://" + microgear.gearauthsite + ":" + microgear.gearapiport + path response = requests.get(url) response = response.url.split("code=") - if len(response)==2: - cached["requesttoken"] = {"token": response[1],"secret": None,"verifier": verifier} - cache.set_item("microgear-"+microgear.gearkey+".cache", cached) + if len(response) == 2: + cached["requesttoken"] = { + "token": response[1], + "secret": None, + "verifier": verifier, + } + cache.set_item("microgear-" + microgear.gearkey + ".cache", cached) microgear.requesttoken = cached["requesttoken"] get_accesstoken(cached) else: on_error("Request token is not issued, please check your appkey and appsecret.") - logging.error("Request token is not issued, please check your appkey and appsecret.") + logging.error( + "Request token is not issued, please check your appkey and appsecret." + ) + def get_accesstoken(cached): microgear.requesttoken = cached.get("requesttoken") @@ -313,33 +381,54 @@ def get_accesstoken(cached): logging.info("Requesting an access token.") verifier = "" url = "" - path = "/oauth2/token?grant_type=authorization_code&code=" + microgear.requesttoken.get("token") + "&client_id=" + microgear.gearkey + "&client_secret=" + microgear.gearsecret + "&state=mgrev:" + microgear.mgrev; + path = ( + "/oauth2/token?grant_type=authorization_code&code=" + + microgear.requesttoken.get("token") + + "&client_id=" + + microgear.gearkey + + "&client_secret=" + + microgear.gearsecret + + "&state=mgrev:" + + microgear.mgrev + ) if microgear.securemode: - url = "https://"+microgear.gearauthsite+":"+microgear.gearapisecureport+path; + url = ( + "https://" + + microgear.gearauthsite + + ":" + + microgear.gearapisecureport + + path + ) else: - url = "http://"+microgear.gearauthsite+":"+microgear.gearapiport+path; + url = "http://" + microgear.gearauthsite + ":" + microgear.gearapiport + path response = requests.post(url) - if response.status_code==200: + if response.status_code == 200: datajson = json.loads(response.text) token = datajson['access_token'].split(':') - revokecode = hmac(token[1]+"&"+microgear.gearsecret,token[0]).replace('/','_') + revokecode = hmac(token[1] + "&" + microgear.gearsecret, token[0]).replace( + '/', '_' + ) cached["requesttoken"] = None cached["accesstoken"] = { "token": token[0], "secret": token[1], "endpoint": datajson['endpoint'], - "revokecode": revokecode + "revokecode": revokecode, } flag = datajson['flag'] if flag == "P": - cache.set_item("microgear-"+microgear.gearkey+".cache", cached) + cache.set_item("microgear-" + microgear.gearkey + ".cache", cached) elif flag == "S": - cache.set_item("microgear-"+microgear.gearkey+".cache", {}) + cache.set_item("microgear-" + microgear.gearkey + ".cache", {}) microgear.accesstoken = cached["accesstoken"] else: resettoken() - on_error("Access token is not issued, please check your consumerkey and consumersecret.") - logging.error("Access token is not issued, please check your consumerkey and consumersecret.") + on_error( + "Access token is not issued, please check your consumerkey and consumersecret." + ) + logging.error( + "Access token is not issued, please check your consumerkey and consumersecret." + ) def hmac(key, message): @@ -347,64 +436,85 @@ def hmac(key, message): import hmac import hashlib - hash = hmac.new(key.encode('utf-8'),message.encode('utf-8'), hashlib.sha1).digest() + hash = hmac.new(key.encode('utf-8'), message.encode('utf-8'), hashlib.sha1).digest() password = base64.encodestring(hash) password = password.strip() return password.decode('utf-8') + def resettoken(): - cached = cache.get_item("microgear-"+microgear.gearkey+".cache") - if cached : - microgear.accesstoken = cached.get("accesstoken",{}) - if "revokecode" in microgear.accesstoken : - path = "/api/revoke/"+microgear.accesstoken["token"]+"/"+microgear.accesstoken["revokecode"] + cached = cache.get_item("microgear-" + microgear.gearkey + ".cache") + if cached: + microgear.accesstoken = cached.get("accesstoken", {}) + if "revokecode" in microgear.accesstoken: + path = ( + "/api/revoke/" + + microgear.accesstoken["token"] + + "/" + + microgear.accesstoken["revokecode"] + ) url = "" if microgear.securemode: - url = "https://"+microgear.gearauthsite+":"+microgear.gearapisecureport+path; + url = ( + "https://" + + microgear.gearauthsite + + ":" + + microgear.gearapisecureport + + path + ) else: - url = "http://"+microgear.gearauthsite+":"+microgear.gearapiport+path; + url = ( + "http://" + + microgear.gearauthsite + + ":" + + microgear.gearapiport + + path + ) response = requests.get(url) - if response.status_code==200: - cache.delete_item("microgear-"+microgear.gearkey+".cache") + if response.status_code == 200: + cache.delete_item("microgear-" + microgear.gearkey + ".cache") else: on_error("Reset token error.") logging.error("Reset token error.") else: - cache.delete_item("microgear-"+microgear.gearkey+".cache") + cache.delete_item("microgear-" + microgear.gearkey + ".cache") logging.warning("Token is still, please check your key on Key Management.") microgear.accesstoken = None + def disconnect(): - microgear.mqtt_client.disconnect(); + microgear.mqtt_client.disconnect() + -def writeFeed(feedid,data,feedkey=""): - if len(feedid)>0 and type(data) is dict: +def writeFeed(feedid, data, feedkey=""): + if len(feedid) > 0 and isinstance(data, dict): json = "{" for key in data: - json += "\""+str(key)+"\""+":"+str(data[key])+"," - json = json[:len(json)-1] + "}" + json += "\"" + str(key) + "\"" + ":" + str(data[key]) + "," + json = json[: len(json) - 1] + "}" if feedkey == "": - publish("/@writefeed/"+feedid,json) + publish("/@writefeed/" + feedid, json) else: - publish("/@writefeed/"+feedid+"/"+feedkey,json) + publish("/@writefeed/" + feedid + "/" + feedkey, json) logging.info(json) else: logging.info("Invalid parameters, please try again") -def setConfig(key,value): - if(key=="GEARAUTH"): + +def setConfig(key, value): + if key == "GEARAUTH": config_list["GEARAUTH"] = value microgear.gearauthsite = value + def getConfig(key): return config_list[key] + def useTLS(boolean): microgear.securemode = boolean + def connected(): return micrgear.state - - -