Changeset 188 for trunk/src


Ignore:
Timestamp:
05/13/09 15:37:11 (15 years ago)
Author:
bas
Message:

src/PBSQuery.py:

  • implemented a new datastructure, closes #16
  • default is old data structure
  • function new_data_structure() will activate new behaviour
  • we can now also specifiy 'Resource_List.walltime', just will get all 'Resource_List' parameters
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PBSQuery.py

    r184 r188  
    7777
    7878class PBSQuery:
     79
     80        # a[key] = value, key and value are data type string
     81        #
     82        OLD_DATA_STRUCTURE = True
     83
    7984        def __init__(self, server=None):
    8085                if not server:
     
    8287                else:
    8388                        self.server = server
    84 
    8589
    8690        def _connect(self):
     
    101105                i = 0
    102106                for attrib in list:
    103                         self.attribs[i].name = attrib
     107                        # So we can user Resource
     108                        attrib = attrib.split('.')
     109                        self.attribs[i].name = attrib[0]
    104110                        i = i + 1
    105111
     
    111117
    112118        def _list_2_dict(self, l, class_func):
    113                 """Convert a pbsstat function list to a class dictionary"""
     119                """
     120                Convert a pbsstat function list to a class dictionary, The
     121                data structure depends on the function new_data_structure().
     122               
     123                Default data structure is:
     124                        class[key] = value, Where key and value are of type string
     125
     126                Future release, can be set by new_data_structure():
     127                        - class[key] = value where value can be:
     128                          1. a list of values of type string
     129                          2. a dictionary with as list of values of type string. If
     130                             values contain a '=' character
     131
     132                          eg:
     133                            print node['np']
     134                                >> [ '2' ]
     135
     136                                print node['status']['arch']
     137                                >> [ 'x86_64' ]
     138                """
    114139                self.d = {}
    115140                for item in l:
    116141                        new = class_func()
     142
    117143                        self.d[item.name] = new
    118144                       
     145                        new.name = item.name
     146
    119147                        for a in item.attribs:
    120                                 new.name = item.name
     148
    121149                                if a.resource:
    122                                         new[a.name + '.' + a.resource] = a.value
     150                                        key = '%s.%s' %(a.name, a.resource)
    123151                                else:
    124                                         new[a.name] = a.value
     152                                        key = a.name
     153
     154                                if self.OLD_DATA_STRUCTURE:
     155                                        new[key] = a.value
     156                                else:
     157                                        values = string.split(a.value, ',')
     158                                       
     159                                        if len(values) == 1:
     160                                                # simple form
     161                                                # print 'simple %s =  %s' %(key, values[0])
     162                                                #
     163                                                new[key] = values
     164
     165                                        else:
     166                                                # list check
     167                                                list_or_dict = string.split(a.value, '=')
     168
     169
     170                                                if len(list_or_dict) == 1:
     171                                                        # This is a list
     172                                                        # print 'list %s = %s' %(key, values)
     173                                                        #
     174                                                        new[key] = values
     175
     176                                                else:
     177                                                        # This is dictionary
     178                                                        # print 'dict %s = %s' %(key, values)
     179                                                        #
     180                                                        new[key] = dict()
     181                                                        for v in values:
     182                                                                a,b = v.split('=')
     183                                                                new[key][a] = [ b ]
     184                                               
    125185                self._free(l)
    126186               
     
    128188                """
    129189                freeing up used memmory
     190
    130191                """
    131192                pbs.pbs_statfree(memory)
     
    225286                return self.server
    226287
     288        def new_data_structure(self):
     289                """
     290                Use new data structure, will be standard in future release
     291                """
     292                self.OLD_DATA_STRUCTURE = False
     293
    227294class _PBSobject(UserDict.UserDict):
    228295        TRUE  = 1
     
    238305                else:
    239306                        return None
     307
     308        def __repr__(self):
     309                return repr(self.data)
     310
     311        def __str__(self):
     312                return str(self.data)
     313
     314        def __getattr__(self, name):
     315                try:
     316                        return self.data[name]
     317                except KeyError:
     318                        error = 'invalid attribute %s' %(name)
     319                        raise PBSError(error)
    240320
    241321        def uniq(self, list):
     
    295375
    296376
    297 
    298 
    299 
    300377class queue(_PBSobject):
    301378        """PBS queue class"""
Note: See TracChangeset for help on using the changeset viewer.