summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-07-20 14:24:08 +0200
committerSanto Cariotti <sancn@live.com>2017-07-20 14:24:08 +0200
commitb34e6c27aa640e4bd4bde91d684e1e59095ba8b1 (patch)
tree14aa4c5b2b384535c6d379143f441625c7039722 /lib
parentfb3d4d39eded67b145aa17eac72ae85c9793bf67 (diff)
Moved files
Moved all files of lib into a lib dir
Diffstat (limited to 'lib')
-rw-r--r--lib/__init__.py0
-rw-r--r--lib/app.py64
-rw-r--r--lib/classes.py38
-rw-r--r--lib/commands.py56
-rw-r--r--lib/config.py6
-rw-r--r--lib/listcommands.py27
6 files changed, 191 insertions, 0 deletions
diff --git a/lib/__init__.py b/lib/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/__init__.py
diff --git a/lib/app.py b/lib/app.py
new file mode 100644
index 0000000..ab2f90e
--- /dev/null
+++ b/lib/app.py
@@ -0,0 +1,64 @@
+from classes import PersonalError, Colors, r, s, host, clear
+from commands import Commands
+from listcommands import ListCommands
+
+class Stout(Commands):
+
+ def __init__(self):
+ self.name = u'\U0001F37A'
+ self.user = Stout.username()
+
+ def getName(self):
+ word = ' (' + self.name + ') '
+ if self.user == '':
+ return word
+ else:
+ return word + Colors.grey + '(' + self.user + ':' + host + ') '
+
+ @staticmethod
+ def username():
+ if r.hget('user:'+host, 'name') is not None:
+ return r.hget('user:'+host, 'name').decode('utf-8')
+ else:
+ return ''
+
+ def action(self, cmd):
+ if cmd is None:
+ return None
+ else:
+ cmd = cmd.split()
+ count = len(cmd)
+
+ if (count == 1 or count == 2) and cmd[0] not in ListCommands.commands:
+ try:
+ if cmd[0] == 'info' and count == 1:
+ print(ListCommands.info['info'])
+ elif cmd[0] == 'info' and count == 2:
+ print(ListCommands.info[cmd[1]][0])
+ else:
+ raise KeyError
+ except (KeyError, IndexError):
+ ListCommands.err('keyword')
+ else:
+ what = cmd[0]
+ if what in ListCommands.commands:
+ self.command(what, cmd)
+ else:
+ ListCommands.err('keyword')
+
+
+if __name__ == '__main__':
+ clear()
+ app = Stout()
+ cmd = ''
+ while cmd != 'quit':
+ try:
+ cmd = input('>' + Colors.yellow + app.getName() + Colors.black)
+ except EOFError:
+ break
+
+ app.action(cmd)
+ r.save()
+
+ s.close()
+ clear()
diff --git a/lib/classes.py b/lib/classes.py
new file mode 100644
index 0000000..7f872af
--- /dev/null
+++ b/lib/classes.py
@@ -0,0 +1,38 @@
+import redis, socket, os
+from config import config as co
+
+r = redis.Redis(host=co['host'], port=co['port'], unix_socket_path=co['unix_socket_path'], db=co['db'])
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+s.connect(('8.8.8.8', 80))
+host = s.getsockname()[0]
+
+def clear():
+ try:
+ os.system('clear')
+ except:
+ os.system('cls')
+
+def userexist(name):
+ lista = r.zrange('usersname', 0, -1)
+ for i in lista:
+ if name == i.decode('utf-8'):
+ return True
+ else:
+ return False
+
+class PersonalError(Exception):
+
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return repr(self.value)
+
+
+class Colors(object):
+
+ red = '\033[91m'
+ yellow = '\033[93m'
+ grey = '\033[90m'
+ black = '\033[0m'
+ bold = '\033[1m'
diff --git a/lib/commands.py b/lib/commands.py
new file mode 100644
index 0000000..c9525db
--- /dev/null
+++ b/lib/commands.py
@@ -0,0 +1,56 @@
+from classes import userexist, PersonalError, r, host, clear, Colors
+from listcommands import ListCommands
+
+class Commands(object):
+
+ def command(self, what, cmd):
+ if what == 'clear':
+ clear()
+ elif what == 'set':
+ try:
+ if cmd[1] == 'user' and cmd[2] is not None:
+ if len(cmd[2]) > 10: raise PersonalError('lunghezza maggiore del consetito. Max 10')
+
+ if userexist(cmd[2]) == True: 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)
+
+ self.user = cmd[2]
+ r.hset('user:'+host, 'name', self.user)
+ r.zadd('usersname', self.user, 0)
+
+ print('Ok')
+ elif cmd[1] is not ListCommands.commands['set']:
+ raise IndexError
+ except IndexError:
+ ListCommands.err('wrong')
+ except PersonalError as e:
+ ListCommands.err('personal', e.value)
+ elif what == 'get':
+ try:
+ if len(cmd) > 2 and cmd[1] != 'user?': raise PersonalError(Colors.grey + 'get ' + Colors.red + 'accetta 1 parametro')
+
+ if cmd[1] == 'user':
+ if self.user != '':
+ print(r.hget('user:'+host, 'name').decode('utf-8'))
+ else:
+ print('nil')
+ elif cmd[1] == 'host':
+ print('localhost')
+ elif cmd[1] == 'port':
+ print('6379')
+ elif cmd[1] == 'user?':
+ if len(cmd) > 3: raise PersonalError(Colors.grey + 'get user?' + Colors.red + ' accetta 1 parametro')
+ print(userexist(cmd[2]))
+ elif cmd[1] not in ListCommands.commands:
+ raise KeyError
+ else:
+ raise IndexError
+ except IndexError:
+ ListCommands.err('wrong')
+ except KeyError:
+ ListCommands.err('keyword')
+ except PersonalError as e:
+ ListCommands.err('personal', e.value)
diff --git a/lib/config.py b/lib/config.py
new file mode 100644
index 0000000..3ccd82d
--- /dev/null
+++ b/lib/config.py
@@ -0,0 +1,6 @@
+config = {
+ 'host' : 'localhost',
+ 'port' : 6379,
+ 'db' : 0,
+ 'unix_socket_path' : None,
+}
diff --git a/lib/listcommands.py b/lib/listcommands.py
new file mode 100644
index 0000000..8313d26
--- /dev/null
+++ b/lib/listcommands.py
@@ -0,0 +1,27 @@
+import sys
+from classes import Colors
+
+class ListCommands(object):
+
+ info = {
+ 'info' : 'this is stout',
+ 'set' : ['set a value', 3],
+ }
+
+ commands = {
+ 'quit' : None,
+ 'clear' : None,
+ 'set' : ['user'],
+ 'get' : ['user', 'user?', 'host', 'port']
+ }
+
+ @staticmethod
+ def err(err, info = ''):
+ if err == 'keyword':
+ sys.stderr.write(Colors.red + 'keyword inesistente\n' + Colors.black)
+ elif err == 'wrong':
+ sys.stderr.write(Colors.red + 'sintassi comando errata\n' + Colors.black)
+ elif err == 'personal':
+ sys.stderr.write(Colors.red + str(info) + '\n' + Colors.black)
+ else:
+ pass