ominfra 0.0.0.dev142__py3-none-any.whl → 0.0.0.dev143__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.
@@ -2166,21 +2166,26 @@ TODO:
2166
2166
  ##
2167
2167
 
2168
2168
 
2169
+ @dc.dataclass(frozen=True)
2170
+ class ObjMarshalOptions:
2171
+ raw_bytes: bool = False
2172
+
2173
+
2169
2174
  class ObjMarshaler(abc.ABC):
2170
2175
  @abc.abstractmethod
2171
- def marshal(self, o: ta.Any) -> ta.Any:
2176
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2172
2177
  raise NotImplementedError
2173
2178
 
2174
2179
  @abc.abstractmethod
2175
- def unmarshal(self, o: ta.Any) -> ta.Any:
2180
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2176
2181
  raise NotImplementedError
2177
2182
 
2178
2183
 
2179
2184
  class NopObjMarshaler(ObjMarshaler):
2180
- def marshal(self, o: ta.Any) -> ta.Any:
2185
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2181
2186
  return o
2182
2187
 
2183
- def unmarshal(self, o: ta.Any) -> ta.Any:
2188
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2184
2189
  return o
2185
2190
 
2186
2191
 
@@ -2188,29 +2193,29 @@ class NopObjMarshaler(ObjMarshaler):
2188
2193
  class ProxyObjMarshaler(ObjMarshaler):
2189
2194
  m: ta.Optional[ObjMarshaler] = None
2190
2195
 
2191
- def marshal(self, o: ta.Any) -> ta.Any:
2192
- return check_not_none(self.m).marshal(o)
2196
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2197
+ return check_not_none(self.m).marshal(o, opts)
2193
2198
 
2194
- def unmarshal(self, o: ta.Any) -> ta.Any:
2195
- return check_not_none(self.m).unmarshal(o)
2199
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2200
+ return check_not_none(self.m).unmarshal(o, opts)
2196
2201
 
2197
2202
 
2198
2203
  @dc.dataclass(frozen=True)
2199
2204
  class CastObjMarshaler(ObjMarshaler):
2200
2205
  ty: type
2201
2206
 
2202
- def marshal(self, o: ta.Any) -> ta.Any:
2207
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2203
2208
  return o
2204
2209
 
2205
- def unmarshal(self, o: ta.Any) -> ta.Any:
2210
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2206
2211
  return self.ty(o)
2207
2212
 
2208
2213
 
2209
2214
  class DynamicObjMarshaler(ObjMarshaler):
2210
- def marshal(self, o: ta.Any) -> ta.Any:
2215
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2211
2216
  return marshal_obj(o)
2212
2217
 
2213
- def unmarshal(self, o: ta.Any) -> ta.Any:
2218
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2214
2219
  return o
2215
2220
 
2216
2221
 
@@ -2218,21 +2223,36 @@ class DynamicObjMarshaler(ObjMarshaler):
2218
2223
  class Base64ObjMarshaler(ObjMarshaler):
2219
2224
  ty: type
2220
2225
 
2221
- def marshal(self, o: ta.Any) -> ta.Any:
2226
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2222
2227
  return base64.b64encode(o).decode('ascii')
2223
2228
 
2224
- def unmarshal(self, o: ta.Any) -> ta.Any:
2229
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2225
2230
  return self.ty(base64.b64decode(o))
2226
2231
 
2227
2232
 
2233
+ @dc.dataclass(frozen=True)
2234
+ class BytesSwitchedObjMarshaler(ObjMarshaler):
2235
+ m: ObjMarshaler
2236
+
2237
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2238
+ if opts.raw_bytes:
2239
+ return o
2240
+ return self.m.marshal(o, opts)
2241
+
2242
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2243
+ if opts.raw_bytes:
2244
+ return o
2245
+ return self.m.unmarshal(o, opts)
2246
+
2247
+
2228
2248
  @dc.dataclass(frozen=True)
2229
2249
  class EnumObjMarshaler(ObjMarshaler):
2230
2250
  ty: type
2231
2251
 
2232
- def marshal(self, o: ta.Any) -> ta.Any:
2252
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2233
2253
  return o.name
2234
2254
 
2235
- def unmarshal(self, o: ta.Any) -> ta.Any:
2255
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2236
2256
  return self.ty.__members__[o] # type: ignore
2237
2257
 
2238
2258
 
@@ -2240,15 +2260,15 @@ class EnumObjMarshaler(ObjMarshaler):
2240
2260
  class OptionalObjMarshaler(ObjMarshaler):
2241
2261
  item: ObjMarshaler
2242
2262
 
2243
- def marshal(self, o: ta.Any) -> ta.Any:
2263
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2244
2264
  if o is None:
2245
2265
  return None
2246
- return self.item.marshal(o)
2266
+ return self.item.marshal(o, opts)
2247
2267
 
2248
- def unmarshal(self, o: ta.Any) -> ta.Any:
2268
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2249
2269
  if o is None:
