annet 0.16.24__py3-none-any.whl → 0.16.26__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of annet might be problematic. Click here for more details.

annet/annlib/jsontools.py CHANGED
@@ -4,6 +4,7 @@ import copy
4
4
  import fnmatch
5
5
  import json
6
6
  from collections.abc import Mapping, Sequence
7
+ from operator import itemgetter
7
8
  from typing import Any, Dict, List, Optional
8
9
 
9
10
  import jsonpatch
@@ -73,7 +74,7 @@ def _ensure_pointer_exists(doc: Dict[str, Any], pointer: jsonpointer.JsonPointer
73
74
 
74
75
  def make_patch(old: Dict[str, Any], new: Dict[str, Any]) -> List[Dict[str, Any]]:
75
76
  """Generate a JSON patch by comparing the old document with the new one."""
76
- return jsonpatch.make_patch(old, new).patch
77
+ return sorted(jsonpatch.make_patch(old, new).patch, key=itemgetter("path"))
77
78
 
78
79
 
79
80
  def apply_patch(content: Optional[bytes], patch_bytes: bytes) -> bytes:
annet/annlib/patching.py CHANGED
@@ -54,11 +54,30 @@ class PatchItem:
54
54
  row: str
55
55
  child: "Union[PatchTree, None]"
56
56
  context: Dict[str, str]
57
+ sort_key: Tuple[Any, ...]
57
58
 
58
- def __init__(self, row, child, context):
59
+ def __init__(self, row, child, context, sort_key):
59
60
  self.row = row
60
61
  self.child = child
61
62
  self.context = context
63
+ self.sort_key = sort_key
64
+
65
+ def to_json(self) -> dict[str, Any]:
66
+ return {
67
+ "row": self.row,
68
+ "child": self.child.to_json() if self.child is not None else None,
69
+ "context": self.context,
70
+ "sort_key": self.sort_key,
71
+ }
72
+
73
+ @classmethod
74
+ def from_json(cls, data: dict[str, Any]) -> "PatchItem":
75
+ return cls(
76
+ row=data["row"],
77
+ child=PatchTree.from_json(data["child"]) if data["child"] is not None else None,
78
+ context=data["context"],
79
+ sort_key=data["sort_key"],
80
+ )
62
81
 
63
82
  def __str__(self):
64
83
  return (
@@ -66,6 +85,7 @@ class PatchItem:
66
85
  f' row="{self.row}",\n'
67
86
  f" child={textwrap.indent(str(self.child), ' ').strip()},\n"
68
87
  f" context={self.context}\n"
88
+ f" sort_key={self.sort_key}\n"
69
89
  f")"
70
90
  )
71
91
 
@@ -78,15 +98,15 @@ class PatchTree:
78
98
  if row:
79
99
  self.add(row, {})
80
100
 
81
- def add(self, row: str, context: Dict[str, str]) -> None:
82
- self.itms.append(PatchItem(row, None, context))
101
+ def add(self, row: str, context: Dict[str, str], sort_key=()) -> None:
102
+ self.itms.append(PatchItem(row, None, context, sort_key))
83
103
 
84
- def add_block(self, row: str, subtree: "Optional[PatchTree]" = None, context: Dict[str, str] = None) -> "PatchTree":
104
+ def add_block(self, row: str, subtree: "Optional[PatchTree]" = None, context: Dict[str, str] = None, sort_key=()) -> "PatchTree":
85
105
  if subtree is None:
86
106
  subtree = PatchTree()
87
107
  if context is None:
88
108
  context = {}
89
- self.itms.append(PatchItem(row, subtree, context))
109
+ self.itms.append(PatchItem(row, subtree, context, sort_key))
90
110
  return subtree
91
111
 
92
112
  def items(self) -> "Iterator[Tuple[str, Union[PatchTree, None]]]":
@@ -106,6 +126,21 @@ class PatchTree:
106
126
  ret[str(row)] = None
107
127
  return ret
108
128
 
129
+ def to_json(self) -> list[dict[str, Any]]:
130
+ return [i.to_json() for i in self.itms]
131
+
132
+ @classmethod
133
+ def from_json(cls, data: list[dict[str, Any]]) -> "PatchTree":
134
+ ret = cls()
135
+ ret.itms = [PatchItem.from_json(i) for i in data]
136
+ return ret
137
+
138
+ def sort(self) -> None:
139
+ self.itms.sort(key=operator.attrgetter("sort_key"))
140
+ for item in self.itms:
141
+ if item.child:
142
+ item.child.sort()
143
+
109
144
  def __bool__(self):
110
145
  return bool(self.itms)
111
146
 
@@ -409,18 +444,19 @@ def make_patch(pre, rb, hw, add_comments, orderer=None, _root_pre=None, do_commi
409
444
  "context": attrs["context"],
410
445
  })
411
446
  tree = PatchTree()
412
- sorted_patch = sorted(patch, key=(lambda item: (
413
- (item["order"] if item["order_direct"] else -item["order"]),
414
- item["raw_rule"],
415
- item["order_direct"],
416
- )))
417
- for item in sorted_patch:
447
+ for item in patch:
448
+ sort_key = (
449
+ (item["order"] if item["order_direct"] else -item["order"]),
450
+ item["raw_rule"],
451
+ item["order_direct"],
452
+ )
418
453
  if (not item["children"] and not item["parent"]) or not item["direct"]:
