Skip to content

Commit

Permalink
Start fixing up python linter errors, code formatting, replace % stri…
Browse files Browse the repository at this point in the history
…ngs with f'' strings.
  • Loading branch information
civerachb-cpr committed Nov 4, 2024
1 parent 70fccb6 commit baed5bf
Show file tree
Hide file tree
Showing 71 changed files with 989 additions and 1,113 deletions.
26 changes: 12 additions & 14 deletions clearpath_config/clearpath_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@
# POSSIBILITY OF SUCH DAMAGE.
from clearpath_config.common.types.config import BaseConfig
from clearpath_config.common.utils.yaml import read_yaml, write_yaml
from clearpath_config.system.system import SystemConfig
from clearpath_config.platform.platform import PlatformConfig
from clearpath_config.links.links import LinksConfig
from clearpath_config.manipulators.manipulators import ManipulatorConfig
from clearpath_config.mounts.mounts import MountsConfig
from clearpath_config.platform.platform import PlatformConfig
from clearpath_config.sensors.sensors import SensorConfig
from clearpath_config.system.system import SystemConfig


# ClearpathConfig:
# - top level configurator
# - contains
class ClearpathConfig(BaseConfig):

VERSION = "version"
SERIAL_NUMBER = "serial_number"
SYSTEM = "system"
PLATFORM = "platform"
LINKS = "links"
MANIPULATORS = "manipulators"
MOUNTS = "mounts"
SENSORS = "sensors"
VERSION = 'version'
SERIAL_NUMBER = 'serial_number'
SYSTEM = 'system'
PLATFORM = 'platform'
LINKS = 'links'
MANIPULATORS = 'manipulators'
MOUNTS = 'mounts'
SENSORS = 'sensors'

