def __init__(self, initial: int = 0) -> None:
self._level = initial
- def __int__(self) -> int:
- return self._level
-
def __repr__(self) -> str:
return "{}({:d})".format(type(self).__name__, self._level)
"""Return the current indentation as a string of spaces."""
return ' ' * self._level
- def __bool__(self) -> bool:
- """True when there is a non-zero indentation."""
- return bool(self._level)
-
def increase(self, amount: int = 4) -> None:
"""Increase the indentation level by ``amount``, default 4."""
self._level += amount
def decrease(self, amount: int = 4) -> None:
"""Decrease the indentation level by ``amount``, default 4."""
- if self._level < amount:
- raise ArithmeticError(
- f"Can't remove {amount:d} spaces from {self!r}")
+ assert amount <= self._level
self._level -= amount
Obey `indent`, and strip `EATSPACE`.
"""
raw = code % kwds
- if indent:
- raw = re.sub(r'^(?!(#|$))', str(indent), raw, flags=re.MULTILINE)
+ pfx = str(indent)
+ if pfx:
+ raw = re.sub(r'^(?!(#|$))', pfx, raw, flags=re.MULTILINE)
return re.sub(re.escape(EATSPACE) + r' *', '', raw)
cond_fmt: str, not_fmt: str,
all_operator: str, any_operator: str) -> str:
- def do_gen(ifcond: Union[str, Dict[str, Any]], need_parens: bool):
+ def do_gen(ifcond: Union[str, Dict[str, Any]],
+ need_parens: bool) -> str:
if isinstance(ifcond, str):
return cond_fmt % ifcond
assert isinstance(ifcond, dict) and len(ifcond) == 1
info,
"'if' condition of %s has conflicting keys" % source)
- oper, operands = next(iter(cond.items()))
+ if 'not' in cond:
+ _check_if(cond['not'])
+ elif 'all' in cond:
+ _check_infix('all', cond['all'])
+ else:
+ _check_infix('any', cond['any'])
+
+ def _check_infix(operator: str, operands: object) -> None:
+ if not isinstance(operands, list):
+ raise QAPISemError(
+ info,
+ "'%s' condition of %s must be an array"
+ % (operator, source))
if not operands:
raise QAPISemError(
info, "'if' condition [] of %s is useless" % source)
-
- if oper == "not":
- _check_if(operands)
- return
- if oper in ("all", "any") and not isinstance(operands, list):
- raise QAPISemError(
- info, "'%s' condition of %s must be an array" % (oper, source))
for operand in operands:
_check_if(operand)