From 17160db6038277b44f6f347c590bca1951b3efdb Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Thu, 25 Aug 2016 23:21:24 -0400 Subject: [PATCH] clean up the use of globals in the samples -- some missing, many extraneous --- samples/CommandableMixin.py | 4 -- samples/ConsoleCmd.py | 1 - samples/MultiStateValueObject.py | 4 -- samples/MultipleReadProperty.py | 8 +-- samples/RandomAccumulatorObject.py | 4 -- samples/RandomAnalogValueObject.py | 5 -- samples/ReadProperty.py | 11 ++-- samples/ReadPropertyAny.py | 4 +- samples/ReadPropertyMultiple.py | 3 +- samples/ReadPropertyMultipleServer.py | 4 -- samples/ReadRange.py | 14 ++--- samples/ReadWriteFile.py | 12 ++-- samples/ReadWriteFileServer.py | 2 + samples/ReadWriteProperty.py | 14 ++--- samples/RecurringMultipleReadProperty.py | 16 ++--- samples/RecurringTask.py | 10 +-- samples/SampleApplication.py | 13 ++-- samples/SampleConsoleCmd.py | 14 ++--- samples/SubscribeCOV.py | 15 ++--- samples/TCPServer.py | 7 +-- samples/UDPConsole.py | 10 +-- samples/VendorAVObject.py | 77 +++++++++++------------- samples/VendorReadWriteProperty.py | 18 +++--- samples/WhoHasIHaveApplication.py | 14 ++--- samples/WhoIsIAm.py | 14 ++--- samples/WhoIsIAmApplication.py | 14 ++--- samples/WhoIsIAmForeign.py | 6 +- samples/WhoIsRouter.py | 12 ++-- 28 files changed, 136 insertions(+), 194 deletions(-) diff --git a/samples/CommandableMixin.py b/samples/CommandableMixin.py index 8cc53a9e..f58a2901 100755 --- a/samples/CommandableMixin.py +++ b/samples/CommandableMixin.py @@ -21,10 +21,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # # CommandableMixin # diff --git a/samples/ConsoleCmd.py b/samples/ConsoleCmd.py index 6cd5bd49..8055d965 100755 --- a/samples/ConsoleCmd.py +++ b/samples/ConsoleCmd.py @@ -63,4 +63,3 @@ def main(): if __name__ == "__main__": main() - diff --git a/samples/MultiStateValueObject.py b/samples/MultiStateValueObject.py index a45637a7..873de076 100755 --- a/samples/MultiStateValueObject.py +++ b/samples/MultiStateValueObject.py @@ -17,10 +17,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # # __main__ # diff --git a/samples/MultipleReadProperty.py b/samples/MultipleReadProperty.py index 4f3124f8..6e483afd 100755 --- a/samples/MultipleReadProperty.py +++ b/samples/MultipleReadProperty.py @@ -26,13 +26,7 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None -this_console = None - -# point list -# set according to your device +# point list, set according to your device point_list = [ ('1.2.3.4', 'analogValue', 1, 'presentValue'), ('1.2.3.4', 'analogValue', 2, 'presentValue'), diff --git a/samples/RandomAccumulatorObject.py b/samples/RandomAccumulatorObject.py index 4bbfa700..59d4c63b 100755 --- a/samples/RandomAccumulatorObject.py +++ b/samples/RandomAccumulatorObject.py @@ -21,10 +21,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # # RandomUnsignedValueProperty # diff --git a/samples/RandomAnalogValueObject.py b/samples/RandomAnalogValueObject.py index 9c79f2f9..d12b8e8b 100755 --- a/samples/RandomAnalogValueObject.py +++ b/samples/RandomAnalogValueObject.py @@ -28,10 +28,6 @@ # settings RANDOM_OBJECT_COUNT = int(os.getenv('RANDOM_OBJECT_COUNT', 10)) -# globals -this_device = None -this_application = None - # # RandomValueProperty # @@ -125,6 +121,5 @@ def main(): _log.debug("fini") - if __name__ == "__main__": main() diff --git a/samples/ReadProperty.py b/samples/ReadProperty.py index 2a13ff14..7fe3a537 100755 --- a/samples/ReadProperty.py +++ b/samples/ReadProperty.py @@ -27,7 +27,6 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None this_console = None @@ -151,7 +150,9 @@ def do_rtn(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -189,7 +190,5 @@ def do_rtn(self, args): _log.debug("fini") -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") +if __name__ == "__main__": + main() diff --git a/samples/ReadPropertyAny.py b/samples/ReadPropertyAny.py index d3ef6a65..3e62fd82 100755 --- a/samples/ReadPropertyAny.py +++ b/samples/ReadPropertyAny.py @@ -26,9 +26,7 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None -this_console = None # # ReadPropertyAnyApplication @@ -136,6 +134,8 @@ def do_read(self, args): # def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() diff --git a/samples/ReadPropertyMultiple.py b/samples/ReadPropertyMultiple.py index a539a576..01514475 100755 --- a/samples/ReadPropertyMultiple.py +++ b/samples/ReadPropertyMultiple.py @@ -207,8 +207,7 @@ def do_read(self, args): def main(): global this_application - global this_device - global this_console + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() diff --git a/samples/ReadPropertyMultipleServer.py b/samples/ReadPropertyMultipleServer.py index 48b7c1a2..9ee8f212 100755 --- a/samples/ReadPropertyMultipleServer.py +++ b/samples/ReadPropertyMultipleServer.py @@ -25,10 +25,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # # RandomValueProperty # diff --git a/samples/ReadRange.py b/samples/ReadRange.py index ce512372..fe4fb27f 100755 --- a/samples/ReadRange.py +++ b/samples/ReadRange.py @@ -24,9 +24,7 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None -this_console = None # # ReadRangeApplication @@ -124,7 +122,9 @@ def do_readrange(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -160,7 +160,7 @@ def do_readrange(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/ReadWriteFile.py b/samples/ReadWriteFile.py index 2628e4cd..93205349 100755 --- a/samples/ReadWriteFile.py +++ b/samples/ReadWriteFile.py @@ -227,7 +227,9 @@ def do_writestream(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -263,7 +265,7 @@ def do_writestream(self, args): run() -except Exception as err: - _log.exception("an error has occurred: %s", err) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/ReadWriteFileServer.py b/samples/ReadWriteFileServer.py index 5ee3c2d1..7cc0fe6b 100755 --- a/samples/ReadWriteFileServer.py +++ b/samples/ReadWriteFileServer.py @@ -218,5 +218,7 @@ def main(): run() + _log.debug("fini") + if __name__ == "__main__": main() diff --git a/samples/ReadWriteProperty.py b/samples/ReadWriteProperty.py index 79c01b2e..a8e6aa8e 100755 --- a/samples/ReadWriteProperty.py +++ b/samples/ReadWriteProperty.py @@ -30,9 +30,7 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None -this_console = None # # ReadPropertyApplication @@ -234,7 +232,9 @@ def do_rtn(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -270,7 +270,7 @@ def do_rtn(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/RecurringMultipleReadProperty.py b/samples/RecurringMultipleReadProperty.py index 137eff7d..785f2f55 100644 --- a/samples/RecurringMultipleReadProperty.py +++ b/samples/RecurringMultipleReadProperty.py @@ -27,11 +27,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None -this_console = None - # point list point_list = [ ('1.2.3.4', 'analogValue', 1, 'presentValue'), @@ -150,7 +145,7 @@ def confirmation(self, apdu): # __main__ # -try: +def main(): # parse the command line arguments parser = ConfigArgumentParser(description=__doc__) @@ -176,6 +171,7 @@ def confirmation(self, apdu): # make a dog this_application = PrairieDog(args.interval, this_device, args.ini.address) + if _debug: _log.debug(" - this_application: %r", this_application) # get the services supported services_supported = this_application.get_services_supported() @@ -188,7 +184,7 @@ def confirmation(self, apdu): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/RecurringTask.py b/samples/RecurringTask.py index 0d63e1e9..b9fc6ba2 100755 --- a/samples/RecurringTask.py +++ b/samples/RecurringTask.py @@ -44,7 +44,7 @@ def process_task(self): # __main__ # -try: +def main(): # parse the command line arguments parser = ArgumentParser(description=__doc__) @@ -68,7 +68,7 @@ def process_task(self): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/SampleApplication.py b/samples/SampleApplication.py index 358e0e4e..a7771939 100755 --- a/samples/SampleApplication.py +++ b/samples/SampleApplication.py @@ -25,10 +25,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # # SampleApplication # @@ -61,7 +57,7 @@ def confirmation(self, apdu): # __main__ # -try: +def main(): # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -93,8 +89,7 @@ def confirmation(self, apdu): run() -except Exception as err: - _log.exception("an error has occurred: %s", err) -finally: - _log.debug("finally") + _log.debug("fini") +if __name__ == "__main__": + main() diff --git a/samples/SampleConsoleCmd.py b/samples/SampleConsoleCmd.py index edc5510d..a0d733fc 100755 --- a/samples/SampleConsoleCmd.py +++ b/samples/SampleConsoleCmd.py @@ -20,11 +20,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None -this_console = None - # # SampleApplication # @@ -70,7 +65,7 @@ def do_nothing(self, args): # __main__ # -try: +def main(): # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -106,8 +101,7 @@ def do_nothing(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") +if __name__ == "__main__": + main() diff --git a/samples/SubscribeCOV.py b/samples/SubscribeCOV.py index f8d10cf3..be8e9dde 100755 --- a/samples/SubscribeCOV.py +++ b/samples/SubscribeCOV.py @@ -24,9 +24,7 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None -this_console = None # how the application should respond rsvp = (True, None, None) @@ -189,7 +187,9 @@ def do_abort(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -217,6 +217,7 @@ def do_abort(self, args): # make a console this_console = SubscribeCOVConsoleCmd() + if _debug: _log.debug(" - this_console: %r", this_console) # enable sleeping will help with threads enable_sleeping() @@ -225,7 +226,7 @@ def do_abort(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/TCPServer.py b/samples/TCPServer.py index fd851547..b020e42a 100755 --- a/samples/TCPServer.py +++ b/samples/TCPServer.py @@ -24,9 +24,6 @@ SERVER_HOST = os.getenv('SERVER_HOST', 'any') SERVER_PORT = int(os.getenv('SERVER_PORT', 9000)) -# globals -server_address = None - # # EchoMaster # @@ -89,8 +86,7 @@ def main(): host = args.host if host == "any": host = '' - port = args.port - server_address = (host, port) + server_address = (host, args.port) if _debug: _log.debug(" - server_address: %r", server_address) # create a director listening to the address @@ -114,4 +110,3 @@ def main(): if __name__ == "__main__": main() - diff --git a/samples/UDPConsole.py b/samples/UDPConsole.py index e85926cc..ca4912e2 100755 --- a/samples/UDPConsole.py +++ b/samples/UDPConsole.py @@ -175,7 +175,7 @@ def confirmation(self, pdu): # __main__ # -try: +def main(): # parse the command line arguments parser = ArgumentParser(description=__doc__) parser.add_argument("address", @@ -235,7 +235,7 @@ def confirmation(self, pdu): run() -except Exception as e: - _log.exception("an error has occurred: %s", e) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main() diff --git a/samples/VendorAVObject.py b/samples/VendorAVObject.py index 1823f44e..fdc8fdc3 100755 --- a/samples/VendorAVObject.py +++ b/samples/VendorAVObject.py @@ -84,59 +84,54 @@ def __init__(self, **kwargs): @bacpypes_debugging def main(): - if _debug: main._debug("initialization") + global vendor_id - try: - # parse the command line arguments - args = ConfigArgumentParser(description=__doc__).parse_args() + # parse the command line arguments + args = ConfigArgumentParser(description=__doc__).parse_args() - if _debug: main._debug("initialization") - if _debug: main._debug(" - args: %r", args) + if _debug: _log.debug("initialization") + if _debug: _log.debug(" - args: %r", args) - # make a device object - this_device = LocalDeviceObject( - objectName=args.ini.objectname, - objectIdentifier=int(args.ini.objectidentifier), - maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted), - segmentationSupported=args.ini.segmentationsupported, - vendorIdentifier=vendor_id, - ) + # make a device object + this_device = LocalDeviceObject( + objectName=args.ini.objectname, + objectIdentifier=int(args.ini.objectidentifier), + maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted), + segmentationSupported=args.ini.segmentationsupported, + vendorIdentifier=vendor_id, + ) - # make a sample application - this_application = BIPSimpleApplication(this_device, args.ini.address) + # make a sample application + this_application = BIPSimpleApplication(this_device, args.ini.address) - # get the services supported - services_supported = this_application.get_services_supported() - if _debug: _log.debug(" - services_supported: %r", services_supported) + # get the services supported + services_supported = this_application.get_services_supported() + if _debug: _log.debug(" - services_supported: %r", services_supported) - # let the device object know - this_device.protocolServicesSupported = services_supported.value + # let the device object know + this_device.protocolServicesSupported = services_supported.value - # make some objects - ravo1 = VendorAVObject( - objectIdentifier=(513, 1), objectName='Random1' - ) - if _debug: main._debug(" - ravo1: %r", ravo1) + # make some objects + ravo1 = VendorAVObject( + objectIdentifier=(513, 1), objectName='Random1' + ) + if _debug: _log.debug(" - ravo1: %r", ravo1) - ravo2 = VendorAVObject( - objectIdentifier=(513, 2), objectName='Random2' - ) - if _debug: main._debug(" - ravo2: %r", ravo2) + ravo2 = VendorAVObject( + objectIdentifier=(513, 2), objectName='Random2' + ) + if _debug: _log.debug(" - ravo2: %r", ravo2) - # add it to the device - this_application.add_object(ravo1) - this_application.add_object(ravo2) - if _debug: main._debug(" - object list: %r", this_device.objectList) + # add it to the device + this_application.add_object(ravo1) + this_application.add_object(ravo2) + if _debug: _log.debug(" - object list: %r", this_device.objectList) - if _debug: main._debug("running") + if _debug: _log.debug("running") - run() + run() - except Exception as error: - main._exception("an error has occurred: %s", error) - finally: - if _debug: main._debug("finally") + if _debug: _log.debug("fini") if __name__ == '__main__': main() - diff --git a/samples/VendorReadWriteProperty.py b/samples/VendorReadWriteProperty.py index 375913a0..75802b03 100755 --- a/samples/VendorReadWriteProperty.py +++ b/samples/VendorReadWriteProperty.py @@ -31,6 +31,9 @@ _debug = 0 _log = ModuleLogger(globals()) +# globals +this_application = None + # # ReadPropertyApplication # @@ -97,8 +100,6 @@ class ReadWritePropertyConsoleCmd(ConsoleCmd): def do_read(self, args): """read [ ]""" - global this_application - args = args.split() if _debug: ReadWritePropertyConsoleCmd._debug("do_read %r", args) @@ -141,8 +142,6 @@ def do_read(self, args): def do_write(self, args): """write [ ] [ ]""" - global this_application - args = args.split() ReadWritePropertyConsoleCmd._debug("do_write %r", args) @@ -235,16 +234,14 @@ def do_write(self, args): # main # -@bacpypes_debugging def main(): - if _debug: main._debug("initialization") global this_application # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() - if _debug: main._debug("initialization") - if _debug: main._debug(" - args: %r", args) + if _debug: _log.debug("initialization") + if _debug: _log.debug(" - args: %r", args) # make a device object this_device = LocalDeviceObject( @@ -272,10 +269,11 @@ def main(): # enable sleeping will help with threads enable_sleeping() - main._debug("running") + _log.debug("running") run() + _log.debug("fini") + if __name__ == '__main__': main() - diff --git a/samples/WhoHasIHaveApplication.py b/samples/WhoHasIHaveApplication.py index 3600b169..65a90f2b 100755 --- a/samples/WhoHasIHaveApplication.py +++ b/samples/WhoHasIHaveApplication.py @@ -21,10 +21,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # counters who_has_counter = defaultdict(int) i_have_counter = defaultdict(int) @@ -76,7 +72,7 @@ def do_IHaveRequest(self, apdu): # __main__ # -try: +def main(): # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -107,6 +103,8 @@ def do_IHaveRequest(self, apdu): # run until stopped, ^C works run() + _log.debug("fini") + print("----- Who Has -----") for (src, objname), count in sorted(who_has_counter.items()): print("%-20s %-30s %4d" % (src, objname, count)) @@ -117,7 +115,5 @@ def do_IHaveRequest(self, apdu): print("%-20s %-20s %-20s %-20s %4d" % (src, devid, objid, objname, count)) print("") -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") +if __name__ == "__main__": + main() diff --git a/samples/WhoIsIAm.py b/samples/WhoIsIAm.py index 7af67aa1..c00803b2 100755 --- a/samples/WhoIsIAm.py +++ b/samples/WhoIsIAm.py @@ -26,9 +26,7 @@ _log = ModuleLogger(globals()) # globals -this_device = None this_application = None -this_console = None # # WhoIsIAmApplication @@ -162,7 +160,9 @@ def do_rtn(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -200,6 +200,7 @@ def do_rtn(self, args): # make a console this_console = WhoIsIAmConsoleCmd() + if _debug: _log.debug(" - this_console: %r", this_console) # enable sleeping will help with threads enable_sleeping() @@ -208,8 +209,7 @@ def do_rtn(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") +if __name__ == "__main__": + main() diff --git a/samples/WhoIsIAmApplication.py b/samples/WhoIsIAmApplication.py index 2efb3a8f..73d06d5a 100755 --- a/samples/WhoIsIAmApplication.py +++ b/samples/WhoIsIAmApplication.py @@ -20,10 +20,6 @@ _debug = 0 _log = ModuleLogger(globals()) -# globals -this_device = None -this_application = None - # counters who_is_counter = defaultdict(int) i_am_counter = defaultdict(int) @@ -73,7 +69,7 @@ def do_IAmRequest(self, apdu): # __main__ # -try: +def main(): # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -103,6 +99,8 @@ def do_IAmRequest(self, apdu): run() + _log.debug("fini") + print("----- Who Is -----") for (src, lowlim, hilim), count in sorted(who_is_counter.items()): print("%-20s %8s %8s %4d" % (src, lowlim, hilim, count)) @@ -113,7 +111,5 @@ def do_IAmRequest(self, apdu): print("%-20s %8d %4d" % (src, devid, count)) print("") -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") +if __name__ == "__main__": + main() diff --git a/samples/WhoIsIAmForeign.py b/samples/WhoIsIAmForeign.py index 7b3a9c65..2e3b9e1d 100755 --- a/samples/WhoIsIAmForeign.py +++ b/samples/WhoIsIAmForeign.py @@ -28,7 +28,6 @@ # globals this_device = None this_application = None -this_console = None # # WhoIsIAmApplication @@ -163,7 +162,7 @@ def do_rtn(self, args): # def main(): - global this_device, this_application, this_console + global this_device, this_application # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -217,7 +216,8 @@ def main(): run() - _log.debug("finally") + _log.debug("fini") + if __name__ == "__main__": main() diff --git a/samples/WhoIsRouter.py b/samples/WhoIsRouter.py index c4b47fff..af33767d 100755 --- a/samples/WhoIsRouter.py +++ b/samples/WhoIsRouter.py @@ -103,7 +103,9 @@ def do_wirtn(self, args): # __main__ # -try: +def main(): + global this_application + # parse the command line arguments args = ConfigArgumentParser(description=__doc__).parse_args() @@ -125,7 +127,7 @@ def do_wirtn(self, args): run() -except Exception as error: - _log.exception("an error has occurred: %s", error) -finally: - _log.debug("finally") + _log.debug("fini") + +if __name__ == "__main__": + main()