2250
2270
  return None
2251
- return self.item.unmarshal(o)
2271
+ return self.item.unmarshal(o, opts)
2252
2272
 
2253
2273
 
2254
2274
  @dc.dataclass(frozen=True)
@@ -2257,11 +2277,11 @@ class MappingObjMarshaler(ObjMarshaler):
2257
2277
  km: ObjMarshaler
2258
2278
  vm: ObjMarshaler
2259
2279
 
2260
- def marshal(self, o: ta.Any) -> ta.Any:
2261
- return {self.km.marshal(k): self.vm.marshal(v) for k, v in o.items()}
2280
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2281
+ return {self.km.marshal(k, opts): self.vm.marshal(v, opts) for k, v in o.items()}
2262
2282
 
2263
- def unmarshal(self, o: ta.Any) -> ta.Any:
2264
- return self.ty((self.km.unmarshal(k), self.vm.unmarshal(v)) for k, v in o.items())
2283
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2284
+ return self.ty((self.km.unmarshal(k, opts), self.vm.unmarshal(v, opts)) for k, v in o.items())
2265
2285
 
2266
2286
 
2267
2287
  @dc.dataclass(frozen=True)
@@ -2269,11 +2289,11 @@ class IterableObjMarshaler(ObjMarshaler):
2269
2289
  ty: type
2270
2290
  item: ObjMarshaler
2271
2291
 
2272
- def marshal(self, o: ta.Any) -> ta.Any:
2273
- return [self.item.marshal(e) for e in o]
2292
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2293
+ return [self.item.marshal(e, opts) for e in o]
2274
2294
 
2275
- def unmarshal(self, o: ta.Any) -> ta.Any:
2276
- return self.ty(self.item.unmarshal(e) for e in o)
2295
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2296
+ return self.ty(self.item.unmarshal(e, opts) for e in o)
2277
2297
 
2278
2298
 
2279
2299
  @dc.dataclass(frozen=True)
@@ -2282,11 +2302,11 @@ class DataclassObjMarshaler(ObjMarshaler):
2282
2302
  fs: ta.Mapping[str, ObjMarshaler]
2283
2303
  nonstrict: bool = False
2284
2304
 
2285
- def marshal(self, o: ta.Any) -> ta.Any:
2286
- return {k: m.marshal(getattr(o, k)) for k, m in self.fs.items()}
2305
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2306
+ return {k: m.marshal(getattr(o, k), opts) for k, m in self.fs.items()}
2287
2307
 
2288
- def unmarshal(self, o: ta.Any) -> ta.Any:
2289
- return self.ty(**{k: self.fs[k].unmarshal(v) for k, v in o.items() if not self.nonstrict or k in self.fs})
2308
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2309
+ 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})
2290
2310
 
2291
2311
 
2292
2312
  @dc.dataclass(frozen=True)
@@ -2306,50 +2326,50 @@ class PolymorphicObjMarshaler(ObjMarshaler):
2306
2326
  {i.tag: i for i in impls},
2307
2327
  )
2308
2328
 
2309
- def marshal(self, o: ta.Any) -> ta.Any:
2329
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2310
2330
  impl = self.impls_by_ty[type(o)]
2311
- return {impl.tag: impl.m.marshal(o)}
2331
+ return {impl.tag: impl.m.marshal(o, opts)}
2312
2332
 
2313
- def unmarshal(self, o: ta.Any) -> ta.Any:
2333
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2314
2334
  [(t, v)] = o.items()
2315
2335
  impl = self.impls_by_tag[t]
2316
- return impl.m.unmarshal(v)
2336
+ return impl.m.unmarshal(v, opts)
2317
2337
 
2318
2338
 
2319
2339
  @dc.dataclass(frozen=True)
2320
2340
  class DatetimeObjMarshaler(ObjMarshaler):
2321
2341
  ty: type
2322
2342
 
2323
- def marshal(self, o: ta.Any) -> ta.Any:
2343
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2324
2344
  return o.isoformat()
2325
2345
 
2326
- def unmarshal(self, o: ta.Any) -> ta.Any:
2346
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2327
2347
  return self.ty.fromisoformat(o) # type: ignore
2328
2348
 
2329
2349
 
2330
2350
  class DecimalObjMarshaler(ObjMarshaler):
2331
- def marshal(self, o: ta.Any) -> ta.Any:
2351
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2332
2352
  return str(check_isinstance(o, decimal.Decimal))
2333
2353
 
