|
| 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