def check_number(string): try: number = int(string) except ValueError: system.error(string + '\nWrong number!\n', 2) if number >= 0: return number else: system.error(string + '\nWrong number!\n', 2)
def openConnection(dsn): """ open a postgres connection """ conn = None try: conn = psycopg2.connect(dsn) except Exception, message: msg = "Cannot connect to database with dsn '%s': %s" % (dsn, message) error(msg) raise Exception(msg)
def fofError(self): error = system.error(self.code, '"') if error == None: error = system.error(self.code, "'") if error == None: self.repeatError() else: self.result = error else: self.result = error
def openConnection(dsn): """ open a postgres connection """ conn = None try: conn = psycopg2.connect(dsn) except Exception as message: msg = "Cannot connect to database with dsn '%s': %s" % (dsn, message) error(msg) raise Exception(msg) return conn
def selectWithSQLRequest(dsn, sql, TRACE=False): """ select multiple lines in a table with a complete sql """ conn = openConnection(dsn) cursor = conn.cursor() req = sql trace(TRACE, "Selection: %s" % req) try: cursor.execute(req) data = cursor.fetchall() cursor.close() except Exception, message: error("Cannot select from database : %s" % message) error("Request was : '%s'" % req) conn.close() return None
def insertInTable(dsn, table, columns, vals, TRACE=False): """ insert values in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "insert into %s(%s) values(%s)" % (table, columns, vals) trace(TRACE, "Insertion: %s" % req) try: cursor.execute(req) cursor.close() except Exception, message: conn.rollback() error("Cannot insert in database : %s" % message) error("Request was : '%s'" % req) conn.close() return False
def __init__(self, argv): self.__block = None self.__floor = None self.__ping = False self.__room = None self.__empty_rooms = False for arg in argv[1:]: # BLOCK if re.match('^--block=.+$', arg): self.__block = check_block(arg[8:]) elif re.match('^-b=.+$', arg): self.__block = check_block(arg[3:]) # FLOOR elif re.match('^--floor=.+$', arg): self.__floor = check_number(arg[8:]) elif re.match('^-f=.+$', arg): self.__floor = check_number(arg[3:]) # ROOM elif re.match('^--room=.+$', arg): self.__room = check_number(arg[7:]) self.__floor = room_to_floor(self.__room) elif re.match('^-r=.+$', arg): self.__room = check_number(arg[3:]) self.__floor = room_to_floor(self.__room) # ENABLE EMPTY ROOM PRINTING elif re.match('^--empty$', arg) or re.match('^-e$', arg): self.__empty_rooms = True # UNSUPPORTED ARGS else: system.error('Wrong arguments!\n', 1) if self.__block.startswith(('b', 'B')): if self.__block.endswith(('4', '7')): self.__max = 22 else: # 5 and 2 self.__max = 39 elif self.__block.startswith(('a', 'A')): self.__max = 41 elif self.__block.startswith(('d', 'D')): self.__max = 30 elif self.__block.startswith(('c', 'C')): self.__max = 32
def selectAllInTable(dsn, table, selection, condition='', TRACE=False): """ select multiple lines in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "select %s from %s" % (selection, table) if condition: req += ' where %s' % condition trace(TRACE, "Selection: %s" % req) try: cursor.execute(req) data = cursor.fetchall() cursor.close() except Exception, message: error("Cannot select from database : %s" % message) error("Request was : '%s'" % req) conn.close() return None
def updateTable(dsn, table, updates, condition='', TRACE=False): """ update columns in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "update %s set %s" % (table, updates) if condition: req += ' where %s' % condition trace(TRACE, "Update: %s" % req) try: cursor.execute(req) cursor.close() except Exception, message: conn.rollback() error("Cannot update in database : %s" % message) error("Request was : '%s'" % req) conn.close() return False
def deleteTable(dsn, table, condition='', TRACE=False): """ delete a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "delete from %s" % (table) if condition: req += ' where %s' % condition trace("Deletion : %s" % req) try: cursor.execute(req) cursor.close() except Exception, message: conn.rollback() error("Cannot delete from database : %s" % message) error("Request was : '%s'" % req) conn.close() return False
def selectWithSQLRequest(dsn, sql, TRACE=False): """ select multiple lines in a table with a complete sql """ conn = openConnection(dsn) cursor = conn.cursor() req = sql trace(TRACE, "Selection: %s" % req) try: cursor.execute(req) data = cursor.fetchall() cursor.close() except Exception as message: error("Cannot select from database : %s" % message) error("Request was : '%s'" % req) conn.close() return None conn.close() return data
def insertInTable(dsn, table, columns, vals, TRACE=False): """ insert values in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "insert into %s(%s) values(%s)" % (table, columns, vals) trace(TRACE, "Insertion: %s" % req) try: cursor.execute(req) cursor.close() except Exception as message: conn.rollback() error("Cannot insert in database : %s" % message) error("Request was : '%s'" % req) conn.close() return False conn.commit() conn.close() return True
def selectAllInTable(dsn, table, selection, condition='', TRACE=False): """ select multiple lines in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "select %s from %s" % (selection, table) if condition: req += ' where %s' % condition trace(TRACE, "Selection: %s" % req) try: cursor.execute(req) data = cursor.fetchall() cursor.close() except Exception as message: error("Cannot select from database : %s" % message) error("Request was : '%s'" % req) conn.close() return None conn.close() return data
def updateTable(dsn, table, updates, condition='', TRACE=False): """ update columns in a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "update %s set %s" % (table, updates) if condition: req += ' where %s' % condition trace(TRACE, "Update: %s" % req) try: cursor.execute(req) cursor.close() except Exception as message: conn.rollback() error("Cannot update in database : %s" % message) error("Request was : '%s'" % req) conn.close() return False conn.commit() conn.close() return True
def deleteTable(dsn, table, condition='', TRACE=False): """ delete a table """ conn = openConnection(dsn) cursor = conn.cursor() req = "delete from %s" % table if condition: req += ' where %s' % condition trace(TRACE, "Deletion : %s" % req) try: cursor.execute(req) cursor.close() except Exception as message: conn.rollback() error("Cannot delete from database : %s" % message) error("Request was : '%s'" % req) conn.close() return False conn.commit() conn.close() return True
def check_block(string): if re.match('^(B0[2457]|A0[2-5]|C0[1-3]|D0[1-2])$', string, re.IGNORECASE): return string.lower() else: system.error(string + '\nWrong block!\n', 3)