omlish 0.0.0.dev202__py3-none-any.whl → 0.0.0.dev203__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev202'
2
- __revision__ = '3da244e2a7f1e04753682c9f4ddc813289a82a42'
1
+ __version__ = '0.0.0.dev203'
2
+ __revision__ = '8499b6c7847c312b26968efc48f599be879b4f3a'
3
3
 
4
4
 
5
5
  #
omlish/formats/xml.py CHANGED
@@ -18,6 +18,49 @@ else:
18
18
  ##
19
19
 
20
20
 
21
+ def strip_ns(tag: str) -> str:
22
+ # It really do just be like this:
23
+ # https://github.com/python/cpython/blob/ff3bc82f7c9882c27aad597aac79355da7257186/Lib/xml/etree/ElementTree.py#L803-L804
24
+ if tag[:1] == '{':
25
+ _, tag = tag[1:].rsplit('}', 1)
26
+ return tag
27
+
28
+
29
+ ##
30
+
31
+
32
+ ITER_PARSE_EVENTS = ('start', 'end', 'comment', 'pi', 'start-ns', 'end-ns')
33
+
34
+
35
+ def yield_root_children(
36
+ source: ta.Any,
37
+ *,
38
+ retain_on_root: bool = False,
39
+ **kwargs: ta.Any,
40
+ ) -> ta.Iterator['ET.Element']:
41
+ it = iter(ET.iterparse(source, ('start', 'end'), **kwargs))
42
+
43
+ ev, root = next(it)
44
+ if ev != 'start':
45
+ raise RuntimeError(ev)
46
+ yield root
47
+
48
+ depth = 0
49
+ for ev, el in it:
50
+ if ev == 'start':
51
+ depth += 1
52
+
53
+ elif ev == 'end':
54
+ depth -= 1
55
+ if not depth:
56
+ if not retain_on_root:
57
+ root.remove(el)
58
+ yield el
59
+
60
+
61
+ ##
62
+
63
+
21
64
  @dc.dataclass(frozen=True)
22
65
  class SimpleElement:
23
66
  tag: str
@@ -36,27 +79,38 @@ class SimpleElement:
36
79
  return dct
37
80
 
38
81
 
39
- def build_simple_element(element: 'ET.Element') -> SimpleElement:
40
- atts = {}
41
- for name, value in element.attrib.items():
42
- atts[name] = value # noqa
82
+ def build_simple_element(
83
+ element: 'ET.Element',
84
+ *,
85
+ strip_tag_ns: bool = False,
86
+ ) -> SimpleElement:
87
+ def rec(cur: 'ET.Element') -> SimpleElement:
88
+ atts = {}
89
+ for name, value in cur.attrib.items():
90
+ atts[name] = value # noqa
43
91
 
44
- body: list[SimpleElement | str] = []
92
+ body: list[SimpleElement | str] = []
45
93
 
46
- if element.text and (s := element.text.strip()):
47
- body.append(s)
94
+ if cur.text and (s := cur.text.strip()):
95
+ body.append(s)
48
96
 
49
- for child in element:
50
- body.append(build_simple_element(child))
97
+ for child in cur:
98
+ body.append(rec(child))
51
99
 
52
- if child.tail and (s := child.tail.strip()):
53
- body.append(s)
100
+ if child.tail and (s := child.tail.strip()):
101
+ body.append(s)
102
+
103
+ tag = cur.tag
104
+ if strip_tag_ns:
105
+ tag = strip_ns(tag)
106
+
107
+ return SimpleElement(
108
+ tag,
109
+ atts,
110
+ body,
111
+ )
54
112
 
55
- return SimpleElement(
56
- element.tag,
57
- atts,
58
- body,
59
- )
113
+ return rec(element)
60
114
 
61
115
 
