ominfra 0.0.0.dev144__py3-none-any.whl → 0.0.0.dev145__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
ominfra/scripts/manage.py CHANGED
@@ -2328,23 +2328,24 @@ TODO:
2328
2328
  @dc.dataclass(frozen=True)
2329
2329
  class ObjMarshalOptions:
2330
2330
  raw_bytes: bool = False
2331
+ nonstrict_dataclasses: bool = False
2331
2332
 
2332
2333
 
2333
2334
  class ObjMarshaler(abc.ABC):
2334
2335
  @abc.abstractmethod
2335
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2336
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2336
2337
  raise NotImplementedError
2337
2338
 
2338
2339
  @abc.abstractmethod
2339
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2340
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2340
2341
  raise NotImplementedError
2341
2342
 
2342
2343
 
2343
2344
  class NopObjMarshaler(ObjMarshaler):
2344
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2345
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2345
2346
  return o
2346
2347
 
2347
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2348
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2348
2349
  return o
2349
2350
 
2350
2351
 
@@ -2352,29 +2353,29 @@ class NopObjMarshaler(ObjMarshaler):
2352
2353
  class ProxyObjMarshaler(ObjMarshaler):
2353
2354
  m: ta.Optional[ObjMarshaler] = None
2354
2355
 
2355
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2356
- return check_not_none(self.m).marshal(o, opts)
2356
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2357
+ return check_not_none(self.m).marshal(o, ctx)
2357
2358
 
2358
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2359
- return check_not_none(self.m).unmarshal(o, opts)
2359
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2360
+ return check_not_none(self.m).unmarshal(o, ctx)
2360
2361
 
2361
2362
 
2362
2363
  @dc.dataclass(frozen=True)
2363
2364
  class CastObjMarshaler(ObjMarshaler):
2364
2365
  ty: type
2365
2366
 
2366
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2367
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2367
2368
  return o
2368
2369
 
2369
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2370
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2370
2371
  return self.ty(o)
2371
2372
 
2372
2373
 
2373
2374
  class DynamicObjMarshaler(ObjMarshaler):
2374
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2375
- return marshal_obj(o)
2375
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2376
+ return ctx.manager.marshal_obj(o, opts=ctx.options)
2376
2377
 
2377
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2378
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2378
2379
  return o
2379
2380
 
2380
2381
 
@@ -2382,10 +2383,10 @@ class DynamicObjMarshaler(ObjMarshaler):
2382
2383
  class Base64ObjMarshaler(ObjMarshaler):
2383
2384
  ty: type
2384
2385
 
2385
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2386
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2386
2387
  return base64.b64encode(o).decode('ascii')
2387
2388
 
2388
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2389
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2389
2390
  return self.ty(base64.b64decode(o))
2390
2391
 
2391
2392
 
@@ -2393,25 +2394,25 @@ class Base64ObjMarshaler(ObjMarshaler):
2393
2394
  class BytesSwitchedObjMarshaler(ObjMarshaler):
2394
2395
  m: ObjMarshaler
2395
2396
 
2396
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2397
- if opts.raw_bytes:
2397
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2398
+ if ctx.options.raw_bytes:
2398
2399
  return o
2399
- return self.m.marshal(o, opts)
2400
+ return self.m.marshal(o, ctx)
2400
2401
 
2401
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2402
- if opts.raw_bytes:
2402
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2403
+ if ctx.options.raw_bytes:
2403
2404
  return o
2404
- return self.m.unmarshal(o, opts)
2405
+ return self.m.unmarshal(o, ctx)
2405
2406
 
2406
2407
 
2407
2408
  @dc.dataclass(frozen=True)
2408
2409
  class EnumObjMarshaler(ObjMarshaler):
2409
2410
  ty: type
2410
2411
 
2411
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2412
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2412
2413
  return o.name
2413
2414
 
2414
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2415
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2415
2416
  return self.ty.__members__[o] # type: ignore
2416
2417
 
2417
2418
 
@@ -2419,15 +2420,15 @@ class EnumObjMarshaler(ObjMarshaler):
2419
2420
  class OptionalObjMarshaler(ObjMarshaler):
2420
2421
  item: ObjMarshaler
2421
2422
 
2422
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2423
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2423
2424
  if o is None:
2424
2425
  return None
2425
- return self.item.marshal(o, opts)
2426
+ return self.item.marshal(o, ctx)
2426
2427
 
