annet 0.7__py3-none-any.whl → 0.8__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
@@ -87,3 +87,26 @@ def apply_patch(content: Optional[bytes], patch_bytes: bytes) -> bytes:
87
87
 
88
88
  new_contents = format_json(new_doc, stable=True).encode()
89
89
  return new_contents
90
+
91
+
92
+ def apply_acl_filters(content: Dict[str, Any], filters: List[str]) -> Dict[str, Any]:
93
+ result = {}
94
+ for f in filters:
95
+ pointer = jsonpointer.JsonPointer(f)
96
+
97
+ try:
98
+ part = pointer.get(copy.deepcopy(content))
99
+
100
+ sub_tree = result
101
+ for i in pointer.get_parts():
102
+ if i not in sub_tree:
103
+ sub_tree[i] = {}
104
+ sub_tree = sub_tree[i]
105
+
106
+ patch = jsonpatch.JsonPatch([{"op": "add", "path": f, "value": part}])
107
+ result = patch.apply(result)
108
+ except jsonpointer.JsonPointerException:
109
+ # no value found in new_fragment by the pointer, skip the ACL item
110
+ continue
111
+
112
+ return result
annet/gen.py CHANGED
@@ -182,6 +182,7 @@ def _old_new_per_device(ctx: OldNewDeviceContext, device: Device, filterer: Filt
182
182
  if not ctx.args.no_acl:
183
183
  acl_rules = generators.compile_acl_text(res.acl_text(), device.hw.vendor)
184
184
  old = (old and patching.apply_acl(old, acl_rules))
185
+
185
186
  new = patching.apply_acl(
186
187
  new,
187
188
  acl_rules,
@@ -232,11 +233,29 @@ def _old_new_per_device(ctx: OldNewDeviceContext, device: Device, filterer: Filt
232
233
 
233
234
  entire_results = res.entire_results
234
235
  json_fragment_results = res.json_fragment_results
236
+ old_json_fragment_files = old_files.json_fragment_files
237
+
235
238
  new_files = res.new_files()
236
- new_json_fragment_files = res.new_json_fragment_files(old_files.json_fragment_files)
239
+ new_json_fragment_files = res.new_json_fragment_files(old_json_fragment_files)
240
+
237
241
  if ctx.args.acl_safe:
238
242
  safe_new_files = res.new_files(safe=True)
239
243
 
244
+ filters = build_filter_text(filterer, device, ctx.stdin, ctx.args, ctx.config).split("\n")
245
+
246
+ for file_name in new_json_fragment_files:
247
+ new_json_fragment_files = _update_json_config(
248
+ new_json_fragment_files,
249
+ file_name,
250
+ jsontools.apply_acl_filters(new_json_fragment_files[file_name][0], filters)
251
+ )
252
+ for file_name in old_json_fragment_files:
253
+ old_json_fragment_files = _update_json_config(
254
+ old_json_fragment_files,
255
+ file_name,
256
+ jsontools.apply_acl_filters(old_json_fragment_files[file_name][0], filters)
257
+ )
258
+
240
259
  if ctx.args.profile:
241
260
  perf = res.perf_mesures()
242
261
  combined_perf[ALL_GENS] = {"total": time.monotonic() - start}
@@ -253,7 +272,7 @@ def _old_new_per_device(ctx: OldNewDeviceContext, device: Device, filterer: Filt
253
272
  new_files=new_files,
254
273
  partial_result=partial_results,
255
274
  entire_result=entire_results,
256
- old_json_fragment_files=old_files.json_fragment_files,
275
+ old_json_fragment_files=old_json_fragment_files,
257
276
  new_json_fragment_files=new_json_fragment_files,
258
277
  json_fragment_result=json_fragment_results,
259
278
  implicit_rules=implicit_rules,
@@ -266,6 +285,13 @@ def _old_new_per_device(ctx: OldNewDeviceContext, device: Device, filterer: Filt
266
285
  )
267
286
 
268
287
 
288
+ def _update_json_config(json_files, file_name, new_config):
289
+ file = list(json_files[file_name])
290
+ file[0] = new_config
291
+ json_files[file_name] = tuple(file)
292
+ return json_files
293
+
294
+
269
295
  @dataclasses.dataclass
270
296
  class DeviceDownloadedFiles:
271
297
  # map file path to file content for entire generators
@@ -296,8 +296,8 @@ def run_partial_generators(gens: List["PartialGenerator"], run_args: GeneratorPa
296
296
  for gen in gens:
297
297
  try:
298
298
  result = _run_partial_generator(gen, run_args)
299
- except NotSupportedDevice:
300
- logger.info("generator %s is not supported for this device, skip generator for this devices!", gen)
299
+ except NotSupportedDevice as exc:
300
+ logger.info("generator %s raised unsupported error: %r", gen, exc)
301
301
  continue
302
302
 
303
303
  if not result:
@@ -317,13 +317,17 @@ def run_partial_generators(gens: List["PartialGenerator"], run_args: GeneratorPa
317
317
 
318
318
 
319
319
  @tracing.function(name="run_partial_generator")
320
- def _run_partial_generator(gen: "PartialGenerator", run_args: GeneratorPartialRunArgs) -> GeneratorPartialResult:
320
+ def _run_partial_generator(gen: "PartialGenerator", run_args: GeneratorPartialRunArgs) -> Optional[GeneratorPartialResult]:
321
321
  logger = get_logger(generator=_make_generator_ctx(gen))
322
322
  device = run_args.device
323
323
  output = ""
324
324
  config = odict()
325
325
  safe_config = odict()
326
326
 
327
+ if not gen.supports_device(device):
328
+ logger.info("generator %s is not supported for device %s", gen, device.hostname)
329
+ return None
330
+
327
331
  span = tracing_connector.get().get_current_span()
328
332
  if span:
329
333
  tracing_connector.get().set_device_attributes(span, run_args.device)
@@ -439,8 +443,8 @@ def run_file_generators(
439
443
  raise RuntimeError(f"Unknown generator class type: cls={gen.__class__} TYPE={gen.__class__.TYPE}")
440
444
  try:
441
445
  result = run_generator_fn(gen, device, storage)
442
- except NotSupportedDevice:
443
- logger.info("generator %s is not supported for this device", gen)
446
+ except NotSupportedDevice as exc:
447
+ logger.info("generator %s raised unsupported error: %r", gen, exc)
444
448
  continue
445
449
  if result:
446
450
  add_result_fn(result)
@@ -450,33 +454,34 @@ def run_file_generators(
450
454
 
451
455
  @tracing.function(min_duration="0.5")
452
456
  def _run_entire_generator(gen: "Entire", device: "Device", storage: Storage) -> Optional[GeneratorResult]:
457
+ logger = get_logger(generator=_make_generator_ctx(gen))
458
+ if not gen.supports_device(device):
459
+ logger.info("generator %s is not supported for device %s", gen, device.hostname)
460
+ return
461
+
453
462
  span = tracing_connector.get().get_current_span()
454
463
  if span:
455
464
  tracing_connector.get().set_device_attributes(span, device)
456
465
  tracing_connector.get().set_dimensions_attributes(span, gen, device)
457
466
 
458
- logger = get_logger(generator=_make_generator_ctx(gen))
459
467
  path = gen.path(device)
460
- if path:
461
- logger.info("Generating ENTIRE ...")
462
-
463
- with GeneratorPerfMesurer(gen, storage, trace_min_duration="0.5") as pm:
464
- output = gen(device)
465
-
466
- reload_cmds = gen.get_reload_cmds(device)
467
- prio = gen.prio
468
-
469
- return GeneratorEntireResult(
470
- name=gen.__class__.__name__,
471
- tags=gen.TAGS,
472
- path=path,
473
- output=output,
474
- reload=reload_cmds,
475
- prio=prio,
476
- perf=pm.last_result,
477
- is_safe=gen.is_safe(device),
478
- )
479
- return None
468
+ if not path:
469
+ raise RuntimeError("entire generator should return non-empty path")
470
+
471
+ logger.info("Generating ENTIRE ...")
472
+ with GeneratorPerfMesurer(gen, storage, trace_min_duration="0.5") as pm:
473
+ output = gen(device)
474
+
475
+ return GeneratorEntireResult(
476
+ name=gen.__class__.__name__,
477
+ tags=gen.TAGS,
478
+ path=path,
479
+ output=output,
480
+ reload=gen.get_reload_cmds(device),
481
+ prio=gen.prio,
482
+ perf=pm.last_result,
483
+ is_safe=gen.is_safe(device),
484
+ )
480
485
 
481
486
 
482
487
  def _make_generator_ctx(gen):
@@ -489,34 +494,36 @@ def _run_json_fragment_generator(
489
494
  storage: Storage,
490
495
  ) -> Optional[GeneratorResult]:
491
496
  logger = get_logger(generator=_make_generator_ctx(gen))
497
+ if not gen.supports_device(device):
498
+ logger.info("generator %s is not supported for device %s", gen, device.hostname)
499
+
492
500
  path = gen.path(device)
501
+ if not path:
502
+ raise RuntimeError("json fragment generator should return non-empty path")
493
503
 
494
504
  acl_item_or_list_of_items = gen.acl(device)
505
+ if not acl_item_or_list_of_items:
506
+ raise RuntimeError("json fragment generator should return non-empty acl")
495
507
  if isinstance(acl_item_or_list_of_items, list):
496
508
  acl = acl_item_or_list_of_items
497
509
  else:
498
510
  acl = [acl_item_or_list_of_items]
499
511
 
500
- if path:
501
- logger.info("Generating JSON_FRAGMENT ...")
502
-
503
- with GeneratorPerfMesurer(gen, storage) as pm:
504
- config = gen(device)
505
-
506
- reload_cmds = gen.get_reload_cmds(device)
507
-
508
- return GeneratorJSONFragmentResult(
509
- name=gen.__class__.__name__,
510
- tags=gen.TAGS,
511
- path=path,
512
- acl=acl,
513
- config=config,
514
- reload=reload_cmds,
515
- perf=pm.last_result,
516
- is_safe=gen.is_safe(device),
517
- reload_prio=gen.reload_prio,
518
- )
519
- return None
512
+ logger.info("Generating JSON_FRAGMENT ...")
513
+ with GeneratorPerfMesurer(gen, storage) as pm:
514
+ config = gen(device)
515
+ reload_cmds = gen.get_reload_cmds(device)
516
+ return GeneratorJSONFragmentResult(
517
+ name=gen.__class__.__name__,
518
+ tags=gen.TAGS,
519
+ path=path,
520
+ acl=acl,
521
+ config=config,
522
+ reload=reload_cmds,
523
+ perf=pm.last_result,
524
+ is_safe=gen.is_safe(device),
525
+ reload_prio=gen.reload_prio,
526
+ )
520
527
 
521
528
 
522
529
  def _get_generators(module_paths: Union[List[str], dict], storage, device=None):
@@ -587,7 +594,7 @@ class BaseGenerator:
587
594
  TYPE: str
588
595
  TAGS: list[str]
589
596
 
590
- def supports_vendor(self, vendor: str) -> bool: # pylint: disable=unused-argument
597
+ def supports_device(self, device: Device) -> bool: # pylint: disable=unused-argument
591
598
  return True
592
599
 
593
600
 
@@ -681,36 +688,36 @@ class PartialGenerator(TreeGenerator):
681
688
  self._annotations = []
682
689
  self._annotation_module = self.__class__.__module__ or ""
683
690
 
684
- def supports_vendor(self, vendor: str) -> bool:
691
+ def supports_device(self, device: Device) -> bool:
685
692
  if self.__class__.run is PartialGenerator.run:
686
- return hasattr(self, f"run_{vendor}")
693
+ return bool(self._get_vendor_func(device.hw.vendor, "run"))
687
694
  else:
688
695
  return True
689
696
 
690
697
  def acl(self, device):
691
- if hasattr(self, "acl_" + device.hw.vendor):
692
- return getattr(self, "acl_" + device.hw.vendor)(device)
698
+ acl_func = self._get_vendor_func(device.hw.vendor, "acl")
699
+ if acl_func:
700
+ return acl_func(device)
693
701
 
694
702
  def acl_safe(self, device):
695
- if hasattr(self, "acl_safe_" + device.hw.vendor):
696
- return getattr(self, "acl_safe_" + device.hw.vendor)(device)
703
+ acl_func = self._get_vendor_func(device.hw.vendor, "acl_safe")
704
+ if acl_func:
705
+ return acl_func(device)
697
706
 
698
707
  def run(self, device) -> Iterable[Union[str, tuple]]:
699
- if hasattr(self, "run_" + device.hw.vendor):
700
- return getattr(self, "run_" + device.hw.vendor)(device)
701
- logger = get_logger()
702
- logger.info(
703
- "generator %s is not supported for vendor %s",
704
- self,
705
- device.hw.vendor,
706
- )
707
- return iter(())
708
+ run_func = self._get_vendor_func(device.hw.vendor, "run")
709
+ if run_func:
710
+ return run_func(device)
708
711
 
709
712
  def get_user_runner(self, device):
710
713
  if self.__class__.run is not PartialGenerator.run:
711
714
  return self.run
712
- elif hasattr(self, "run_" + device.hw.vendor):
713
- return getattr(self, "run_" + device.hw.vendor)
715
+ return self._get_vendor_func(device.hw.vendor, "run")
716
+
717
+ def _get_vendor_func(self, vendor: str, func_name: str):
718
+ attr_name = f"{func_name}_{vendor}"
719
+ if hasattr(self, attr_name):
720
+ return getattr(self, attr_name)
714
721
  return None
715
722
 
716
723
  # =====
@@ -790,6 +797,9 @@ class Entire(BaseGenerator):
790
797
  self.prio = 100
791
798
  self.__device = None
792
799
 
800
+ def supports_device(self, device: Device):
801
+ return bool(self.path(device))
802
+
793
803
  def run(self, device) -> Union[None, str, Iterable[Union[str, tuple]]]:
794
804
  raise NotImplementedError
795
805
 
@@ -907,6 +917,9 @@ class JSONFragment(TreeGenerator):
907
917
  if not hasattr(self, "reload_prio"):
908
918
  self.reload_prio = 100
909
919
 
920
+ def supports_device(self, device: Device):
921
+ return bool(self.path(device))
922
+
910
923
  def path(self, device: Device) -> Optional[str]:
911
924
  raise NotImplementedError("Required PATH for JSON_FRAGMENT generator")
912
925
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: annet
3
- Version: 0.7
3
+ Version: 0.8
4
4
  Summary: annet
5
5
  Home-page: https://github.com/annetutil/annet
6
6
  License: MIT
@@ -8,7 +8,7 @@ annet/deploy.py,sha256=B8E0P_VvCrS2URjFvgmUiIkHp95g7pAWfmT34igaDeo,18893
8
8
  annet/diff.py,sha256=zLcaCnb4lZRUb7frpH1CstQ3kacRcCblZs1uLG8J5lk,3391
9
9
  annet/executor.py,sha256=bw7COJNtssuxIqbQehlHYJnkdUeYvvL8WO5B-c67XE0,19040
10
10
  annet/filtering.py,sha256=F4ZKUCU3Yb1RlL2zKdyHtVUaWPzjWF4vWyMcdygKNYk,852
11
- annet/gen.py,sha256=WC3ZMjTKtUCRXE4yy-Z3n5MOm3LkLJbXvspyfW8BeVs,31426
11
+ annet/gen.py,sha256=MI82tdc-r3leiyp4Oa35PBsrfj0jNKdbASRnQ8Rk39g,32336
12
12
  annet/hardware.py,sha256=HYPDfji_GdRn5S0_0fl4rvM7byOY9aHxG6VMAtsLaxE,1090
13
13
  annet/implicit.py,sha256=QigK4uoxvrFho2h9yFjOq1d9rLsC5xFlAU4bKkCuMWk,5139
14
14
  annet/lib.py,sha256=zQIfNBg7fAAk2BHbHAqy_McAiXhz0RqpBAcfdU-6pAA,3742
@@ -44,7 +44,7 @@ annet/annlib/command.py,sha256=uuBddMQphtn8P5MO5kzIa8_QrtMns-k05VeKv1bcAuA,1043
44
44
  annet/annlib/diff.py,sha256=UPt3kYFQdQdVVy3ePYzNHPAxMVmHxCrCnZnMCV6ou2Q,4587
45
45
  annet/annlib/errors.py,sha256=jBcSFzY6Vj-FxR__vqjFm-87AwYQ0xHuAopTirii5AU,287
46
46
  annet/annlib/filter_acl.py,sha256=0w1VF6WcONiTYTQh0yWi6_j9rCTc_kMLAUMr0hbdkNU,7203
47
- annet/annlib/jsontools.py,sha256=X4gtKcnDArnzKcZDJmRKcZGuUgi3nEFSVKdGPi2uGDw,2644
47
+ annet/annlib/jsontools.py,sha256=TJazOP-1njwbGN7iIz8oGYgWPdry4XfKLjpzNYxdvjg,3364
48
48
  annet/annlib/lib.py,sha256=eJ4hcVuQ6pdYBzLs4YKCHFtq45idOfZCYp92XfF7_QI,15317
49
49
  annet/annlib/output.py,sha256=_SjJ6G6bejvnTKqNHw6xeio0FT9oO3OIkLaOC3cEga4,7569
50
50
  annet/annlib/patching.py,sha256=Gh8uUjFyYND9TJBBQH-DH6-AwFiiR-dXVXOisMS7elg,19784
@@ -68,7 +68,7 @@ annet/annlib/rulebook/common.py,sha256=9kCZwZpsH5UliF2OzaN9nLs2eLlz_d__4L7_oZ3Sr
68
68
  annet/api/__init__.py,sha256=VooP9u9f6e8vF7FPUE-r0JAGqCjck9-2b0dVNWAtoLM,33752
69
69
  annet/configs/context.yml,sha256=nt4QnzYzrG2hqmaWOUsab2wgFl-CXmFSzbto6rqqFt4,291
70
70
  annet/configs/logging.yaml,sha256=Hu42lRK248dssp9TgkbHCaZNl0E6f4IChGb0XaQnTVo,970
71
- annet/generators/__init__.py,sha256=LQoqGz_0imhtxvyuwBrBpB6iSuzS2S0UxNJEpO77BiM,33829
71
+ annet/generators/__init__.py,sha256=iOTaIfTQfGoExmSAzqt1RSWP7V1O_pEkuqvjnsEjBkQ,34476
72
72
  annet/generators/common/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
73
73
  annet/generators/common/initial.py,sha256=XI7uWLMuLrHC-uXm38oRbM1YVuELAvMHLZFHM9x5IF8,1229
74
74
  annet/rulebook/__init__.py,sha256=14IpOfTbeJtre7JKrfXVYiH0qAXsUSOL7AatUFmSQs0,3847
@@ -120,10 +120,10 @@ annet/rulebook/texts/routeros.rul,sha256=ipfxjj0mjFef6IsUlupqx4BY_Je_OUb8u_U1019
120
120
  annet_generators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
121
121
  annet_generators/example/__init__.py,sha256=zVd8_DrXuOASrNzg2Ab94rPyvJff83L-_HppDFxnUjM,228
122
122
  annet_generators/example/lldp.py,sha256=68CLrK7vxTQQy9XIBxtywuEdBNlIlfXGYj8_wYWs5UI,1146
123
- annet-0.7.dist-info/AUTHORS,sha256=rh3w5P6gEgqmuC-bw-HB68vBCr-yIBFhVL0PG4hguLs,878
124
- annet-0.7.dist-info/LICENSE,sha256=yPxl7dno02Pw7gAcFPIFONzx_gapwDoPXsIsh6Y7lC0,1079
125
- annet-0.7.dist-info/METADATA,sha256=hkf7Zql0Tekm9u10L6exfVSV6LjhQ7367c7VZ8WxOw8,691
126
- annet-0.7.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
127
- annet-0.7.dist-info/entry_points.txt,sha256=yHimujIzR2bwNIbb--MTs_GpXiAve89Egpu2LlgTEkg,119
128
- annet-0.7.dist-info/top_level.txt,sha256=QsoTZBsUtwp_FEcmRwuN8QITBmLOZFqjssRfKilGbP8,23
129
- annet-0.7.dist-info/RECORD,,
123
+ annet-0.8.dist-info/AUTHORS,sha256=rh3w5P6gEgqmuC-bw-HB68vBCr-yIBFhVL0PG4hguLs,878
124
+ annet-0.8.dist-info/LICENSE,sha256=yPxl7dno02Pw7gAcFPIFONzx_gapwDoPXsIsh6Y7lC0,1079
125
+ annet-0.8.dist-info/METADATA,sha256=biTv9OtMLS-HnbvanEbTm3B-tYG9SGFV-TSQCtem7cE,691
126
+ annet-0.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
127
+ annet-0.8.dist-info/entry_points.txt,sha256=yHimujIzR2bwNIbb--MTs_GpXiAve89Egpu2LlgTEkg,119
128
+ annet-0.8.dist-info/top_level.txt,sha256=QsoTZBsUtwp_FEcmRwuN8QITBmLOZFqjssRfKilGbP8,23
129
+ annet-0.8.dist-info/RECORD,,
File without changes
File without changes
File without changes