Commit 8f84f67f authored by Phyks's avatar Phyks
Browse files

Refactor courses etc

parent d115d7d5
......@@ -12,10 +12,9 @@ Jarvis can be easily installed on a Raspberry Pi, starting from an up-to-date Ra
2. Start the Raspberry Pi. Set it correctly running `sudo raspi-config` (enable camera and so on).
3. Clone Jarvis repo.
4. Run the scripts in `system/` folder to install the requirements.
5. Copy `config.py.example` to `config.py` and then edit it. All items should be self-explicit.
6. Make sure your user is member of the `gpio` and `video` groups.
6. Make sure your user is member of the `gpio` and `video` groups.
7. Jarvis does not automatically export the GPIO pins to prevent it from running as `root`. So you should `gpio export PIN out` for the pins you use (1 and 7 by defautl).
7. Run `jarvis.py` to start Jarvis.
7. Run `jarvis.py` and follow the instructions to start Jarvis.
*Note :* Jarvis requires a MySQL database to be used, and a webserver to serve the web visualisation (repo [Jarvis web](https://github.com/hackEns/Jarvis_web)). As our webserver does not run on the Raspberry Pi, the above scripts do not include the setup for the webserver and the MySQL database. You should install and set them yourself. `system/jarvis.sql` contains all the necessary information to setup the database and tables.
......
import mysql.connector
from ._shared import *
class Courses(Rule):
"""Handles shopping list"""
def __init__(self, bot, config):
self.bot = bot
self.config = config
def __call__(self, serv, author, args):
"""Handles shopping list"""
if len(args) < 3:
raise InvalidArgs
try:
comment = " ".join(args[3:])
except KeyError:
comment = ""
if args[1] == "acheter":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
values = (args[2], '%'+comment+'%')
try:
assert(self.bot.bdd_cursor is not None)
self.bot.bdd_cursor.execute(query, values)
row = self.bot.bdd_cursor.fetchone()
if row[0] > 0:
self.bot.ans(serv,
author,
"Item déjà présent dans la liste de courses")
return
query = ("INSERT INTO shopping(item, author, comment, date, " +
"bought) VALUES(%s, %s, %s, %s, 0)")
values = (args[2], author, comment, datetime.datetime.now())
self.bot.bdd_cursor.execute(query, values)
except AssertionError:
self.bot.ans(serv, author,
"Impossible d'ajouter l'objet à la " +
"liste de courses, base de données injoignable.")
return
except mysql.connector.errors.Error as err:
self.bot.ans(serv,
author,
"Impossible d'ajouter l'objet à la liste " +
"de courses. (%s)" % (err,))
return
self.bot.ans(serv, author, "Item ajouté à la liste de courses.")
elif args[1] == "annuler":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
values = (args[2], '%'+comment+'%')
try:
assert(self.bot.bdd_cursor is not None)
self.bot.bdd_cursor.execute(query, values)
row = self.bot.bdd_cursor.fetchone()
if row[0] > 1:
self.bot.ans(serv, author,
"Requêtes trop ambiguë. Plusieurs entrées " +
"correspondent.")
return
query = ("DELETE FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
self.bot.bdd_cursor.execute(query, values)
except AssertionError:
self.bot.ans(serv, author,
"Impossible de supprimer l'item, base de données " +
"injoignable.")
return
except mysql.connector.errors.Error as err:
self.bot.ans(serv,
author,
"Impossible de supprimer l'item. (%s)" % (err,))
return
self.bot.ans(serv, author, "Item supprimé de la liste de courses.")
elif args[1] == "acheté":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s AND bought=0")
values = (args[2], '%'+comment+'%')
try:
assert(self.bot.bdd_cursor is not None)
self.bot.bdd_cursor.execute(query, values)
row = self.bot.bdd_cursor.fetchone()
if row[0] > 1:
self.bot.ans(serv, author,
"Requêtes trop ambiguë. Plusieurs entrées " +
"correspondent.")
return
query = ("UPDATE shopping SET bought=1 WHERE item=%s AND " +
"comment LIKE %s AND bought=0")
self.bot.bdd_cursor.execute(query, values)
except AssertionError:
self.bot.ans(serv, author,
"Impossible de marquer l'item comme acheté, " +
"base de données injoignable.")
return
except mysql.connector.errors.Error as err:
self.bot.ans(serv,
author,
"Impossible de marquer l'item comme " +
"acheté. (%s)" % (err,))
return
self.bot.ans(serv, author, "Item marqué comme acheté.")
else:
raise InvalidArgs
def close(self):
pass
......@@ -4,6 +4,7 @@ from .Atx import Atx
from .Alias import Alias
from .Budget import Budget
from .Camera import Camera
from .Courses import Courses
from .Dis import Dis
from .Disclaimer import Disclaimer
from .Emprunt import Emprunt
......@@ -22,6 +23,7 @@ __all__ = [
"Alias",
"Budget",
"Camera",
"Courses",
"Dis",
"Disclaimer",
"Emprunt",
......
......@@ -67,6 +67,7 @@ class JarvisBot(ircbot.SingleServerIRCBot):
self.alias = Alias(self, self.basepath)
self.budget = Budget(self, config)
self.camera = Camera(self, config)
self.courses = Courses(self, config)
self.dis = Dis(self)
self.disclaimer = Disclaimer(self)
self.emprunt = Emprunt(self, self.bdd, self.bdd_cursor)
......@@ -278,113 +279,6 @@ class JarvisBot(ircbot.SingleServerIRCBot):
for rule in sorted(self.rules):
self.say(serv, self.rules[rule]['help'])
def courses(self, serv, author, args):
"""Handles shopping list"""
if len(args) < 3:
raise InvalidArgs
try:
comment = " ".join(args[3:])
except KeyError:
comment = ""
if args[1] == "acheter":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
values = (args[2], '%'+comment+'%')
try:
assert(self.bdd_cursor is not None)
self.bdd_cursor.execute(query, values)
row = self.bdd_cursor.fetchone()
if row[0] > 0:
self.ans(serv,
author,
"Item déjà présent dans la liste de courses")
return
query = ("INSERT INTO shopping(item, author, comment, date, " +
"bought) VALUES(%s, %s, %s, %s, 0)")
values = (args[2], author, comment, datetime.datetime.now())
self.bdd_cursor.execute(query, values)
except AssertionError:
if config.debug:
tools.warning("Debug : Database disconnected")
self.ans(serv, author,
"Impossible d'ajouter l'objet à la " +
"liste de courses, base de données injoignable.")
return
except mysql.connector.errors.Error as err:
if config.debug:
tools.warning("Debug : " + str(err))
self.ans(serv,
author,
"Impossible d'ajouter l'objet à la liste " +
"de courses. (%s)" % (err,))
return
self.ans(serv, author, "Item ajouté à la liste de courses.")
elif args[1] == "annuler":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
values = (args[2], '%'+comment+'%')
try:
assert(self.bdd_cursor is not None)
self.bdd_cursor.execute(query, values)
row = self.bdd_cursor.fetchone()
if row[0] > 1:
self.ans(serv, author,
"Requêtes trop ambiguë. Plusieurs entrées " +
"correspondent.")
return
query = ("DELETE FROM shopping WHERE item=%s AND " +
"comment LIKE %s")
self.bdd_cursor.execute(query, values)
except AssertionError:
if config.debug:
tools.warning("Debug : Database disconnected")
self.ans(serv, author,
"Impossible de supprimer l'item, base de données " +
"injoignable.")
return
except mysql.connector.errors.Error as err:
if config.debug:
tools.warning("Debug : " + str(err))
self.ans(serv,
author,
"Impossible de supprimer l'item. (%s)" % (err,))
return
self.ans(serv, author, "Item supprimé de la liste de courses.")
elif args[1] == "acheté":
query = ("SELECT COUNT(*) as nb FROM shopping WHERE item=%s AND " +
"comment LIKE %s AND bought=0")
values = (args[2], '%'+comment+'%')
try:
assert(self.bdd_cursor is not None)
self.bdd_cursor.execute(query, values)
row = self.bdd_cursor.fetchone()
if row[0] > 1:
self.ans(serv, author,
"Requêtes trop ambiguë. Plusieurs entrées " +
"correspondent.")
return
query = ("UPDATE shopping SET bought=1 WHERE item=%s AND " +
"comment LIKE %s AND bought=0")
self.bdd_cursor.execute(query, values)
except AssertionError:
if config.debug:
tools.warning("Debug : Database disconnected")
self.ans(serv, author,
"Impossible de marquer l'item comme acheté, " +
"base de données injoignable.")
return
except mysql.connector.errors.Error as err:
if config.debug:
tools.warning("Debug : " + str(err))
self.ans(serv,
author,
"Impossible de marquer l'item comme " +
"acheté. (%s)" % (err,))
return
self.ans(serv, author, "Item marqué comme acheté.")
else:
raise InvalidArgs
def moderation(self, serv, author, args):
"""Handles message to moderate listing"""
if len(config.admins) != 0 and author not in config.admins:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment