utils.py 3.12 KB
Newer Older
1 2 3
"""
Common utils.
"""
muhammad-ammar committed
4
import glob
5
import os
muhammad-ammar committed
6
import shutil
7
import urllib
muhammad-ammar committed
8
import urlparse
muhammad-ammar committed
9
import yaml
10 11 12
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey

13
CONFIG_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14
DEFAULT_CONFIG_FILE_NAME = 'instance_config.yaml'
15
STATIC_CONFIG_FILE_PATH = os.path.join(CONFIG_ROOT_DIR, 'static_config.yaml')
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


def extract_course_org(course_id):
    """
    Extract video organization from course url.
    """
    org = None

    try:
        org = CourseKey.from_string(course_id).org
    except InvalidKeyError:
        pass

    return org


def build_url(*urls, **query_params):
    """
    Build a url from specified params.

    Arguments:
        base_url (str): base url
        relative_url (str): endpoint
        query_params (dict): query params

    Returns:
        absolute url
    """
muhammad-ammar committed
44
    url = '/'.join(item.strip('/') for item in urls if item)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    if query_params:
        url = '{}?{}'.format(url, urllib.urlencode(query_params))

    return url


def get_config(yaml_config_file=DEFAULT_CONFIG_FILE_NAME):
    """
    Read yaml config file.

    Arguments:
        yaml_config_file (str): yaml config file name

    Returns:
        dict: yaml config
    """
    config_dict = {}

    try:
        yaml_config_file = os.environ['VIDEO_PIPELINE_CFG']
    except KeyError:
        yaml_config_file = os.path.join(
67
            CONFIG_ROOT_DIR,
68 69 70 71 72 73
            yaml_config_file
        )

    with open(yaml_config_file, 'r') as config:
        config_dict = yaml.load(config)

74 75 76 77 78
    # read static config file
    with open(STATIC_CONFIG_FILE_PATH, 'r') as config:
        static_config_dict = yaml.load(config)

    return dict(config_dict, **static_config_dict)
muhammad-ammar committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94


def scrub_query_params(url, params_to_scrub):
    """
    Scrub query params present in `params_to_scrub` from `url`

    Arguments:
        url (str): url
        params_to_scrub (list): name of query params to be scrubbed from url

    Returns:
        url with query params scrubbed

    >>> old_url = https://sandbox.veda.com/api/do?api_token=veda_api_key&job_name=12345&language=en&v=1
    >>> new_url = https://sandbox.veda.com/api/do?v=1&job_name=12345&language=en&api_token=XXXXXXXXXXXX
    """
95
    parsed = urlparse.urlparse(url)
muhammad-ammar committed
96 97 98 99 100 101 102 103 104 105 106 107 108

    # query_params will be in the form of [('v', '1'), ('job_name', '12345')]
    query_params = urlparse.parse_qsl(parsed.query)

    new_query_params = {}
    for key, value in query_params:
        new_query_params[key] = len(value) * 'X' if key in params_to_scrub else value

    return build_url(
        '{scheme}://{netloc}'.format(scheme=parsed.scheme, netloc=parsed.netloc),
        parsed.path,
        **new_query_params
    )
muhammad-ammar committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126


def delete_directory_contents(path):
    """
    Deletes everything inside a directory. Do nothing if path is not a directory.

    Arguments:
        path (str): path to a directory.
    """
    if not os.path.isdir(path):
        return

    for file_path in glob.glob('{path}/*'.format(path=path.rstrip('/'))):
        if os.path.isdir(file_path):
            shutil.rmtree(file_path)

        if os.path.isfile(file_path):
            os.remove(file_path)