]> gitweb.pimeys.fr Git - bots/basile.git/blobdiff - nk.py
PEP8
[bots/basile.git] / nk.py
diff --git a/nk.py b/nk.py
index c80adfd0ff303adc7e0be438292e4a06d6f80869..ecad88fd4a482357240b47d4947dd35790d1d2cb 100644 (file)
--- a/nk.py
+++ b/nk.py
@@ -15,7 +15,7 @@ import config
 class NKError(Exception):
     def __init__(self,msg):
         Exception.__init__(self)
-        self.msg=msg
+        self.msg = msg
     def __str__(self):
         return str(self.msg)
     def __unicode__(self):
@@ -31,42 +31,41 @@ class NKUnknownError(NKError):
     pass
 
 def connect():
-    sock=socket.socket()
+    sock = socket.socket()
     try:
         # On établit la connexion sur port 4242
         sock.connect((config.nk_server, config.nk_port))
         # On passe en SSL
-        sock=ssl.wrap_socket(sock,ca_certs='../keys/ca_.crt')
+        sock = ssl.wrap_socket(sock, ca_certs='../keys/ca_.crt')
         # On fait un hello
-        sock.write('["hello", "Basile"]')
+        sock.write(json.dumps(["hello", "Basile"]))
         # On récupère la réponse du hello
-        out=sock.read()
-        out=json.loads(out)
+        out = sock.read()
+        out = json.loads(out)
     except Exception as exc:
         # Si on a foiré quelque part, c'est que le serveur est down
         raise NKRefused(str(exc))
-    if out["retcode"]==0:
+    if out["retcode"] == 0:
         return sock
-    elif out["retcode"]==11:
+    elif out["retcode"] == 11:
         raise NKHelloFailed(out["errmsg"])
     else:
         raise NKUnknownError(out["errmsg"])
 
 
-def login(username,password,typ="bdd"):
-    sock=connect()
-    if typ=="special": # ça c'est pour Basile lui-même
-        masque='[]'
-    elif typ=="bdd":
-        masque='[[], [], true]'
+def login(username, password, typ="bdd"):
+    sock = connect()
+    if typ == "special": # ça c'est pour Basile lui-même
+        masque = []
+    elif typ == "bdd":
+        masque = [[], [], True]
     try:
         # Basile a un compte special user
-        commande='["login", [%s,%s,"%s",%s]]'%(json.dumps(username),json.dumps(password),typ,masque)
-        sock.write(commande)
-        out=sock.read()
+        commande = ["login", [username, password, typ, masque]]
+        sock.write(json.dumps(commande))
+        out = sock.read()
     except Exception as exc:
         # Si on a foiré quelque part, c'est que le serveur est down
         raise NKRefused(str(exc))
-    # On vérifie ensuite que le login
-    return json.loads(out),sock
+    return json.loads(out), sock