Enable pylint checks for "anomalous" string escapes

Escapes in python string literals are well defined, but can be
confusing.  These pylint checks look for backslash escapes in strings
that might be mistakes.  Two code refactors were required to satisfy
these tests:

1. midonet_lib.py used \**kwargs in docstrings.

There doesn't seem to be a sphinx standard for kwargs, so this change
simply replaces them with "kwargs".

2. Regex literals containing escapes replaced with r''.

The assumption with this change (and the underlying pylint
check) is that r'' literals are more straightforward for regular
expressions, where every backslash is important.

While looking at these regexes, this change also removes a few
unnecessary "\-" escapes.

Change-Id: I01528b2482f78b9e851685ebbf6fded4e58355f1
This commit is contained in:
Angus Lees 2014-10-22 21:20:23 +11:00
parent d51bb9b4d0
commit e9cee51e56
10 changed files with 18 additions and 21 deletions

View File

@ -27,11 +27,8 @@ disable=
not-callable, not-callable,
no-value-for-parameter, no-value-for-parameter,
super-on-old-class, super-on-old-class,
too-few-format-args,
# "W" Warnings for stylistic problems or minor programming issues # "W" Warnings for stylistic problems or minor programming issues
abstract-method, abstract-method,
anomalous-backslash-in-string,
anomalous-unicode-escape-in-string,
arguments-differ, arguments-differ,
attribute-defined-outside-init, attribute-defined-outside-init,
bad-builtin, bad-builtin,

View File

@ -45,10 +45,10 @@ class IpLinkConstants(object):
class IpLinkSupport(object): class IpLinkSupport(object):
VF_BLOCK_REGEX = "\[ vf NUM(?P<vf_block>.*) \] \]" VF_BLOCK_REGEX = r"\[ vf NUM(?P<vf_block>.*) \] \]"
CAPABILITY_REGEX = "\[ %s (.*)" CAPABILITY_REGEX = r"\[ %s (.*)"
SUB_CAPABILITY_REGEX = "\[ %(cap)s (.*) \[ %(subcap)s (.*)" SUB_CAPABILITY_REGEX = r"\[ %(cap)s (.*) \[ %(subcap)s (.*)"
@classmethod @classmethod
def get_vf_mgmt_section(cls, root_helper=None): def get_vf_mgmt_section(cls, root_helper=None):

View File

@ -143,7 +143,7 @@ def _validate_range(data, valid_values=None):
def _validate_no_whitespace(data): def _validate_no_whitespace(data):
"""Validates that input has no whitespace.""" """Validates that input has no whitespace."""
if re.search('\s', data): if re.search(r'\s', data):
msg = _("'%s' contains whitespace") % data msg = _("'%s' contains whitespace") % data
LOG.debug(msg) LOG.debug(msg)
raise n_exc.InvalidInput(error_message=msg) raise n_exc.InvalidInput(error_message=msg)

View File

@ -274,7 +274,7 @@ RESOURCE_ATTRIBUTE_MAP = {
'expected_codes': {'allow_post': True, 'allow_put': True, 'expected_codes': {'allow_post': True, 'allow_put': True,
'validate': { 'validate': {
'type:regex': 'type:regex':
'^(\d{3}(\s*,\s*\d{3})*)$|^(\d{3}-\d{3})$'}, r'^(\d{3}(\s*,\s*\d{3})*)$|^(\d{3}-\d{3})$'},
'default': '200', 'default': '200',
'is_visible': True}, 'is_visible': True},
'admin_state_up': {'allow_post': True, 'allow_put': True, 'admin_state_up': {'allow_post': True, 'allow_put': True,

View File

@ -280,7 +280,7 @@ RESOURCE_ATTRIBUTE_MAP = {
'allow_post': True, 'allow_post': True,
'allow_put': True, 'allow_put': True,
'validate': { 'validate': {
'type:regex': '^(\d{3}(\s*,\s*\d{3})*)$|^(\d{3}-\d{3})$' 'type:regex': r'^(\d{3}(\s*,\s*\d{3})*)$|^(\d{3}-\d{3})$'
}, },
'default': '200', 'default': '200',
'is_visible': True 'is_visible': True

View File

@ -119,7 +119,7 @@ class HashHandler(object):
return result.rowcount != 0 return result.rowcount != 0
def _get_lock_owner(self, record): def _get_lock_owner(self, record):
matches = re.findall("^LOCKED_BY\[(\w+)\]", record) matches = re.findall(r"^LOCKED_BY\[(\w+)\]", record)
if not matches: if not matches:
return None return None
return matches[0] return matches[0]

View File

@ -72,7 +72,7 @@ class MidoClient:
def create_bridge(self, **kwargs): def create_bridge(self, **kwargs):
"""Create a new bridge """Create a new bridge
:param \**kwargs: configuration of the new bridge :param kwargs: configuration of the new bridge
:returns: newly created bridge :returns: newly created bridge
""" """
LOG.debug("MidoClient.create_bridge called: " LOG.debug("MidoClient.create_bridge called: "
@ -106,7 +106,7 @@ class MidoClient:
"""Update a bridge of the given id with the new fields """Update a bridge of the given id with the new fields
:param id: id of the bridge :param id: id of the bridge
:param \**kwargs: the fields to update and their values :param kwargs: the fields to update and their values
:returns: bridge object :returns: bridge object
""" """
LOG.debug("MidoClient.update_bridge called: " LOG.debug("MidoClient.update_bridge called: "
@ -250,7 +250,7 @@ class MidoClient:
"""Add a port on a bridge """Add a port on a bridge
:param bridge: bridge to add a new port to :param bridge: bridge to add a new port to
:param \**kwargs: configuration of the new port :param kwargs: configuration of the new port
:returns: newly created port :returns: newly created port
""" """
LOG.debug("MidoClient.add_bridge_port called: " LOG.debug("MidoClient.add_bridge_port called: "
@ -263,7 +263,7 @@ class MidoClient:
"""Update a port of the given id with the new fields """Update a port of the given id with the new fields
:param id: id of the port :param id: id of the port
:param \**kwargs: the fields to update and their values :param kwargs: the fields to update and their values
""" """
LOG.debug("MidoClient.update_port called: " LOG.debug("MidoClient.update_port called: "
"id=%(id)s, kwargs=%(kwargs)s", "id=%(id)s, kwargs=%(kwargs)s",
@ -278,7 +278,7 @@ class MidoClient:
"""Add a new port to an existing router. """Add a new port to an existing router.
:param router: router to add a new port to :param router: router to add a new port to
:param \**kwargs: configuration of the new port :param kwargs: configuration of the new port
:returns: newly created port :returns: newly created port
""" """
return self._create_dto(self.mido_api.add_router_port(router), kwargs) return self._create_dto(self.mido_api.add_router_port(router), kwargs)
@ -287,7 +287,7 @@ class MidoClient:
def create_router(self, **kwargs): def create_router(self, **kwargs):
"""Create a new router """Create a new router
:param \**kwargs: configuration of the new router :param kwargs: configuration of the new router
:returns: newly created router :returns: newly created router
""" """
LOG.debug("MidoClient.create_router called: " LOG.debug("MidoClient.create_router called: "
@ -321,7 +321,7 @@ class MidoClient:
"""Update a router of the given id with the new name """Update a router of the given id with the new name
:param id: id of the router :param id: id of the router
:param \**kwargs: the fields to update and their values :param kwargs: the fields to update and their values
:returns: router object :returns: router object
""" """
LOG.debug("MidoClient.update_router called: " LOG.debug("MidoClient.update_router called: "

View File

@ -42,8 +42,8 @@ from neutron.plugins.ml2.drivers import type_vlan # noqa
from neutron import service from neutron import service
ACI_PORT_DESCR_FORMATS = [ ACI_PORT_DESCR_FORMATS = [
'topology/pod-1/node-(\d+)/sys/conng/path-\[eth(\d+)/(\d+)\]', r'topology/pod-1/node-(\d+)/sys/conng/path-\[eth(\d+)/(\d+)\]',
'topology/pod-1/paths-(\d+)/pathep-\[eth(\d+)/(\d+)\]', r'topology/pod-1/paths-(\d+)/pathep-\[eth(\d+)/(\d+)\]',
] ]
AGENT_FORCE_UPDATE_COUNT = 100 AGENT_FORCE_UPDATE_COUNT = 100
BINARY_APIC_SERVICE_AGENT = 'neutron-cisco-apic-service-agent' BINARY_APIC_SERVICE_AGENT = 'neutron-cisco-apic-service-agent'

View File

@ -30,7 +30,7 @@ class PciOsWrapper(object):
DEVICE_PATH = "/sys/class/net/%s/device" DEVICE_PATH = "/sys/class/net/%s/device"
PCI_PATH = "/sys/class/net/%s/device/virtfn%s/net" PCI_PATH = "/sys/class/net/%s/device/virtfn%s/net"
VIRTFN_FORMAT = "^virtfn(?P<vf_index>\d+)" VIRTFN_FORMAT = r"^virtfn(?P<vf_index>\d+)"
VIRTFN_REG_EX = re.compile(VIRTFN_FORMAT) VIRTFN_REG_EX = re.compile(VIRTFN_FORMAT)
MAC_VTAP_PREFIX = "upper_macvtap*" MAC_VTAP_PREFIX = "upper_macvtap*"

View File

@ -246,7 +246,7 @@ class OwnerCheck(policy.Check):
def __init__(self, kind, match): def __init__(self, kind, match):
# Process the match # Process the match
try: try:
self.target_field = re.findall('^\%\((.*)\)s$', self.target_field = re.findall(r'^\%\((.*)\)s$',
match)[0] match)[0]
except IndexError: except IndexError:
err_reason = (_("Unable to identify a target field from:%s." err_reason = (_("Unable to identify a target field from:%s."