From 42e1a2e85617bbbfc83d5377a2fc456b383703fa Mon Sep 17 00:00:00 2001 From: Konstantin Valetov Date: Sat, 31 Oct 2020 19:00:16 +0300 Subject: [PATCH] increase maximum line length to 120 --- README.md | 6 ++++++ aiosnmp/connection.py | 8 ++------ aiosnmp/exceptions.py | 28 ++++++---------------------- aiosnmp/message.py | 4 +--- aiosnmp/protocols.py | 20 +++++--------------- aiosnmp/snmp.py | 34 +++++++--------------------------- azure-pipelines.yml | 2 +- examples/trap.py | 4 +--- setup.cfg | 4 ++-- tests/conftest.py | 8 ++------ tests/test_asn1.py | 22 +++++----------------- tests/test_snmp.py | 38 +++++++++----------------------------- tox.ini | 10 ++++++++++ 13 files changed, 57 insertions(+), 131 deletions(-) diff --git a/README.md b/README.md index baff077..fb1f162 100644 --- a/README.md +++ b/README.md @@ -45,3 +45,9 @@ aiosnmp is developed and distributed under the MIT license. pip install -r requirements-dev.txt tox ``` + +## Before submitting PR +```shell +pip install -r requirements-dev.txt +tox -e format +``` diff --git a/aiosnmp/connection.py b/aiosnmp/connection.py index 78995ee..99cde28 100644 --- a/aiosnmp/connection.py +++ b/aiosnmp/connection.py @@ -45,15 +45,11 @@ async def _connect(self) -> None: lambda: SnmpProtocol(self.timeout, self.retries), remote_addr=(self.host, self.port), ) - transport, protocol = await asyncio.wait_for( - connect_future, timeout=self.timeout - ) + transport, protocol = await asyncio.wait_for(connect_future, timeout=self.timeout) self._protocol = cast(SnmpProtocol, protocol) self._transport = cast(asyncio.DatagramTransport, transport) - self._peername = self._transport.get_extra_info( - "peername", default=(self.host, self.port) - ) + self._peername = self._transport.get_extra_info("peername", default=(self.host, self.port)) @property def is_closed(self) -> bool: diff --git a/aiosnmp/exceptions.py b/aiosnmp/exceptions.py index a753e9c..dca5ae3 100644 --- a/aiosnmp/exceptions.py +++ b/aiosnmp/exceptions.py @@ -49,10 +49,7 @@ def __init__(self, index: int, oid: Optional[str] = None) -> None: class SnmpErrorTooBig(SnmpErrorStatus): - message = ( - "The agent could not place the results " - "of the requested SNMP operation in a single SNMP message." - ) + message = "The agent could not place the results " "of the requested SNMP operation in a single SNMP message." class SnmpErrorNoSuchName(SnmpErrorStatus): @@ -61,8 +58,7 @@ class SnmpErrorNoSuchName(SnmpErrorStatus): class SnmpErrorBadValue(SnmpErrorStatus): message = ( - "The requested SNMP operation tried to change a variable " - "but it specified either a syntax or value error." + "The requested SNMP operation tried to change a variable " "but it specified either a syntax or value error." ) @@ -75,10 +71,7 @@ class SnmpErrorReadOnly(SnmpErrorStatus): class SnmpErrorGenErr(SnmpErrorStatus): - message = ( - "An error other than one of those listed here " - "occurred during the requested SNMP operation." - ) + message = "An error other than one of those listed here " "occurred during the requested SNMP operation." class SnmpErrorNoAccess(SnmpErrorStatus): @@ -86,17 +79,11 @@ class SnmpErrorNoAccess(SnmpErrorStatus): class SnmpErrorWrongType(SnmpErrorStatus): - message = ( - "The value specifies a type that is inconsistent " - "with the type required for the variable." - ) + message = "The value specifies a type that is inconsistent " "with the type required for the variable." class SnmpErrorWrongLength(SnmpErrorStatus): - message = ( - "The value specifies a length that is inconsistent " - "with the length required for the variable." - ) + message = "The value specifies a length that is inconsistent " "with the length required for the variable." class SnmpErrorWrongEncoding(SnmpErrorStatus): @@ -119,10 +106,7 @@ class SnmpErrorInconsistentValue(SnmpErrorStatus): class SnmpErrorResourceUnavailable(SnmpErrorStatus): - message = ( - "Assigning the value to the variable requires allocation of resources " - "that are currently unavailable." - ) + message = "Assigning the value to the variable requires allocation of resources " "that are currently unavailable." class SnmpErrorCommitFailed(SnmpErrorStatus): diff --git a/aiosnmp/message.py b/aiosnmp/message.py index 9ae9029..030d941 100644 --- a/aiosnmp/message.py +++ b/aiosnmp/message.py @@ -85,9 +85,7 @@ class BulkPDU: _PDUType: PDUType - def __init__( - self, varbinds: List[SnmpVarbind], non_repeaters: int, max_repetitions: int - ) -> None: + def __init__(self, varbinds: List[SnmpVarbind], non_repeaters: int, max_repetitions: int) -> None: self.request_id = random.randrange(1, 2_147_483_647) self.non_repeaters: int = non_repeaters self.max_repetitions: int = max_repetitions diff --git a/aiosnmp/protocols.py b/aiosnmp/protocols.py index 76021db..4d232f9 100644 --- a/aiosnmp/protocols.py +++ b/aiosnmp/protocols.py @@ -74,9 +74,7 @@ def datagram_received(self, data: Union[bytes, Text], addr: Address) -> None: logger.warning(f"could not decode received data from {host}:{port}: {exc}") return - if not message or ( - self.communities and message.community not in self.communities - ): + if not message or (self.communities and message.community not in self.communities): return asyncio.ensure_future(self.handler(host, port, message)) @@ -114,9 +112,7 @@ def datagram_received(self, data: Union[bytes, Text], addr: Address) -> None: oid = None if len(message.data.varbinds) > 0 and index - 1 >= 0: oid = message.data.varbinds[index - 1].oid - exception = _ERROR_STATUS_TO_EXCEPTION[message.data.error_status]( - index, oid - ) + exception = _ERROR_STATUS_TO_EXCEPTION[message.data.error_status](index, oid) try: if exception: self.requests[key].set_exception(exception) @@ -129,20 +125,14 @@ def datagram_received(self, data: Union[bytes, Text], addr: Address) -> None: def is_connected(self) -> bool: return bool(self.transport is not None and not self.transport.is_closing()) - async def _send( - self, message: SnmpMessage, host: str, port: int - ) -> List[SnmpVarbind]: + async def _send(self, message: SnmpMessage, host: str, port: int) -> List[SnmpVarbind]: key = (host, port, message.data.request_id) fut: asyncio.Future = self.loop.create_future() - fut.add_done_callback( - lambda fn: self.requests.pop(key) if key in self.requests else None - ) + fut.add_done_callback(lambda fn: self.requests.pop(key) if key in self.requests else None) self.requests[key] = fut for _ in range(self.retries): self.transport.sendto(message.encode()) - done, _ = await asyncio.wait( - {fut}, timeout=self.timeout, return_when=asyncio.ALL_COMPLETED - ) + done, _ = await asyncio.wait({fut}, timeout=self.timeout, return_when=asyncio.ALL_COMPLETED) if not done: continue r: List[SnmpVarbind] = fut.result() diff --git a/aiosnmp/snmp.py b/aiosnmp/snmp.py index c913ff1..bbde02b 100644 --- a/aiosnmp/snmp.py +++ b/aiosnmp/snmp.py @@ -7,15 +7,7 @@ from .connection import SnmpConnection from .exceptions import SnmpUnsupportedValueType -from .message import ( - GetBulkRequest, - GetNextRequest, - GetRequest, - SetRequest, - SnmpMessage, - SnmpVarbind, - SnmpVersion, -) +from .message import GetBulkRequest, GetNextRequest, GetRequest, SetRequest, SnmpMessage, SnmpVarbind, SnmpVersion class Snmp(SnmpConnection): @@ -77,9 +69,7 @@ async def _send(self, message: SnmpMessage) -> List[SnmpVarbind]: async def get(self, oids: Union[str, List[str]]) -> List[SnmpVarbind]: if isinstance(oids, str): oids = [oids] - message = SnmpMessage( - self.version, self.community, GetRequest([SnmpVarbind(oid) for oid in oids]) - ) + message = SnmpMessage(self.version, self.community, GetRequest([SnmpVarbind(oid) for oid in oids])) return await self._send(message) async def get_next(self, oids: Union[str, List[str]]) -> List[SnmpVarbind]: @@ -112,23 +102,17 @@ async def get_bulk( async def walk(self, oid: str) -> List[SnmpVarbind]: varbinds: List[SnmpVarbind] = [] - message = SnmpMessage( - self.version, self.community, GetNextRequest([SnmpVarbind(oid)]) - ) + message = SnmpMessage(self.version, self.community, GetNextRequest([SnmpVarbind(oid)])) base_oid = oid if oid.startswith(".") else f".{oid}" vbs = await self._send(message) next_oid = vbs[0].oid if not next_oid.startswith(f"{base_oid}."): - message = SnmpMessage( - self.version, self.community, GetRequest([SnmpVarbind(base_oid)]) - ) + message = SnmpMessage(self.version, self.community, GetRequest([SnmpVarbind(base_oid)])) return await self._send(message) varbinds.append(vbs[0]) while True: - message = SnmpMessage( - self.version, self.community, GetNextRequest([SnmpVarbind(next_oid)]) - ) + message = SnmpMessage(self.version, self.community, GetNextRequest([SnmpVarbind(next_oid)])) vbs = await self._send(message) next_oid = vbs[0].oid if not next_oid.startswith(f"{base_oid}."): @@ -136,14 +120,10 @@ async def walk(self, oid: str) -> List[SnmpVarbind]: varbinds.append(vbs[0]) return varbinds - async def set( - self, varbinds: List[Tuple[str, Union[int, str, bytes, ipaddress.IPv4Address]]] - ) -> List[SnmpVarbind]: + async def set(self, varbinds: List[Tuple[str, Union[int, str, bytes, ipaddress.IPv4Address]]]) -> List[SnmpVarbind]: for varbind in varbinds: if not isinstance(varbind[1], (int, str, bytes, ipaddress.IPv4Address)): - raise SnmpUnsupportedValueType( - f"Only int, str, bytes and ip address supported, got {type(varbind[1])}" - ) + raise SnmpUnsupportedValueType(f"Only int, str, bytes and ip address supported, got {type(varbind[1])}") message = SnmpMessage( self.version, self.community, diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 40609b3..897f739 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -32,7 +32,7 @@ jobs: - script: isort -q --check --diff aiosnmp tests examples displayName: 'Run isort' - - script: black -q --check --diff aiosnmp tests examples + - script: black -l 120 -q --check --diff aiosnmp tests examples displayName: 'Run black' - script: mypy aiosnmp diff --git a/examples/trap.py b/examples/trap.py index dca6edf..e4ce87c 100644 --- a/examples/trap.py +++ b/examples/trap.py @@ -10,9 +10,7 @@ async def handler(host: str, port: int, message: aiosnmp.SnmpV2TrapMessage) -> N async def main(): - p = aiosnmp.SnmpV2TrapServer( - host="127.0.0.1", port=162, communities=("public",), handler=handler - ) + p = aiosnmp.SnmpV2TrapServer(host="127.0.0.1", port=162, communities=("public",), handler=handler) await p.run() diff --git a/setup.cfg b/setup.cfg index 3a64c39..392f700 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,12 +6,12 @@ test = pytest [flake8] ignore = E203, E266, E501, W503 -max-line-length = 88 +max-line-length = 120 max-complexity = 18 select = B,C,E,F,W,T4,B9 [isort] -line_length = 88 +line_length = 120 multi_line_output = 3 include_trailing_comma = true known_first_party = aiosnmp diff --git a/tests/conftest.py b/tests/conftest.py index 7452d50..f159813 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,9 +10,7 @@ def pytest_addoption(parser): - parser.addoption( - "--event-loop", action="store", default="asyncio", choices=["asyncio", "uvloop"] - ) + parser.addoption("--event-loop", action="store", default="asyncio", choices=["asyncio", "uvloop"]) def pytest_configure(config): @@ -28,6 +26,4 @@ def pytest_generate_tests(metafunc): if "host" in metafunc.fixturenames: metafunc.parametrize("host", ["127.0.0.1", "localhost", "::1"]) if "port" in metafunc.fixturenames: - metafunc.parametrize( - "port", [int(os.environ.get("KOSHH/AIOSNMP_161_UDP", 161))] - ) + metafunc.parametrize("port", [int(os.environ.get("KOSHH/AIOSNMP_161_UDP", 161))]) diff --git a/tests/test_asn1.py b/tests/test_asn1.py index 5584267..de3651e 100644 --- a/tests/test_asn1.py +++ b/tests/test_asn1.py @@ -29,10 +29,7 @@ def test_long_integer(self) -> None: enc = asn1.Encoder() enc.write(0x0102030405060708090A0B0C0D0E0F) res = enc.output() - assert ( - res - == b"\x02\x0f\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" - ) + assert res == b"\x02\x0f\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" def test_negative_integer(self) -> None: enc = asn1.Encoder() @@ -44,10 +41,7 @@ def test_long_negative_integer(self) -> None: enc = asn1.Encoder() enc.write(-0x0102030405060708090A0B0C0D0E0F) res = enc.output() - assert ( - res - == b"\x02\x0f\xfe\xfd\xfc\xfb\xfa\xf9\xf8\xf7\xf6\xf5\xf4\xf3\xf2\xf1\xf1" - ) + assert res == b"\x02\x0f\xfe\xfd\xfc\xfb\xfa\xf9\xf8\xf7\xf6\xf5\xf4\xf3\xf2\xf1\xf1" @pytest.mark.parametrize( ("number", "result"), @@ -209,9 +203,7 @@ def test_error_stack(self) -> None: with pytest.raises(asn1.Error): enc.output() - @pytest.mark.parametrize( - "value", ["1", "40.2.3", "1.40.3", "1.2.3.", ".1.2.3", "foo", "foo.bar"] - ) + @pytest.mark.parametrize("value", ["1", "40.2.3", "1.40.3", "1.2.3.", ".1.2.3", "foo", "foo.bar"]) def test_error_object_identifier(self, value) -> None: enc = asn1.Encoder() with pytest.raises(asn1.Error): @@ -231,9 +223,7 @@ def test_boolean(self, buf: bytes, result: int) -> None: assert isinstance(val, int) assert val == result - @pytest.mark.parametrize( - ("buf", "result"), ((b"\x02\x01\x01", 1), (b"\x02\x04\xff\xff\xff\xff", -1)) - ) + @pytest.mark.parametrize(("buf", "result"), ((b"\x02\x01\x01", 1), (b"\x02\x04\xff\xff\xff\xff", -1))) def test_integer(self, buf: bytes, result: int) -> None: dec = asn1.Decoder(buf) tag = dec.peek() @@ -591,9 +581,7 @@ def test_error_object_identifier_with_too_large_first_component(self) -> None: dec.read() def test_big_negative_integer(self) -> None: - buf = ( - b"\x02\x10\xff\x7f\x2b\x3a\x4d\xea\x48\x1e\x1f\x37\x7b\xa8\xbd\x7f\xb0\x16" - ) + buf = b"\x02\x10\xff\x7f\x2b\x3a\x4d\xea\x48\x1e\x1f\x37\x7b\xa8\xbd\x7f\xb0\x16" dec = asn1.Decoder(buf) tag, val = dec.read() assert val == -668929531791034950848739021124816874 diff --git a/tests/test_snmp.py b/tests/test_snmp.py index c7d8064..ec0807b 100644 --- a/tests/test_snmp.py +++ b/tests/test_snmp.py @@ -26,9 +26,7 @@ ([".1.3.6.1.4.1.8072.2.255.6.0"], 42), ), ) -async def test_snmp_types( - host: str, port: int, oid: Union[str, List[str]], value: Any -) -> None: +async def test_snmp_types(host: str, port: int, oid: Union[str, List[str]], value: Any) -> None: async with Snmp(host=host, port=port) as snmp: results = await snmp.get(oid) assert len(results) == 1 @@ -59,9 +57,7 @@ async def test_snmp_get_bulk(host: str, port: int, max_repetitions: int) -> None @pytest.mark.asyncio @pytest.mark.parametrize("max_repetitions", (1, 2, 5, 10, 25)) async def test_snmp_bulk_walk(host: str, port: int, max_repetitions: int) -> None: - async with Snmp( - host=host, port=port, timeout=3, max_repetitions=max_repetitions - ) as snmp: + async with Snmp(host=host, port=port, timeout=3, max_repetitions=max_repetitions) as snmp: results = await snmp.bulk_walk(".1.3.6.1.2.1.1.9") assert len(results) == 30 for res in results: @@ -105,9 +101,7 @@ async def test_snmp_bulk_walk_end_of_mibs_from_the_start(host: str, port: int) - @pytest.mark.asyncio -async def test_snmp_bulk_walk_end_of_mibs_after_some_requests( - host: str, port: int -) -> None: +async def test_snmp_bulk_walk_end_of_mibs_after_some_requests(host: str, port: int) -> None: async with Snmp(host=host, port=port, max_repetitions=15) as snmp: results = await snmp.bulk_walk(".1.3.6.1.6.3.16.1.5.2.1") assert len(results) == 24 @@ -136,9 +130,7 @@ async def test_snmp_non_existing_oid(host: str, port: int) -> None: ), ) @pytest.mark.asyncio -async def test_snmp_multiple_oids( - host: str, port: int, oids: List[str], values: List[Any] -) -> None: +async def test_snmp_multiple_oids(host: str, port: int, oids: List[str], values: List[Any]) -> None: async with Snmp(host=host, port=port) as snmp: results = await snmp.get(oids) assert len(results) == len(oids) @@ -165,19 +157,13 @@ async def test_snmp_multiple_oids( ], ), ) -async def test_snmp_set( - host: str, port: int, varbinds: List[Tuple[str, Union[int, str, bytes]]] -) -> None: +async def test_snmp_set(host: str, port: int, varbinds: List[Tuple[str, Union[int, str, bytes]]]) -> None: async with Snmp(host=host, port=port, timeout=3, community="private") as snmp: results = await snmp.set(varbinds) assert len(results) == len(varbinds) for varbind, res in zip(varbinds, results): assert res.oid == varbind[0] - assert ( - res.value == varbind[1] - if not isinstance(varbind[1], str) - else varbind[1].encode() - ) + assert res.value == varbind[1] if not isinstance(varbind[1], str) else varbind[1].encode() @pytest.mark.asyncio @@ -208,9 +194,7 @@ async def test_snmp_get_next_no_leading_dot(host: str, port: int) -> None: @pytest.mark.asyncio @pytest.mark.parametrize("max_repetitions", (1, 2, 5, 10, 25)) -async def test_snmp_get_bulk_no_leading_dot( - host: str, port: int, max_repetitions: int -) -> None: +async def test_snmp_get_bulk_no_leading_dot(host: str, port: int, max_repetitions: int) -> None: async with Snmp(host=host, port=port, max_repetitions=max_repetitions) as snmp: results = await snmp.get_bulk("1.3.6.1.2.1.1") assert len(results) == max_repetitions @@ -220,12 +204,8 @@ async def test_snmp_get_bulk_no_leading_dot( @pytest.mark.asyncio @pytest.mark.parametrize("max_repetitions", (1, 2, 5, 10, 25)) -async def test_snmp_bulk_walk_no_leading_dot( - host: str, port: int, max_repetitions: int -) -> None: - async with Snmp( - host=host, port=port, timeout=3, max_repetitions=max_repetitions - ) as snmp: +async def test_snmp_bulk_walk_no_leading_dot(host: str, port: int, max_repetitions: int) -> None: + async with Snmp(host=host, port=port, timeout=3, max_repetitions=max_repetitions) as snmp: results = await snmp.bulk_walk("1.3.6.1.2.1.1.9") assert len(results) == 30 for res in results: diff --git a/tox.ini b/tox.ini index ce0a8b7..49fdec4 100644 --- a/tox.ini +++ b/tox.ini @@ -27,3 +27,13 @@ commands = mypy aiosnmp docker = skip_install = true + +[testenv:format] +deps = + black + isort +commands = + isort aiosnmp tests examples + black -l 120 aiosnmp tests examples +docker = +skip_install = true