2427
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2428
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2428
2429
  if o is None:
2429
2430
  return None
2430
- return self.item.unmarshal(o, opts)
2431
+ return self.item.unmarshal(o, ctx)
2431
2432
 
2432
2433
 
2433
2434
  @dc.dataclass(frozen=True)
@@ -2436,11 +2437,11 @@ class MappingObjMarshaler(ObjMarshaler):
2436
2437
  km: ObjMarshaler
2437
2438
  vm: ObjMarshaler
2438
2439
 
2439
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2440
- return {self.km.marshal(k, opts): self.vm.marshal(v, opts) for k, v in o.items()}
2440
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2441
+ return {self.km.marshal(k, ctx): self.vm.marshal(v, ctx) for k, v in o.items()}
2441
2442
 
2442
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2443
- return self.ty((self.km.unmarshal(k, opts), self.vm.unmarshal(v, opts)) for k, v in o.items())
2443
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2444
+ return self.ty((self.km.unmarshal(k, ctx), self.vm.unmarshal(v, ctx)) for k, v in o.items())
2444
2445
 
2445
2446
 
2446
2447
  @dc.dataclass(frozen=True)
@@ -2448,11 +2449,11 @@ class IterableObjMarshaler(ObjMarshaler):
2448
2449
  ty: type
2449
2450
  item: ObjMarshaler
2450
2451
 
2451
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2452
- return [self.item.marshal(e, opts) for e in o]
2452
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2453
+ return [self.item.marshal(e, ctx) for e in o]
2453
2454
 
2454
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2455
- return self.ty(self.item.unmarshal(e, opts) for e in o)
2455
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2456
+ return self.ty(self.item.unmarshal(e, ctx) for e in o)
2456
2457
 
2457
2458
 
2458
2459
  @dc.dataclass(frozen=True)
@@ -2461,11 +2462,18 @@ class DataclassObjMarshaler(ObjMarshaler):
2461
2462
  fs: ta.Mapping[str, ObjMarshaler]
2462
2463
  nonstrict: bool = False
2463
2464
 
2464
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2465
- return {k: m.marshal(getattr(o, k), opts) for k, m in self.fs.items()}
2465
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2466
+ return {
2467
+ k: m.marshal(getattr(o, k), ctx)
2468
+ for k, m in self.fs.items()
2469
+ }
2466
2470
 
2467
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2468
- return self.ty(**{k: self.fs[k].unmarshal(v, opts) for k, v in o.items() if not self.nonstrict or k in self.fs})
2471
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2472
+ return self.ty(**{
2473
+ k: self.fs[k].unmarshal(v, ctx)
2474
+ for k, v in o.items()
2475
+ if not (self.nonstrict or ctx.options.nonstrict_dataclasses) or k in self.fs
2476
+ })
2469
2477
 
2470
2478
 
2471
2479
  @dc.dataclass(frozen=True)
@@ -2485,50 +2493,50 @@ class PolymorphicObjMarshaler(ObjMarshaler):
2485
2493
  {i.tag: i for i in impls},
2486
2494
  )
2487
2495
 
2488
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2496
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2489
2497
  impl = self.impls_by_ty[type(o)]
2490
- return {impl.tag: impl.m.marshal(o, opts)}
2498
+ return {impl.tag: impl.m.marshal(o, ctx)}
2491
2499
 
2492
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2500
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2493
2501
  [(t, v)] = o.items()
2494
2502
  impl = self.impls_by_tag[t]
2495
- return impl.m.unmarshal(v, opts)
2503
+ return impl.m.unmarshal(v, ctx)
2496
2504
 
2497
2505
 
2498
2506
  @dc.dataclass(frozen=True)
2499
2507
  class DatetimeObjMarshaler(ObjMarshaler):
2500
2508
  ty: type
2501
2509
 
2502
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2510
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2503
2511
  return o.isoformat()
2504
2512
 
2505
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2513
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2506
2514
  return self.ty.fromisoformat(o) # type: ignore
2507
2515
 
2508
2516
 
2509
2517
  class DecimalObjMarshaler(ObjMarshaler):
2510
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2518
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2511
2519
  return str(check_isinstance(o, decimal.Decimal))
2512
2520
 
