From 37fa67cd3c3ea6b248209cca9e31702ab63c98c1 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Tue, 17 Sep 2019 09:07:12 +0200 Subject: [PATCH] Runnable KeyValueArgType.tokenize doctest --- httpie/cli/argtypes.py | 52 ++++++++++++++++++++------------------ httpie/cli/requestitems.py | 2 +- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/httpie/cli/argtypes.py b/httpie/cli/argtypes.py index 803e066c..849503e5 100644 --- a/httpie/cli/argtypes.py +++ b/httpie/cli/argtypes.py @@ -2,6 +2,7 @@ import argparse import getpass import os import sys +from typing import Union, List from httpie.cli.constants import SEPARATOR_CREDENTIALS from httpie.sessions import VALID_SESSION_NAME_PATTERN @@ -39,11 +40,14 @@ class SessionNameValidator: class Escaped(str): """Represents an escaped character.""" + def __repr__(self): + return f"Escaped({repr(str(self))})" + class KeyValueArgType: """A key-value pair argument type used with `argparse`. - Parses a key-value arg and constructs a `KeyValuArge` instance. + Parses a key-value arg and constructs a `KeyValueArg` instance. Used for headers, form data, and other key-value pair types. """ @@ -65,29 +69,7 @@ class KeyValueArgType: as well (r'\\'). """ - - def tokenize(string): - r"""Tokenize `string`. There are only two token types - strings - and escaped characters: - - tokenize(r'foo\=bar\\baz') - => ['foo', Escaped('='), 'bar', Escaped('\\'), 'baz'] - - """ - tokens = [''] - characters = iter(string) - for char in characters: - if char == '\\': - char = next(characters, '') - if char not in self.special_characters: - tokens[-1] += '\\' + char - else: - tokens.extend([Escaped(char), '']) - else: - tokens[-1] += char - return tokens - - tokens = tokenize(string) + tokens = self.tokenize(string) # Sorting by length ensures that the longest one will be # chosen as it will overwrite any shorter ones starting @@ -126,6 +108,28 @@ class KeyValueArgType: return self.key_value_class( key=key, value=value, sep=sep, orig=string) + def tokenize(self, s: str) -> List[Union[str, Escaped]]: + r"""Tokenize the raw arg string + + There are only two token types - strings and escaped characters: + + >>> KeyValueArgType('=').tokenize(r'foo\=bar\\baz') + ['foo', Escaped('='), 'bar', Escaped('\\'), 'baz'] + + """ + tokens = [''] + characters = iter(s) + for char in characters: + if char == '\\': + char = next(characters, '') + if char not in self.special_characters: + tokens[-1] += '\\' + char + else: + tokens.extend([Escaped(char), '']) + else: + tokens[-1] += char + return tokens + class AuthCredentials(KeyValueArg): """Represents parsed credentials.""" diff --git a/httpie/cli/requestitems.py b/httpie/cli/requestitems.py index c0293bf9..1a0116fe 100644 --- a/httpie/cli/requestitems.py +++ b/httpie/cli/requestitems.py @@ -140,7 +140,7 @@ def process_data_raw_json_embed_arg(arg: KeyValueArg) -> JSONType: return value -def load_text_file(item) -> str: +def load_text_file(item: KeyValueArg) -> str: path = item.value try: with open(os.path.expanduser(path), 'rb') as f: