Skip to content

Commit 80d3cf8

Browse files
committed
Drop old python versions and reformat code
1 parent 0f6e0bc commit 80d3cf8

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

pidfile/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from .pidfile import PIDFile, AlreadyRunningError
2-
from .version import version_info, __version__, __author__, author_info
1+
from .pidfile import AlreadyRunningError, PIDFile
2+
from .version import __author__, __version__, author_info, version_info
3+
34

45
__all__ = (
5-
'__version__', '__author__', 'author_info',
6-
'version_info', 'PIDFile', 'AlreadyRunningError'
6+
"__version__", "__author__", "author_info",
7+
"version_info", "PIDFile", "AlreadyRunningError",
78
)

pidfile/pidfile.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import atexit
22
import os
3+
from typing import Any
4+
35
import psutil
46

57

@@ -8,12 +10,12 @@ class AlreadyRunningError(Exception):
810

911

1012
class PIDFile(object):
11-
def __init__(self, filename='pidfile'):
13+
def __init__(self, filename: Any = "pidfile"):
1214
self._process_name = psutil.Process(os.getpid()).cmdline()[0]
13-
self._file = filename
15+
self._file = str(filename)
1416

1517
@property
16-
def is_running(self):
18+
def is_running(self) -> bool:
1719
if not os.path.exists(self._file):
1820
return False
1921

@@ -32,14 +34,14 @@ def is_running(self):
3234
except psutil.AccessDenied:
3335
return False
3436

35-
def close(self):
37+
def close(self) -> None:
3638
if os.path.exists(self._file):
3739
try:
3840
os.unlink(self._file)
3941
except OSError:
4042
pass
4143

42-
def __enter__(self):
44+
def __enter__(self) -> "PIDFile":
4345
if self.is_running:
4446
raise AlreadyRunningError
4547

@@ -50,6 +52,6 @@ def __enter__(self):
5052

5153
return self
5254

53-
def __exit__(self, *args):
55+
def __exit__(self, *_) -> None:
5456
self.close()
5557
atexit.unregister(self.close)

pidfile/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
("Michele Cancilla", None),
66
)
77

8-
version_info = (3, 1, 0)
8+
version_info = (3, 1, 1)
99

1010
__version__ = ".".join(map(str, version_info))
1111
__author__ = ", ".join("{0} <{1}>".format(*author) for author in author_info)

setup.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
import sys
21
import os
2+
import importlib.util
33
from setuptools import setup, find_packages
44

55

66
version_file = os.path.join('pidfile', 'version.py')
77

8-
if sys.version_info < (3,):
9-
import imp
10-
version = imp.load_source('version', version_file)
11-
elif sys.version_info < (3, 5):
12-
from importlib.machinery import SourceFileLoader
13-
version = SourceFileLoader("version", version_file).load_module()
14-
else:
15-
import importlib.util
16-
spec = importlib.util.spec_from_file_location("version", version_file)
17-
version = importlib.util.module_from_spec(spec)
18-
spec.loader.exec_module(version)
8+
spec = importlib.util.spec_from_file_location("version", version_file)
9+
version = importlib.util.module_from_spec(spec)
10+
spec.loader.exec_module(version)
1911

2012

2113
setup(
@@ -27,22 +19,20 @@
2719
),
2820
url="https://github.com/mosquito/python-pidfile",
2921
license="MIT",
30-
description="PIDFile context processor. Supported py2 and py3",
22+
description="PIDFile context manager.",
3123
long_description=open('README.rst').read(),
3224
platforms="unix",
3325
classifiers=[
3426
'Development Status :: 5 - Production/Stable',
3527
'Natural Language :: English',
3628
'Operating System :: MacOS',
3729
'Operating System :: POSIX',
38-
'Programming Language :: Python :: 2',
39-
'Programming Language :: Python :: 2.7',
4030
'Programming Language :: Python :: 3',
41-
'Programming Language :: Python :: 3.4',
42-
'Programming Language :: Python :: 3.5',
43-
'Programming Language :: Python :: 3.6',
4431
'Programming Language :: Python :: 3.7',
4532
'Programming Language :: Python :: 3.8',
33+
'Programming Language :: Python :: 3.9',
34+
'Programming Language :: Python :: 3.10',
35+
'Programming Language :: Python :: 3.11',
4636
'Programming Language :: Python :: Implementation :: CPython',
4737
'License :: OSI Approved :: MIT License',
4838
],