2513
- def unmarshal(self, v: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2521
+ def unmarshal(self, v: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2514
2522
  return decimal.Decimal(check_isinstance(v, str))
2515
2523
 
2516
2524
 
2517
2525
  class FractionObjMarshaler(ObjMarshaler):
2518
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2526
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2519
2527
  fr = check_isinstance(o, fractions.Fraction)
2520
2528
  return [fr.numerator, fr.denominator]
2521
2529
 
2522
- def unmarshal(self, v: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2530
+ def unmarshal(self, v: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2523
2531
  num, denom = check_isinstance(v, list)
2524
2532
  return fractions.Fraction(num, denom)
2525
2533
 
2526
2534
 
2527
2535
  class UuidObjMarshaler(ObjMarshaler):
2528
- def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2536
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2529
2537
  return str(o)
2530
2538
 
2531
- def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2539
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
2532
2540
  return uuid.UUID(o)
2533
2541
 
2534
2542
 
@@ -2689,6 +2697,12 @@ class ObjMarshalerManager:
2689
2697
 
2690
2698
  #
2691
2699
 
2700
+ def _make_context(self, opts: ta.Optional[ObjMarshalOptions]) -> 'ObjMarshalContext':
2701
+ return ObjMarshalContext(
2702
+ options=opts or self._default_options,
2703
+ manager=self,
2704
+ )
2705
+
2692
2706
  def marshal_obj(
2693
2707
  self,
2694
2708
  o: ta.Any,
@@ -2696,7 +2710,7 @@ class ObjMarshalerManager:
2696
2710
  opts: ta.Optional[ObjMarshalOptions] = None,
2697
2711
  ) -> ta.Any:
2698
2712
  m = self.get_obj_marshaler(ty if ty is not None else type(o))
2699
- return m.marshal(o, opts or self._default_options)
2713
+ return m.marshal(o, self._make_context(opts))
2700
2714
 
2701
2715
  def unmarshal_obj(
2702
2716
  self,
@@ -2705,7 +2719,7 @@ class ObjMarshalerManager:
2705
2719
  opts: ta.Optional[ObjMarshalOptions] = None,
2706
2720
  ) -> T:
2707
2721
  m = self.get_obj_marshaler(ty)
2708
- return m.unmarshal(o, opts or self._default_options)
2722
+ return m.unmarshal(o, self._make_context(opts))
2709
2723
 
2710
2724
  def roundtrip_obj(
2711
2725
  self,
@@ -2720,6 +2734,12 @@ class ObjMarshalerManager:
2720
2734
  return u
2721
2735
 
2722
2736
 
2737
+ @dc.dataclass(frozen=True)
2738
+ class ObjMarshalContext:
2739
+ options: ObjMarshalOptions
2740
+ manager: ObjMarshalerManager
2741
+
2742
+
2723
2743
  ##
2724
2744
 
2725
2745
 
@@ -2767,7 +2787,7 @@ class MainBootstrap:
2767
2787
  CommandExecutorMap = ta.NewType('CommandExecutorMap', ta.Mapping[ta.Type[Command], CommandExecutor])
2768
2788
 
2769
2789
 
2770
- class CommandExecutionService(CommandExecutor):
2790
+ class LocalCommandExecutor(CommandExecutor):
2771
2791
  def __init__(
2772
2792
  self,
2773
2793
  *,
@@ -3264,8 +3284,7 @@ def bind_commands(
3264
3284
  lst.extend([
3265
3285
  inj.bind(provide_command_executor_map, singleton=True),
3266
3286
 
3267
- inj.bind(CommandExecutionService, singleton=True, eager=main_config.debug),
3268
- inj.bind(CommandExecutor, to_key=CommandExecutionService),
3287
+ inj.bind(LocalCommandExecutor, singleton=True, eager=main_config.debug),
3269
3288
  ])
3270
3289
 
3271
3290
  #
@@ -3304,7 +3323,7 @@ def _remote_execution_main() -> None:
3304
3323
 
3305
3324
  chan.set_marshaler(injector[ObjMarshalerManager])
3306
3325
 
3307
- ce = injector[CommandExecutor]
3326
+ ce = injector[LocalCommandExecutor]
3308
3327
 
3309
3328
  while True:
3310
3329
  i = chan.recv_obj(Command)
@@ -3612,7 +3631,7 @@ def _main() -> None:
3612
3631
  ce: CommandExecutor
3613
3632
 
3614
3633
  if args.local:
3615
- ce = injector[CommandExecutor]
3634
+ ce = injector[LocalCommandExecutor]
3616
3635
 
3617
3636
  else:
3618
3637
  tgt = RemoteSpawning.Target(