示例#1
0
def is_csv(objects_file: TextIOWrapper, delim: str):
    try:
        csv.Sniffer().sniff(objects_file.read(1024), delimiters=delim)
        return True
    except:
        return False
    finally:
        objects_file.seek(
            0
        )  # need this to move back to the beginning of the file after sampling
示例#2
0
    def from_csv_file(self, file: TextIOWrapper, headers: bool = True):
        """
        Method make object from csv file.

        :param file:
        :param headers:
        :return:
        """
        read_file = file.read()
        splitted_data = read_file.split('\n')
        self.__make_keys_csv(splitted_data[0], headers)
        shift = 1 if headers else 0
        self.__data.extend(
            dict(zip(self.__keys, element.split(',')))
            for element in splitted_data[shift:])
示例#3
0
def read_textfile(textfile: TextIOWrapper) -> str:
    """
    Read the whole file into memory

    :param textfile: open, readable textfile
    :type textfile: TextIOWrapper
    :return: whole text from file
    :rtype: str
    """
    try:
        logger.debug("Reading textfile {}".format(textfile.name))
        return textfile.read()
    except Exception as exc:
        logger.critical("Unexpected error occured.")
        logger.exception(exc)
        raise exc
示例#4
0
def test_issue25862():
    # CPython issue #25862
    # Assertion failures occurred in tell() after read() and write().
    from _io import TextIOWrapper, BytesIO
    t = TextIOWrapper(BytesIO(b'test'), encoding='ascii')
    t.read(1)
    t.read()
    t.tell()
    t = TextIOWrapper(BytesIO(b'test'), encoding='ascii')
    t.read(1)
    t.write('x')
    t.tell()
示例#5
0
def _parse_json_file(file: _io.TextIOWrapper) -> Tuple[Dict[str, Any], List[FileSyntaxError]]:
    """
    Parse a file in the JSON format.

    :param file: TextIOWrapper object of the file that will be processed.
    :type file: _io.TextIOWrapper
    :return: Tuple with mapping of key and list of values and list of syntax errors
    """

    content = file.read()
    if not content:
        return {}, [FileSyntaxError(line_no=1, message="The file is empty.")]
    try:
        secrets = json.loads(content)
    except JSONDecodeError as e:
        return {}, [FileSyntaxError(line_no=int(e.lineno), message=e.msg)]
    if not isinstance(secrets, dict):
        return {}, [FileSyntaxError(line_no=1, message="The file should contain the object.")]

    return secrets, []
示例#6
0
def _parse_yaml_file(file: _io.TextIOWrapper) -> Tuple[Dict[str, List[str]], List[FileSyntaxError]]:
    """
    Parse a file in the YAML format.

    :param file: TextIOWrapper object of the file that will be processed.
    :type file: _io.TextIOWrapper
    :return: Tuple with mapping of key and list of values and list of syntax errors
    """

    content = file.read()
    if not content:
        return {}, [FileSyntaxError(line_no=1, message="The file is empty.")]
    try:
        secrets = yaml.safe_load(content)

    except yaml.MarkedYAMLError as e:
        return {}, [FileSyntaxError(line_no=e.problem_mark.line, message=str(e))]
    if not isinstance(secrets, dict):
        return {}, [FileSyntaxError(line_no=1, message="The file should contain the object.")]

    return secrets, []
示例#7
0
def _parse_env_file(file: _io.TextIOWrapper) -> Tuple[Dict[str, List[str]], List[FileSyntaxError]]:
    """
    Parse a file in the ``.env '' format.

    .. code-block:: text

        MY_CONN_ID=my-conn-type://my-login:my-pa%2Fssword@my-host:5432/my-schema?param1=val1&param2=val2

    :param file: TextIOWrapper object of the file that will be processed.
    :type file: _io.TextIOWrapper
    :return: Tuple with mapping of key and list of values and list of syntax errors
    """

    content = file.read()
    secrets: Dict[str, List[str]] = defaultdict(list)
    errors: List[FileSyntaxError] = []
    for line_no, line in enumerate(content.splitlines(), 1):
        if not line:
            # Ignore empty line
            continue

        if COMMENT_PATTERN.match(line):
            # Ignore comments
            continue

        var_parts: List[str] = line.split("=", 2)
        if len(var_parts) != 2:
            errors.append(
                FileSyntaxError(
                    line_no=line_no,
                    message='Invalid line format. The line should contain at least one equal sign ("=").',
                )
            )
            continue

        key, value = var_parts
        if not key:
            errors.append(FileSyntaxError(line_no=line_no, message="Invalid line format. Key is empty.",))
        secrets[key].append(value)
    return secrets, errors
示例#8
0
def main(name: str, fin: _io.TextIOWrapper) -> tuple:
    if len(fin.read(CHUNK)) == CHUNK:
        fin.close()
        name = f'{name}{datetime.now()}'
        fin = open(name, 'w')
    return name, fin
示例#9
0
 def __init__(self, record: TextIOWrapper):
     self.record = record.read()
     self.setences: List[Setence] = []
     self.read_record()