<commit_before>########################################################################
#
# University of Southampton IT Innovation Centre, 2011
#
# Copyright in this library belongs to the University of Southampton
# University Road, Highfield, Southampton, UK, SO17 1BJ
#
# This software may not be used, sold, licensed, transferred, copied
# or reproduced in whole or in part in any manner or form or in or
# on any media by any person other than in accordance with the terms
# of the Licence Agreement supplied with the software, or otherwise
# without the prior written consent of the copyright owners.
#
# This software is distributed WITHOUT ANY WARRANTY, without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE, except where stated in the Licence Agreement supplied with
# the software.
#
#	Created By :			<NAME>
#	Created Date :			2011-03-25
#	Created for Project :		PrestoPrime
#
########################################################################
import hashlib
import hashlib
import time
import uuid
import os
import pickle
import base64
import logging
from Crypto.Cipher import AES
import base64
import urllib
import random
import os
import sys
import logging

fmt = "%3.2f"

def get_class( kls ):
    parts = kls.split('.')
    module = ".".join(parts[:-1])
    m = __import__( module )
    for comp in parts[1:]:
        m = getattr(m, comp)
    return m

def get_methods_for_auth(auth):
    methodslist = [ pickle.loads(base64.b64decode(method_enc)) for method_enc in auth.roles.all().values_list('methods_encoded',flat=True) ]
    methods = [item for sublist in methodslist for item in sublist]
    return set(methods)

def get_methods_for_base(baseid):
    from dataservice.models import NamedBase
    base = NamedBase.objects.get(id=baseid)
    methods = []
    for auth in base.auth_set.all():
         ms = get_methods_for_auth(auth)
         methods += ms
    return set(methods)
  
def check_method_for_auth(authid,method):
    from dataservice.models import Auth
    from dataservice.models import NamedBase
    # Find the Authority with id
    auth = Auth.objects.get(id=authid)

    # Find the methods this auth has
    methods = get_methods_for_auth(auth)

    if auth.base is not None:
        # This auth is a direct sub auth of a base class
        basemethods = get_methods_for_base(auth.base.id)
        if method in methods and method in basemethods:
            logging.info("Authority Check OK for method '%s' in auth methods '%s' and basemethods '%s' " % (method,methods,basemethods)  )
            return True
        else:
            logging.info("Authority Check Failed for method '%s'  not in auth methods '%s' and not in base methods '%s'" % (method,methods,basemethods)  )
    else:
        # This auth is a sub authority of another auth
        authparent = auth.parent
        return check_method_for_auth(authparent.id,method) and method in methods

    logging.info("Authority Check Failed for method '%s'  not in auth methods '%s'" % (method,methods)  )
    return False

def mfile_upload_to(instance, filename):
    timeformat = time.strftime("%Y/%m/%d/")
    if instance.service.container.default_path != None and instance.service.container.default_path != "":
        return os.path.join(instance.service.container.default_path, timeformat , random_id() ,filename)
    return os.path.join(timeformat , random_id() ,filename)

def create_filename(instance, filename):
    timeformat = time.strftime("%Y/%m/%d/")
    return os.path.join(timeformat , random_id() ,filename)

def unique_id():
    return str(uuid.uuid4())

def random_id():
    mode = AES.MODE_CBC
    short_key1 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key2 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key3 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key4 = base64.b64encode(str(random.randint(0,sys.maxint)))

    aeskey = (short_key1 + short_key2 + short_key3 + short_key4)[:32]

    short_key5 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key6 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key7 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key8 = base64.b64encode(str(random.randint(0,sys.maxint)))
    key = (short_key5 + short_key6 + short_key7 + short_key8)[:32]

    aes = AES.new(aeskey, mode)<commit_msg>removed unused code<commit_after>########################################################################
#
# University of Southampton IT Innovation Centre, 2011
#
# Copyright in this library belongs to the University of Southampton
# University Road, Highfield, Southampton, UK, SO17 1BJ
#
# This software may not be used, sold, licensed, transferred, copied
# or reproduced in whole or in part in any manner or form or in or
# on any media by any person other than in accordance with the terms
# of the Licence Agreement supplied with the software, or otherwise
# without the prior written consent of the copyright owners.
#
# This software is distributed WITHOUT ANY WARRANTY, without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE, except where stated in the Licence Agreement supplied with
# the software.
#
#	Created By :			<NAME>
#	Created Date :			2011-03-25
#	Created for Project :		PrestoPrime
#
########################################################################
import hashlib
import hashlib
import time
import uuid
import os
import base64
import logging
from Crypto.Cipher import AES
import base64
import random
import os
import sys
import logging

fmt = "%3.2f"

def get_class( kls ):
    parts = kls.split('.')
    module = ".".join(parts[:-1])
    m = __import__( module )
    for comp in parts[1:]:
        m = getattr(m, comp)
    return m

def mfile_upload_to(instance, filename):
    timeformat = time.strftime("%Y/%m/%d/")
    if instance.service.container.default_path != None and instance.service.container.default_path != "":
        return os.path.join(instance.service.container.default_path, timeformat , random_id() ,filename)
    return os.path.join(timeformat , random_id() ,filename)

def create_filename(instance, filename):
    timeformat = time.strftime("%Y/%m/%d/")
    return os.path.join(timeformat , random_id() ,filename)

def unique_id():
    return str(uuid.uuid4())

def random_id():
    mode = AES.MODE_CBC
    short_key1 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key2 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key3 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key4 = base64.b64encode(str(random.randint(0,sys.maxint)))

    aeskey = (short_key1 + short_key2 + short_key3 + short_key4)[:32]

    short_key5 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key6 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key7 = base64.b64encode(str(random.randint(0,sys.maxint)))
    short_key8 = base64.b64encode(str(random.randint(0,sys.maxint)))
    key = (short_key5 + short_key6 + short_key7 + short_key8)[:32]

    aes = AES.new(aeskey, mode)
{'fraction_non_alphanumeric': 0.13919774806474314, 'fraction_numerical': 0.024208304011259677, 'mean_word_length': 4.2288447387785135, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '5973175', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2164, 'n_words': 636}