<commit_before>import time
import datetime
#
from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
    RecordNotFound, SfaNotImplemented, SliverDoesNotExist
from sfa.util.sfalogging import logger
from sfa.util.defaultdict import defaultdict
from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf, urn_to_sliver_id
from sfa.util.cache import Cache
# used to be used in get_ticket
#from sfa.trust.sfaticket import SfaTicket
from sfa.rspecs.version_manager import VersionManager
from sfa.rspecs.rspec import RSpec
# the driver interface, mostly provides default behaviours
from sfa.managers.driver import Driver
from sfa.openstack.nova_shell import NovaShell
from sfa.openstack.osaggregate import OSAggregate
from sfa.plc.plslices import PlSlices
from sfa.util.osxrn import OSXrn


def list_to_dict(recs, key):
    """
    convert a list of dictionaries into a dictionary keyed on the 
    specified dictionary key 
    """
    return dict ( [ (rec[key],rec) for rec in recs ] )

#
# PlShell is just an xmlrpc serverproxy where methods
# can be sent as-is; it takes care of authentication
# from the global config
# 
class NovaDriver (Driver):

    # the cache instance is a class member so it survives across incoming requests
    cache = None

    def __init__ (self, config):
        Driver.__init__ (self, config)
        self.shell = NovaShell (config)
        self.cache=None
        if config.SFA_AGGREGATE_CACHING:
            if NovaDriver.cache is None:
                NovaDriver.cache = Cache()
            self.cache = NovaDriver.cache
 
    ########################################
    ########## registry oriented
    ########################################

    ########## disabled users 
    def is_enabled (self, record):
        # all records are enabled
        return True

    def augment_records_with_testbed_info (self, sfa_records):
        return self.fill_record_info (sfa_records)

    ########## 
    def register (self, sfa_record, hrn, pub_key):
        type = sfa_record['type']
        pl_record = self.sfa_fields_to_pl_fields(type, hrn, sfa_record)

        if type == 'slice':
            acceptable_fields=['url', 'instantiation', 'name', 'description']
            # add slice description, name, researchers, PI 
            pass

        elif type == 'user':
            # add person roles, projects and keys
            pass
        return pointer
        
    ##########
    # xxx actually old_sfa_record comes filled with plc stuff as well in the original code
    def update (self, old_sfa_record, new_sfa_record, hrn, new_key):
        pointer = old_sfa_record['pointer']
        type = old_sfa_record['type']

        # new_key implemented for users only
        if new_key and type not in [ 'user' ]:
            raise UnknownSfaType(type)

        elif type == "slice":
            # can update description, researchers and PI
            pass 
        elif type == "user":
            # can update  slices, keys and roles
            pass
        return True
        

    ##########
    def remove (self, sfa_record):
        type=sfa_record['type']
        name = Xrn(sfa_record['hrn']).get_leaf()     
        if type == 'user':
            if self.shell.user_get(name):
                self.shell.user_delete(name)
        elif type == 'slice':
            if self.shell.project_get(name):
                self.shell.project_delete(name)
        return True


    ####################
    def fill_record_info(self, records):
        """
        Given a (list of) SFA record, fill in the PLC specific 
        and SFA specific fields in the record. 
        """
        if not isinstance(records, list):
            records = [records]

        for record in records:
            name = Xrn(record['hrn']).get_leaf()
            os_record = None
            if record['type'] == 'user':
                os_record = self.shell.auth_manager.user_get(name)
                record['slices'] = [self.hrn + "." + proj.name for \
                                    proj in os_record.projects]
                record['roles'] = [role for role in os_record.roles]
                keys = self.shell.key_pair_get_all_by_user(name)
                record['keys'] = [key.public_key for key in keys]     
            elif record['type'] == 'slice': 
                os_record = self.shell.auth_manager.project_get(name)
                record['description'] = os_record.description
                record['PI'] = self.hrn + "." + os_record.project_manager
                record['geni_creator'] = record['PI'] 
                record['researcher'] = [self.hrn + "." + user.name for \
                                         user in os_record.members]
            else:
                continue
            record['geni_urn'] = hrn_to_urn(record['hrn'], record['type'])
            record['geni_certificate'] = record['gid'] 
            record['name'] = os_record.name
            if os_record.created_at is not None:    
                record['date_created'] = datetime_to_string(utcparse(os_record.created_at))
            if os_record.updated_at is not None:
                record['last_updated'] = datetime_to_string(utcparse(os_record.updated_at))
 
        return records<commit_msg>fix AttributeError<commit_after>import time
import datetime
#
from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
    RecordNotFound, SfaNotImplemented, SliverDoesNotExist
