def setUp(self):
        self.database_name = 'foobar'
        self.table_name = 'foobar'
        columns = ['col1', 'col2', 'col3']
        column_defn = [('col1', 'text'), ('col2', 'text'), ('col3', 'integer')]
        self.row = [['x', 'y', 6]]
        self.qrow = _quotestrs(self.row)
        self.filename = "b64pyshell.txt"
        self.b64row = _quotestrs(DatabaseBase._encode_2darray(self.row))
        database = Database('foobar')

        with database:
            tbl_create(database, self.database_name, column_defn)
            tbl_rows_insert(database, self.table_name, columns, self.b64row)
Esempio n. 2
0
 def insert(cls,
            database_name,
            tbl_name,
            tbl_col_name,
            tbl_rows,
            delete_flag=False,
            encoding="unicode"):
     tbl_rows = _quotestrs(cls._encode_2darray(tbl_rows, encoding))
     cls1 = cls(database_name, delete_flag)
     return cls1._insert_rows(tbl_name, tbl_col_name, tbl_rows)
    def setUp(self):
        self.database_name = 'foobar'
        self.table_name = 'foobar'
        self.columns = ['col1', 'col2', 'col3']
        self.column_defn = [('col1', 'text'), ('col2', 'text'),
                            ('col3', 'integer')]
        self.row = [['x', 'y', 6], ['a', 's', 8], ['f', 'a', 7]]
        self.qrows = _quotestrs(self.row)
        self.filename = "b64pyshell.txt"
        self.encoding = "unicode"

        DatabaseCreateTable.create(self.database_name, self.table_name,
                                   self.column_defn)
 def setUp(self):
     self.database_name = 'foobar'
     self.table_name = 'foobar'
     self.columns = ['col1', 'col2', 'col3']
     self.column_defn = [('col1', 'text'), ('col2', 'text'),
                         ('col3', 'integer')]
     self.row = [['x', 'y', 6], ['a', 's', 8], ['f', 'a', 7]]
     self.qrows = _quotestrs(self.row)
     self.filename = "b64pyshell.txt"
     self.runtime_path = "C:\\Users\\burtnolej"
     DatabaseCreateTable.create(self.database_name,
                                self.table_name,
                                self.column_defn,
                                runtime_path=self.runtime_path)
    def setUp(self):
        self.database_name = 'foobar'
        self.table_name = 'foobar'
        self.columns = ['col1', 'col2', 'col3']
        self.column_defn = [('col1', 'text'), ('col2', 'text'),
                            ('col3', 'integer')]
        self.row = [['x', 'y', 6]]
        self.qrow = _quotestrs(self.row)
        self.filename = "b64pyshell.txt"
        self.b64row = DatabaseBase._encode_2darray(self.row)

        self.runtime_path = "C:\\Users\\burtnolej"
        append_text_to_file(
            self.filename,
            "database_name:" + b64encode(self.database_name) + "\n")
        append_text_to_file(self.filename,
                            "table_name:" + b64encode(self.table_name) + "\n")
        append_text_to_file(self.filename,
                            "delete_flag:" + b64encode("False") + "\n")
        append_text_to_file(
            self.filename, "columns:" +
            "$$".join([b64encode(field) for field in self.columns]) + "\n")
        append_text_to_file(
            self.filename, "column_defns:" + "$$".join([
                b64encode(_name) + "^" + b64encode(_type)
                for _name, _type in self.column_defn
            ]) + "\n")
        append_text_to_file(
            self.filename, "qry_str:" +
            b64encode("select col1,col2,col3 from " + self.table_name) + "\n")

        put_2darray_in_file(self.filename, self.b64row, suffix="rows:")
        DatabaseCreateTable.create_by_file(self.filename,
                                           runtime_path=self.runtime_path)
        DatabaseInsertRows.insert_by_file(self.filename,
                                          runtime_path=self.runtime_path)
