Skip to content

Commit

Permalink
Apply some black magic
Browse files Browse the repository at this point in the history
  • Loading branch information
otaku42 committed May 13, 2023
1 parent e20e852 commit 567b7c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
14 changes: 10 additions & 4 deletions examples/v4l2py-ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def save_to_file(device: str, legacy_controls: bool, filename) -> None:
elif isinstance(filename, str):
filename = pathlib.Path(filename)
else:
raise TypeError(f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}")
raise TypeError(
f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}"
)

with Device(device, legacy_controls) as cam:
print(f"Saving device configuration to {filename.resolve()}")
Expand All @@ -165,13 +167,17 @@ def save_to_file(device: str, legacy_controls: bool, filename) -> None:
print("")


def load_from_file(device: str, legacy_controls: bool, filename, pedantic: bool) -> None:
def load_from_file(
device: str, legacy_controls: bool, filename, pedantic: bool
) -> None:
if isinstance(filename, pathlib.Path):
pass
elif isinstance(filename, str):
filename = pathlib.Path(filename)
else:
raise TypeError(f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}")
raise TypeError(
f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}"
)

with Device(device, legacy_controls) as cam:
print(f"Loading device configuration from {filename.resolve()}")
Expand Down Expand Up @@ -218,7 +224,7 @@ def csv(string: str) -> list:
"--pedantic",
default=False,
action="store_true",
help="be pedantic when validating a loaded configuration (default: %(default)s)"
help="be pedantic when validating a loaded configuration (default: %(default)s)",
)

actions = parser.add_argument_group("Actions")
Expand Down
23 changes: 17 additions & 6 deletions v4l2py/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def __init__(self, device: Device):

@property
def has_config(self) -> bool:
return isinstance(self.config, configparser.ConfigParser) and self.config.sections()
return (
isinstance(self.config, configparser.ConfigParser)
and self.config.sections()
)

@property
def config_loaded(self) -> bool:
Expand Down Expand Up @@ -68,7 +71,9 @@ def save(self, filename) -> None:
elif isinstance(filename, str):
filename = pathlib.Path(filename)
else:
raise TypeError(f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}")
raise TypeError(
f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}"
)

if self.device.closed:
raise V4L2Error(f"{self.device} must be opened to save configuration")
Expand All @@ -86,7 +91,9 @@ def load(self, filename) -> None:
elif isinstance(filename, str):
filename = pathlib.Path(filename)
else:
raise TypeError(f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}")
raise TypeError(
f"filename expected to be str or pathlib.Path, not {filename.__class__.__name__}"
)

if not (filename.exists() and filename.is_file()):
raise RuntimeError(f"{filename} must be an existing file")
Expand All @@ -113,20 +120,24 @@ def validate(self, pedantic: bool = False) -> None:
controls = self.device.controls.named_keys()
for ctrl, _ in self.config.items("controls"):
if ctrl not in controls:
raise CompatibilityError(f"{self.device.filename} has no control named {ctrl}")
raise CompatibilityError(
f"{self.device.filename} has no control named {ctrl}"
)

if pedantic:
if not self.config.has_section("device"):
raise ConfigurationError("Section 'device' is missing")
for (option, have) in (
for option, have in (
("card", str(self.device.info.card)),
("driver", str(self.device.info.driver)),
("version", str(self.device.info.version)),
("legacy_controls", str(self.device.legacy_controls)),
):
want = self.config["device"][option]
if not (want == have):
raise CompatibilityError(f"{option.title()} mismatch: want '{want}', have '{have}'")
raise CompatibilityError(
f"{option.title()} mismatch: want '{want}', have '{have}'"
)
self.log.info("configuration validated")

def apply(self, cycles: int = 2) -> None:
Expand Down

0 comments on commit 567b7c9

Please sign in to comment.