Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
 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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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
Пример #7
0
    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
Пример #8
0
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
Пример #9
0
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
Пример #10
0
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
Пример #11
0
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
Пример #12
0
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
Пример #13
0
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
Пример #14
0
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
Пример #15
0
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
Пример #16
0
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)