419
- tree.add(item["row"], item["context"])
454
+ tree.add(item["row"], item["context"], sort_key)
420
455
  else:
421
- tree.add_block(item["row"], item["children"], item["context"])
456
+ tree.add_block(item["row"], item["children"], item["context"], sort_key)
422
457
  if item["force_commit"]:
423
- tree.add("commit", item["context"])
458
+ tree.add("commit", item["context"], sort_key)
459
+ tree.sort()
424
460
  return tree
425
461
 
426
462
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: annet
3
- Version: 0.16.24
3
+ Version: 0.16.26
4
4
  Summary: annet
5
5
  Home-page: https://github.com/annetutil/annet
6
6
  License: MIT
@@ -46,10 +46,10 @@ annet/annlib/command.py,sha256=uuBddMQphtn8P5MO5kzIa8_QrtMns-k05VeKv1bcAuA,1043
46
46
  annet/annlib/diff.py,sha256=MZ6eQAU3cadQp8KaSE6uAYFtcfMDCIe_eNuVROnYkCk,4496
47
47
  annet/annlib/errors.py,sha256=jBcSFzY6Vj-FxR__vqjFm-87AwYQ0xHuAopTirii5AU,287
48
48
  annet/annlib/filter_acl.py,sha256=0w1VF6WcONiTYTQh0yWi6_j9rCTc_kMLAUMr0hbdkNU,7203
49
- annet/annlib/jsontools.py,sha256=DVmA6kmKGSCsKrFnag4EMR1WKe39Owlvzpfdo726ufk,5471
49
+ annet/annlib/jsontools.py,sha256=BS7s4rI8R9c_y3zz0zYl1l6con65oQ0MvfsC1dsXZts,5535
50
50
  annet/annlib/lib.py,sha256=eJ4hcVuQ6pdYBzLs4YKCHFtq45idOfZCYp92XfF7_QI,15317
51
51
  annet/annlib/output.py,sha256=_SjJ6G6bejvnTKqNHw6xeio0FT9oO3OIkLaOC3cEga4,7569
52
- annet/annlib/patching.py,sha256=2CpAT3T43IUFJR57qTYSwjQ0smg0uHWN43df4n1WArs,19937
52
+ annet/annlib/patching.py,sha256=p5u3jl3_Iod0CGcnfZsfFR3Izo_roorny_v0stjDCWs,21142
53
53
  annet/annlib/tabparser.py,sha256=Xsje7t2bEZqZ8hhgnEYgjQGaDZ5mBWgNxwE2wpCkwXQ,28961
54
54
  annet/annlib/types.py,sha256=VHU0CBADfYmO0xzB_c5f-mcuU3dUumuJczQnqGlib9M,852
55
55
  annet/annlib/netdev/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -164,10 +164,10 @@ annet_generators/rpl_example/__init__.py,sha256=z4-gsDv06BBpgTwRohc50VBQYFD26QVu
164
164
  annet_generators/rpl_example/items.py,sha256=6x7b0wZ7Vjn6yCaJ-aGbpTHm7fyqO77b-LRqzzhEbh4,615
165
165
  annet_generators/rpl_example/policy_generator.py,sha256=KFCqn347CIPcnllOHfINYeKgNSr6Wl-bdM5Xj_YKhYM,11183
166
166
  annet_generators/rpl_example/route_policy.py,sha256=QjxFjkePHfTo2CpMeRVaDqZXNXLM-gGlE8EocHuOR4Y,1189
167
- annet-0.16.24.dist-info/AUTHORS,sha256=rh3w5P6gEgqmuC-bw-HB68vBCr-yIBFhVL0PG4hguLs,878
168
- annet-0.16.24.dist-info/LICENSE,sha256=yPxl7dno02Pw7gAcFPIFONzx_gapwDoPXsIsh6Y7lC0,1079
169
- annet-0.16.24.dist-info/METADATA,sha256=130Nbd58J6hQQ0ceLhvJwc6qNBRjfTcMVbd0WXEPy8w,728
170
- annet-0.16.24.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
171
- annet-0.16.24.dist-info/entry_points.txt,sha256=5lIaDGlGi3l6QQ2ry2jZaqViP5Lvt8AmsegdD0Uznck,192
172
- annet-0.16.24.dist-info/top_level.txt,sha256=QsoTZBsUtwp_FEcmRwuN8QITBmLOZFqjssRfKilGbP8,23
173
- annet-0.16.24.dist-info/RECORD,,
167
+ annet-0.16.26.dist-info/AUTHORS,sha256=rh3w5P6gEgqmuC-bw-HB68vBCr-yIBFhVL0PG4hguLs,878
168
+ annet-0.16.26.dist-info/LICENSE,sha256=yPxl7dno02Pw7gAcFPIFONzx_gapwDoPXsIsh6Y7lC0,1079
169
+ annet-0.16.26.dist-info/METADATA,sha256=PvcIlUXb9JMjCR24ySbtdw4B3m-0CNowKmQKyh1qh0s,728
170
+ annet-0.16.26.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
171
+ annet-0.16.26.dist-info/entry_points.txt,sha256=5lIaDGlGi3l6QQ2ry2jZaqViP5Lvt8AmsegdD0Uznck,192
172
+ annet-0.16.26.dist-info/top_level.txt,sha256=QsoTZBsUtwp_FEcmRwuN8QITBmLOZFqjssRfKilGbP8,23
173
+ annet-0.16.26.dist-info/RECORD,,