PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/xml/etree/ElementPath.py

https://gitlab.com/abhi1tb/build
Python | 405 lines | 345 code | 5 blank | 55 comment | 0 complexity | 06bbfd7d8dea4a10c59da1318face4c0 MD5 | raw file
  1. #
  2. # ElementTree
  3. # $Id: ElementPath.py 3375 2008-02-13 08:05:08Z fredrik $
  4. #
  5. # limited xpath support for element trees
  6. #
  7. # history:
  8. # 2003-05-23 fl created
  9. # 2003-05-28 fl added support for // etc
  10. # 2003-08-27 fl fixed parsing of periods in element names
  11. # 2007-09-10 fl new selection engine
  12. # 2007-09-12 fl fixed parent selector
  13. # 2007-09-13 fl added iterfind; changed findall to return a list
  14. # 2007-11-30 fl added namespaces support
  15. # 2009-10-30 fl added child element value filter
  16. #
  17. # Copyright (c) 2003-2009 by Fredrik Lundh. All rights reserved.
  18. #
  19. # fredrik@pythonware.com
  20. # http://www.pythonware.com
  21. #
  22. # --------------------------------------------------------------------
  23. # The ElementTree toolkit is
  24. #
  25. # Copyright (c) 1999-2009 by Fredrik Lundh
  26. #
  27. # By obtaining, using, and/or copying this software and/or its
  28. # associated documentation, you agree that you have read, understood,
  29. # and will comply with the following terms and conditions:
  30. #
  31. # Permission to use, copy, modify, and distribute this software and
  32. # its associated documentation for any purpose and without fee is
  33. # hereby granted, provided that the above copyright notice appears in
  34. # all copies, and that both that copyright notice and this permission
  35. # notice appear in supporting documentation, and that the name of
  36. # Secret Labs AB or the author not be used in advertising or publicity
  37. # pertaining to distribution of the software without specific, written
  38. # prior permission.
  39. #
  40. # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  41. # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
  42. # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
  43. # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  44. # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  45. # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  46. # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  47. # OF THIS SOFTWARE.
  48. # --------------------------------------------------------------------
  49. # Licensed to PSF under a Contributor Agreement.
  50. # See http://www.python.org/psf/license for licensing details.
  51. ##
  52. # Implementation module for XPath support. There's usually no reason
  53. # to import this module directly; the <b>ElementTree</b> does this for
  54. # you, if needed.
  55. ##
  56. import re
  57. xpath_tokenizer_re = re.compile(
  58. r"("
  59. r"'[^']*'|\"[^\"]*\"|"
  60. r"::|"
  61. r"//?|"
  62. r"\.\.|"
  63. r"\(\)|"
  64. r"[/.*:\[\]\(\)@=])|"
  65. r"((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
  66. r"\s+"
  67. )
  68. def xpath_tokenizer(pattern, namespaces=None):
  69. default_namespace = namespaces.get('') if namespaces else None
  70. parsing_attribute = False
  71. for token in xpath_tokenizer_re.findall(pattern):
  72. ttype, tag = token
  73. if tag and tag[0] != "{":
  74. if ":" in tag:
  75. prefix, uri = tag.split(":", 1)
  76. try:
  77. if not namespaces:
  78. raise KeyError
  79. yield ttype, "{%s}%s" % (namespaces[prefix], uri)
  80. except KeyError:
  81. raise SyntaxError("prefix %r not found in prefix map" % prefix) from None
  82. elif default_namespace and not parsing_attribute:
  83. yield ttype, "{%s}%s" % (default_namespace, tag)
  84. else:
  85. yield token
  86. parsing_attribute = False
  87. else:
  88. yield token
  89. parsing_attribute = ttype == '@'
  90. def get_parent_map(context):
  91. parent_map = context.parent_map
  92. if parent_map is None:
  93. context.parent_map = parent_map = {}
  94. for p in context.root.iter():
  95. for e in p:
  96. parent_map[e] = p
  97. return parent_map
  98. def _is_wildcard_tag(tag):
  99. return tag[:3] == '{*}' or tag[-2:] == '}*'
  100. def _prepare_tag(tag):
  101. _isinstance, _str = isinstance, str
  102. if tag == '{*}*':
  103. # Same as '*', but no comments or processing instructions.
  104. # It can be a surprise that '*' includes those, but there is no
  105. # justification for '{*}*' doing the same.
  106. def select(context, result):
  107. for elem in result:
  108. if _isinstance(elem.tag, _str):
  109. yield elem
  110. elif tag == '{}*':
  111. # Any tag that is not in a namespace.
  112. def select(context, result):
  113. for elem in result:
  114. el_tag = elem.tag
  115. if _isinstance(el_tag, _str) and el_tag[0] != '{':
  116. yield elem
  117. elif tag[:3] == '{*}':
  118. # The tag in any (or no) namespace.
  119. suffix = tag[2:] # '}name'
  120. no_ns = slice(-len(suffix), None)
  121. tag = tag[3:]
  122. def select(context, result):
  123. for elem in result:
  124. el_tag = elem.tag
  125. if el_tag == tag or _isinstance(el_tag, _str) and el_tag[no_ns] == suffix:
  126. yield elem
  127. elif tag[-2:] == '}*':
  128. # Any tag in the given namespace.
  129. ns = tag[:-1]
  130. ns_only = slice(None, len(ns))
  131. def select(context, result):
  132. for elem in result:
  133. el_tag = elem.tag
  134. if _isinstance(el_tag, _str) and el_tag[ns_only] == ns:
  135. yield elem
  136. else:
  137. raise RuntimeError(f"internal parser error, got {tag}")
  138. return select
  139. def prepare_child(next, token):
  140. tag = token[1]
  141. if _is_wildcard_tag(tag):
  142. select_tag = _prepare_tag(tag)
  143. def select(context, result):
  144. def select_child(result):
  145. for elem in result:
  146. yield from elem
  147. return select_tag(context, select_child(result))
  148. else:
  149. if tag[:2] == '{}':
  150. tag = tag[2:] # '{}tag' == 'tag'
  151. def select(context, result):
  152. for elem in result:
  153. for e in elem:
  154. if e.tag == tag:
  155. yield e
  156. return select
  157. def prepare_star(next, token):
  158. def select(context, result):
  159. for elem in result:
  160. yield from elem
  161. return select
  162. def prepare_self(next, token):
  163. def select(context, result):
  164. yield from result
  165. return select
  166. def prepare_descendant(next, token):
  167. try:
  168. token = next()
  169. except StopIteration:
  170. return
  171. if token[0] == "*":
  172. tag = "*"
  173. elif not token[0]:
  174. tag = token[1]
  175. else:
  176. raise SyntaxError("invalid descendant")
  177. if _is_wildcard_tag(tag):
  178. select_tag = _prepare_tag(tag)
  179. def select(context, result):
  180. def select_child(result):
  181. for elem in result:
  182. for e in elem.iter():
  183. if e is not elem:
  184. yield e
  185. return select_tag(context, select_child(result))
  186. else:
  187. if tag[:2] == '{}':
  188. tag = tag[2:] # '{}tag' == 'tag'
  189. def select(context, result):
  190. for elem in result:
  191. for e in elem.iter(tag):
  192. if e is not elem:
  193. yield e
  194. return select
  195. def prepare_parent(next, token):
  196. def select(context, result):
  197. # FIXME: raise error if .. is applied at toplevel?
  198. parent_map = get_parent_map(context)
  199. result_map = {}
  200. for elem in result:
  201. if elem in parent_map:
  202. parent = parent_map[elem]
  203. if parent not in result_map:
  204. result_map[parent] = None
  205. yield parent
  206. return select
  207. def prepare_predicate(next, token):
  208. # FIXME: replace with real parser!!! refs:
  209. # http://effbot.org/zone/simple-iterator-parser.htm
  210. # http://javascript.crockford.com/tdop/tdop.html
  211. signature = []
  212. predicate = []
  213. while 1:
  214. try:
  215. token = next()
  216. except StopIteration:
  217. return
  218. if token[0] == "]":
  219. break
  220. if token == ('', ''):
  221. # ignore whitespace
  222. continue
  223. if token[0] and token[0][:1] in "'\"":
  224. token = "'", token[0][1:-1]
  225. signature.append(token[0] or "-")
  226. predicate.append(token[1])
  227. signature = "".join(signature)
  228. # use signature to determine predicate type
  229. if signature == "@-":
  230. # [@attribute] predicate
  231. key = predicate[1]
  232. def select(context, result):
  233. for elem in result:
  234. if elem.get(key) is not None:
  235. yield elem
  236. return select
  237. if signature == "@-='":
  238. # [@attribute='value']
  239. key = predicate[1]
  240. value = predicate[-1]
  241. def select(context, result):
  242. for elem in result:
  243. if elem.get(key) == value:
  244. yield elem
  245. return select
  246. if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
  247. # [tag]
  248. tag = predicate[0]
  249. def select(context, result):
  250. for elem in result:
  251. if elem.find(tag) is not None:
  252. yield elem
  253. return select
  254. if signature == ".='" or (signature == "-='" and not re.match(r"\-?\d+$", predicate[0])):
  255. # [.='value'] or [tag='value']
  256. tag = predicate[0]
  257. value = predicate[-1]
  258. if tag:
  259. def select(context, result):
  260. for elem in result:
  261. for e in elem.findall(tag):
  262. if "".join(e.itertext()) == value:
  263. yield elem
  264. break
  265. else:
  266. def select(context, result):
  267. for elem in result:
  268. if "".join(elem.itertext()) == value:
  269. yield elem
  270. return select
  271. if signature == "-" or signature == "-()" or signature == "-()-":
  272. # [index] or [last()] or [last()-index]
  273. if signature == "-":
  274. # [index]
  275. index = int(predicate[0]) - 1
  276. if index < 0:
  277. raise SyntaxError("XPath position >= 1 expected")
  278. else:
  279. if predicate[0] != "last":
  280. raise SyntaxError("unsupported function")
  281. if signature == "-()-":
  282. try:
  283. index = int(predicate[2]) - 1
  284. except ValueError:
  285. raise SyntaxError("unsupported expression")
  286. if index > -2:
  287. raise SyntaxError("XPath offset from last() must be negative")
  288. else:
  289. index = -1
  290. def select(context, result):
  291. parent_map = get_parent_map(context)
  292. for elem in result:
  293. try:
  294. parent = parent_map[elem]
  295. # FIXME: what if the selector is "*" ?
  296. elems = list(parent.findall(elem.tag))
  297. if elems[index] is elem:
  298. yield elem
  299. except (IndexError, KeyError):
  300. pass
  301. return select
  302. raise SyntaxError("invalid predicate")
  303. ops = {
  304. "": prepare_child,
  305. "*": prepare_star,
  306. ".": prepare_self,
  307. "..": prepare_parent,
  308. "//": prepare_descendant,
  309. "[": prepare_predicate,
  310. }
  311. _cache = {}
  312. class _SelectorContext:
  313. parent_map = None
  314. def __init__(self, root):
  315. self.root = root
  316. # --------------------------------------------------------------------
  317. ##
  318. # Generate all matching objects.
  319. def iterfind(elem, path, namespaces=None):
  320. # compile selector pattern
  321. if path[-1:] == "/":
  322. path = path + "*" # implicit all (FIXME: keep this?)
  323. cache_key = (path,)
  324. if namespaces:
  325. cache_key += tuple(sorted(namespaces.items()))
  326. try:
  327. selector = _cache[cache_key]
  328. except KeyError:
  329. if len(_cache) > 100:
  330. _cache.clear()
  331. if path[:1] == "/":
  332. raise SyntaxError("cannot use absolute path on element")
  333. next = iter(xpath_tokenizer(path, namespaces)).__next__
  334. try:
  335. token = next()
  336. except StopIteration:
  337. return
  338. selector = []
  339. while 1:
  340. try:
  341. selector.append(ops[token[0]](next, token))
  342. except StopIteration:
  343. raise SyntaxError("invalid path") from None
  344. try:
  345. token = next()
  346. if token[0] == "/":
  347. token = next()
  348. except StopIteration:
  349. break
  350. _cache[cache_key] = selector
  351. # execute selector pattern
  352. result = [elem]
  353. context = _SelectorContext(elem)
  354. for select in selector:
  355. result = select(context, result)
  356. return result
  357. ##
  358. # Find first matching object.
  359. def find(elem, path, namespaces=None):
  360. return next(iterfind(elem, path, namespaces), None)
  361. ##
  362. # Find all matching objects.
  363. def findall(elem, path, namespaces=None):
  364. return list(iterfind(elem, path, namespaces))
  365. ##
  366. # Find text for first matching object.
  367. def findtext(elem, path, default=None, namespaces=None):
  368. try:
  369. elem = next(iterfind(elem, path, namespaces))
  370. return elem.text or ""
  371. except StopIteration:
  372. return default