Skip to content

Commit dc30919

Browse files
committed
use constants
1 parent fb82f44 commit dc30919

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

httpie/cli/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,9 @@ class RequestType(enum.Enum):
125125
FORM = enum.auto()
126126
MULTIPART = enum.auto()
127127
JSON = enum.auto()
128+
129+
130+
OPEN_BRACKET = '['
131+
CLOSE_BRACKET = ']'
132+
BACKSLASH = '\\'
133+
HIGHLIGHTER = '^'

httpie/cli/nested_json.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Type,
1010
Union,
1111
)
12+
from httpie.cli.constants import OPEN_BRACKET, CLOSE_BRACKET, BACKSLASH, HIGHLIGHTER
1213

1314

1415
class HTTPieSyntaxError(ValueError):
@@ -30,7 +31,7 @@ def __str__(self):
3031
lines.append(self.source)
3132
lines.append(
3233
' ' * (self.token.start)
33-
+ '^' * (self.token.end - self.token.start)
34+
+ HIGHLIGHTER * (self.token.end - self.token.start)
3435
)
3536
return '\n'.join(lines)
3637

@@ -49,8 +50,8 @@ def to_name(self) -> str:
4950
return 'a ' + self.name.lower()
5051

5152

52-
OPERATORS = {'[': TokenKind.LEFT_BRACKET, ']': TokenKind.RIGHT_BRACKET}
53-
SPECIAL_CHARS = OPERATORS.keys() | {'\\'}
53+
OPERATORS = {OPEN_BRACKET: TokenKind.LEFT_BRACKET, CLOSE_BRACKET: TokenKind.RIGHT_BRACKET}
54+
SPECIAL_CHARS = OPERATORS.keys() | {BACKSLASH}
5455

5556

5657
class Token(NamedTuple):
@@ -65,7 +66,7 @@ def assert_cant_happen() -> NoReturn:
6566

6667

6768
def check_escaped_int(value: str) -> str:
68-
if not value.startswith('\\'):
69+
if not value.startswith(BACKSLASH):
6970
raise ValueError('Not an escaped int')
7071

7172
try:
@@ -114,7 +115,7 @@ def can_advance() -> bool:
114115
if index in OPERATORS:
115116
yield from send_buffer()
116117
yield Token(OPERATORS[index], index, cursor, cursor + 1)
117-
elif index == '\\' and can_advance():
118+
elif index == BACKSLASH and can_advance():
118119
if source[cursor + 1] in SPECIAL_CHARS:
119120
backslashes += 1
120121
else:
@@ -159,11 +160,11 @@ def reconstruct(self) -> str:
159160
if self.kind is PathAction.KEY:
160161
if self.is_root:
161162
return str(self.accessor)
162-
return '[' + self.accessor + ']'
163+
return OPEN_BRACKET + self.accessor + CLOSE_BRACKET
163164
elif self.kind is PathAction.INDEX:
164-
return '[' + str(self.accessor) + ']'
165+
return OPEN_BRACKET + str(self.accessor) + CLOSE_BRACKET
165166
elif self.kind is PathAction.APPEND:
166-
return '[]'
167+
return OPEN_BRACKET + CLOSE_BRACKET
167168
else:
168169
assert_cant_happen()
169170

0 commit comments

Comments
 (0)