<commit_before># @(#) $Id: Enum.py,v 1.4 2005/06/13 18:04:24 dfugate Exp $
#
# Copyright (C) 2001
# Associated Universities, Inc. Washington DC, USA.
#
# Produced for the ALMA project
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Library General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
# details.
#
# You should have received a copy of the GNU Library General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 675 Massachusetts Ave, Cambridge, MA 02139, USA.  Correspondence concerning
# ALMA should be addressed as follows:
#
# Internet email: <EMAIL>
# "@(#) $Id: Enum.py,v 1.4 2005/06/13 18:04:24 dfugate Exp $"
#
# who       when        what
# --------  ----------  ----------------------------------------------
# dfugate   2004/07/21  Created.
#------------------------------------------------------------------------------

'''
This module provides an implementation of the Penum IDL interface:
'''

__version__ = "$Id: Enum.py,v 1.4 2005/06/13 18:04:24 dfugate Exp $"

#--REGULAR IMPORTS-------------------------------------------------------------
from traceback import print_exc
#--CORBA STUBS-----------------------------------------------------------------
import ACS__POA
from ACS import RED, YELLOW, GREEN, GREY  #Condition enumerations
from ACS import CBDescOut
import CORBA
#--ACS Imports-----------------------------------------------------------------
from ACSImpl.GenericProperty     import GenericProperty
from ACSImpl.Monitors            import Monitorpattern
from Acspy.Util.ACSCorba         import interfaceRepository
#--GLOBALS---------------------------------------------------------------------