TEMPLATE = {
SERIAL_NUMBER: SERIAL_NUMBER,
Expand All @@ -63,7 +63,7 @@ class ClearpathConfig(BaseConfig):
KEYS = TEMPLATE

DEFAULTS = {
SERIAL_NUMBER: "generic",
SERIAL_NUMBER: 'generic',
VERSION: 0,
SYSTEM: SystemConfig.DEFAULTS,
PLATFORM: PlatformConfig.DEFAULTS,
Expand Down Expand Up @@ -136,9 +136,7 @@ def version(self) -> int:

@version.setter
def version(self, v: int) -> None:
assert isinstance(v, int), (
"version must be of type 'int'"
)
assert isinstance(v, int), 'version must be of type "int"'
self._version = v
# Add propagators here

Expand Down
24 changes: 11 additions & 13 deletions clearpath_config/common/types/accessory.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_xyz(self) -> List[float]:
def set_xyz(self, xyz: List[float]) -> None:
self.assert_valid_triplet(
xyz,
"XYZ must be a list of exactly three float values"
'XYZ must be a list of exactly three float values'
)
self.xyz = xyz

Expand All @@ -103,27 +103,25 @@ def get_rpy(self) -> List[float]:
def set_rpy(self, rpy: List[float]) -> None:
self.assert_valid_triplet(
rpy,
"RPY must be a list of exactly three float values"
'RPY must be a list of exactly three float values'
)
self.rpy = rpy

@staticmethod
def assert_valid_link(link: str) -> None:
# Link name must be a string
assert isinstance(link, str), "Link name '%s' must be string" % link
assert isinstance(link, str), f'Link name "{link}" must be string'
# Link name must not be empty
assert link, "Link name '%s' must not be empty" % link
assert link, f'Link name "{link}" must not be empty'
# Link name must not have spaces
assert " " not in link, "Link name '%s' must no have spaces" % link
assert ' ' not in link, f'Link name "{link}" must no have spaces'
# Link name must not start with a digit
assert not link[0].isdigit(), (
"Link name '%s' must not start with a digit" % link
)
assert not link[0].isdigit(), f'Link name "{link} must not start with a digit'

@staticmethod
def assert_valid_triplet(tri: List[float], msg: str = None) -> None:
if msg is None:
msg = "Triplet must be a list of three float values"
msg = 'Triplet must be a list of three float values'
# Triplet must be a list
assert isinstance(tri, list), msg
# Triplet must have a length of 3
Expand All @@ -138,7 +136,7 @@ def assert_is_supported():
When disabling an accessory, raise a
clearpath_config.common.types.exception.UnsupportedAccessoryException
with a suitable mesage (e.g. "SpamEggs driver is not yet released for ROS 2 Jazzy")
with a suitable mesage (e.g. 'SpamEggs driver is not yet released for ROS 2 Jazzy')
@return None
Expand Down Expand Up @@ -194,12 +192,12 @@ def __init__(

@classmethod
def get_name_from_idx(idx):
return "accessory_%s" % idx
return 'accessory_%s' % idx

def get_idx(self) -> str:
return self.idx

def set_idx(self, idx: int) -> None:
assert isinstance(idx, int), "Index must be an integer"
assert idx >= 0, "Index must be a positive integer"
assert isinstance(idx, int), 'Index must be an integer'
assert idx >= 0, 'Index must be a positive integer'
self.name = self.get_name_from_idx(idx)
18 changes: 6 additions & 12 deletions clearpath_config/common/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@


class BaseConfig:
_SERIAL_NUMBER = SerialNumber("generic")
_SERIAL_NUMBER = SerialNumber('generic')
_NAMESPACE = Namespace()
_VERSION = 0
DLIM = "."
DLIM = '.'

def __init__(
self,
Expand Down Expand Up @@ -71,14 +71,12 @@ def template(self) -> dict:

@template.setter
def template(self, value: dict) -> None:
assert isinstance(value, dict), (
"template must of type 'dict'"
)
assert isinstance(value, dict), 'template must be of type "dict"'
# Check that template has all properties
flat_template = flatten_dict(d=value, dlim=BaseConfig.DLIM)
for _, val in flat_template.items():
assert isinstance(val, property), (
"All entries in template must be properties"
'All entries in template must be properties'
)
self._template = value

Expand All @@ -94,9 +92,7 @@ def config(self) -> dict:
def config(self, value: dict) -> None:
if value is None:
return
assert isinstance(value, dict), (
"config must be of type 'dict'"
)
assert isinstance(value, dict), 'config must be of type "dict"'
if self._parent_key is not None and self._parent_key not in value:
value = {self._parent_key: value}
value = unflatten_dict(value)
Expand Down Expand Up @@ -143,6 +139,4 @@ def set_namespace(cls, namespace: str | Namespace) -> None:
elif isinstance(namespace, str):
BaseConfig._NAMESPACE = Namespace(namespace)
else:
assert isinstance(namespace, str) or isinstance(namespace, Namespace), (
"Namespace must be of type 'str' or 'Namespace'"
)
assert isinstance(namespace, str) or isinstance(namespace, Namespace), 'Namespace must be of type "str" or "Namespace"' # noqa:E501
10 changes: 5 additions & 5 deletions clearpath_config/common/types/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
# POSSIBILITY OF SUCH DAMAGE.

class Discovery:
SIMPLE = "simple"
SERVER = "server"
SIMPLE = 'simple'
SERVER = 'server'

# All supported discovery modes, currently only set up for FastDDS
ALL_SUPPORTED = [SIMPLE, SERVER]
Expand Down Expand Up @@ -60,7 +60,7 @@ def is_valid(cls, mode: str) -> bool:

@classmethod
def assert_valid(cls, mode: str) -> None:
assert cls.is_valid(mode), ("\n".join[
f"Discovery mode '{mode}' not supported."
f"Discovery mode must be one of: '{cls.ALL_SUPPORTED}'"
assert cls.is_valid(mode), ('\n'.join[
f'Discovery mode '{mode}' not supported.'
f'Discovery mode must be one of: '{cls.ALL_SUPPORTED}''
])
4 changes: 2 additions & 2 deletions clearpath_config/common/types/domain_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def is_valid(id: int) -> bool:
def assert_valid(id: int) -> None:
# Check Type
assert isinstance(id, int), (
"Domain ID must be an integer"
'Domain ID must be an integer'
)
# 0 - 101 Range
assert 0 <= id <= 101, (
"Domain ID must be in range 0 - 101"
'Domain ID must be in range 0 - 101'
)
return
2 changes: 1 addition & 1 deletion clearpath_config/common/types/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __eq__(self, other: object) -> bool:
@staticmethod
def clean(path: str, make_abs=True) -> str:
if not path:
return ""
return ''
path = os.path.expanduser(path)
path = os.path.normpath(path)
if make_abs:
Expand Down
26 changes: 13 additions & 13 deletions clearpath_config/common/types/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# Hostname
# - hostname class
class Hostname:
def __init__(self, hostname: str = "hostname") -> None:
def __init__(self, hostname: str = 'hostname') -> None:
self.assert_valid(hostname)
self.hostname = hostname

Expand All @@ -54,35 +54,35 @@ def is_valid(hostname: str) -> bool:
# No Trailing Dots
# - not exactly a standard, but generally results in undefined
# behaviour and should be avoided
if hostname[-1] == ".":
if hostname[-1] == '.':
return False
# Only [A-Z][0-9] and '-' Allowed
# - cannot end or start with a hyphen ('-')
allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
allowed = re.compile(r'(?!-)[A-Z\d-]{1,63}(?<!-)$', re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split('.'))

@staticmethod
def assert_valid(hostname: str):
assert isinstance(hostname, str), (
"Hostname '%s' must be of type 'str'" % hostname
'Hostname '%s' must be of type 'str'' % hostname
)
# Min 1 ASCII Characters
assert len(hostname) > 0, (
"Hostname '%s' is blank." % hostname
'Hostname '%s' is blank.' % hostname
)
# Max 253 ASCII Characters
assert len(hostname) < 254, (
"Hostname '%s' exceeds 253 ASCII character limit." % hostname
'Hostname '%s' exceeds 253 ASCII character limit.' % hostname
)
# No Trailing Dots
assert hostname[-1] != ".", (
"Hostname '%s' should not end with a ('.') period." % hostname
assert hostname[-1] != '.', (
'Hostname '%s' should not end with a ('.') period.' % hostname
)
# Only [A-Z][0-9] and '-' Allowed
allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
assert all(allowed.match(x) for x in hostname.split(".")), (
"Hostname '%s' cannot contain characters other than %s." % (
allowed = re.compile(r'(?!-)[A-Z\d-]{1,63}(?<!-)$', re.IGNORECASE)
assert all(allowed.match(x) for x in hostname.split('.')), (
'Hostname '%s' cannot contain characters other than %s.' % (
hostname,
"[A-Z][0-9] and hypens ('-')"
'[A-Z][0-9] and hypens ('-')'
)
)
18 changes: 7 additions & 11 deletions clearpath_config/common/types/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# IP
# - ip class
class IP:
def __init__(self, ip: str = "0.0.0.0") -> None:
def __init__(self, ip: str = '0.0.0.0') -> None:
self.assert_valid(ip)
self.ip_str = ip

Expand All @@ -50,7 +50,7 @@ def is_valid(ip: str) -> bool:
if not isinstance(ip, str):
return False
# Must have Four Fields Delimited by '.'
fields = ip.split(".")
fields = ip.split('.')
if not len(fields) == 4:
return False
# All Fields must be Integers and 8 Bit Wide
Expand All @@ -65,17 +65,13 @@ def is_valid(ip: str) -> bool:
@staticmethod
def assert_valid(ip: str) -> None:
# Must be String
assert isinstance(ip, str), (
"IP '%s' must be string" % ip)
assert isinstance(ip, str), f'IP "{ip}" must be of type "str"'
# Must have Four Fields Delimited by '.'
fields = ip.split(".")
assert len(fields) == 4, (
"IP '%s' must have four entries" % ip)
fields = ip.split('.')
assert len(fields) == 4, f'IP "{ip}" must have 4 fields'
for field in fields:
# Fields Must be Integer
assert field.isdecimal(), (
"IP '%s' entries must be integers" % ip)
assert field.isdecimal(), f'IP "{ip}" fields must be integers'
# Fields Must be 8-Bits Wide
field_int = int(field)
assert 0 <= field_int < 256, (
"IP '%s' entries must in range 0 to 255" % ip)
assert 0 <= field_int < 256, f'IP "{ip} fields must be in range 0 to 255'
20 changes: 10 additions & 10 deletions clearpath_config/common/types/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@


# ListConfigs: Generic Types
T = TypeVar("T")
U = TypeVar("U")
T = TypeVar('T')
U = TypeVar('U')


# ListConfigs
Expand Down Expand Up @@ -72,7 +72,7 @@ def find(
# Error
else:
raise AssertionError(
"Object must be of type %s or %s" % (
'Object must be of type %s or %s' % (
self.__type_T.__name__,
self.__type_U.__name__
)
Expand All @@ -87,12 +87,12 @@ def add(
obj: T,
) -> None:
assert isinstance(obj, self.__type_T), (
"Object must be of type %s" % (
'Object must be of type %s' % (
self.__type_T.__name__
)
)
assert self.find(obj) is None, (
"Object with uid %s is not unique." % (
'Object with uid %s is not unique.' % (
self.__uid(obj)
)
)
Expand All @@ -103,10 +103,10 @@ def replace(
obj: T,
) -> None:
assert isinstance(obj, self.__type_T), (
"Object must be of type %s" % T
'Object must be of type %s' % T
)
assert self.find(obj) is not None, (
"Object with uid %s cannot be replaced. Does not exist." % (
'Object with uid %s cannot be replaced. Does not exist.' % (
self.__uid(obj)
)
)
Expand Down Expand Up @@ -201,7 +201,7 @@ def find(
idx = obj
else:
raise AssertionError(
"Object must of type %s or %s" % (
'Object must of type %s or %s' % (
self.__type_T, int
)
)
Expand All @@ -220,7 +220,7 @@ def add(
obj: T
) -> None:
assert isinstance(obj, self.__type_T), (
"Object must be of type %s" % T
'Object must be of type %s' % T
)
self.__list.append(obj)
self.update()
Expand All @@ -231,7 +231,7 @@ def replace(
) -> None:
idx = self.find(obj)
assert idx is not None, (
"Object not found. Cannot be replaced"
'Object not found. Cannot be replaced'
)
self.__list[idx - self.start_idx] = obj
self.update()
Expand Down
Loading

0 comments on commit baed5bf

Please sign in to comment.