From dde0239f522330db3b50c906d65df187762a3627 Mon Sep 17 00:00:00 2001 From: Caden Gobat <36030084+cgobat@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:42:45 -0700 Subject: [PATCH 1/5] Actually use `mainloop` argument when provided --- pyindi/device.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyindi/device.py b/pyindi/device.py index 7c9e771..3fd3443 100644 --- a/pyindi/device.py +++ b/pyindi/device.py @@ -855,7 +855,8 @@ def start(self): astart """ - self.mainloop = asyncio.get_event_loop() + if self.mainloop is None: + self.mainloop = asyncio.get_event_loop() self.reader, self.writer = self.mainloop.run_until_complete(stdio()) self.running = True future = asyncio.gather( @@ -874,7 +875,8 @@ async def astart(self, *tasks): other device tasks. """ - self.mainloop = asyncio.get_running_loop() + if self.mainloop is None: + self.mainloop = asyncio.get_event_loop() self.reader, self.writer = await stdio() self.running = True future = asyncio.gather( From cfcc12d956bdd964fd4fdfe915c3c07496d40694 Mon Sep 17 00:00:00 2001 From: Caden Gobat <36030084+cgobat@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:04:21 -0700 Subject: [PATCH 2/5] Clean up string line wrapping --- pyindi/device.py | 63 ++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/pyindi/device.py b/pyindi/device.py index 3fd3443..16bc907 100644 --- a/pyindi/device.py +++ b/pyindi/device.py @@ -282,9 +282,8 @@ def Def(self, msg=None): tagname = "def" + self.tagcontext dtd_elements = {tag.name: tag for tag in self.dtd.iterelements()} if tagname not in dtd_elements: - raise AttributeError( - "{tagname} not defined in \ - Document Type Definition") + raise AttributeError(f"{tagname} not defined in Document Type " + "Definition") ele_definition = dtd_elements[tagname] ele = etree.Element(ele_definition.name) @@ -316,9 +315,8 @@ def Set(self, msg=None): tagname = "set" + self.tagcontext dtd_elements = {tag.name: tag for tag in self.dtd.iterelements()} if tagname not in dtd_elements: - raise AttributeError( - f"{tagname} not defined in \ - Document Type Definition") + raise AttributeError(f"{tagname} not defined in Document Type " + "Definition") ele_definition = dtd_elements[tagname] ele = etree.Element(ele_definition.name) @@ -394,9 +392,8 @@ def Def(self): dtd_elements = {tag.name: tag for tag in self.dtd.iterelements()} if tagname not in dtd_elements: - raise AttributeError( - f"{tagname} not defined in \ - Document Type Definition") + raise AttributeError(f"{tagname} not defined in Document Type " + "Definition") ele_definition = dtd_elements[tagname] ele = etree.Element(ele_definition.name) @@ -415,9 +412,8 @@ def Set(self): dtd_elements = {tag.name: tag for tag in self.dtd.iterelements()} if tagname not in dtd_elements: - raise AttributeError( - f"{tagname} not defined in \ - Document Type Definition") + raise AttributeError(f"{tagname} not defined in Document Type " + "Definition") ele_definition = dtd_elements[tagname] ele = etree.Element(ele_definition.name) @@ -442,10 +438,8 @@ def value(self): elif isinstance(self, IBLOB): return self.data - raise TypeError(f"""value method must be called with INumber, - IText, ILight, ISwitch or IBLOB not {type(self)} - {isinstance(self, INumber)} - """) + raise TypeError(f"value method must be called with INumber, IText," + f" ILight, ISwitch or IBLOB not {type(self)}") @value.setter def value(self, val): @@ -462,14 +456,8 @@ def value(self, val): elif isinstance(self, IBLOB): self.data = val else: - raise TypeError(f""" - value method must be called with INumber, IText, - ILight, ISwitch or IBLOB not {type(self)} {self} - {isinstance(self, INumber)} {self.tagcontext} - {type(self) == INumber} - {self.tagcontext == "Switch"} - {self.__class__ == ISwitch} - """) + raise TypeError("value method must be called with INumber, IText," + f" ILight, ISwitch or IBLOB, not {type(self)}") class INumberVector(IVectorProperty): @@ -898,12 +886,9 @@ async def repeat_queuer(self): func(self) except Exception as error: - sys.stderr.write( - f"There was an exception in the \ - later decorated fxn {func}:") - - sys.stderr.write(f"{error}") - sys.stderr.write("See traceback below.") + sys.stderr.write("There was an exception in the later " + f"decorated fxn {func}:\n{error}\nSee " + "traceback below.") traceback.print_exc(file=sys.stderr) def exception(self, loop, context): @@ -1117,16 +1102,15 @@ def buildSkeleton(self, skelfile): try: ivec = self.vectorFactory(xml_def.tag, xml_def.attrib, properties) except Exception as error: - logging.error(f"The following error was caused by this xml tag \ - \n {etree.tostring(xml_def)}") + logging.error("The following error was caused by this xml tag:" + f"\n{etree.tostring(xml_def).decode()}") logging.error(error) raise self.IDDef(ivec) def ISNewNumber(self, dev: str, name: str, values: list, names: list): raise NotImplementedError( - "Device driver must \ - overload ISNewNumber method.") + "Device driver must overload ISNewNumber method.") def IUFind(self, name, device=None, group=None): """ @@ -1174,9 +1158,8 @@ def IUUpdate(self, device, name, values, names, Set=False): return vp def ISGetProperties(self, device): - raise NotImplementedError( - f"Subclass of {self} must \ - implement ISGetProperties") + raise NotImplementedError(f"Subclass of {self} must implement " + "ISGetProperties") def IDMessage( self, msg: str, @@ -1372,8 +1355,8 @@ def vectorFactory(vector_type, attribs, properties): vec.tp.append(iprop) else: - message = f"vector_type argument must be a string containing \ - Light, Number, Switch, Text or BLOB not {vector_type}" - raise ValueError(message) + raise ValueError("vector_type argument must be a string containing" + " Light, Number, Switch, Text or BLOB, not " + f"{vector_type}") return vec From 47f80a2948cf33b7a3e39a074e230cd5dce7662a Mon Sep 17 00:00:00 2001 From: Caden Gobat <36030084+cgobat@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:08:42 -0700 Subject: [PATCH 3/5] Use `isinstance()` instead of checking `type()` equality --- pyindi/device.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyindi/device.py b/pyindi/device.py index 16bc907..662bc11 100644 --- a/pyindi/device.py +++ b/pyindi/device.py @@ -69,7 +69,7 @@ def printa(msg: Union[str, bytes]): We use the stdio fxn now. """ - if type(msg) == bytes: + if isinstance(msg, bytes): msg = msg.decode() sys.stdout.write(msg) sys.stdout.flush() @@ -772,7 +772,7 @@ def value(self) -> str: @value.setter def value(self, val: bytes): - if type(val) != bytes: + if not isinstance(val, bytes): raise ValueError("""IBLOB value must by bytes type""") self.size = len(val) @@ -1177,7 +1177,7 @@ def IDMessage( msgtype : str, optional one of "DEBUG", "INFO", "WARN", by default "INFO" """ - if type(timestamp) == datetime.datetime: + if isinstance(timestamp, datetime.datetime): timestamp = timestamp.strftime("%Y-%m-%dT%H:%M:%S") elif timestamp is None: From f70b805c7376d7d848d03f5ece2f1cf710a40590 Mon Sep 17 00:00:00 2001 From: Caden Gobat <36030084+cgobat@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:10:31 -0700 Subject: [PATCH 4/5] Demote verbose prop set log message from warning to debug --- pyindi/device.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyindi/device.py b/pyindi/device.py index 662bc11..109f201 100644 --- a/pyindi/device.py +++ b/pyindi/device.py @@ -324,7 +324,7 @@ def Set(self, msg=None): if hasattr(self, attribute.name): ele.set(attribute.name, str(getattr(self, attribute.name))) for prop in self.iprops: - logging.warning(prop) + logging.debug(prop) ele.append(prop.Set()) if msg is not None: From f54be2767514ac2998fba399cdcc0e9fba796d86 Mon Sep 17 00:00:00 2001 From: Caden Gobat <36030084+cgobat@users.noreply.github.com> Date: Thu, 14 Nov 2024 22:11:45 -0700 Subject: [PATCH 5/5] Construct `IDMessage()` XML as an etree object instead of a string --- pyindi/device.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyindi/device.py b/pyindi/device.py index 109f201..3a3a0a5 100644 --- a/pyindi/device.py +++ b/pyindi/device.py @@ -1183,11 +1183,11 @@ def IDMessage( elif timestamp is None: timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") - xml = f' ' - - self.outq.put_nowait(xml.encode()) + xml = etree.Element("message", + attrib={"message": f"[{msgtype}] {msg}", + "timestamp": timestamp, + "device": self.name()}) + self.outq.put_nowait(etree.tostring(xml)) # self.writer.write(xml.encode()) def IDSetNumber(self, n: INumberVector, msg=None):