Skip to content

Commit f989f94

Browse files
committed
fix linter
1 parent 15b6757 commit f989f94

4 files changed

Lines changed: 60 additions & 68 deletions

File tree

lib/pyclamd.py

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,8 @@ class ConnectionError(socket.error):
157157
"""Class for errors communication with clamd"""
158158

159159

160-
# Python 2/3 compatibility
161-
try:
162-
basestring # attempt to evaluate basestring
163-
164-
def isstr(s):
165-
return isinstance(s, basestring)
166-
167-
except NameError:
168-
169-
def isstr(s):
170-
return isinstance(s, str)
160+
def isstr(s):
161+
return isinstance(s, str)
171162

172163

173164
############################################################################
@@ -311,9 +302,9 @@ def scan_file(self, file):
311302
- socket.timeout: if timeout has expired
312303
"""
313304

314-
assert isstr(
315-
file
316-
), "Wrong type for [file], should be a string [was {0}]".format(type(file))
305+
assert isstr(file), (
306+
"Wrong type for [file], should be a string [was {0}]".format(type(file))
307+
)
317308

318309
try:
319310
self._init_socket()
@@ -359,9 +350,9 @@ def multiscan_file(self, file):
359350
May raise:
360351
- ConnectionError: in case of communication problem
361352
"""
362-
assert isstr(
363-
file
364-
), "Wrong type for [file], should be a string [was {0}]".format(type(file))
353+
assert isstr(file), (
354+
"Wrong type for [file], should be a string [was {0}]".format(type(file))
355+
)
365356

366357
try:
367358
self._init_socket()
@@ -407,9 +398,9 @@ def allmatchscan(self, file):
407398
- ConnectionError: in case of communication problem
408399
- socket.timeout: if timeout has expired
409400
"""
410-
assert isstr(
411-
file
412-
), "Wrong type for [file], should be a string [was {0}]".format(type(file))
401+
assert isstr(file), (
402+
"Wrong type for [file], should be a string [was {0}]".format(type(file))
403+
)
413404

414405
dr = {}
415406

@@ -469,9 +460,9 @@ def contscan_file(self, file):
469460
May raise:
470461
- ConnectionError: in case of communication problem
471462
"""
472-
assert isstr(
473-
file
474-
), "Wrong type for [file], should be a string [was {0}]".format(type(file))
463+
assert isstr(file), (
464+
"Wrong type for [file], should be a string [was {0}]".format(type(file))
465+
)
475466

476467
try:
477468
self._init_socket()
@@ -521,17 +512,17 @@ def scan_stream(self, stream, chunk_size=4096):
521512
"""
522513
if sys.version_info[0] <= 2:
523514
# Python2
524-
assert hasattr(stream, "read") or isinstance(
525-
stream, str
526-
), "Wrong type for [stream], should be str/file-like [was {0}]".format(
527-
type(stream)
515+
assert hasattr(stream, "read") or isinstance(stream, str), (
516+
"Wrong type for [stream], should be str/file-like [was {0}]".format(
517+
type(stream)
518+
)
528519
)
529520
else:
530521
# Python3
531-
assert hasattr(stream, "read") or isinstance(
532-
stream, (bytes, bytearray)
533-
), "Wrong type for [stream], should be bytes/bytearray/file-like [was {0}]".format(
534-
type(stream)
522+
assert hasattr(stream, "read") or isinstance(stream, (bytes, bytearray)), (
523+
"Wrong type for [stream], should be bytes/bytearray/file-like [was {0}]".format(
524+
type(stream)
525+
)
535526
)
536527

537528
is_file_like = hasattr(stream, "read")
@@ -580,7 +571,6 @@ def scan_stream(self, stream, chunk_size=4096):
580571
raise ConnectionError("Unable to scan stream")
581572

582573
if len(result) > 0:
583-
584574
if result == "INSTREAM size limit exceeded. ERROR":
585575
raise BufferTooLongError(result)
586576

@@ -693,7 +683,11 @@ def __init__(self, filename=None, timeout=None):
693683

694684
# try to get unix socket from clamd.conf
695685
if filename is None:
696-
for clamdpath in ["/etc/clamav/clamd.conf", "/etc/clamd.conf", "/opt/homebrew/etc/clamav/clamd.conf"]:
686+
for clamdpath in [
687+
"/etc/clamav/clamd.conf",
688+
"/etc/clamd.conf",
689+
"/opt/homebrew/etc/clamav/clamd.conf",
690+
]:
697691
if os.path.isfile(clamdpath):
698692
break
699693
else:
@@ -715,13 +709,13 @@ def __init__(self, filename=None, timeout=None):
715709
"Could not find clamd unix socket from /etc/clamav/clamd.conf or /etc/clamd.conf"
716710
)
717711

718-
assert isstr(
719-
filename
720-
), "Wrong type for [file], should be a string [was {0}]".format(type(file))
721-
assert (
722-
isinstance(timeout, (float, int)) or timeout is None
723-
), "Wrong type for [timeout], should be either None or a float [was {0}]".format(
724-
type(timeout)
712+
assert isstr(filename), (
713+
"Wrong type for [file], should be a string [was {0}]".format(type(filename))
714+
)
715+
assert isinstance(timeout, (float, int)) or timeout is None, (
716+
"Wrong type for [timeout], should be either None or a float [was {0}]".format(
717+
type(timeout)
718+
)
725719
)
726720

727721
_ClamdGeneric.__init__(self)
@@ -740,7 +734,7 @@ def _init_socket(self):
740734
internal use only
741735
"""
742736
self.clamd_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
743-
if not self.timeout is None:
737+
if self.timeout:
744738
self.clamd_socket.settimeout(self.timeout)
745739

