def load_text_file(item: KeyValueArg) -> str:
    path = item.value
    try:
        with open(os.path.expanduser(path), 'rb') as f:
            return f.read().decode()
    except IOError as e:
        raise ParseError('"%s": %s' % (item.orig, e))
    except UnicodeDecodeError:
        raise ParseError('"%s": cannot embed the content of "%s",'
                         ' not a UTF8 or ASCII-encoded text file' %
                         (item.orig, item.value))
Пример #2
0
def parse_file_item_chunked(arg: KeyValueArg):
    fn = arg.value
    try:
        f = open(os.path.expanduser(fn), 'rb')
    except IOError as e:
        raise ParseError('"%s": %s' % (arg.orig, e))
    return os.path.basename(fn), f, get_content_type(fn)
Пример #3
0
def process_empty_header_arg(arg: KeyValueArg) -> str:
    if arg.value:
        raise ParseError(
            'Invalid item "%s" '
            '(to specify an empty header use `Header;`)'
            % arg.orig
        )
    return arg.value
def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]:
    filename = arg.value
    try:
        with open(os.path.expanduser(filename), 'rb') as f:
            contents = f.read()
    except IOError as e:
        raise ParseError('"%s": %s' % (arg.orig, e))
    return (
        os.path.basename(filename),
        BytesIO(contents),
        get_content_type(filename),
    )
Пример #5
0
def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]:
    parts = arg.value.split(SEPARATOR_FILE_UPLOAD_TYPE)
    filename = parts[0]
    mime_type = parts[1] if len(parts) > 1 else None
    try:
        f = open(os.path.expanduser(filename), 'rb')
    except IOError as e:
        raise ParseError('"%s": %s' % (arg.orig, e))
    return (
        os.path.basename(filename),
        f,
        mime_type or get_content_type(filename),
    )
def load_json(arg: KeyValueArg, contents: str) -> JSONType:
    try:
        return load_json_preserve_order(contents)
    except ValueError as e:
        raise ParseError('"%s": %s' % (arg.orig, e))