tests/test_pidfile.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
from unittest import TestCase
2+
from unittest.mock import mock_open, patch
23

3-
try:
4-
# Python 3.x
5-
from unittest.mock import patch, mock_open
6-
open_name = 'builtins.open'
7-
except ImportError:
8-
# Python 2.7
9-
from mock import patch, mock_open
10-
open_name = '__builtin__.open'
114

12-
import pidfile
5+
open_name = "builtins.open"
6+
137
import os
8+
149
import psutil
1510

11+
import pidfile
12+
13+
1614
builtins_open = open
1715

1816

1917
def open_patcher(data):
2018
def patched_open(*args, **kwargs):
21-
if args[0] == 'pidfile':
19+
if args[0] == "pidfile":
2220
return mock_open(read_data=data)(*args, **kwargs)
2321
else:
2422
return builtins_open(*args, **kwargs)
@@ -27,7 +25,7 @@ def patched_open(*args, **kwargs):
2725

2826
def open_patcher_exception():
2927
def patched_open(*args, **kwargs):
30-
if args[0] == 'pidfile':
28+
if args[0] == "pidfile":
3129
mo = mock_open()
3230
mo.return_value.read.side_effect = OSError
3331
return mo(*args, **kwargs)
@@ -37,48 +35,52 @@ def patched_open(*args, **kwargs):
3735

3836

3937
class PIDFileTestCase(TestCase):
40-
@patch(open_name, new=open_patcher('1'))
41-
@patch('os.path.exists')
38+
@patch(open_name, new=open_patcher("1"))
39+
@patch("os.path.exists")
4240
def test_pidfile_not_exists(self, exists_mock):
4341
exists_mock.return_value = False
4442
with pidfile.PIDFile():
4543
assert True
4644

47-
@patch(open_name, new=open_patcher('1'))
48-
@patch('psutil.pid_exists')
49-
@patch('psutil.Process')
50-
@patch('os.path.exists')
51-
def test_pidfile_exists_process_running(self, exists_mock, Process_mock,
52-
pid_exists_mock):
45+
@patch(open_name, new=open_patcher("1"))
46+
@patch("psutil.pid_exists")
47+
@patch("psutil.Process")
48+
@patch("os.path.exists")
49+
def test_pidfile_exists_process_running(
50+
self, exists_mock, Process_mock,
51+
pid_exists_mock,
52+
):
5353
exists_mock.return_value = True
5454
pid_exists_mock.return_value = True
5555
Process_mock.return_value = psutil.Process(os.getpid())
5656
with self.assertRaises(pidfile.AlreadyRunningError):
5757
with pidfile.PIDFile():
5858
assert True
5959

60-
@patch(open_name, new=open_patcher('1'))
61-
@patch('psutil.pid_exists')
62-
@patch('os.path.exists')
63-
def test_pidfile_exists_process_not_running(self, exists_mock,
64-
pid_exists_mock):
60+
@patch(open_name, new=open_patcher("1"))
61+
@patch("psutil.pid_exists")
62+
@patch("os.path.exists")
63+
def test_pidfile_exists_process_not_running(
64+
self, exists_mock,
65+
pid_exists_mock,
66+
):
6567
exists_mock.return_value = True
6668
pid_exists_mock.return_value = False
6769
with pidfile.PIDFile():
6870
assert True
6971

70-
@patch(open_name, new=open_patcher(''))
71-
@patch('psutil.pid_exists')
72-
@patch('os.path.exists')
72+
@patch(open_name, new=open_patcher(""))
73+
@patch("psutil.pid_exists")
74+
@patch("os.path.exists")
7375
def test_pidfile_exists_empty(self, exists_mock, pid_exists_mock):
7476
exists_mock.return_value = True
7577
pid_exists_mock.return_value = True
7678
with pidfile.PIDFile():
7779
assert True
7880

7981
@patch(open_name, new=open_patcher_exception())
80-
@patch('psutil.pid_exists')
81-
@patch('os.path.exists')
82+
@patch("psutil.pid_exists")
83+
@patch("os.path.exists")
8284
def test_pidfile_exists_read_fail(self, exists_mock, pid_exists_mock):
8385
exists_mock.return_value = True
8486
pid_exists_mock.return_value = True

0 commit comments

Comments
 (0)