Skip to content

Commit

Permalink
Reformat with black, cleanup based on flake8 (#38)
Browse files Browse the repository at this point in the history
* replaced hardcoded IP addresses and Ports with those loaded from a configuration json file (ApplicationEntities.json)

* reformatted with black -l 127
reformatted with isort --profile black

* cleaned out unused imports per flake8
using treatment_delivery_type in rtbdi_factory (real bug caught by flake8) instead of hardcoded TREATMENT

* enforce more comprehensive flake8 now that the ui files from Qt Creator have per file excludes and the entire repo has been tidied
  • Loading branch information
sjswerdloff authored May 5, 2024
1 parent 5f5299b commit 304c0c8
Show file tree
Hide file tree
Showing 25 changed files with 733 additions and 854 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
# while flake8 complains (this warning is not PEP 8 compliant), If W503 pops up, that should be disabled also
# poetry run flake8 tdwii_plus_examples --count --exit-zero --extend-ignore=E203 --max-line-length=127 --statistics
# poetry run flake8 tdwii_plus_examples --count --exit-zero --max-complexity=10 --statistics
poetry run flake8 --count --exit-zero --extend-ignore=E203 --max-line-length=127 --statistics --per-file-ignores="tdwii_plus_examples/rtbdi_creator/ui_form.py:E266,F401,E501 tdwii_plus_examples/TDWII_PPVS_subscriber/ui_tdwii_ppvs_subscriber.py:E266,F401,E501" tdwii_plus_examples
poetry run flake8 --count --extend-ignore=E203 --max-line-length=127 --statistics --per-file-ignores="tdwii_plus_examples/rtbdi_creator/ui_form.py:E266,F401,E501 tdwii_plus_examples/TDWII_PPVS_subscriber/ui_tdwii_ppvs_subscriber.py:E266,F401,E501" tdwii_plus_examples
- name: Test with pytest
run: |
poetry run pytest tdwii_plus_examples/tests
49 changes: 11 additions & 38 deletions tdwii_plus_examples/TDWII_PPVS_subscriber/basescp.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
import logging
import os
import sys
from argparse import Namespace
from configparser import ConfigParser
from datetime import datetime
from typing import Tuple

import pydicom.config
from tdwii_plus_examples.TDWII_PPVS_subscriber.nevent_receiver_handlers import handle_echo
from pynetdicom import (
AE,
ALL_TRANSFER_SYNTAXES,
UnifiedProcedurePresentationContexts,
_config,
_handlers,
evt,
)
from argparse import Namespace
from pynetdicom import AE
from pynetdicom.apps.common import setup_logging
from pynetdicom.sop_class import Verification
from pynetdicom.utils import set_ae



class BaseSCP:
def __init__(self,
ae_title:str="BASE_SCP",
port:int=11112,
logger=None,
bind_address:str=""
):

def __init__(self, ae_title: str = "BASE_SCP", port: int = 11112, logger=None, bind_address: str = ""):

self.ae_title = ae_title
self.port = port
if logger is None:
logger_args = Namespace(log_type='d', log_level='debug')
self.logger = setup_logging(logger_args, "base_scp")
logger_args = Namespace(log_type="d", log_level="debug")
self.logger = setup_logging(logger_args, "base_scp")
else:
self.logger = logger
self.bind_address = bind_address
Expand All @@ -45,17 +22,13 @@ def __init__(self,
self._add_handlers()

def _add_contexts(self):
# self.ae.add_supported_context(Verification, ALL_TRANSFER_SYNTAXES)
pass # base class, do nothing, pure virtual

# self.ae.add_supported_context(Verification, ALL_TRANSFER_SYNTAXES)
pass # base class, do nothing, pure virtual

def _add_handlers(self):
# self.handlers.append((evt.EVT_C_ECHO, handle_echo, [None, self.logger]))
pass # base class, do nothing, pure virtual

# self.handlers.append((evt.EVT_C_ECHO, handle_echo, [None, self.logger]))
pass # base class, do nothing, pure virtual

def run(self):
# Listen for incoming association requests
self.threaded_server = self.ae.start_server((self.bind_address, self.port),
evt_handlers=self.handlers,
block=False)
self.threaded_server = self.ae.start_server((self.bind_address, self.port), evt_handlers=self.handlers, block=False)
13 changes: 5 additions & 8 deletions tdwii_plus_examples/TDWII_PPVS_subscriber/cstore_handler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""Utility classes and functions for the apps."""

import logging
import os
from struct import pack

from pydicom import dcmread
from pydicom.datadict import tag_for_keyword, repeater_has_keyword, get_entry

# from pydicom import dcmread
# from pydicom.datadict import get_entry, repeater_has_keyword, tag_for_keyword
from pydicom.dataset import Dataset
from pydicom.filewriter import write_file_meta_info
from pydicom.tag import Tag
# from pydicom.tag import Tag
from pydicom.uid import DeflatedExplicitVRLittleEndian

from pynetdicom.dsutils import encode


Expand Down Expand Up @@ -56,8 +54,7 @@ def handle_store(event, args, app_logger):
sop_instance = ds.SOPInstanceUID
except Exception as exc:
app_logger.error(
"Unable to decode the received dataset or missing 'SOP Class "
"UID' and/or 'SOP Instance UID' elements"
"Unable to decode the received dataset or missing 'SOP Class " "UID' and/or 'SOP Instance UID' elements"
)
app_logger.exception(exc)
# Unable to decode dataset
Expand Down
55 changes: 17 additions & 38 deletions tdwii_plus_examples/TDWII_PPVS_subscriber/echoscp.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,39 @@
import logging
import os
import sys
from argparse import Namespace
from configparser import ConfigParser
from datetime import datetime
from typing import Tuple

from time import sleep
import pydicom.config
from tdwii_plus_examples.TDWII_PPVS_subscriber.nevent_receiver_handlers import handle_echo

from pynetdicom import (
AE,
ALL_TRANSFER_SYNTAXES,
UnifiedProcedurePresentationContexts,
_config,
_handlers,
evt,
)
from pynetdicom.apps.common import setup_logging
from pynetdicom.sop_class import Verification
from pynetdicom.utils import set_ae


from tdwii_plus_examples.TDWII_PPVS_subscriber.basescp import BaseSCP
from tdwii_plus_examples.TDWII_PPVS_subscriber.nevent_receiver_handlers import (
handle_echo,
)


class EchoSCP:
def __init__(self,
ae_title:str="ECHO_SCP",
port:int=11112,
logger=None,
bind_address:str=""
):

BaseSCP.__init__(self,
ae_title=ae_title,
port=port,
logger=logger,
bind_address=bind_address)

def __init__(self, ae_title: str = "ECHO_SCP", port: int = 11112, logger=None, bind_address: str = ""):

BaseSCP.__init__(self, ae_title=ae_title, port=port, logger=logger, bind_address=bind_address)

def _add_contexts(self):
BaseSCP._add_contexts(self)
self.ae.add_supported_context(Verification, ALL_TRANSFER_SYNTAXES)



def _add_handlers(self):
BaseSCP._add_handlers(self)
self.handlers.append((evt.EVT_C_ECHO, handle_echo, [None, self.logger]))



BaseSCP._add_handlers(self)
self.handlers.append((evt.EVT_C_ECHO, handle_echo, [None, self.logger]))

def run(self):
# Listen for incoming association requests
BaseSCP.run(self)

if __name__ == '__main__':

if __name__ == "__main__":
myecho_scp = EchoSCP()
myecho_scp.run()
while True: sleep(100) # sleep forever

while True:
sleep(100) # sleep forever
51 changes: 17 additions & 34 deletions tdwii_plus_examples/TDWII_PPVS_subscriber/nevent_receiver.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import logging
import os
import sys
from argparse import Namespace
from configparser import ConfigParser
from datetime import datetime
from typing import Tuple

from time import sleep
import pydicom.config
from tdwii_plus_examples.TDWII_PPVS_subscriber.nevent_receiver_handlers import handle_echo, handle_nevent
from pynetdicom import (
AE,
ALL_TRANSFER_SYNTAXES,
UnifiedProcedurePresentationContexts,
_config,
_handlers,
evt,
)
from pynetdicom.apps.common import setup_logging
from pynetdicom.sop_class import Verification
from pynetdicom.utils import set_ae

from tdwii_plus_examples.TDWII_PPVS_subscriber.basescp import BaseSCP
from tdwii_plus_examples.TDWII_PPVS_subscriber.echoscp import EchoSCP
from tdwii_plus_examples.TDWII_PPVS_subscriber.nevent_receiver_handlers import (
handle_nevent,
)


def nevent_cb(**kwargs):
logger = None
Expand All @@ -31,9 +21,7 @@ def nevent_cb(**kwargs):
logger.info("nevent_cb invoked")
event_type_id = 0 # not a valid type ID
if logger:
logger.info(
"TODO: Invoke application response appropriate to content of N-EVENT-REPORT-RQ"
)
logger.info("TODO: Invoke application response appropriate to content of N-EVENT-REPORT-RQ")
if "type_id" in kwargs.keys():
event_type_id = kwargs["type_id"]
if logger:
Expand All @@ -54,9 +42,7 @@ def nevent_cb(**kwargs):
elif event_type_id == 3:
if logger:
logger.info("UPS Progress Report")
logger.info(
"Probably time to see if the Beam (number) changed, or if adaptation is taking or took place"
)
logger.info("Probably time to see if the Beam (number) changed, or if adaptation is taking or took place")
elif event_type_id == 4:
if logger:
logger.info("SCP Status Change")
Expand All @@ -75,26 +61,21 @@ def nevent_cb(**kwargs):
if logger:
logger.warning(f"Unknown Event Type ID: {event_type_id}")


class NEventReceiver(EchoSCP):
def __init__(self,
nevent_callback=None,
ae_title:str="NEVENT_RECEIVER",
port:int=11115,
logger=None,
bind_address:str=""
):
def __init__(
self, nevent_callback=None, ae_title: str = "NEVENT_RECEIVER", port: int = 11115, logger=None, bind_address: str = ""
):
if nevent_callback is None:
self.nevent_callback = nevent_cb # fallback to something that just logs incoming events
else:
self.nevent_callback = nevent_callback
EchoSCP.__init__(self,ae_title=ae_title,port=port,logger=logger,bind_address=bind_address)
EchoSCP.__init__(self, ae_title=ae_title, port=port, logger=logger, bind_address=bind_address)

def _add_contexts(self):
EchoSCP._add_contexts(self)
for cx in UnifiedProcedurePresentationContexts:
self.ae.add_supported_context(
cx.abstract_syntax, ALL_TRANSFER_SYNTAXES, scp_role=True, scu_role=False
)
self.ae.add_supported_context(cx.abstract_syntax, ALL_TRANSFER_SYNTAXES, scp_role=True, scu_role=False)

def _add_handlers(self):
EchoSCP._add_handlers(self)
Expand All @@ -103,7 +84,9 @@ def _add_handlers(self):
def run(self):
BaseSCP.run(self)

if __name__ == '__main__':

if __name__ == "__main__":
my_scp = NEventReceiver()
my_scp.run()
while True: sleep(100) # sleep forever
while True:
sleep(100) # sleep forever
Loading

0 comments on commit 304c0c8

Please sign in to comment.