Esempio n. 1
0
class FromFilePopulator(object):
    _populators = {
        "setting": SettingTablePopulator,
        "variable": VariableTablePopulator,
        "testcase": TestTablePopulator,
        "keyword": KeywordTablePopulator,
    }

    def __init__(self, datafile):
        self._datafile = datafile
        self._current_populator = NullPopulator()
        self._curdir = self._get_curdir(datafile.directory)

    def _get_curdir(self, path):
        return path.replace("\\", "\\\\") if path else None

    def populate(self, path):
        LOGGER.info("Parsing file '%s'." % path)
        source = self._open(path)
        try:
            self._get_reader(path).read(source, self)
        except:
            raise DataError(utils.get_error_message())
        finally:
            source.close()

    def _open(self, path):
        if not os.path.isfile(path):
            raise DataError("Data source does not exist.")
        try:
            return open(path, "rb")
        except:
            raise DataError(utils.get_error_message())

    def _get_reader(self, path):
        extension = os.path.splitext(path.lower())[-1][1:]
        try:
            return READERS[extension]()
        except KeyError:
            raise DataError("Unsupported file format '%s'." % extension)

    def start_table(self, header):
        self._current_populator.populate()
        table = self._datafile.start_table(DataRow(header).all)
        self._current_populator = self._populators[table.type](table) if table is not None else NullPopulator()
        return bool(self._current_populator)

    def eof(self):
        self._current_populator.populate()

    def add(self, row):
        if PROCESS_CURDIR and self._curdir:
            row = self._replace_curdirs_in(row)
        data = DataRow(row)
        if data:
            self._current_populator.add(data)

    def _replace_curdirs_in(self, row):
        return [cell.replace("${CURDIR}", self._curdir) for cell in row]
Esempio n. 2
0
class FromFilePopulator(object):
    _populators = {'setting': SettingTablePopulator,
                  'variable': VariableTablePopulator,
                  'testcase': TestTablePopulator,
                  'keyword': KeywordTablePopulator}

    def __init__(self, datafile):
        self._datafile = datafile
        self._current_populator = NullPopulator()
        self._curdir = self._get_curdir(datafile.directory)

    def _get_curdir(self, path):
        return path.replace('\\','\\\\') if path else None

    def populate(self, path):
        LOGGER.info("Parsing file '%s'." % path)
        source = self._open(path)
        try:
            self._get_reader(path).read(source, self)
        except:
            raise DataError(utils.get_error_message())
        finally:
            source.close()

    def _open(self, path):
        if not os.path.isfile(path):
            raise DataError("Data source does not exist.")
        try:
            return open(path, 'rb')
        except:
            raise DataError(utils.get_error_message())

    def _get_reader(self, path):
        extension = os.path.splitext(path.lower())[-1][1:]
        try:
            return READERS[extension]()
        except KeyError:
            raise DataError("Unsupported file format '%s'." % extension)

    def start_table(self, header):
        self._current_populator.populate()
        table = self._datafile.start_table(DataRow(header).all)
        self._current_populator = self._populators[table.type](table) \
                                    if table is not None else NullPopulator()
        return bool(self._current_populator)

    def eof(self):
        self._current_populator.populate()

    def add(self, row):
        if PROCESS_CURDIR and self._curdir:
            row = self._replace_curdirs_in(row)
        data = DataRow(row)
        if data:
            self._current_populator.add(data)

    def _replace_curdirs_in(self, row):
        return [cell.replace('${CURDIR}', self._curdir) for cell in row]
Esempio n. 3
0
 def __init__(self, datafile):
     self._datafile = datafile
     self._current_populator = NullPopulator()
     self._curdir = self._get_curdir(datafile.directory)
Esempio n. 4
0
 def start_table(self, header):
     self._current_populator.populate()
     table = self._datafile.start_table(DataRow(header).all)
     self._current_populator = self._populators[table.type](table) \
                                 if table is not None else NullPopulator()
     return bool(self._current_populator)
Esempio n. 5
0
 def __init__(self, datafile):
     self._datafile = datafile
     self._current_populator = NullPopulator()
     self._curdir = self._get_curdir(datafile.directory)