22import logging
33import re
44
5- from typing import Any , Optional
5+ from typing import Any
66
77from qtoggleserver .core import ports as core_ports
88from qtoggleserver .core .typing import NullablePortValue , PortValue
99from qtoggleserver .lib import polled
1010
11- from .exceptions import CommandTimeout
1211from .. import cmdline
12+ from .exceptions import CommandTimeout
1313
1414
1515class CommandLine (polled .PolledPeripheral ):
@@ -22,20 +22,19 @@ class CommandLine(polled.PolledPeripheral):
2222 def __init__ (
2323 self ,
2424 * ,
25- output_regexp : Optional [ str ] = None ,
26- read_command : Optional [ str ] = None ,
27- write_command : Optional [ str ] = None ,
25+ output_regexp : str | None = None ,
26+ read_command : str | None = None ,
27+ write_command : str | None = None ,
2828 ports : list [dict [str , Any ]] = None ,
2929 port : dict [str , Any ] = None ,
3030 timeout : int = DEFAULT_TIMEOUT ,
31- ** kwargs
31+ ** kwargs ,
3232 ) -> None :
33-
3433 super ().__init__ (** kwargs )
3534
36- self ._output_regexp : Optional [ re .Pattern ] = None
37- self ._read_command : Optional [ str ] = read_command
38- self ._write_command : Optional [ str ] = write_command
35+ self ._output_regexp : re .Pattern | None = None
36+ self ._read_command : str | None = read_command
37+ self ._write_command : str | None = write_command
3938 self ._port_details : list [dict [str , Any ]] = ports
4039 self ._timeout : int = timeout
4140
@@ -45,16 +44,13 @@ def __init__(
4544 if output_regexp :
4645 self ._output_regexp = re .compile (output_regexp , re .MULTILINE | re .DOTALL )
4746
48- self ._values : dict [str , Optional [ float ]] = {p ['id' ]: None for p in self ._port_details }
47+ self ._values : dict [str , float | None ] = {p ["id" ]: None for p in self ._port_details }
4948
50- async def run_command (self , cmd : str , env : Optional [ dict [str , str ]] ) -> tuple [str , int ]:
49+ async def run_command (self , cmd : str , env : dict [str , str ] | None ) -> tuple [str , int ]:
5150 self .debug ('executing command "%s"' , cmd )
5251
5352 p = await asyncio .create_subprocess_shell (
54- cmd ,
55- stdout = asyncio .subprocess .PIPE ,
56- stderr = asyncio .subprocess .PIPE ,
57- env = env
53+ cmd , stdout = asyncio .subprocess .PIPE , stderr = asyncio .subprocess .PIPE , env = env
5854 )
5955
6056 try :
@@ -64,8 +60,8 @@ async def run_command(self, cmd: str, env: Optional[dict[str, str]]) -> tuple[st
6460
6561 if stderr :
6662 stderr = stderr .decode ().strip ()
67- stderr = stderr .replace (' \n ' , ' \\ n' )
68- self .warning (' command returned stderr: %s' , stderr )
63+ stderr = stderr .replace (" \n " , " \\ n" )
64+ self .warning (" command returned stderr: %s" , stderr )
6965
7066 stdout = stdout .decode ().strip ()
7167
@@ -91,7 +87,7 @@ async def poll(self) -> None:
9187 groups = [output ] * len (self ._port_details )
9288
9389 while len (groups ) < len (self ._port_details ):
94- groups .append ('' )
90+ groups .append ("" )
9591
9692 for i , p in enumerate (self ._port_details ):
9793 g = groups [i ].strip ().lower ()
@@ -103,14 +99,14 @@ async def poll(self) -> None:
10399 except ValueError :
104100 value = None
105101
106- if (p [' type' ] == core_ports .TYPE_BOOLEAN ) and (value is None ):
107- value = int (g == ' true' ) # for boolean ports, text "true" is also accepted
102+ if (p [" type" ] == core_ports .TYPE_BOOLEAN ) and (value is None ):
103+ value = int (g == " true" ) # for boolean ports, text "true" is also accepted
108104
109- self ._values [p ['id' ]] = value
105+ self ._values [p ["id" ]] = value
110106 else :
111107 # When no regexp is given, use exit code
112108 for i , k in enumerate (self ._values ):
113- if self ._port_details [i ][' type' ] == core_ports .TYPE_BOOLEAN :
109+ if self ._port_details [i ][" type" ] == core_ports .TYPE_BOOLEAN :
114110 self ._values [k ] = int (not exit_code ) # process exit code 0 means true
115111 else :
116112 self ._values [k ] = exit_code
@@ -128,27 +124,25 @@ async def write_values(self) -> None:
128124 env = {}
129125 for port_id , value in self ._values .items ():
130126 if value is None :
131- value = ''
127+ value = ""
132128 else :
133129 value = str (value )
134130
135- port_id = re .sub (' [^a-zA-Z0-9_-]' , '_' , port_id )
131+ port_id = re .sub (" [^a-zA-Z0-9_-]" , "_" , port_id )
136132 env [port_id ] = value
137133
138134 _ , exit_code = await self .run_command (self ._write_command , env = env )
139135
140136 if exit_code :
141- self .warning (' command returned non-zero exit code %d' , exit_code )
137+ self .warning (" command returned non-zero exit code %d" , exit_code )
142138
143139 # Poll values immediately after writing
144140 await self .poll ()
145141
146142 async def make_port_args (self ) -> list [dict [str , Any ]]:
147143 from .ports import CommandLinePort
148144
149- return [{
150- 'driver' : CommandLinePort ,
151- 'id' : p ['id' ],
152- 'type' : p ['type' ],
153- 'writable' : self ._write_command is not None
154- } for p in self ._port_details ]
145+ return [
146+ {"driver" : CommandLinePort , "id" : p ["id" ], "type" : p ["type" ], "writable" : self ._write_command is not None }
147+ for p in self ._port_details
148+ ]
0 commit comments