Guest User

Untitled

a guest
Sep 13th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. (Pdb) print(indexer.query("skip_chars func"))
  2. return False
  3.  
  4. class NestedDict:
  5. def __init__(self) -> None:
  6. # The parsed content of the TOML document
  7. self.dict: Dict[str, Any] = {}
  8.  
  9. def get_or_create_nest(
  10. self,
  11. key: Key,
  12. *,
  13. access_lists: bool = True,
  14. ) -> dict:
  15. cont: Any = self.dict
  16. for k in key:
  17. if k not in cont:
  18. cont[k] = {}
  19. cont = cont[k]
  20. if access_lists and isinstance(cont, list):
  21. cont = cont[-1]
  22. if not isinstance(cont, dict):
  23. raise KeyError("There is no nest behind this key")
  24. return cont
  25.  
  26. def append_nest_to_list(self, key: Key) -> None:
  27. cont = self.get_or_create_nest(key[:-1])
  28. last_key = key[-1]
  29. if last_key in cont:
  30. list_ = cont[last_key]
  31. try:
  32. list_.append({})
  33. except AttributeError:
  34. raise KeyError("An object other than list found behind this key")
  35. else:
  36. cont[last_key] = [{}]
  37.  
  38.  
  39. class Output(NamedTuple):
  40. data: NestedDict
  41. flags: Flags
  42.  
  43. def skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
  44. try:
  45. while src[pos] in chars:
  46. pos += 1
  47. except IndexError:
  48. pass
  49. return pos
  50.  
  51. while True:
  52. # 1. Skip line leading whitespace
  53.  
  54. escape_id = src[pos : pos + 2]
  55. pos += 2
  56. if multiline and escape_id in {"\\ ", "\\\t", "\\\n"}:
  57. # Skip whitespace until next non-whitespace character or end of
  58. # the doc. Error if non-whitespace is found before newline.
  59. if escape_id != "\\\n":
  60. pos = skip_chars(src, pos, TOML_WS)
  61. try:
  62. char = src[pos]
  63. except IndexError:
  64. return pos, ""
  65. if char != "\n":
  66. raise suffixed_err(src, pos, 'Unescaped "\\" in a string')
  67. pos += 1
  68. pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
  69. return pos, ""
  70. if escape_id == "\\u":
  71. return parse_hex_char(src, pos, 4)
  72. if escape_id == "\\U":
  73. return parse_hex_char(src, pos, 8)
  74. try:
  75. return pos, BASIC_STR_ESCAPE_REPLACEMENTS[escape_id]
  76. except KeyError:
  77. if len(escape_id) != 2:
  78. raise suffixed_err(src, pos, "Unterminated string") from None
  79.  
  80. pos = skip_chars(src, pos, TOML_WS)
  81.  
  82. # 2. Parse rules. Expect one of the following:
  83. # - end of file
  84. # - end of line
  85. # - comment
  86. # - key/value pair
  87. # - append dict to list (and move to its namespace)
  88. # - create dict (and move to its namespace)
  89. # Skip trailing whitespace when applicable.
  90. try:
  91. char = src[pos]
  92. except IndexError:
  93. break
  94. if char == "\n":
  95. pos += 1
  96. continue
  97. if char in KEY_INITIAL_CHARS:
  98. pos = key_value_rule(src, pos, out, header, parse_float)
  99. pos = skip_chars(src, pos, TOML_WS)
  100. elif char == "[":
  101. try:
  102. second_char: Optional[str] = src[pos + 1]
  103. except IndexError:
  104. second_char = None
  105. if second_char == "[":
  106. pos, header = create_list_rule(src, pos, out)
  107. else:
  108. pos, header = create_dict_rule(src, pos, out)
  109. pos = skip_chars(src, pos, TOML_WS)
  110. elif char != "#":
  111. raise suffixed_err(src, pos, "Invalid statement")
  112.  
  113. # 3. Skip comment
  114. pos = skip_comment(src, pos)
  115.  
  116. # 4. Expect end of line or end of file
Add Comment
Please, Sign In to add comment