From ab8c215a50b9d9b0868caed0fce5874b961222b7 Mon Sep 17 00:00:00 2001 From: Justin Boss Date: Wed, 6 Dec 2023 14:10:42 -0500 Subject: [PATCH] Python3 changes --- scripts/ConfigureFEP.py | 8 +-- scripts/PassSetup.py | 4 +- scripts/SetMomentumWheelSpeed.py | 2 +- space/__init__.py | 106 +++++++++++++++---------------- space/commands.py | 8 +-- space/gems.py | 20 +++--- space/gemsdir.py | 8 +-- space/links.py | 22 +++---- space/loader.py | 51 +++++++-------- space/parameters.py | 16 ++--- space/queries.py | 14 ++-- space/shell.py | 2 +- space/system.py | 20 +++--- space/times.py | 2 +- 14 files changed, 142 insertions(+), 141 deletions(-) diff --git a/scripts/ConfigureFEP.py b/scripts/ConfigureFEP.py index 4f9e60c..2c4acf2 100644 --- a/scripts/ConfigureFEP.py +++ b/scripts/ConfigureFEP.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 ''' This SpacePython module provides an example of a spacecraft operations procedure that configures a GEMS device and then @@ -27,10 +27,10 @@ def invoke(args): equipment.get(['Syncword']) try: verify(syncword.value()==newPattern) - print 'Sync pattern is {0:#X}'.format(syncword.value()) + print('Sync pattern is {0:#X}'.format(syncword.value())) except VerifyError: - print 'Sync pattern is {0:#X} instead of {1:#X}'\ - .format(syncword.value(), newPattern) + print('Sync pattern is {0:#X} instead of {1:#X}'\ + .format(syncword.value(), newPattern)) return FAILED return SUCCESSFUL # diff --git a/scripts/PassSetup.py b/scripts/PassSetup.py index 589ac69..6f45e97 100644 --- a/scripts/PassSetup.py +++ b/scripts/PassSetup.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 ''' This SpacePython module provides an example of a spacecraft operations procedure that queries the operator, invokes a native @@ -28,7 +28,7 @@ def invoke(args): try: waitFor(lambda:sat1.state()=='UP') except TimeoutError: - print 'Timed out waiting for contact' + print('Timed out waiting for contact') return FAILED return SUCCESSFUL # diff --git a/scripts/SetMomentumWheelSpeed.py b/scripts/SetMomentumWheelSpeed.py index 0d34a7f..c8e63ab 100644 --- a/scripts/SetMomentumWheelSpeed.py +++ b/scripts/SetMomentumWheelSpeed.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 ''' This SpacePython module provides an example of a spacecraft operations procedure that checks a subsystem state, optionally diff --git a/space/__init__.py b/space/__init__.py index 0bcc12b..8151273 100644 --- a/space/__init__.py +++ b/space/__init__.py @@ -8,56 +8,56 @@ databases. import logging log = logging.getLogger(__name__) # -from errors import SpacePythonException -from errors import GemsError -from errors import IllegalLinkError -from errors import IllegalValueError -from errors import QueryCanceledError -from errors import QueryAbortedError -from errors import TimeoutError -from errors import TransmissionError -from errors import UndefinedTypeError -from errors import UnknownParameterError -from errors import VerificationError -from errors import VerifyError -# -from commands import CommandRequest -from commands import Command -# -from gems import GemsDevice -# -from gemsdir import GemsDirective -# -from links import Link -from links import Downlink -# -from parameters import Parameter -from parameters import GemsParameter -from parameters import XtceParameter -from parameters import GroundParameter -# -from queries import operatorQuery -# -from shell import Namespace, parseArgs -# -from system import activeEquipment -from system import activeLinks -from system import directives -from system import equipment -from system import FAILED -from system import links -from system import loadProcedure -from system import lookupDirective -from system import lookupParameter -from system import now -from system import parameters -from system import procedures -from system import SUCCESSFUL -from system import today -from system import verify -# -from times import SpecificTime -from times import TimeInterval -from times import wait -from times import waitFor -from times import waitUntil +from .errors import SpacePythonException +from .errors import GemsError +from .errors import IllegalLinkError +from .errors import IllegalValueError +from .errors import QueryCanceledError +from .errors import QueryAbortedError +from .errors import TimeoutError +from .errors import TransmissionError +from .errors import UndefinedTypeError +from .errors import UnknownParameterError +from .errors import VerificationError +from .errors import VerifyError +# +from .commands import CommandRequest +from .commands import Command +# +from .gems import GemsDevice +# +from .gemsdir import GemsDirective +# +from .links import Link +from .links import Downlink +# +from .parameters import Parameter +from .parameters import GemsParameter +from .parameters import XtceParameter +from .parameters import GroundParameter +# +from .queries import operatorQuery +# +from .shell import Namespace, parseArgs +# +from .system import activeEquipment +from .system import activeLinks +from .system import directives +from .system import equipment +from .system import FAILED +from .system import links +from .system import loadProcedure +from .system import lookupDirective +from .system import lookupParameter +from .system import now +from .system import parameters +from .system import procedures +from .system import SUCCESSFUL +from .system import today +from .system import verify +# +from .times import SpecificTime +from .times import TimeInterval +from .times import wait +from .times import waitFor +from .times import waitUntil diff --git a/space/commands.py b/space/commands.py index bc98389..d6d9544 100644 --- a/space/commands.py +++ b/space/commands.py @@ -3,7 +3,7 @@ Commands and CommandRequests may be sent through a Link ''' __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' -from errors import UnknownParameterError +from .errors import UnknownParameterError class Command(object): #Normative ''' The Command class incorporates the command name a list of typed @@ -21,9 +21,9 @@ class Command(object): #Normative def setValues(self, **args): '''Set the argument values from the Keyword=Value pairs passed ''' - params = args.keys() + params = list(args.keys()) for param in params: - if self.arg.has_key(param): + if param in self.arg: self.arg[param].setValue(args[param]) else: raise UnknownParameterError('Specified command argument {0} not defined for {1}' @@ -34,7 +34,7 @@ class Command(object): #Normative '''Send the command to the link with the defined argument values ''' args = dict() - for argName in self.arg.keys(): + for argName in list(self.arg.keys()): args[argName] = self.arg[argName].value() self.link.send(self.name, _flags=_flags, **args) class CommandRequest(object): #Normative diff --git a/space/gems.py b/space/gems.py index 854c331..0fea8fa 100644 --- a/space/gems.py +++ b/space/gems.py @@ -6,7 +6,7 @@ __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' import re from space import log -from errors import GemsError +from .errors import GemsError # Initialize the list of known devices devices_ = dict() @@ -22,7 +22,7 @@ class GemsDevice: #Normative self.device = device devices_[device.name] = device else: - if devices_.has_key(device): + if device in devices_: self.device = devices_[device] else: raise GemsError('GemsDevice {0} is not defined'.format(device)) @@ -48,7 +48,7 @@ class GemsDevice: #Normative def set(self, **arguments): '''Set the value for the parameter=value pairs specified ''' - params = arguments.keys() + params = list(arguments.keys()) if len(params) > 0: out = 'Setting {0} parameters:'.format(self.device.name) for param in params: @@ -61,7 +61,7 @@ class GemsDevice: #Normative '''Return a directive with the specified name, if the device has a defined directive. ''' - if self.device.dSet.has_key(name): + if name in self.device.dSet: return self.device.dSet[name] else: return None @@ -71,8 +71,8 @@ class GemsDevice: #Normative device and subsystem whose names pass the provided filter. Default values return all known parameters. ''' - keys = self.device.dSet.keys() - if regexp is not '': + keys = list(self.device.dSet.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys @@ -80,7 +80,7 @@ class GemsDevice: #Normative def lookupParameter(self, name): '''Lookup a parameter associated with this device. ''' - if self.device.pSet.has_key(name): + if name in self.device.pSet: return self.device.pSet[name] else: return None @@ -90,8 +90,8 @@ class GemsDevice: #Normative device and subsystem whose names pass the provided filters. Default values return all known parameters. ''' - keys = self.device.pSet.keys() - if regexp is not '': + keys = list(self.device.pSet.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys @@ -108,7 +108,7 @@ class GemsDevice: #Normative # if possible else: log.info('Sending {dir} to GemsDevice {dev}'.format(dir=directive, dev=self.device.name)) - params = arguments.keys() + params = list(arguments.keys()) if len(params) > 0: out = ' Directive parameters:' for param in params: diff --git a/space/gemsdir.py b/space/gemsdir.py index 89e2360..a0d1e80 100644 --- a/space/gemsdir.py +++ b/space/gemsdir.py @@ -3,7 +3,7 @@ GEMS Directives ''' __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' -from errors import UnknownParameterError +from .errors import UnknownParameterError class GemsDirective(object): #Normative ''' GEMS directives are similar to spacecraft commands. There is a directive name @@ -22,9 +22,9 @@ class GemsDirective(object): #Normative def setValues(self, **args): '''Set the Parameter values from the Keyword=Value pairs passed ''' - params = args.keys() + params = list(args.keys()) for param in params: - if self.arg.has_key(param): + if param in self.arg: self.arg[param].setValue(args[param]) else: raise UnknownParameterError('GEMS Parameter {0} not defined for {1}' @@ -35,6 +35,6 @@ class GemsDirective(object): #Normative '''Send the directive to the device with the defined parameter values ''' args = dict() - for argName in self.arg.keys(): + for argName in list(self.arg.keys()): args[argName] = self.arg[argName].value() self.device.send(self.name, **args) \ No newline at end of file diff --git a/space/links.py b/space/links.py index e4e7b4f..c5113bb 100644 --- a/space/links.py +++ b/space/links.py @@ -6,8 +6,8 @@ __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' import re from space import log -from errors import IllegalLinkError -from commands import CommandRequest +from .errors import IllegalLinkError +from .commands import CommandRequest # Create a dictionary for the known links links_ = dict() @@ -33,7 +33,7 @@ class Link(object): #Normative links_[spaceSystem.name] = spaceSystem self.system.storeLink(self) else: - if links_.has_key(spaceSystem): + if spaceSystem in links_: self.system = links_[spaceSystem] else: raise IllegalLinkError('SpaceSystem {0} is not linked' @@ -55,7 +55,7 @@ class Link(object): #Normative # if possible else: log.info('Sending {cmd} to spaceSystem {sys}'.format(cmd=command, sys=self.system.name)) - params = arguments.keys() + params = list(arguments.keys()) if len(params) > 0: out = ' Command arguments:' for param in params: @@ -63,13 +63,13 @@ class Link(object): #Normative log.info(out) if len(_flags) > 0: out = ' Command flags:' - for flag in _flags.keys(): + for flag in list(_flags.keys()): out += ' {name}={value}'.format(name=flag, value=_flags[flag]) log.info(out) def lookupParameter(self, name): '''Lookup a parameter associated with this Link/spaceSystem ''' - if self.system.pSet.has_key(name): + if name in self.system.pSet: return self.system.pSet[name] else: return None @@ -79,8 +79,8 @@ class Link(object): #Normative The default value results in a list of all parameters, which is not recommended due to the potential list size ''' - keys = self.system.pSet.keys() - if regexp is not '': + keys = list(self.system.pSet.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys @@ -88,7 +88,7 @@ class Link(object): #Normative def lookupCommand(self, name): '''Lookup a command associated with this Link/spaceSystem ''' - if self.system.cSet.has_key(name): + if name in self.system.cSet: return self.system.cSet[name] else: return None @@ -98,8 +98,8 @@ class Link(object): #Normative The default value results in a list of all commands, which is not recommended due to the potential list size. ''' - keys = self.system.cSet.keys() - if regexp is not '': + keys = list(self.system.cSet.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys diff --git a/space/loader.py b/space/loader.py index 0d31396..f5f9b0b 100644 --- a/space/loader.py +++ b/space/loader.py @@ -9,11 +9,11 @@ import sys import yaml from space import log -from parameters import Parameter, XtceParameter, GemsParameter -from commands import Command -from gemsdir import GemsDirective -from links import Link -from gems import GemsDevice +from .parameters import Parameter, XtceParameter, GemsParameter +from .commands import Command +from .gemsdir import GemsDirective +from .links import Link +from .gems import GemsDevice def loadFromYaml(specFile=None): '''Load the datasets from the optionally specified file name. @@ -33,15 +33,16 @@ def loadFromYaml(specFile=None): f = None for fname in datasetList: if os.path.isfile(fname): - f = file(fname) + file = open(fname, 'r') + f = file.read() break if f is None: raise Exception('Could not find dataset SpacePythonDataset.yaml') # Add the local type tag constructors loader = yaml.SafeLoader(f) - loader.add_constructor(u'!SpaceSystem', ss_constructor) - loader.add_constructor(u'!GemsDevice', dev_constructor) - loader.add_constructor(u'!Restriction', restriction_constructor) + loader.add_constructor('!SpaceSystem', ss_constructor) + loader.add_constructor('!GemsDevice', dev_constructor) + loader.add_constructor('!Restriction', restriction_constructor) # Load the datasets for item in loader.get_data(): if isinstance(item, SpaceSystem): @@ -55,9 +56,9 @@ class SpaceSystem(object): self.pSet = pSet self.cSet = cSet def storeLink(self, link): - for param in self.pSet.itervalues(): + for param in self.pSet.values(): param.link = link - for cmd in self.cSet.itervalues(): + for cmd in self.cSet.values(): cmd.link = link class Device(object): @@ -66,9 +67,9 @@ class Device(object): self.pSet = pSet self.dSet = dSet def storeDevice(self, device): - for param in self.pSet.itervalues(): + for param in self.pSet.values(): param.device = device - for cmd in self.dSet.itervalues(): + for cmd in self.dSet.values(): cmd.device = device def ss_constructor(loader, node): @@ -84,26 +85,26 @@ def ss_constructor(loader, node): log.warn('No CommandSet mapping for {0}'.format(name)) pSet = dict() for pMap in ps: - pName = pMap.keys()[0] + pName = list(pMap.keys())[0] defSeq = pMap[pName] xp = XtceParameter(pName, None, defSeq[0], **defSeq[1]) - if pSet.has_key(pName): + if pName in pSet: log.warn('Duplicate parameter name {0} in {1}'.format(pName, name)) pSet[pName] = xp cSet = dict() for command in cs: - cName = command.keys()[0] + cName = list(command.keys())[0] paramSeq = command[cName] args = dict() for pMap in paramSeq: - pName = pMap.keys()[0] + pName = list(pMap.keys())[0] defSeq = pMap[pName] p = Parameter(pName, defSeq[0], **defSeq[1]) - if args.has_key(pName): + if pName in args: log.warn('Duplicate parameter name {0} in command {1}'.format(pName, cName)) args[pName] = p cmd = Command(cName, None, args) - if cSet.has_key(cName): + if cName in cSet: log.warn('Duplicate command name {0} in {1}'.format(cName, name)) cSet[cName] = cmd return SpaceSystem(name, pSet, cSet) @@ -121,26 +122,26 @@ def dev_constructor(loader, node): log.debug('No DirectiveSet mapping for {0}'.format(name)) pSet = dict() for pMap in ps: - pName = pMap.keys()[0] + pName = list(pMap.keys())[0] defSeq = pMap[pName] xp = GemsParameter(pName, None, defSeq[0], **defSeq[1]) - if pSet.has_key(pName): + if pName in pSet: log.warn('Duplicate parameter name {0} in {1}'.format(pName, name)) pSet[pName] = xp cSet = dict() for directive in cs: - cName = directive.keys()[0] + cName = list(directive.keys())[0] paramSeq = directive[cName] args = dict() for pMap in paramSeq: - pName = pMap.keys()[0] + pName = list(pMap.keys())[0] defSeq = pMap[pName] p = Parameter(pName, defSeq[0], **defSeq[1]) - if args.has_key(pName): + if pName in args: log.warn('Duplicate parameter name {0} in directive {1}'.format(pName, cName)) args[pName] = p drct = GemsDirective(cName, None, args) - if cSet.has_key(cName): + if cName in cSet: log.warn('Duplicate directive name {0} in {1}'.format(cName, name)) cSet[cName] = drct return Device(name, pSet, cSet) diff --git a/space/parameters.py b/space/parameters.py index 620c78f..ddbafeb 100644 --- a/space/parameters.py +++ b/space/parameters.py @@ -5,8 +5,8 @@ are items defined by the ground system itself. __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' import re -from times import SpecificTime, TimeInterval -from errors import UndefinedTypeError, IllegalValueError +from .times import SpecificTime, TimeInterval +from .errors import UndefinedTypeError, IllegalValueError # define an integer conversion for unsigned types def unsigned(value): i = int(value) # Let python do all the checking @@ -19,7 +19,7 @@ def unsigned(value): # and provides a conversion function for each type. typeNames = {'boolean': bool, 'byte': int, 'ubyte': unsigned, \ 'short': int, 'ushort': unsigned, 'int': int, \ - 'uint': unsigned, 'long': long, 'ulong': long,\ + 'uint': unsigned, 'long': int, 'ulong': int,\ 'float': float, 'double': float, 'string': str,\ 'posixTime': SpecificTime.fromStr,'hexBitField': int,\ 'uTime': SpecificTime.fromStr, 'interval': TimeInterval.fromStr} @@ -31,7 +31,7 @@ class Parameter: #Normative '''Create a new Parameter instance ''' self.name = name - if typeNames.has_key(dType): + if dType in typeNames: self.dType = dType else: raise UndefinedTypeError('Could not create parameter of data type <{0}>'.format(dType)) @@ -42,7 +42,7 @@ class Parameter: #Normative self.value_ = None self.raw_ = None self.time_ = None - options = kwds.keys() + options = list(kwds.keys()) for option in options: if hasattr(self, option): setattr(self, option, kwds[option]) @@ -97,7 +97,7 @@ class GemsParameter(Parameter): #Normative ''' def __init__(self, name, device=None, dType='double', **kwds): self.device = device #Active device connection for sets/gets - if kwds.has_key('writable'): + if 'writable' in kwds: self.writable = kwds['writable'] else: self.writable = True @@ -106,7 +106,7 @@ class GemsParameter(Parameter): #Normative class XtceParameter(Parameter): #Normative def __init__(self, name, link=None, dType='double', **kwds): self.link = link #Active link for data refresh - if kwds.has_key('writable'): + if 'writable' in kwds: self.writable = kwds['writable'] else: self.writable = True @@ -116,7 +116,7 @@ class GroundParameter(Parameter): #Normative '''Creates a Ground system parameter ''' def __init__(self, name, dType='double', **kwds): - if kwds.has_key('writable'): + if 'writable' in kwds: self.writable = kwds['writable'] else: self.writable = True diff --git a/space/queries.py b/space/queries.py index 369918a..16a87d2 100644 --- a/space/queries.py +++ b/space/queries.py @@ -7,8 +7,8 @@ __copyright__ = 'Copyright 2012, Harris Corporation' # a or input from the operator, but it is # anticipated that a GUI-based query could, so exceptions are # defined for the interface. -from errors import QueryAbortedError, QueryCanceledError -from shell import Namespace +from .errors import QueryAbortedError, QueryCanceledError +from .shell import Namespace # # This provides a simple console input implementation of the operator # query. It is expected that an integrated procedure environment @@ -26,17 +26,17 @@ def operatorQuery(prompt='', **parameters): #Normative namespace = Namespace() if len(parameters) > 0: if prompt != '': - print prompt, '\n' - for name in parameters.keys(): + print(prompt, '\n') + for name in list(parameters.keys()): pr_str = 'New value for %s, or for default (%s) ' % (name,parameters[name]) - response = raw_input(pr_str) + response = input(pr_str) if response != '': namespace.__setattr__(name, response) else: namespace.__setattr__(name, parameters[name]) else: # No values to get if prompt != '': - response = raw_input(prompt) + response = input(prompt) else: - response = raw_input(' to continue') + response = input(' to continue') return namespace diff --git a/space/shell.py b/space/shell.py index 8c6035d..20608d7 100644 --- a/space/shell.py +++ b/space/shell.py @@ -52,7 +52,7 @@ class Parser(object): argname = arg[0:equals] value = arg[equals+1:] name = argname[2:] - if self.parms.has_key(name): + if name in self.parms: self.parseValue(self.parms[name], value) else: self.error('unrecognized argument %s' % argname) diff --git a/space/system.py b/space/system.py index 60ba608..7a85abf 100644 --- a/space/system.py +++ b/space/system.py @@ -7,11 +7,11 @@ import re import inspect # from space import log -from times import SpecificTime -from errors import VerifyError -import loader -import links -import gems +from .times import SpecificTime +from .errors import VerifyError +from . import loader +from . import links +from . import gems # SpacePython script return values FAILED = -1 # Script failure return SUCCESSFUL = 0 # Script success return @@ -33,7 +33,7 @@ class NativeProcedure(object): log.info('Invoking native procedure {0}'.format(self.__scriptname__)) if args is not None: logstr = ' with arguments (' - for name, value in args.__dict__.iteritems(): + for name, value in args.__dict__.items(): logstr += '{0}={1}'.format(name, value) logstr += ')' log.info(logstr) @@ -87,8 +87,8 @@ def links(regexp=''): #Normative The default value results in list of all SpaceSystems with telemetry, command, and/or procedure definitions. ''' - keys = links.links_.keys() - if regexp is not '': + keys = list(links.links_.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys @@ -103,8 +103,8 @@ def equipment(regexp=''): #Normative '''Return a list of GEMS devices with names passing the regexp filter. The default value results in a list of all devices. ''' - keys = gems.devices_.keys() - if regexp is not '': + keys = list(gems.devices_.keys()) + if regexp != '': does_it = re.compile(regexp) keys = list(filter(does_it.match, keys)) return keys diff --git a/space/times.py b/space/times.py index 7a106a9..8c49d3a 100644 --- a/space/times.py +++ b/space/times.py @@ -7,7 +7,7 @@ __author__ = 'Brad Kizzort' __copyright__ = 'Copyright 2012, Harris Corporation' import inspect import datetime, time -from errors import TimeoutError +from .errors import TimeoutError class SpecificTime(datetime.datetime): #Normative '''SpecificTime(year, month, day[, hour[, minute[, second[, microsecond]]]) -- 2.42.0.windows.2