summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-07-20 19:03:57 +0200
committerSanto Cariotti <sancn@live.com>2017-07-20 19:03:57 +0200
commit99d5a8a7c7f86725d772f19e007c24e9f1397740 (patch)
tree7bdf257903276b85583e25a606f8a47918264029 /lib
parentb70d022b34f496b5ff05b9836cdb044f12e02c0e (diff)
added docstrings
Diffstat (limited to 'lib')
-rw-r--r--lib/app.py19
-rw-r--r--lib/classes.py6
-rw-r--r--lib/commands.py20
-rw-r--r--lib/listcommands.py3
4 files changed, 36 insertions, 12 deletions
diff --git a/lib/app.py b/lib/app.py
index f9696cd..f994863 100644
--- a/lib/app.py
+++ b/lib/app.py
@@ -5,7 +5,7 @@ from listcommands import ListCommands
class Stout(Commands):
def __init__(self):
- self.name = u'\U0001F37A'
+ self.name = u'\U0001F37A' #berr icon
self.user = Stout.username()
def getName(self):
@@ -17,12 +17,21 @@ class Stout(Commands):
@staticmethod
def username():
+ """
+ if users exists, return username:0.0.0.0, else return an empty string.
+ there is a set into user:HOST with a variable called name
+ """
if r.hget('user:'+host, 'name') is not None:
- return r.hget('user:'+host, 'name').decode('utf-8')
+ return r.hget('user:'+host, 'name').decode('utf-8') #default is a byte
else:
return ''
def action(self, cmd):
+ """
+ if cmd is empty, do nothing.
+ if length of cmd is less than 3 and the first word isn't in list of commands, the command is INFO
+ else execute command into ListCommands.commands (<- list)
+ """
if cmd is None:
return None
else:
@@ -31,9 +40,9 @@ class Stout(Commands):
if (count == 1 or count == 2) and cmd[0] not in ListCommands.commands:
try:
- if cmd[0] == 'info' and count == 1:
+ if cmd[0] == 'info' and count == 1: #general info
print(ListCommands.info['info'])
- elif cmd[0] == 'info' and count == 2:
+ elif cmd[0] == 'info' and count == 2: #info of a command
print(ListCommands.info[cmd[1]][0])
else:
raise KeyError
@@ -56,7 +65,7 @@ if __name__ == '__main__':
cmd = input('>' + Colors.yellow + app.getName() + Colors.black)
except EOFError:
break
-
+
app.action(cmd)
r.save()
diff --git a/lib/classes.py b/lib/classes.py
index 7f872af..fc04e2d 100644
--- a/lib/classes.py
+++ b/lib/classes.py
@@ -7,12 +7,18 @@ s.connect(('8.8.8.8', 80))
host = s.getsockname()[0]
def clear():
+ """
+ clear the window
+ """
try:
os.system('clear')
except:
os.system('cls')
def userexist(name):
+ """
+ lista is a list for the set called USERSNAME. check if name is in lista
+ """
lista = r.zrange('usersname', 0, -1)
for i in lista:
if name == i.decode('utf-8'):
diff --git a/lib/commands.py b/lib/commands.py
index 95f3563..1b5a5a8 100644
--- a/lib/commands.py
+++ b/lib/commands.py
@@ -5,25 +5,28 @@ from config import config as co
class Commands(object):
def command(self, what, cmd):
+ """
+ this function is used for execute a command
+ """
if what == 'clear':
clear()
elif what == 'set':
try:
- if cmd[1] == 'user' and cmd[2] is not None:
+ if cmd[1] == 'user' and cmd[2] is not None: #set name for user
if len(cmd[2]) > 10:
raise PersonalError('lunghezza maggiore del consetito. Max 10')
- if userexist(cmd[2]) == True:
+ if userexist(cmd[2]) == True: #check if the user exists
raise PersonalError('questo nome utente esiste giĆ ')
if len(cmd) > ListCommands.info['set'][1]:
raise PersonalError(Colors.grey + 'set user' + Colors.red + ' accetta 1 parametro')
- if self.user != '': r.zrem('usersname', self.user)
+ if self.user != '': r.zrem('usersname', self.user) #if user already exists, del the name from set
self.user = cmd[2]
- r.hset('user:'+host, 'name', self.user)
- r.zadd('usersname', self.user, 0)
+ r.hset('user:'+host, 'name', self.user) #change name of user:0.0.0.0
+ r.zadd('usersname', self.user, 0) #insert new name into the set
print('Ok')
elif cmd[1] is not ListCommands.commands['set']:
@@ -39,19 +42,22 @@ class Commands(object):
if len(cmd) > ListCommands.info['get'][1]:
raise PersonalError(Colors.grey + 'get ' + Colors.red + 'accetta 2 parametri')
- if cmd[1] == 'i':
+ if cmd[1] == 'i': #if first word after get is 'i', there is an info
if cmd[2] == 'user':
+ #if self.user is empty, print 'nil'
if self.user != '':
print(r.hget('user:'+host, 'name').decode('utf-8'))
else:
print('nil')
- elif cmd[2] not in ListCommands.commands['get'][1]:
+ elif cmd[2] not in ListCommands.commands['get'][1]: #check if the word after 'i' exists
ListCommands.err('keyword')
else:
print(co[cmd[2]])
elif cmd[1] == 'user?':
+ #if there are more than 2 word after 'get', the func doens't work
if len(cmd) > 3:
raise PersonalError(Colors.grey + 'get user?' + Colors.red + ' accetta 1 parametro')
+ #return 1 if the user exists
print((lambda x: '0' if x == False else '1')(userexist(cmd[2])))
else:
raise KeyError
diff --git a/lib/listcommands.py b/lib/listcommands.py
index 2d90cea..6c7ea65 100644
--- a/lib/listcommands.py
+++ b/lib/listcommands.py
@@ -2,6 +2,9 @@ import sys
from classes import Colors
class ListCommands(object):
+ """
+ ListCommands is used for return errors and check what commands exists
+ """
info = {
'info' : 'this is stout',