Example #1
0
def exists_field(field_name, table_name, db_path):
    """ Check whether a field exists in the table or not.

        :param str field_name: name of the field to check
        :param str table_name: name of the table that contains the field
        :param str db_path: path to the database
        :returns: True or False

        :raises IOError: cannot open file
    """
    try:
        if not exists_table(table_name, db_path):
            raise Exception("Table %s does not exist" % table_name)

        db_file = codecs.open(db_path, "r", "utf-8")
        db_data = json.load(db_file)
        db_file.close()

        for f in db_data[codecs.decode(table_name, "utf-8")]["fields"]:
            if field_name.decode("utf-8") in f:
                return True

        return False

    except IOError as e:
        raise e
Example #2
0
def swap_fields(index1, index2, table_name, db_path):
    """ Swap two field indexes in the specified table. This affects the
        priority order of the fields.

        :param int index1: first field index to swap
        :param int index2: second field index to swap
        :param str table_name: table in which to perform the swap
        :param str db_path: path to the database

        :raises IndexError: invalid index
        :raises IOError: cannot open file
        :raises OSError: error writing to database
        :raises TypeError: invalid index type
        :raises Exception: field does not exist 
    """
    try:
        if not exists_table(table_name, db_path):
            raise Exception("Table %s does not exist" % table_name)

        db_file = codecs.open(db_path, "r", "utf-8")
        db_data = json.load(db_file)
        db_file.close()

        temp = db_data[codecs.decode(table_name, "utf-8")]["fields"][index1]
        db_data[codecs.decode(table_name, "utf-8")]["fields"][index1] = db_data[codecs.decode(table_name, "utf-8")][
            "fields"
        ][index2]
        db_data[codecs.decode(table_name, "utf-8")]["fields"][index2] = temp

        db_file = codecs.open(db_path, "w", "utf-8")
        db_file.write(json.dumps(db_data, ensure_ascii=False, sort_keys=True, indent=4))
        db_file.close()

    except IndexError as e:
        raise e
    except IOError as e:
        raise e
    except OSError as e:
        raise e
    except TypeError as e:
        raise e
Example #3
0
def exists_row(index, table_name, db_path):
    """ Check if a row with the given index exists in the table.

        :param int index: index of check
        :param str table_name: name of the table
        :param str db_path: path to the database
        :returns: True or false

        :raises IndexError: invalid index
        :raises IOError: cannot open file
        :raises KeyError: invalid key
        :raises TypeError: invalid index type
        :raises Exception: table does not exist
    """
    try:
        if not exists_table(table_name, db_path):
            raise Exception('Table %s does not exist' % table_name)

        db_file = codecs.open(db_path, 'r', 'utf-8')
        db_data = json.load(db_file)
        db_file.close()

        if index >= 0 and index < len(
                db_data[codecs.decode(table_name, 'utf-8')]['rows']):
            return True
        else:
            return False

    except IndexError as e:
        raise e
    except IOError as e:
        raise e
    except KeyError as e:
        raise e
    except TypeError as e:
        raise e
Example #4
0
def create_row(element_list, table_name, db_path):
    """ Creates a row of elements in the given table.

        Loops through the list of fields in the table and adds the
        corresponding element.

        :param element_list: list of elements to add to the table. Elements
            must appear in the order the fields are listed in the table, and
            there must be one element per field in the list, even if they are
            left blank
        :param str table_name: name of the table that will contain the row
        :param str db_path: path to the database

        :rasies IndexError: invalid number of elements
        :raises IOError: cannot open file
        :raises KeyError: invalid key
        :raises OSError: error writing to database
        :raises TypeError: data type error
        :raises Exception: table does not exist
    """
    try:
        if not exists_table(table_name, db_path):
            raise Exception('Table %s does not exist', table_name)

        db_file = codecs.open(db_path, 'r', 'utf-8')
        db_data = json.load(db_file)
        db_file.close()

        if len(element_list) != len(
                db_data[codecs.decode(table_name, 'utf-8')]['fields']):
            raise Exception('Number of elements is not equal to the number of available fields')
        new_row = {}
        for index, f in enumerate(
                db_data[codecs.decode(table_name, 'utf-8')]['fields']):
            if element_list[index] == "":
                new_row[f.keys()[0]] = ""
            elif f.values()[0] == 'str':
                new_row[f.keys()[0]] = codecs.decode(element_list[index], 'utf-8')
            elif f.values()[0] == 'int' or f.values()[0] == 'bool':
                # Boolean values a represented with 0 or 1
                new_row[f.keys()[0]] = int(element_list[index])
            elif f.values()[0] == 'float':
                new_row[f.keys()[0]] = float(element_list[index])

        db_data[codecs.decode(table_name, 'utf-8')]['rows'].append(new_row)

        db_file = codecs.open(db_path, 'w', 'utf-8')
        db_file.write(json.dumps(db_data, ensure_ascii=False,
                sort_keys=True, indent=4))
        db_file.close()

    except IndexError as e:
        raise e
    except IOError as e:
        raise e
    except KeyError as e:
        raise e
    except OSError as e:
        raise e
    except TypeError as e:
        raise e
    except ValueError as e:
        raise e