from sfa.util.sfalogging import logger
from sfa.util.defaultdict import defaultdict
from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf, urn_to_sliver_id
from sfa.util.cache import Cache
# used to be used in get_ticket
#from sfa.trust.sfaticket import SfaTicket
from sfa.rspecs.version_manager import VersionManager
from sfa.rspecs.rspec import RSpec
# the driver interface, mostly provides default behaviours
from sfa.managers.driver import Driver
from sfa.openstack.nova_shell import NovaShell
from sfa.openstack.osaggregate import OSAggregate
from sfa.plc.plslices import PlSlices
from sfa.util.osxrn import OSXrn


def list_to_dict(recs, key):
    """
    convert a list of dictionaries into a dictionary keyed on the 
    specified dictionary key 
    """
    return dict ( [ (rec[key],rec) for rec in recs ] )

#
# PlShell is just an xmlrpc serverproxy where methods
# can be sent as-is; it takes care of authentication
# from the global config
# 
class NovaDriver (Driver):

    # the cache instance is a class member so it survives across incoming requests
    cache = None

    def __init__ (self, config):
        Driver.__init__ (self, config)
        self.shell = NovaShell (config)
        self.cache=None
        if config.SFA_AGGREGATE_CACHING:
            if NovaDriver.cache is None:
                NovaDriver.cache = Cache()
            self.cache = NovaDriver.cache
 
    ########################################
    ########## registry oriented
    ########################################

    ########## disabled users 
    def is_enabled (self, record):
        # all records are enabled
        return True

    def augment_records_with_testbed_info (self, sfa_records):
        return self.fill_record_info (sfa_records)

    ########## 
    def register (self, sfa_record, hrn, pub_key):
        type = sfa_record['type']
        pl_record = self.sfa_fields_to_pl_fields(type, hrn, sfa_record)

        if type == 'slice':
            acceptable_fields=['url', 'instantiation', 'name', 'description']
            # add slice description, name, researchers, PI 
            pass

        elif type == 'user':
            # add person roles, projects and keys
            pass
        return pointer
        
    ##########
    # xxx actually old_sfa_record comes filled with plc stuff as well in the original code
    def update (self, old_sfa_record, new_sfa_record, hrn, new_key):
        pointer = old_sfa_record['pointer']
        type = old_sfa_record['type']

        # new_key implemented for users only
        if new_key and type not in [ 'user' ]:
            raise UnknownSfaType(type)

        elif type == "slice":
            # can update description, researchers and PI
            pass 
        elif type == "user":
            # can update  slices, keys and roles
            pass
        return True
        

    ##########
    def remove (self, sfa_record):
        type=sfa_record['type']
        name = Xrn(sfa_record['hrn']).get_leaf()     
        if type == 'user':
            if self.shell.user_get(name):
                self.shell.user_delete(name)
        elif type == 'slice':
            if self.shell.project_get(name):
                self.shell.project_delete(name)
        return True


    ####################
    def fill_record_info(self, records):
        """
        Given a (list of) SFA record, fill in the PLC specific 
        and SFA specific fields in the record. 
        """
        if not isinstance(records, list):
            records = [records]

        for record in records:
            name = Xrn(record['hrn']).get_leaf()
            os_record = None
            if record['type'] == 'user':
                os_record = self.shell.auth_manager.get_user(name)
                record['slices'] = [self.hrn + "." + proj.name for \
                                    proj in os_record.projects]
                record['roles'] = [role for role in os_record.roles]
                keys = self.shell.key_pair_get_all_by_user(name)
                record['keys'] = [key.public_key for key in keys]     
            elif record['type'] == 'slice': 
                os_record = self.shell.auth_manager.get_project(name)
                record['description'] = os_record.description
                record['PI'] = self.hrn + "." + os_record.project_manager
                record['geni_creator'] = record['PI'] 
                record['researcher'] = [self.hrn + "." + user.name for \
                                         user in os_record.members]
            else:
                continue
            record['geni_urn'] = hrn_to_urn(record['hrn'], record['type'])
            record['geni_certificate'] = record['gid'] 
            record['name'] = os_record.name
            if os_record.created_at is not None:    
                record['date_created'] = datetime_to_string(utcparse(os_record.created_at))
            if os_record.updated_at is not None:
                record['last_updated'] = datetime_to_string(utcparse(os_record.updated_at))
 
        return records
{'fraction_non_alphanumeric': 0.11239762778876024, 'fraction_numerical': 0.0, 'mean_word_length': 2.2891640866873066, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410883', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2962, 'n_words': 824}