62
116
  def parse_tree(s: str) -> 'ET.ElementTree':
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: omlish
3
- Version: 0.0.0.dev202
3
+ Version: 0.0.0.dev203
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=dyIpveH7Z8OnQp2pTn6NVv7LCDXVrozJWAzbk8PBavg,7950
2
- omlish/__about__.py,sha256=TkrqZrE-0H4UPhtpzOoT1oElyBJWd2jFOEJucdouLTY,3409
2
+ omlish/__about__.py,sha256=5uwjOU7ezPs6bQf_eJwOpEl8jPeW9UzKUtjquPH_WN4,3409
3
3
  omlish/__init__.py,sha256=SsyiITTuK0v74XpKV8dqNaCmjOlan1JZKrHQv5rWKPA,253
4
4
  omlish/c3.py,sha256=ubu7lHwss5V4UznbejAI0qXhXahrU01MysuHOZI9C4U,8116
5
5
  omlish/cached.py,sha256=UI-XTFBwA6YXWJJJeBn-WkwBkfzDjLBBaZf4nIJA9y0,510
@@ -228,7 +228,7 @@ omlish/formats/json5.py,sha256=odpZIShlUpv19aACWe58SoMPcv0AHKIa6zSMjlKgaMI,515
228
228
  omlish/formats/pickle.py,sha256=jdp4E9WH9qVPBE3sSqbqDtUo18RbTSIiSpSzJ-IEVZw,529
229
229
  omlish/formats/props.py,sha256=auCv-Jx79KGlWfyG1-Qo0ou-Ex0W_mF3r_lDFdsVkWI,18920
230
230
  omlish/formats/repr.py,sha256=kYrNs4o-ji8nOdp6u_L3aMgBMWN1ZAZJSAWgQQfStSQ,414
231
- omlish/formats/xml.py,sha256=ggiOwSERt4d9XmZwLZiDIh5qnFJS4jdmow9m9_9USps,1491
231
+ omlish/formats/xml.py,sha256=rKXN1RDJV-jliM4fZ2NtmsujB5cAGQBMzRHRHEnH9Ko,2661
232
232
  omlish/formats/yaml.py,sha256=ffOwGnLA6chdiFyaS7X0TBMGmHG9AoGudzKVWfQ1UOs,7389
233
233
  omlish/formats/ini/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
234
234
  omlish/formats/ini/codec.py,sha256=omuFg0kiDksv8rRlWd_v32ebzEcKlgmiPgGID3bRi2M,631
@@ -609,9 +609,9 @@ omlish/text/indent.py,sha256=YjtJEBYWuk8--b9JU_T6q4yxV85_TR7VEVr5ViRCFwk,1336
609
609
  omlish/text/minja.py,sha256=jZC-fp3Xuhx48ppqsf2Sf1pHbC0t8XBB7UpUUoOk2Qw,5751
610
610
  omlish/text/parts.py,sha256=7vPF1aTZdvLVYJ4EwBZVzRSy8XB3YqPd7JwEnNGGAOo,6495
611
611
  omlish/text/random.py,sha256=jNWpqiaKjKyTdMXC-pWAsSC10AAP-cmRRPVhm59ZWLk,194
612
- omlish-0.0.0.dev202.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
613
- omlish-0.0.0.dev202.dist-info/METADATA,sha256=XEiYKss22Lx_Cerj_g1QURXuSPir4iCoi-tiPex6nik,4264
614
- omlish-0.0.0.dev202.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
615
- omlish-0.0.0.dev202.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
616
- omlish-0.0.0.dev202.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
617
- omlish-0.0.0.dev202.dist-info/RECORD,,
612
+ omlish-0.0.0.dev203.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
613
+ omlish-0.0.0.dev203.dist-info/METADATA,sha256=21p9UObqpEGCyCoTCTdNVLdZk3MYwxkiG8uZAVs4U5Y,4264
614
+ omlish-0.0.0.dev203.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
615
+ omlish-0.0.0.dev203.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
616
+ omlish-0.0.0.dev203.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
617
+ omlish-0.0.0.dev203.dist-info/RECORD,,