<commit_before>'''
Created on Mar 26, 2015

@author: ronaldjosephdesmarais
'''
import logging,yaml,json,requests
import keystoneclient.v2_0.client as ksclient

class glint_api(object):
    def __init__(self,log_name,log_lvl,glint_cfg):
        #setup logging for glint api
        self.log = logging.getLogger('glint_api')
        fh = logging.FileHandler(log_name)
        self.log.setLevel(log_lvl)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        self.log.addHandler(fh)
        
        #setup configuration to talk to glint
        cfg_f = yaml.load( open("api/%s"%glint_cfg,'r') )
        self.glint_url=cfg_f['glint_url']
        self.auth_url=cfg_f['auth_url']
        self.un=cfg_f['keystone_un']
        self.pw=cfg_f['keystone_pw']
        self.tenant_id=cfg_f['keystone_tenant_id']
        self.tenant_name=cfg_f['keystone_tenant_name']
        self.log.debug("Configuring glint api with %s:%s:%s:%s:%s"%(self.glint_url,self.auth_url,self.un,self.pw,self.tenant_id))
        
        #use keystone client un and pw to get an auth token 
        keystone = ksclient.Client(auth_url=self.auth_url, username=self.un, password=self.pw, tenant_id=self.tenant_id)
        self.token = keystone.auth_ref['token']['id']
        self.log.debug("Received token %s"%self.token)
        
    def getImages(self):
        self.log.debug("getImages  from %s"%( "%s/imagedistribution/"%self.glint_url))
        data_json = requests.post("%s/imagedistribution/"%self.glint_url,data={"USER_ID":self.un,"USER_TOKEN":"%s"%self.token,"USER_TENANT":self.tenant_name},cookies=None).text  
        data_obj = json.loads(data_json)
        return data_obj

    def imageCopy(self,image_name,src_site,dest_sites):
        self.log.debug("copy image %s from %s to %s"%(image_name,src_site,dest_sites))
        print "copy image %s from %s to %s"%(image_name,src_site,dest_sites)
        
        #getImages from glint and start copy process
        json_images=self.getImages()
        #find row object with image_name in it
        for row in json_images['rows']:
            #print row
            if row['image'] == image_name:
                print "found image to copy now make sure src site exists"
                for site in row['image']['sites']:
                    if site['name'] ==  src_site:
                        print "found source site to copy image from, now check for valid destination sites"
                        for dest_site in dest_sites:
                            if len(dest_sites) == 0:
                                print "Please Submit list of destination sites to copy image to"
                                return {"Result":"Error, no destinatoin sites"}
                            for avail_site in json_images['sites']:
                                fnd_site=False
                                if avail_site['name'] == src_site:
                                    return {"Result":"Dest site cannot be a source site"}
                                if avail_site['name'] == dest_site:
                                    fnd_site=True
                                if not fnd_site:
                                    print "Sorry destination site %s is not available please remove "%(avail_site)
                                    return {"Result":"Destination site not found %s"%(avail_site)}
                            print "All checks passed, prepare copy json"
                            
                            
                
            
        
        
    def save(self,jsonMsg, USER_TOKEN, USER_TENANT):
        return jsonMsg, USER_TOKEN, USER_TENANT

    def credentials(self,USER_TOKEN, USER_TENANT, USER_ID):
        return USER_TOKEN, USER_TENANT, USER_ID

    def listSites(self):
        self.log.debug("getImages  from %s"%( "%s/listsites/"%self.glint_url))<commit_msg>added copy image to api bug fix<commit_after>'''
Created on Mar 26, 2015

@author: ronaldjosephdesmarais
'''
import logging,yaml,json,requests
import keystoneclient.v2_0.client as ksclient

class glint_api(object):
    def __init__(self,log_name,log_lvl,glint_cfg):
        #setup logging for glint api
        self.log = logging.getLogger('glint_api')
        fh = logging.FileHandler(log_name)
        self.log.setLevel(log_lvl)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        fh.setFormatter(formatter)
        self.log.addHandler(fh)
        
        #setup configuration to talk to glint
        cfg_f = yaml.load( open("api/%s"%glint_cfg,'r') )
        self.glint_url=cfg_f['glint_url']
        self.auth_url=cfg_f['auth_url']
        self.un=cfg_f['keystone_un']
        self.pw=cfg_f['keystone_pw']
        self.tenant_id=cfg_f['keystone_tenant_id']
        self.tenant_name=cfg_f['keystone_tenant_name']
        self.log.debug("Configuring glint api with %s:%s:%s:%s:%s"%(self.glint_url,self.auth_url,self.un,self.pw,self.tenant_id))
        
        #use keystone client un and pw to get an auth token 
        keystone = ksclient.Client(auth_url=self.auth_url, username=self.un, password=self.pw, tenant_id=self.tenant_id)
        self.token = keystone.auth_ref['token']['id']
        self.log.debug("Received token %s"%self.token)
        
    def getImages(self):
        self.log.debug("getImages  from %s"%( "%s/imagedistribution/"%self.glint_url))
        data_json = requests.post("%s/imagedistribution/"%self.glint_url,data={"USER_ID":self.un,"USER_TOKEN":"%s"%self.token,"USER_TENANT":self.tenant_name},cookies=None).text  
        data_obj = json.loads(data_json)
        return data_obj

    def imageCopy(self,image_name,src_site,dest_sites):
        self.log.debug("copy image %s from %s to %s"%(image_name,src_site,dest_sites))
        print "copy image %s from %s to %s"%(image_name,src_site,dest_sites)
        
        #getImages from glint and start copy process
        json_images=self.getImages()
        #find row object with image_name in it
        for row in json_images['rows']:
            #print row
            if row['image'] == image_name:
                print "found image to copy now make sure src site exists"
                for site in row['sites']:
                    if site['name'] ==  src_site:
                        print "found source site to copy image from, now check for valid destination sites"
                        for dest_site in dest_sites:
                            if len(dest_sites) == 0:
                                print "Please Submit list of destination sites to copy image to"
                                return {"Result":"Error, no destinatoin sites"}
                            for avail_site in json_images['sites']:
                                fnd_site=False
                                if avail_site['name'] == src_site:
                                    return {"Result":"Dest site cannot be a source site"}
                                if avail_site['name'] == dest_site:
                                    fnd_site=True
                                if not fnd_site:
                                    print "Sorry destination site %s is not available please remove "%(avail_site)
                                    return {"Result":"Destination site not found %s"%(avail_site)}
                            print "All checks passed, prepare copy json"
                            
                            
                
            
        
        
    def save(self,jsonMsg, USER_TOKEN, USER_TENANT):
        return jsonMsg, USER_TOKEN, USER_TENANT

    def credentials(self,USER_TOKEN, USER_TENANT, USER_ID):
        return USER_TOKEN, USER_TENANT, USER_ID

    def listSites(self):
        self.log.debug("getImages  from %s"%( "%s/listsites/"%self.glint_url))
{'fraction_non_alphanumeric': 0.10499555103597305, 'fraction_numerical': 0.0022880386424304054, 'mean_word_length': 1.9814323607427056, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '571307', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2332, 'n_words': 513}