2334
- def unmarshal(self, v: ta.Any) -> ta.Any:
2354
+ def unmarshal(self, v: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2335
2355
  return decimal.Decimal(check_isinstance(v, str))
2336
2356
 
2337
2357
 
2338
2358
  class FractionObjMarshaler(ObjMarshaler):
2339
- def marshal(self, o: ta.Any) -> ta.Any:
2359
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2340
2360
  fr = check_isinstance(o, fractions.Fraction)
2341
2361
  return [fr.numerator, fr.denominator]
2342
2362
 
2343
- def unmarshal(self, v: ta.Any) -> ta.Any:
2363
+ def unmarshal(self, v: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2344
2364
  num, denom = check_isinstance(v, list)
2345
2365
  return fractions.Fraction(num, denom)
2346
2366
 
2347
2367
 
2348
2368
  class UuidObjMarshaler(ObjMarshaler):
2349
- def marshal(self, o: ta.Any) -> ta.Any:
2369
+ def marshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2350
2370
  return str(o)
2351
2371
 
2352
- def unmarshal(self, o: ta.Any) -> ta.Any:
2372
+ def unmarshal(self, o: ta.Any, opts: ObjMarshalOptions) -> ta.Any:
2353
2373
  return uuid.UUID(o)
2354
2374
 
2355
2375
 
@@ -2359,7 +2379,7 @@ class UuidObjMarshaler(ObjMarshaler):
2359
2379
  _DEFAULT_OBJ_MARSHALERS: ta.Dict[ta.Any, ObjMarshaler] = {
2360
2380
  **{t: NopObjMarshaler() for t in (type(None),)},
2361
2381
  **{t: CastObjMarshaler(t) for t in (int, float, str, bool)},
2362
- **{t: Base64ObjMarshaler(t) for t in (bytes, bytearray)},
2382
+ **{t: BytesSwitchedObjMarshaler(Base64ObjMarshaler(t)) for t in (bytes, bytearray)},
2363
2383
  **{t: IterableObjMarshaler(t, DynamicObjMarshaler()) for t in (list, tuple, set, frozenset)},
2364
2384
  **{t: MappingObjMarshaler(t, DynamicObjMarshaler(), DynamicObjMarshaler()) for t in (dict,)},
2365
2385
 
@@ -2392,12 +2412,16 @@ class ObjMarshalerManager:
2392
2412
  def __init__(
2393
2413
  self,
2394
2414
  *,
2415
+ default_options: ObjMarshalOptions = ObjMarshalOptions(),
2416
+
2395
2417
  default_obj_marshalers: ta.Dict[ta.Any, ObjMarshaler] = _DEFAULT_OBJ_MARSHALERS, # noqa
2396
2418
  generic_mapping_types: ta.Dict[ta.Any, type] = _OBJ_MARSHALER_GENERIC_MAPPING_TYPES, # noqa
2397
2419
  generic_iterable_types: ta.Dict[ta.Any, type] = _OBJ_MARSHALER_GENERIC_ITERABLE_TYPES, # noqa
2398
2420
  ) -> None:
2399
2421
  super().__init__()
2400
2422
 
2423
+ self._default_options = default_options
2424
+
2401
2425
  self._obj_marshalers = dict(default_obj_marshalers)
2402
2426
  self._generic_mapping_types = generic_mapping_types
2403
2427
  self._generic_iterable_types = generic_iterable_types
@@ -2506,11 +2530,35 @@ class ObjMarshalerManager:
2506
2530
 
2507
2531
  #
2508
2532
 
2509
- def marshal_obj(self, o: ta.Any, ty: ta.Any = None) -> ta.Any:
2510
- return self.get_obj_marshaler(ty if ty is not None else type(o)).marshal(o)
2511
-
2512
- def unmarshal_obj(self, o: ta.Any, ty: ta.Union[ta.Type[T], ta.Any]) -> T:
2513
- return self.get_obj_marshaler(ty).unmarshal(o)
2533
+ def marshal_obj(
2534
+ self,
2535
+ o: ta.Any,
2536
+ ty: ta.Any = None,
2537
+ opts: ta.Optional[ObjMarshalOptions] = None,
2538
+ ) -> ta.Any:
2539
+ m = self.get_obj_marshaler(ty if ty is not None else type(o))
2540
+ return m.marshal(o, opts or self._default_options)
2541
+
2542
+ def unmarshal_obj(
2543
+ self,
2544
+ o: ta.Any,
2545
+ ty: ta.Union[ta.Type[T], ta.Any],
2546
+ opts: ta.Optional[ObjMarshalOptions] = None,
2547
+ ) -> T:
2548
+ m = self.get_obj_marshaler(ty)
2549
+ return m.unmarshal(o, opts or self._default_options)
2550
+
2551
+ def roundtrip_obj(
2552
+ self,
2553
+ o: ta.Any,
2554
+ ty: ta.Any = None,
2555
+ opts: ta.Optional[ObjMarshalOptions] = None,
2556
+ ) -> ta.Any:
2557
+ if ty is None:
2558
+ ty = type(o)
2559
+ m: ta.Any = self.marshal_obj(o, ty, opts)
2560
+ u: ta.Any = self.unmarshal_obj(m, ty, opts)
2561
+ return u
2514
2562
 
2515
2563
 
2516
2564
  ##