예제 #1
0
    def _load(self):

        dir_info = self._data['connect_info'].get('directory')
        file_n = self._data['connect_info'].get('file_name')
        full_name = os.path.join(dir_info, file_n)

        i = 0
        with open(full_name, 'r') as txt_file:
            csv_d_rdr = csv.DictReader(txt_file)
            for r in csv_d_rdr:
                if i == 0:
                    if not Helper.is_empty(self._data['key_columns']):
                        if not Helper.is_column_list_valid(
                                self._data['key_columns'], r.keys()):
                            self._logger.error(
                                'Specified primary keys don\'t match table columns'
                            )
                            raise Exception
                self._add_row(r)
                i = i + 1

        if not Helper.is_empty(self._data['key_columns']):
            if not self._check_primary_key_constraint_for_first_load():
                self._rows = []
                self._logger.error(
                    'The specified primary keys don\'t comply with primary key constraint'
                )
                raise Exception

        self._logger.debug('CSVDataTable._load: Loaded ' +
                           str(len(self._rows)) + ' rows')
예제 #2
0
    def find_by_template(self,
                         template,
                         field_list=None,
                         limit=None,
                         offset=None,
                         order_by=None):
        '''

        :param template: A dictionary of the form { 'field1' : value1, 'field2': value2, ...}
        :param field_list: A list of request fields of the form, ['fielda', 'fieldb', ...]
        :param limit: Do not worry about this for now.
        :param offset: Do not worry about this for now.
        :param order_by: Do not worry about this for now.
        :return: A list containing dictionaries. A dictionary is in the list representing each record
            that matches the template. The dictionary only contains the requested fields.
        '''
        if not Helper.is_empty(template):
            if not Helper.is_template_valid(template, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified template don\'t match table columns'
                )
                raise Exception

        if not Helper.is_empty(field_list):
            if not Helper.is_column_list_valid(field_list, self.get_columns()):
                self._logger.error(
                    'Some columns in the specified field_list don\'t match table columns'
                )
                raise Exception

        matching_rows = []
        for row in self.get_rows():
            if limit is not None:
                if len(matching_rows) == limit:
                    break

            if Helper.matches_template(row, template):
                matching_rows.append(
                    Helper.extract_needed_fields(field_list, row))

        return matching_rows