Skip to content

Commit

Permalink
Format modules with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
delfick committed Dec 26, 2024
1 parent 807c69e commit c4e314e
Show file tree
Hide file tree
Showing 205 changed files with 1,394 additions and 4,060 deletions.
35 changes: 14 additions & 21 deletions modules/photons_app/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
from delfick_project.errors import DelfickError
from delfick_project.norms import Meta, dictobj, sb
from delfick_project.option_merge import Collector, MergedOptions
from photons_messages import protocol_register

from photons_app import helpers as hp
from photons_app.errors import BadConfiguration, BadYaml, UserQuit
from photons_app.formatter import MergedOptionStringFormatter
from photons_app.photons_app import PhotonsAppSpec
from photons_app.tasks.runner import Runner
from photons_messages import protocol_register

log = logging.getLogger("photons_app.collector")

Expand Down Expand Up @@ -73,9 +74,7 @@ def setup(self, source):
self.extras_spec = Extras.FieldSpec()

def normalise_filled(self, meta, val):
options_spec = sb.set_options(
filename=sb.required(sb.string_spec()), optional=sb.defaulted(sb.boolean(), False)
)
options_spec = sb.set_options(filename=sb.required(sb.string_spec()), optional=sb.defaulted(sb.boolean(), False))
lst_spec = sb.listof(sb.or_spec(sb.string_spec(), options_spec))

if isinstance(val, list):
Expand Down Expand Up @@ -156,8 +155,8 @@ def run_coro_as_main(self, coro, catch_delfick_error=True):

print("")
print("!" * 80)
print("Something went wrong! -- {0}".format(error.__class__.__name__))
print("\t{0}".format(error))
print(f"Something went wrong! -- {error.__class__.__name__}")
print(f"\t{error}")
if conf["photons_app"].debug:
raise
sys.exit(1)
Expand Down Expand Up @@ -210,7 +209,10 @@ def extra_prepare(self, configuration, args_dict):

def find_photons_app_options(self, configuration, args_dict):
"""Return us all the photons_app options"""
d = lambda r: {} if r in (None, "", sb.NotSpecified) else r

def d(r):
return {} if r in (None, "", sb.NotSpecified) else r

