2from abc
import ABC, abstractmethod
3from collections.abc
import Iterator
4from typing
import Callable, Generic, Optional, Protocol, TypeVar, Union
6from .Snapshot
import Snapshot, SnapshotValue
12 def __call__(self, snapshot: Snapshot) -> Snapshot:
13 raise NotImplementedError
18 self.lenses: list[Lens] = []
20 def __call__(self, snapshot: Snapshot) -> Snapshot:
22 for lens
in self.lenses:
23 current = lens(current)
26 def add(self, lens: Lens) ->
"CompoundLens":
27 self.lenses.append(lens)
31 self, perString: Callable[[str], Optional[str]]
33 def _mutate_each(snapshot: Snapshot) -> Iterator[tuple[str, SnapshotValue]]:
34 for entry
in snapshot.items():
35 if entry[1].is_binary:
38 mapped = perString(entry[1].value_string())
39 if mapped
is not None:
40 yield (entry[0], SnapshotValue.of(mapped))
42 return self.
add(
lambda snapshot: Snapshot.of_items(_mutate_each(snapshot)))
44 def replace_all(self, toReplace: str, replacement: str) ->
"CompoundLens":
48 self, pattern: Union[str, re.Pattern[str]], replacement: str
53 self, target: str, source: str, function: Callable[[str], Optional[str]]
55 def _set_facet_from(snapshot: Snapshot) -> Snapshot:
56 source_value = snapshot.subject_or_facet_maybe(source)
57 if source_value
is None:
61 snapshot, target, function(source_value.value_string())
64 return self.
add(_set_facet_from)
67 self, snapshot: Snapshot, target: str, new_value: Optional[str]
72 return snapshot.plus_or_replace(target, SnapshotValue.of(new_value))
75 self, target: str, function: Callable[[str], Optional[str]]
88 class WithLensCamera(
Camera):
89 def snapshot(self, subject: T) -> Snapshot:
90 return lens(parent.snapshot(subject))
92 return WithLensCamera()
95 def of(lambda_func: Callable[[T], Snapshot]) ->
"Camera[T]":
96 class LambdaCamera(
Camera):
97 def snapshot(self, subject: T) -> Snapshot:
98 return lambda_func(subject)
100 return LambdaCamera()
Snapshot snapshot(self, T subject)
"Camera[T]" of(Callable[[T], Snapshot] lambda_func)
"Camera[T]" with_lens(self, Lens lens)
"CompoundLens" replace_all_regex(self, Union[str, re.Pattern[str]] pattern, str replacement)
"CompoundLens" replace_all(self, str toReplace, str replacement)
Snapshot __call__(self, Snapshot snapshot)
"CompoundLens" mutate_facet(self, str target, Callable[[str], Optional[str]] function)
"CompoundLens" add(self, Lens lens)
"CompoundLens" mutate_all_facets(self, Callable[[str], Optional[str]] perString)
Snapshot __set_facet_of(self, Snapshot snapshot, str target, Optional[str] new_value)
"CompoundLens" set_facet_from(self, str target, str source, Callable[[str], Optional[str]] function)
Snapshot __call__(self, Snapshot snapshot)