Esempio n. 6
0
class DatabaseBase(ExcelBase):
    def __init__(self, database_name, delete_flag=False, **kwargs):
        super(DatabaseBase, self).__init__(**kwargs)
        self.database_name = database_name
        self.database = Database(
            self.runtime_path + "/" + self.database_name + ".sqlite",
            delete_flag)

    @classmethod
    def _validate_decode_flag(self, encoding="unicode"):
        self._validate_flag("decode_flag", encoding)

    @classmethod
    def _validate_delete_flag(self, encoding="unicode"):
        self._validate_flag("delete_flag", encoding)

    @classmethod
    def _validate_database_name(self, encoding="unicode"):
        self._validate_field("database_name", encoding)

    @classmethod
    def _validate_result_file(self, encoding="unicode"):
        self._validate_filename("result_file", encoding, mustexist=False)

    @classmethod
    def _validate_runtime_dir(self, encoding="unicode"):
        self._validate_filename("runtime_dir", encoding)

    @classmethod
    def _validate_table_name(self, encoding="unicode"):
        self._validate_field("table_name", encoding)

    @classmethod
    def _validate_qry_str(self, encoding="unicode"):
        self._validate_field("qry_str", encoding)

    @classmethod
    def _validate_columns(self, encoding="unicode"):
        if hasattr(self, "columns") == False:
            log.log(PRIORITY.FAILURE, msg="columns must be passed")
            return ([-1])
        else:
            if encoding == "base64":
                self.columns = [
                    b64decode(_field) for _field in self.columns.split("$$")
                ]
            else:
                self.columns = [_field for _field in self.columns.split("$$")]

    @classmethod
    def _validate_column_defns(self, encoding="unicode"):
        if hasattr(self, "column_defns") == False:
            log.log(PRIORITY.FAILURE, msg="column_defns must be passed")
            return ([-1])
        else:
            _column_defns = []
            _field_pairs = [_field for _field in self.column_defns.split("$$")]
            for _field_pair in _field_pairs:
                _name, _type = _field_pair.split("^")
                if encoding == "base64":
                    _column_defns.append((b64decode(_name), b64decode(_type)))
                else:
                    _column_defns.append((_name, _type))
            setattr(self, "column_defns", _column_defns)

    @classmethod
    def _validate_rows(self, encoding="unicode"):
        if hasattr(self, "rows") == False:
            log.log(PRIORITY.FAILURE, msg="rows must be passed")
            return ([-1])
        else:
            self.urows = []
            for row in self.rows.split("$$"):
                _row = row.split("^")
                if encoding == "base64":
                    try:
                        tmp = [b64decode(_field) for _field in _row]
                    except TypeError, e:
                        raise Exception("rows are not base64 encoded")

                self.urows.append(_row)

        setattr(self, "rows", _quotestrs(self.urows))