return MergedOptions.using(
dict(d(configuration.get("photons_app")).items()),
dict(d(args_dict.get("photons_app")).items()),
Expand All @@ -225,13 +227,8 @@ def determine_mainline_module(self):
except ImportError:
pass
else:
if any(
hasattr(getattr(__main__, attr, None), "_delfick_project_addon_entry")
for attr in dir(__main__)
):
sys.meta_path = [
thing for thing in sys.meta_path if not isinstance(thing, MainFinder)
]
if any(hasattr(getattr(__main__, attr, None), "_delfick_project_addon_entry") for attr in dir(__main__)):
sys.meta_path = [thing for thing in sys.meta_path if not isinstance(thing, MainFinder)]
sys.meta_path.append(MainFinder(__main__))
else:
__main__ = None
Expand Down Expand Up @@ -276,9 +273,7 @@ def extra_prepare_after_activation(self, configuration, args_dict):
This will determine the target and artifact for you given the
configuration in the collector.
"""
configuration.update(
{"final_future": configuration["photons_app"].final_future}, source="<photons_app>"
)
configuration.update({"final_future": configuration["photons_app"].final_future}, source="<photons_app>")

# Post register our addons
extra_args = {"lifx.photons": {}}
Expand Down Expand Up @@ -314,7 +309,7 @@ def read_file(self, location):
"Failed to read yaml",
location=location,
error_type=error.__class__.__name__,
error="{0}{1}".format(error.problem, error.problem_mark),
error=f"{error.problem}{error.problem_mark}",
)

def add_configuration(self, configuration, collect_another_source, done, result, src):
Expand All @@ -329,9 +324,7 @@ def add_configuration(self, configuration, collect_another_source, done, result,
if "config_root" in configuration:
# if we already have a config root then we only keep new config root if it's not the home location
# i.e. if it is the home configuration, we don't delete the new config_root
if configuration["config_root"] != os.path.dirname(
self.home_dir_configuration_location()
):
if configuration["config_root"] != os.path.dirname(self.home_dir_configuration_location()):
if "config_root" in result:
del result["config_root"]

Expand Down
7 changes: 3 additions & 4 deletions modules/photons_app/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

from delfick_project.app import App, OptionalFileType
from delfick_project.norms import sb

from photons_app import VERSION
from photons_app.collector import Collector
from photons_app.tasks import task_register


def library_setup(
config_filename="lifx.yml", photons_modules=True, extra_files=None, photons_app_options=None
):
def library_setup(config_filename="lifx.yml", photons_modules=True, extra_files=None, photons_app_options=None):
"""
Get us a setup photons Collector instance.
Expand Down Expand Up @@ -108,7 +107,7 @@ def mainline(self, argv=None, print_errors_to=sys.stdout, **execute_args):
if task in ("list_tasks", "help"):
os.environ["PHOTONS_SILENT_BY_DEFAULT"] = "1"

super(App, self).mainline(original_argv, print_errors_to, **execute_args)
super().mainline(original_argv, print_errors_to, **execute_args)

def setup_collector(self, args_dict, logging_handler, extra_files):
"""Create and initialize a collector"""
Expand Down
11 changes: 3 additions & 8 deletions modules/photons_app/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import operator

from delfick_project.option_merge import MergedOptionStringFormatter

from photons_app import helpers as hp


Expand Down Expand Up @@ -68,12 +69,6 @@ def special_format_field(self, obj, format_spec):
"""Know about any special formats"""
if format_spec == "resource":
parts = obj.split("/")
return str(
functools.reduce(
operator.truediv, [importlib.resources.files(parts[0]), *parts[1:]]
).resolve()
)
elif any(
isinstance(obj, f) for f in (asyncio.Future, hp.ChildOfFuture, hp.ResettableFuture)
):
return str(functools.reduce(operator.truediv, [importlib.resources.files(parts[0]), *parts[1:]]).resolve())
elif any(isinstance(obj, f) for f in (asyncio.Future, hp.ChildOfFuture, hp.ResettableFuture)):
return obj
82 changes: 23 additions & 59 deletions modules/photons_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from queue import Queue as NormalQueue

from delfick_project.logging import lc
from photons_app.errors import PhotonsAppError
from photons_messages import fields

from photons_app.errors import PhotonsAppError

log = logging.getLogger("photons_app.helpers")

# Make vim be quiet
Expand Down Expand Up @@ -69,7 +70,7 @@ def __eq__(self, other):

other = Color(*other)

if not isinstance(other, (fields.Color, dict)) and not hasattr(other, "as_dict"):
if not isinstance(other, dict | fields.Color) and not hasattr(other, "as_dict"):
return False

if not isinstance(other, fields.Color):
Expand Down Expand Up @@ -120,11 +121,7 @@ def __eq__(self, other):
if isinstance(other, tuple) and len(other) == 2:
return (self.major, self.minor) == other
elif isinstance(other, Firmware):
return (
self.major == other.major
and self.minor == other.minor
and self.build == other.build
)
return self.major == other.major and self.minor == other.minor and self.build == other.build
else:
raise ValueError(f"Can't compare firmware with {type(other)}: {other}")

Expand Down Expand Up @@ -181,7 +178,7 @@ async def ensure_aexit_cm():
exc_info = None
try:
yield
except:
except Exception:
exc_info = sys.exc_info()

if exc_info is not None:
Expand Down Expand Up @@ -355,8 +352,7 @@ def __init__(

self.waiter = ResettableFuture(name=f"ATicker({self.name})::__init__[waiter]")
self.final_future = ChildOfFuture(
final_future
or create_future(name=f"ATicker({self.name})::__init__[owned_final_future]"),
final_future or create_future(name=f"ATicker({self.name})::__init__[owned_final_future]"),
name=f"ATicker({self.name})::__init__[final_future]",
)

Expand All @@ -366,17 +362,13 @@ async def start(self):

def __aiter__(self):
if not hasattr(self, "gen"):
raise Exception(
"The ticker must be used as a context manager before being used as an async iterator"
)
raise Exception("The ticker must be used as a context manager before being used as an async iterator")
return self.gen

async def finish(self, exc_typ=None, exc=None, tb=None):
if hasattr(self, "gen"):
try:
await stop_async_generator(
self.gen, exc=exc or self.Stop(), name=f"ATicker({self.name})::stop[stop_gen]"
)
await stop_async_generator(self.gen, exc=exc or self.Stop(), name=f"ATicker({self.name})::stop[stop_gen]")
except self.Stop:
pass

Expand Down Expand Up @@ -440,9 +432,7 @@ async def pause():
async with self.pauser:
pass

ts_final_future = ChildOfFuture(
self.final_future, name=f"ATicker({self.name})::_wait_for_next[with_pause]"
)
ts_final_future = ChildOfFuture(self.final_future, name=f"ATicker({self.name})::_wait_for_next[with_pause]")

async with TaskHolder(ts_final_future) as ts:
ts.add(pause())
Expand Down Expand Up @@ -605,14 +595,10 @@ def __init__(self, final_future, *, name=None):
self.name = name

self.ts = []
self.final_future = ChildOfFuture(
final_future, name=f"TaskHolder({self.name})::__init__[final_future]"
)
self.final_future = ChildOfFuture(final_future, name=f"TaskHolder({self.name})::__init__[final_future]")

self._cleaner = None
self._cleaner_waiter = ResettableFuture(
name=f"TaskHolder({self.name})::__init__[cleaner_waiter]"
)
self._cleaner_waiter = ResettableFuture(name=f"TaskHolder({self.name})::__init__[cleaner_waiter]")

def add(self, coro, *, silent=False):
return self.add_task(async_as_background(coro, silent=silent))
Expand Down Expand Up @@ -674,9 +660,7 @@ async def finish(self, exc_typ=None, exc=None, tb=None):
async def _final(self):
if self._cleaner:
self._cleaner.cancel()
await wait_for_all_futures(
self._cleaner, name=f"TaskHolder({self.name})::finish[finally_wait_for_cleaner]"
)
await wait_for_all_futures(self._cleaner, name=f"TaskHolder({self.name})::finish[finally_wait_for_cleaner]")

await wait_for_all_futures(
async_as_background(self.clean()),
Expand Down Expand Up @@ -708,9 +692,7 @@ async def clean(self):
else:
remaining.append(t)

await wait_for_all_futures(
*destroyed, name=f"TaskHolder({self.name})::clean[wait_for_destroyed]"
)
await wait_for_all_futures(*destroyed, name=f"TaskHolder({self.name})::clean[wait_for_destroyed]")
self.ts = remaining + [t for t in self.ts if t not in destroyed and t not in remaining]


Expand Down Expand Up @@ -841,19 +823,13 @@ def __repr__(self):
status = "success" if self.successful else "failed"
return f"<Result {status}: {self.value}: {self.context}>"

def __init__(
self, final_future, *, error_catcher=None, exceptions_only_to_error_catcher=False, name=None
):
def __init__(self, final_future, *, error_catcher=None, exceptions_only_to_error_catcher=False, name=None):
self.name = name
self.final_future = ChildOfFuture(
final_future, name=f"ResultStreamer({self.name})::__init__[final_future]"
)
self.final_future = ChildOfFuture(final_future, name=f"ResultStreamer({self.name})::__init__[final_future]")
self.error_catcher = error_catcher
self.exceptions_only_to_error_catcher = exceptions_only_to_error_catcher

self.queue_future = ChildOfFuture(
final_future, name=f"ResultStreamer({self.name})::__init__[queue_future]"
)
self.queue_future = ChildOfFuture(final_future, name=f"ResultStreamer({self.name})::__init__[queue_future]")
self.queue = Queue(
self.queue_future,
empty_on_finished=True,
Expand Down Expand Up @@ -896,9 +872,7 @@ async def run():
task.gen = gen

if self.final_future.done():
await cancel_futures_and_wait(
task, name=f"ResultStreamer({self.name})::add_generator[already_stopped_task]"
)
await cancel_futures_and_wait(task, name=f"ResultStreamer({self.name})::add_generator[already_stopped_task]")
await wait_for_first_future(
async_as_background(gen.aclose()),
name=f"ResultStreamer({self.name})::add_generator[already_stopped_gen]",
Expand All @@ -924,13 +898,9 @@ async def return_value():
async def add_task(self, task, *, context=None, on_done=None, force=False):
if self.final_future.done():
if force:
await wait_for_all_futures(
task, name=f"ResultStreamer({self.name})::add_task[force_already_stopped]"
)
await wait_for_all_futures(task, name=f"ResultStreamer({self.name})::add_task[force_already_stopped]")
else:
await cancel_futures_and_wait(
task, name=f"ResultStreamer({self.name})::add_task[already_stopped]"
)
await cancel_futures_and_wait(task, name=f"ResultStreamer({self.name})::add_task[already_stopped]")
return task

def add_to_queue(res):
Expand Down Expand Up @@ -1296,9 +1266,7 @@ async def cancel_futures_and_wait(*futs, name=None):
fut.cancel()
waiting.append(fut)

await wait_for_all_futures(
*waiting, name=f"||cancel_futures_and_wait({name})[wait_for_everything]"
)
await wait_for_all_futures(*waiting, name=f"||cancel_futures_and_wait({name})[wait_for_everything]")


class memoized_property(tp.Generic[T]):
Expand Down Expand Up @@ -1339,7 +1307,7 @@ def __init__(self, func):
self.func = func
self.name = func.__name__
self.__doc__ = func.__doc__
self.cache_name = "_{0}".format(self.name)
self.cache_name = f"_{self.name}"

def __get__(self, instance: object = None, owner: object = None) -> T:
if instance is None:
Expand Down Expand Up @@ -1790,9 +1758,7 @@ def __init__(self, final_future, *, timeout=0.05, empty_on_finished=False, name=
self.name = name
self.timeout = timeout
self.collection = NormalQueue()
self.final_future = ChildOfFuture(
final_future, name=f"SyncQueue({self.name})::__init__[final_future]"
)
self.final_future = ChildOfFuture(final_future, name=f"SyncQueue({self.name})::__init__[final_future]")
self.empty_on_finished = empty_on_finished

def append(self, item):
Expand Down Expand Up @@ -1865,9 +1831,7 @@ def __init__(self, final_future, *, empty_on_finished=False, name=None):
self.name = name
self.waiter = ResettableFuture(name=f"Queue({self.name})::__init__[waiter]")
self.collection = deque()
self.final_future = ChildOfFuture(
final_future, name=f"Queue({self.name})::__init__[final_future]"
)
self.final_future = ChildOfFuture(final_future, name=f"Queue({self.name})::__init__[final_future]")
self.empty_on_finished = empty_on_finished

self.stop = False
Expand Down
13 changes: 5 additions & 8 deletions modules/photons_app/mimic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import os
import sys

from photons_messages import Services, protocol_register
from photons_transport.targets import LanTarget

from photons_app import helpers as hp
from photons_app.mimic.device import Device
from photons_app.mimic.event import Events
from photons_app.mimic.transport import MemoryTarget
from photons_messages import Services, protocol_register
from photons_transport.targets import LanTarget

this_dir = os.path.dirname(__file__)
for fle in os.listdir(os.path.join(this_dir, "operators")):
Expand Down Expand Up @@ -296,9 +297,7 @@ async def for_test(self, final_future, udp=False):
for session in sessions:
tt.append(ts.add(session.start()))

await hp.wait_for_all_futures(
*tt, name="DeviceCollection::for_test[wait_for_start]"
)
await hp.wait_for_all_futures(*tt, name="DeviceCollection::for_test[wait_for_start]")

configuration = {
"final_future": final_future,
Expand Down Expand Up @@ -328,9 +327,7 @@ async def for_test(self, final_future, udp=False):
ends = []
for session in sessions:
ends.append(ts.add(session.finish(exc_typ=exc_typ, exc=exc, tb=tb)))
await hp.wait_for_all_futures(
*ends, name="DeviceCollection::for_test[wait_for_session_ends]"
)
await hp.wait_for_all_futures(*ends, name="DeviceCollection::for_test[wait_for_session_ends]")


__all__ = ["Device", "DeviceCollection"]
Loading

0 comments on commit c4e314e

Please sign in to comment.