#------------------------------------------------------------------------------
#--P property------------------------------------------------------------------
#------------------------------------------------------------------------------
class Penum(GenericProperty):
    '''
    Properties can be derived from Penum only if their IDL derives from
    ACS::Penum.
    '''
    #--------------------------------------------------------------------------
    def __init__(self, name, charCompRef, devIORef):
        '''
        Constructor

        Params:
        - name is the quite literally the name of the property
        - charCompRef is the characteristic component object which contains this
        property
        - devIORef is a reference to a DevIO to be used with this property

        Returns: Nothing

        Raises: Nothing.
        '''
        #------------------------------------------------------------------------------
        #determine the real type of the BACI property using the IFR
        #First determine the property's name
        self.enumIFRName = None
        
        IR = interfaceRepository()
        interf = IR.lookup_id(self._NP_RepositoryId)
        
        #determine the underlying enumeration type
        for attr in interf._narrow(CORBA.InterfaceDef).describe_interface().attributes:
            #we can use the default_value to get at an instance of the real enumeration
            if attr.name == "default_value":
                #ifr name of the enum
                self.enumIFRName = attr.type.id()
                break

        #determine the IDL type
        self.enumName = self.enumIFRName.split(':')[1].split('/')[1:]
        self.modName = self.enumName[0]

        #convert the list to a stringified Python package structure which can
        #be used with eval
        self.enumName = reduce((lambda x, y : str(x) + '.' + str(y)), self.enumName)

        #now comes the complicated part...importing the correct CORBA stub
        #without polluting the local namespace...
        self.tGlobals = {}
        self.tLocals  = {}
        exec "import " + self.modName in self.tGlobals, self.tLocals
        
        GenericProperty.__init__(self, name, charCompRef, devIORef)
        
        return
    #--------------------------------------------------------------------------
    def coerceToPropertyType(self, value=None):
        '''
        Overriden.
        '''
        #something went wrong. Return default value
        if value==None:
            #just use the first enum!
            return eval(self.enumName + "._items[0]", self.tGlobals, self.tLocals)
        
        try:
            #coerce into an enum type
            return eval(self.enumName + "." + value, self.tGlobals, self.tLocals) 
        except:
            #warn them about CDB access
            self.getLogger().logAlert("Unble to coerce '" + str(value) + "' into the correct type!")
            print_exc()
            #return an acceptable default value instead...an empty sequence
            return eval(self.enumName + "._items[0]", self.tGlobals, self.tLocals)
    #--------------------------------------------------------------------------
    def getMonitorObject(self, scheduler, timeoutID):
        '''
        Helper method.used to return a monitor of the correct type.
        '''
        return Monitorpattern(scheduler, timeoutID)
    #--------------------------------------------------------------------------
    def monitorRunner(self, cb, desc):
        '''<commit_msg>Fixed bug <NAME> discovered related to enum properties with an entry within the ACS CDB. Bigger problem of mapping IDL enums to integers still remains.<commit_after># @(#) $Id: Enum.py,v 1.5 2006/06/06 20:38:29 dfugate Exp $
#
# Copyright (C) 2001
# Associated Universities, Inc. Washington DC, USA.
#
# Produced for the ALMA project
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Library General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
# details.
#
# You should have received a copy of the GNU Library General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 675 Massachusetts Ave, Cambridge, MA 02139, USA.  Correspondence concerning
# ALMA should be addressed as follows:
#
# Internet email: <EMAIL>
# "@(#) $Id: Enum.py,v 1.5 2006/06/06 20:38:29 dfugate Exp $"
#
# who       when        what
# --------  ----------  ----------------------------------------------
# dfugate   2004/07/21  Created.
#------------------------------------------------------------------------------

'''
This module provides an implementation of the Penum IDL interface:
'''

__version__ = "$Id: Enum.py,v 1.5 2006/06/06 20:38:29 dfugate Exp $"

#--REGULAR IMPORTS-------------------------------------------------------------
from traceback import print_exc
#--CORBA STUBS-----------------------------------------------------------------
import ACS__POA
from ACS import RED, YELLOW, GREEN, GREY  #Condition enumerations
from ACS import CBDescOut
import CORBA
#--ACS Imports-----------------------------------------------------------------
from ACSImpl.GenericProperty     import GenericProperty
from ACSImpl.Monitors            import Monitorpattern
from Acspy.Util.ACSCorba         import interfaceRepository
#--GLOBALS---------------------------------------------------------------------

#------------------------------------------------------------------------------
#--P property------------------------------------------------------------------
#------------------------------------------------------------------------------
class Penum(GenericProperty):
    '''
    Properties can be derived from Penum only if their IDL derives from
    ACS::Penum.
    '''
    #--------------------------------------------------------------------------
    def __init__(self, name, charCompRef, devIORef):
        '''
        Constructor

        Params:
        - name is the quite literally the name of the property
        - charCompRef is the characteristic component object which contains this
        property
        - devIORef is a reference to a DevIO to be used with this property

        Returns: Nothing

        Raises: Nothing.
        '''
        #------------------------------------------------------------------------------
        #determine the real type of the BACI property using the IFR
        #First determine the property's name
        self.enumIFRName = None
        
        IR = interfaceRepository()
        interf = IR.lookup_id(self._NP_RepositoryId)
        
        #determine the underlying enumeration type
        for attr in interf._narrow(CORBA.InterfaceDef).describe_interface().attributes:
            #we can use the default_value to get at an instance of the real enumeration
            if attr.name == "default_value":
                #ifr name of the enum
                self.enumIFRName = attr.type.id()
                break

        #determine the IDL type
        self.enumName = self.enumIFRName.split(':')[1].split('/')[1:]
        self.modName = self.enumName[0]

        #convert the list to a stringified Python package structure which can
        #be used with eval
        self.enumName = reduce((lambda x, y : str(x) + '.' + str(y)), self.enumName)

        #now comes the complicated part...importing the correct CORBA stub
        #without polluting the local namespace...
        self.tGlobals = {}
        self.tLocals  = {}
        exec "import " + self.modName in self.tGlobals, self.tLocals
        
        GenericProperty.__init__(self, name, charCompRef, devIORef)
        
        return
    #--------------------------------------------------------------------------
    def coerceToPropertyType(self, value=None):
        '''
        Overriden.
        '''
        #something went wrong. Return default value
        if value==None:
            #just use the first enum!
            return eval(self.enumName + "._items[0]", self.tGlobals, self.tLocals)
        
        try:
            #coerce into an enum type
            return eval(self.enumName + "._items[" + value + "]",
                        self.tGlobals,
                        self.tLocals) 
        except:
            #warn them about CDB access
            self.getLogger().logAlert("Unble to coerce '" + str(value) + "' into the correct type!")
            print_exc()
            #return an acceptable default value instead...an empty sequence
            return eval(self.enumName + "._items[0]", self.tGlobals, self.tLocals)
    #--------------------------------------------------------------------------
    def getMonitorObject(self, scheduler, timeoutID):
        '''
        Helper method.used to return a monitor of the correct type.
        '''
        return Monitorpattern(scheduler, timeoutID)
    #--------------------------------------------------------------------------
    def monitorRunner(self, cb, desc):
        '''
{'fraction_non_alphanumeric': 0.24580662158601915, 'fraction_numerical': 0.012997277597260033, 'mean_word_length': 1.4111793351683253, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549242', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2608, 'n_words': 971}