'''Utility functions.
Created on Sep 13, 2010

@author: jnaous
'''
import os
import re
from django.conf import settings
from expedient.common.federation.geni.util.urn_util import URN
from expedient.common.federation.geni.util.cert_util import create_cert
from expedient.common.federation.sfa.trust.gid import GID
from django.core.urlresolvers import reverse
from expedient.common.federation.geni.util import cred_util
import uuid

def get_user_cert_fname(user):
    """Get the filename of the user's GCF x509 certificate.
    
    @param user: The user whose certificate filename we want.
    @type user: L{django.contrib.auth.models.User}
    
    @return: The certificate absolute path
    @rtype: C{str}
    """
    
    return os.path.join(
        settings.GCF_X509_USER_CERT_DIR,
        settings.GCF_X509_USER_CERT_FNAME_PREFIX + user.username + ".crt"
    )
    
def get_user_key_fname(user):
    """Get the filename of the user's GCF x509 certificate key.
    
    @param user: The user whose certificate key filename we want.
    @type user: L{django.contrib.auth.models.User}
    
    @return: The certificate key's absolute path
    @rtype: C{str}
    """
    
    return os.path.join(
        settings.GCF_X509_KEY_DIR,
        settings.GCF_X509_USER_CERT_FNAME_PREFIX + user.username + ".key"
    )

def get_user_urn(username):
    """Get a user's URN
    
    @param username: The username of the user whose URN we want.
    @type username: C{str}
    
    @return: The URN
    @rtype: C{str}
    """
    
    return URN(
        str(settings.GCF_BASE_NAME), str("user"), str(username)
    ).urn_string()

def get_ch_urn():
    """Get the URN for Expedient as a clearinghouse.
    
    @return: The URN
    @rtype: C{str}
    """
    
    return URN(
        settings.GCF_BASE_NAME, "authority", "sa",
    ).urn_string()

def get_slice_urn(name):
    """Get the URN for a slice with name C{name}.
    
    @param name: Name of the slice. Must be unique.
    @type name: C{str}
    @return: a slice URN
    @rtype: C{str}