746740
try:
@@ -770,16 +764,16 @@ def __init__(self, host="127.0.0.1", port=3310, timeout=None):
770764
timeout (float or None) : socket timeout
771765
"""
772766

773-
assert isinstance(
774-
host, str
775-
), "Wrong type for [host], should be a string [was {0}]".format(type(host))
776-
assert isinstance(
777-
port, int
778-
), "Wrong type for [port], should be an int [was {0}]".format(type(port))
779-
assert (
780-
isinstance(timeout, (float, int)) or timeout is None
781-
), "Wrong type for [timeout], should be either None or a float [was {0}]".format(
782-
type(timeout)
767+
assert isinstance(host, str), (
768+
"Wrong type for [host], should be a string [was {0}]".format(type(host))
769+
)
770+
assert isinstance(port, int), (
771+
"Wrong type for [port], should be an int [was {0}]".format(type(port))
772+
)
773+
assert isinstance(timeout, (float, int)) or timeout is None, (
774+
"Wrong type for [timeout], should be either None or a float [was {0}]".format(
775+
type(timeout)
776+
)
783777
)
784778

785779
_ClamdGeneric.__init__(self)
@@ -799,7 +793,7 @@ def _init_socket(self):
799793
internal use only
800794
"""
801795
self.clamd_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
802-
if not self.timeout is None:
796+
if self.timeout:
803797
self.clamd_socket.settimeout(self.timeout)
804798
try:
805799
self.clamd_socket.connect((self.host, self.port))
@@ -920,7 +914,6 @@ def _print_doc():
920914

921915
# MAIN -------------------
922916
if __name__ == "__main__":
923-
924917
_non_regression_test()
925918

926919

lib/scan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def scan_file(self, file):
7474
self.logger.debug(message, extra={"filepath": filepath})
7575
elif result == "FOUND":
7676
self.logger.info(
77-
f"File match", extra={"file": filepath, "signature": message}
77+
"File match", extra={"file": filepath, "signature": message}
7878
)
7979
return True
8080
else:

pyclamav.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
from lib.config import load_config
42
from lib.log import get_logger
53

@@ -11,13 +9,15 @@ def main():
119
logger = get_logger(config.log_folder, config.verbose)
1210
scanner = Scan(config.modified_file_datetime, logger)
1311

14-
logger.info(f"Scanning {len(config.folders)} folders with files changed during the last {config.modified_file_since}")
12+
logger.info(
13+
f"Scanning {len(config.folders)} folders with files changed during the last {config.modified_file_since}"
14+
)
1515
for folder in config.folders:
1616
logger.info(
17-
f"Scanning folder",
17+
"Scanning folder",
1818
extra={"folder": folder},
1919
)
20-
r = scanner.scan_folder(folder)
20+
scanner.scan_folder(folder)
2121

2222

2323
if __name__ == "__main__":

tests/pyclamav_test.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest.mock import patch, MagicMock, mock_open
33
import datetime
44
import argparse
5-
from typing import List
65
from lib.config import parse_arg, Config, load_config
76
from lib.utils import create_file_folder
87
import logging
@@ -12,13 +11,14 @@
1211
import tempfile
1312
import shutil
1413

14+
1515
class TestPyclamav(unittest.TestCase):
1616
def setUp(self):
1717
self.test_dir = tempfile.mkdtemp()
18-
18+
1919
def tearDown(self):
2020
shutil.rmtree(self.test_dir)
21-
21+
2222
@patch(
2323
"argparse.ArgumentParser.parse_args",
2424
return_value=argparse.Namespace(
@@ -65,14 +65,14 @@ def test_load_config(self, mock_args, mock_file):
6565
self.assertEqual(config.verbose, False)
6666

6767
def test_create_file_folder(self):
68-
filepath = Path(self.test_dir) / 'subdir' / 'file.txt'
68+
filepath = Path(self.test_dir) / "subdir" / "file.txt"
6969

7070
create_file_folder(filepath)
7171

7272
self.assertTrue(filepath.parent.exists())
7373

7474
def test_create_file_folder_existing_directory(self):
75-
filepath = Path(self.test_dir) / 'existing_file.txt'
75+
filepath = Path(self.test_dir) / "existing_file.txt"
7676

7777
filepath.parent.mkdir(parents=True, exist_ok=True)
7878

@@ -81,12 +81,11 @@ def test_create_file_folder_existing_directory(self):
8181
self.assertTrue(filepath.parent.exists())
8282

8383
def test_create_file_folder_nested_directories(self):
84-
filepath = Path(self.test_dir) / 'nested' / 'dir1' / 'dir2' / 'file.txt'
85-
84+
filepath = Path(self.test_dir) / "nested" / "dir1" / "dir2" / "file.txt"
85+
8686
create_file_folder(filepath)
87-
88-
self.assertTrue(filepath.parent.exists())
8987

88+
self.assertTrue(filepath.parent.exists())
9089

9190
@patch("lib.pyclamd.ClamdUnixSocket")
9291
@patch("lib.pyclamd.ClamdNetworkSocket")

0 commit comments

Comments
 (0)