Skip to content

Commit 19ff1a7

Browse files
authored
implement __main__.py enty points (#409)
* convert `server` and `client` to full sub-packages * Server: implement a `__main__` entrypint * small fixes and typos
1 parent 4f051c8 commit 19ff1a7

File tree

7 files changed

+101
-35
lines changed

7 files changed

+101
-35
lines changed

doc/API/server.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
Server
22
======
33

4+
If you just need a quick server with some default values initalised, this package provides a default implementation.
5+
To use it you first need to install some aditional dependencies, using:
6+
7+
.. code:: bash
8+
9+
pip install python-snap7[cli]
10+
11+
Now you can start it using one of the following commands:
12+
13+
.. code:: bash
14+
15+
python -m snap7.server
16+
# or, if your Python `Scripts/` folder is on PATH:
17+
snap7-server
18+
19+
You can optionally provide the port to be used as an argument, like this:
20+
21+
.. code:: bash
22+
23+
python -m snap7.server --port 102
24+
25+
----
26+
427
.. automodule:: snap7.server
5-
:members:
28+
:members:
29+
30+
----
31+
32+
.. automodule:: snap7.server.__main__
33+
34+
.. autofunction:: main(port, dll)

setup.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
tests_require = ['pytest', 'pytest-asyncio', 'mypy', 'pycodestyle', 'types-setuptools']
88

99
extras_require = {
10+
'cli': ['click'],
1011
'test': tests_require,
1112
'doc': ['sphinx', 'sphinx_rtd_theme'],
1213
}
@@ -38,7 +39,11 @@ def read(fname):
3839
package_data={'snap7': ['py.typed']},
3940
license='MIT',
4041
long_description=read('README.rst'),
41-
scripts=['snap7/bin/snap7-server.py'],
42+
entry_points={
43+
'console_scripts': [
44+
'snap7-server = snap7.server.__main__:main',
45+
]
46+
},
4247
classifiers=[
4348
"Development Status :: 5 - Production/Stable",
4449
"Environment :: Console",
@@ -50,7 +55,7 @@ def read(fname):
5055
"Programming Language :: Python :: 3.7",
5156
"Programming Language :: Python :: 3.8",
5257
"Programming Language :: Python :: 3.9",
53-
"Programming Language :: Python :: 3.10",
58+
"Programming Language :: Python :: 3.10",
5459
],
5560
python_requires='>=3.7',
5661
extras_require=extras_require,

snap7/bin/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

snap7/bin/snap7-server.py

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
from datetime import datetime
99
from typing import List, Optional, Tuple, Union
1010

11-
from .common import check_error, ipv4, load_library
12-
from .types import S7SZL, Areas, BlocksList, S7CpInfo, S7CpuInfo, S7DataItem
13-
from .types import S7OrderCode, S7Protection, S7SZLList, TS7BlockInfo, WordLen
14-
from .types import S7Object, buffer_size, buffer_type, cpu_statuses, param_types
15-
from .types import S7CpuInfo, RemotePort, wordlen_to_ctypes, block_types
11+
from ..common import check_error, ipv4, load_library
12+
from ..types import S7SZL, Areas, BlocksList, S7CpInfo, S7CpuInfo, S7DataItem
13+
from ..types import S7OrderCode, S7Protection, S7SZLList, TS7BlockInfo, WordLen
14+
from ..types import S7Object, buffer_size, buffer_type, cpu_statuses, param_types
15+
from ..types import S7CpuInfo, RemotePort, wordlen_to_ctypes, block_types
1616
logger = logging.getLogger(__name__)
1717

1818

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import logging
99
from typing import Any, Tuple, Callable, Optional
1010

11-
from . import server as snap7server
12-
from .common import ipv4, check_error, load_library
13-
from .types import SrvEvent, LocalPort, cpu_statuses, server_statuses
14-
from .types import longword, wordlen_to_ctypes, WordLen, S7Object
15-
from .types import srvAreaDB, srvAreaPA, srvAreaTM, srvAreaCT
11+
from ..common import ipv4, check_error, load_library
12+
from ..types import SrvEvent, LocalPort, cpu_statuses, server_statuses
13+
from ..types import longword, wordlen_to_ctypes, WordLen, S7Object
14+
from ..types import srvAreaDB, srvAreaPA, srvAreaTM, srvAreaCT
1615

1716
logger = logging.getLogger(__name__)
1817

@@ -384,7 +383,7 @@ def mainloop(tcpport: int = 1102, init_standard_values: bool = False):
384383
init_standard_values: if `True` will init some defaults values to be read on DB0.
385384
"""
386385

387-
server = snap7server.Server()
386+
server = Server()
388387
size = 100
389388
DBdata = (wordlen_to_ctypes[WordLen.Byte.value] * size)()
390389
PAdata = (wordlen_to_ctypes[WordLen.Byte.value] * size)()

snap7/server/__main__.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
The :code:`__main__` module is used as an entrypoint when calling the module from the terminal using python -m flag.
3+
It contains functions providing a comandline interface to the server module.
4+
5+
Its :code:`main()` function is also exported as an consol-entrypoint.
6+
"""
7+
8+
import logging
9+
10+
try:
11+
import click
12+
except ImportError as e:
13+
print(e)
14+
print("Try using 'pip install python-snap7[cli]'")
15+
exit()
16+
17+
from snap7 import __version__
18+
from snap7.common import load_library
19+
from snap7.server import mainloop
20+
21+
logger = logging.getLogger("Snap7.Server")
22+
23+
24+
@click.command()
25+
@click.option("-p", "--port", default=1102, help="Port the server will listen on.")
26+
@click.option(
27+
"--dll",
28+
hidden=True,
29+
type=click.Path(exists=True, file_okay=True, dir_okay=False, resolve_path=True),
30+
help="Path to the snap7 DLL (for emergencies if it can't be put on PATH).",
31+
)
32+
@click.option("-v", "--verbose", is_flag=True, help="Also print debug-output.")
33+
@click.version_option(__version__)
34+
@click.help_option("-h", "--help")
35+
def main(port, dll, verbose):
36+
"""Start a S7 dummy server with some default values."""
37+
38+
# setup logging
39+
if verbose:
40+
logging.basicConfig(format="[%(levelname)s]: %(message)s", level=logging.DEBUG)
41+
else:
42+
logging.basicConfig(format="[%(levelname)s]: %(message)s", level=logging.INFO)
43+
44+
# normally the snap7.dll should be on PATH and will be loaded automatically by the mainloop,
45+
# but for emergencies, we allow the DLL's location to be passed as an argument and load it here
46+
if dll:
47+
load_library(dll)
48+
49+
# start the server mainloop
50+
mainloop(port, init_standard_values=True)
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)