<commit_before>#
# Copyright (c) 2012 <NAME>
# This file is part of pynetdicom, released under a modified MIT license.
#    See the file license.txt included with this distribution, also
#    available at http://pynetdicom.googlecode.com
#

import threading
import time
import socket
import os
import sys
import select
import platform
from SOPclass import *
from dicom.UID import ExplicitVRLittleEndian, ImplicitVRLittleEndian, \
    ExplicitVRBigEndian, UID
from DULprovider import DULServiceProvider
from DIMSEprovider import DIMSEServiceProvider
from ACSEprovider import ACSEServiceProvider
from DIMSEparameters import *
from DULparameters import *
from weakref import proxy
import gc
import struct
import timer

import logging
logger = logging.getLogger(__name__)

class Association(threading.Thread):

    def __init__(self, LocalAE, ClientSocket=None, RemoteAE=None, AssociateRequestTimeout=30):
        if not ClientSocket and not RemoteAE:
            raise
        if ClientSocket and RemoteAE:
            raise
        if ClientSocket:
            # must respond for request from a remote AE
            self.Mode = 'Acceptor'
        if RemoteAE:
            # must request
            self.Mode = 'Requestor'
        self.ClientSocket = ClientSocket
        self.AE = LocalAE
        self.DUL = DULServiceProvider(ClientSocket,
                                      MaxIdleSeconds=self.AE.MaxAssociationIdleSeconds,
                                      ConnectTimeoutSeconds=self.AE.ConnectTimeoutSeconds)
        self.RemoteAE = RemoteAE
        self._Kill = False
        threading.Thread.__init__(self)
        self.daemon = True
        self.SOPClassesAsSCP = []
        self.SOPClassesAsSCU = []
        self.AssociationEstablished = False
        self.AssociationRefused = None
        self.AssociateRequestTimeout = AssociateRequestTimeout
        self.start()

    def GetSOPClass(self, ds):
        sopclass = UID2SOPClass(ds.SOPClassUID)

    def SCU(self, ds, id):
        obj = UID2SOPClass(ds.SOPClassUID)()
        try:
            obj.pcid, obj.sopclass, obj.transfersyntax = \
                [x for x in self.SOPClassesAsSCU if
                 x[1] == obj.__class__][0]
        except IndexError:
            raise Exception("SOP Class %s not supported as SCU" % ds.SOPClassUID)

        obj.maxpdulength = self.ACSE.MaxPDULength
        obj.DIMSE = self.DIMSE
        obj.AE = self.AE
        return obj.SCU(ds, id)

    def __getattr__(self, attr):
        # while not self.AssociationEstablished:
        #    time.sleep(0.001)
        try:
           obj = eval(attr)()
        except NameError:
           raise AttributeError

        try:
            obj.pcid, obj.sopclass, obj.transfersyntax = \
                [x for x in self.SOPClassesAsSCU if
                 x[1] == obj.__class__][0]
        except IndexError:
            raise "SOP Class %s not supported as SCU" % attr

        obj.maxpdulength = self.ACSE.MaxPDULength
        obj.DIMSE = self.DIMSE
        obj.AE = self.AE
        obj.RemoteAE = self.AE
        return obj

    def Kill(self):
        self._Kill = True
        for ii in range(1000):
            if self.DUL.Stop():
                continue
            time.sleep(0.001)
        self.DUL.Kill()
        # self.ACSE.Kill()<commit_msg>correctly raise exception<commit_after>#
# Copyright (c) 2012 <NAME>
# This file is part of pynetdicom, released under a modified MIT license.
#    See the file license.txt included with this distribution, also
#    available at http://pynetdicom.googlecode.com
#

import threading
import time
import socket
import os
import sys
import select
import platform
from SOPclass import *
from dicom.UID import ExplicitVRLittleEndian, ImplicitVRLittleEndian, \
    ExplicitVRBigEndian, UID
from DULprovider import DULServiceProvider
from DIMSEprovider import DIMSEServiceProvider
from ACSEprovider import ACSEServiceProvider
from DIMSEparameters import *
from DULparameters import *
from weakref import proxy
import gc
import struct
import timer

import logging
logger = logging.getLogger(__name__)

class Association(threading.Thread):

    def __init__(self, LocalAE, ClientSocket=None, RemoteAE=None, AssociateRequestTimeout=30):
        if not ClientSocket and not RemoteAE:
            raise
        if ClientSocket and RemoteAE:
            raise
        if ClientSocket:
            # must respond for request from a remote AE
            self.Mode = 'Acceptor'
        if RemoteAE:
            # must request
            self.Mode = 'Requestor'
        self.ClientSocket = ClientSocket
        self.AE = LocalAE
        self.DUL = DULServiceProvider(ClientSocket,
                                      MaxIdleSeconds=self.AE.MaxAssociationIdleSeconds,
                                      ConnectTimeoutSeconds=self.AE.ConnectTimeoutSeconds)
        self.RemoteAE = RemoteAE
        self._Kill = False
        threading.Thread.__init__(self)
        self.daemon = True
        self.SOPClassesAsSCP = []
        self.SOPClassesAsSCU = []
        self.AssociationEstablished = False
        self.AssociationRefused = None
        self.AssociateRequestTimeout = AssociateRequestTimeout
        self.start()

    def GetSOPClass(self, ds):
        sopclass = UID2SOPClass(ds.SOPClassUID)

    def SCU(self, ds, id):
        obj = UID2SOPClass(ds.SOPClassUID)()
        try:
            obj.pcid, obj.sopclass, obj.transfersyntax = \
                [x for x in self.SOPClassesAsSCU if
                 x[1] == obj.__class__][0]
        except IndexError:
            raise Exception("SOP Class %s not supported as SCU" % ds.SOPClassUID)

        obj.maxpdulength = self.ACSE.MaxPDULength
        obj.DIMSE = self.DIMSE
        obj.AE = self.AE
        return obj.SCU(ds, id)

    def __getattr__(self, attr):
        # while not self.AssociationEstablished:
        #    time.sleep(0.001)
        try:
           obj = eval(attr)()
        except NameError:
           raise AttributeError

        try:
            obj.pcid, obj.sopclass, obj.transfersyntax = \
                [x for x in self.SOPClassesAsSCU if
                 x[1] == obj.__class__][0]
        except IndexError:
            raise Exception("SOP Class %s not supported as SCU" % attr)

        obj.maxpdulength = self.ACSE.MaxPDULength
        obj.DIMSE = self.DIMSE
        obj.AE = self.AE
        obj.RemoteAE = self.AE
        return obj

    def Kill(self):
        self._Kill = True
        for ii in range(1000):
            if self.DUL.Stop():
                continue
            time.sleep(0.001)
        self.DUL.Kill()
        # self.ACSE.Kill()
{'fraction_non_alphanumeric': 0.07419695370230735, 'fraction_numerical': 0.007238727190469009, 'mean_word_length': 2.120941176470588, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2859604', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1897, 'n_words': 503}