示例#1
0
def read_file_text(filename: Path):
    """return all text contents in line-based array of file or 'None' if not a text file"""
    filename = Path(filename)
    current_app.logger.info('Reading text from file: %s', filename.resolve())
    text_lines = []
    try:
        filename.open()
        text_lines = filename.read_text().splitlines()
        current_app.logger.debug('from %s text_lines[0:1]: %s', filename.name,
                                 str(text_lines[0:1]))
        return text_lines

    except Exception as e:
        current_app.logger.error('Errors reading from file: %s!',
                                 filename.resolve())
        return None
示例#2
0
def make_train_from_yml(file: PosixPath) -> Train:
    try:
        td = yaml.safe_load(file.read_text())
    except yaml.YAMLError as e:
        print(f'Error reading train from {file.name}.')
        raise e

    sections = [__make_section_from_dict(d) for d in td['sections']]
    # Give each sections a unique section id:
    for i in range(0, len(sections)):
        s = sections[i]
        if s.section_id is None:
            s.section_id = i

    result = Train(td['coreID'], sections=sections)
    result.version = td.get('version', 1)
    return result