Esempio n. 7
0
class DataStoredProcBase(ExcelBase):
    def __init__(self, database_name, **kwargs):
        super(DataStoredProcBase, self).__init__(**kwargs)
        self.database_name = database_name
        self.database = Database(self.database_name + ".sqlite", False)

    @classmethod
    def _validate_database_name(self, encoding="unicode"):
        self._validate_field("database_name", encoding)

    @classmethod
    def _validate_header_flag(self, encoding="unicode"):
        self._validate_flag("header_flag", encoding)

    @classmethod
    def _validate_runtime_dir(self, encoding="unicode"):
        self._validate_filename("runtime_dir", encoding)

    @classmethod
    def _validate_result_file(self, encoding="unicode"):
        self._validate_filename("result_file", encoding, mustexist=False)

    @classmethod
    def _validate_delete_flag(self, encoding="unicode"):
        self._validate_flag("delete_flag", encoding)

    @classmethod
    def _validate_sp_name(self, encoding="unicode"):
        """purpose: make sure the sp_name matches a function in data_utils modules and 
        store the func name a member attribute (sp_func_name)"""

        assert hasattr(self, 'sp_name'), sp_name
        from sys import modules
        from utils.misc_basic.module_utils import _getmembers

        if self.sp_name.startswith(
                "insert_") == False and self.sp_name.startswith(
                    "delete_") == False and self.sp_name.startswith(
                        "update_") == False:
            sp_func_name = "get_" + self.sp_name
        else:
            sp_func_name = self.sp_name

        for _module in [
                'app.quad.utils.data_utils', 'app.quad.utils.data_utils_v2'
        ]:
            #module = modules['app.quad.utils.data_utils']
            module = modules[_module]
            module_members = _getmembers(module)

            if hasattr(module, sp_func_name) == True:
                setattr(self, 'sp_name', sp_func_name)
                #setattr(self,'sp_module','app.quad.utils.data_utils')
                setattr(self, 'sp_module', _module)

                log.log(
                    PRIORITY.INFO,
                    msg="located qry function; member attr sp_name set to [" +
                    sp_func_name + "]")
                return True
            #    return [-1]
            #return True

        return [-1]

    @classmethod
    def _validate_sp_args(self, encoding="unicode"):
        """purpose: parse nvp's that need to be passed to the sp_func_name """
        assert hasattr(self, 'sp_args'), sp_args
        import xml.etree.ElementTree as xmltree
        valid_args = [
            'days', 'periods', 'teachers', 'students', 'classlectures',
            'courses', 'subjects', 'sections', 'sectionschedules',
            'studentlevels', 'idacadperiods'
        ]

        sp_args_dict = xmlstr2dict(decode(self.sp_args, encoding),
                                   doublequote=True)

        for arg in sp_args_dict.keys():
            if arg not in valid_args:
                log.log(PRIORITY.FAILURE,
                        msg="arg is not a valid sp args [" +
                        str(sp_args_dict) + "]")
                return [-1]

        setattr(self, 'sp_args', sp_args_dict)
        log.log(PRIORITY.INFO,
                msg="validated sp args, set member attr sp_arg to [" +
                str(sp_args_dict) + "]")
        return True

    # DUPLICATE CODE - need to reuse code from excel_database_util
    # BUT NEED TO ADD TO SP_ARGS

    @classmethod
    def _validate_list(self, listname, encoding="unicode"):
        if hasattr(self, listname) == False:
            log.log(PRIORITY.FAILURE, msg=listname + " must be passed")
            return ([-1])
        else:
            if encoding == "base64":
                setattr(self, listname, [
                    b64decode(_field)
                    for _field in getattr(self, listname).split("$$")
                ])
            else:

                #setattr(self,listname,[decode(_field,encoding) for _field in getattr(self,listname).split("$$")])
                setattr(
                    self, listname,
                    [_field for _field in getattr(self, listname).split("$$")])

        if hasattr(self, "sp_args") == False:
            setattr(self, "sp_args", {})

        sp_args_dict = getattr(self, 'sp_args')
        sp_args_dict[listname] = getattr(self, listname)
        setattr(self, 'sp_args', sp_args_dict)

    @classmethod
    def _validate_columns(self, encoding="unicode"):
        self._validate_list("columns", encoding)
        '''if hasattr(self,"columns") == False:
            log.log(PRIORITY.FAILURE,msg="columns must be passed")   
            return([-1])
        else:
            if encoding == "base64":
                self.columns = [b64decode(_field) for _field in self.columns.split("$$")]
            else:
                self.columns = [_field for _field in self.columns.split("$$")]

        if hasattr(self,"sp_args") == False:
            setattr(self,"sp_args",{})
                    
        sp_args_dict = getattr(self,'sp_args')
        sp_args_dict['columns'] = self.columns
        setattr(self,'sp_args',sp_args_dict)'''

    @classmethod
    def _validate_row(self, encoding="unicode"):
        self._validate_list("row", encoding)

    @classmethod
    def _validate_column_defns(self, encoding="unicode"):
        if hasattr(self, "column_defns") == False:
            log.log(PRIORITY.FAILURE, msg="column_defns must be passed")
            return ([-1])
        else:
            if column_defns == []:  # allows method to be called statically
                column_defns = self.column_defns

            _column_defns = []
            _field_pairs = [_field for _field in self.column_defns.split("$$")]
            for _field_pair in _field_pairs:
                _name, _type = _field_pair.split("^")
                if encoding == "base64":
                    _column_defns.append((b64decode(_name), b64decode(_type)))
                else:
                    _column_defns.append((_name, _type))
            setattr(self, "column_defns", _column_defns)

        if hasattr(self, "sp_args") == False:
            setattr(self, "sp_args", {})

        sp_args_dict = getattr(self, 'sp_args')
        sp_args_dict['column_defns'] = self.column_defns
        setattr(self, 'sp_args', sp_args_dict)

    @classmethod
    def _validate_rows(self, encoding="unicode"):
        if hasattr(self, "rows") == False:
            log.log(PRIORITY.FAILURE, msg="rows must be passed")
            return ([-1])
        else:
            self.urows = []
            for row in self.rows.split("$$"):
                _row = row.split("^")
                if encoding == "base64":
                    try:
                        tmp = [b64decode(_field) for _field in _row]
                    except TypeError, e:
                        raise Exception("rows are not base64 encoded")

                #self.urows.append(_row)
                self.urows.append(decode(_field, encoding) for _field in _row)

        setattr(self, "rows", _quotestrs(self.urows))

        if hasattr(self, "sp_args") == False:
            setattr(self, "sp_args", {})

        sp_args_dict = getattr(self, 'sp_args')
        sp_args_dict['rows'] = self.rows
        setattr(self, 'sp_args', sp_args_dict)