Skip to content

Commit

Permalink
add refactoring to fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Денис Петров committed May 1, 2024
1 parent 89a6fb8 commit 1e9320c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions snap7/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ def get_string(bytearray_: bytearray, byte_index: int) -> str:
if str_length > max_string_size or max_string_size > 254:
logger.error("The string is too big for the size encountered in specification")
logger.error("WRONG SIZED STRING ENCOUNTERED")
raise TypeError("String contains {} chars, but max. {} chars are expected or is larger than 254."
"Bytearray doesn't seem to be a valid string.".format(str_length, max_string_size))
raise TypeError(
f"String contains {str_length} chars, but max. {max_string_size} chars are expected or is larger than 254."
"Bytearray doesn't seem to be a valid string."
)
data = map(chr, bytearray_[byte_index + 2:byte_index + 2 + str_length])
return "".join(data)

Expand Down Expand Up @@ -1783,19 +1785,11 @@ def set_value(self, byte_index: Union[str, int], type_: str, value: Union[bool,
byte_index = self.get_offset(byte_index)

if type_.startswith('FSTRING') and isinstance(value, str):
max_size = re.search(r'\d+', type_)
if max_size is None:
raise ValueError("Max size could not be determinate. re.search() returned None")
max_size_grouped = max_size.group(0)
max_size_int = int(max_size_grouped)
max_size_int = self._get_max_size(type_)
return set_fstring(bytearray_, byte_index, value, max_size_int)

if type_.startswith('STRING') and isinstance(value, str):
max_size = re.search(r'\d+', type_)
if max_size is None:
raise ValueError("Max size could not be determinate. re.search() returned None")
max_size_grouped = max_size.group(0)
max_size_int = int(max_size_grouped)
max_size_int = self._get_max_size(type_)
return set_string(bytearray_, byte_index, value, max_size_int)

if type_ == 'REAL':
Expand Down Expand Up @@ -1878,3 +1872,12 @@ def read(self, client: Client) -> None:
# replace data in bytearray
for i, b in enumerate(bytearray_):
data[i + self.db_offset] = b

def _get_max_size(self, type_: str) -> int:
max_size = re.search(r"\d+", type_)
if max_size is None:
raise ValueError(
"Max size could not be determinate. re.search() returned None"
)
max_size_grouped = max_size.group(0)
return int(max_size_grouped)

0 comments on commit 1e9320c

Please sign in to comment.