<commit_before>from numba import types
from numba import extending
from numba import cgutils

def _unsafe_cast_ptr_to_class(int_type, class_type):
    inst_typ = class_type.instance_type
    sig = inst_typ(types.voidptr, class_type)

    def codegen(context, builder, signature, args):
        ptr, _ = args
        alloc_type = context.get_data_type(inst_typ.get_data_type())

        inst_struct = context.make_helper(builder, inst_typ)

        # Set meminfo to NULL
        inst_struct.meminfo = cgutils.get_null_value(inst_struct.meminfo.type)

        # Set data from the given pointer
        inst_struct.data = builder.bitcast(ptr, alloc_type.as_pointer())
        return inst_struct._getvalue()

    return sig, codegen

@extending.intrinsic
def unsafe_cast(typingctx, src, dst):
    """Cast a voidptr to a jitclass
    """
    if isinstance(src, types.RawPointer) and isinstance(dst, types.ClassType):
        return _unsafe_cast_ptr_to_class(src, dst)
    raise TypeError(
        'Unable to cast pointer type {} to class type {}'.format(src, dst)
    )

@extending.intrinsic
def sizeof(typingctx, src):
    sig = types.int64(src)

    def codegen(context, builder, signature, args):
        return context.get_constant(
            sig.return_type,
            context.get_abi_sizeof(context.get_data_type(src.instance_type))
        )
    if isinstance(src, types.ClassType):
        return sig, codegen
    raise TypeError()

@extending.intrinsic
def not_null(typingctx, src):
    sig = types.boolean(src)

    def codegen(context, builder, signature, args):
        instance, = args

        # TODO: probably a more general way to do this
        second_element = builder.extract_value(instance, [1])
        result = cgutils.is_not_null(builder, second_element)
        return result

    if isinstance(src, types.ClassInstanceType):
        return sig, codegen

    raise TypeError()
<commit_msg>Fix because of 7f2c108eb0ee8bcb12693a4d627b6d30add2f23f<commit_after>from numba import types
from numba import extending
from numba import cgutils

def _unsafe_cast_ptr_to_class(int_type, class_type):
    inst_typ = class_type.instance_type
    sig = inst_typ(types.voidptr, class_type)

    def codegen(context, builder, signature, args):
        ptr, _ = args
        alloc_type = context.get_data_type(inst_typ.get_data_type())

        inst_struct = context.make_helper(builder, inst_typ)

        # Set meminfo to NULL
        inst_struct.meminfo = cgutils.get_null_value(inst_struct.meminfo.type)

        # Set data from the given pointer
        inst_struct.data = builder.bitcast(ptr, alloc_type.as_pointer())
        return inst_struct._getvalue()

    return sig, codegen

@extending.intrinsic
def unsafe_cast(typingctx, src, dst):
    """Cast a voidptr to a jitclass
    """
    if isinstance(src, (types.RawPointer, types.Integer)) and isinstance(
        dst, types.ClassType
    ):
        return _unsafe_cast_ptr_to_class(src, dst)
    raise TypeError(
        'Unable to cast pointer type {} to class type {}'.format(src, dst)
    )

@extending.intrinsic
def sizeof(typingctx, src):
    sig = types.int64(src)

    def codegen(context, builder, signature, args):
        return context.get_constant(
            sig.return_type,
            context.get_abi_sizeof(context.get_data_type(src.instance_type))
        )
    if isinstance(src, types.ClassType):
        return sig, codegen
    raise TypeError()

@extending.intrinsic
def not_null(typingctx, src):
    sig = types.boolean(src)

    def codegen(context, builder, signature, args):
        instance, = args

        # TODO: probably a more general way to do this
        second_element = builder.extract_value(instance, [1])
        result = cgutils.is_not_null(builder, second_element)
        return result

    if isinstance(src, types.ClassInstanceType):
        return sig, codegen

    raise TypeError()
<|endoftext|>


<commit_before>"""smartcard.guid

Utility functions to handle GUIDs as strings or list of bytes

__author__ = "http://www.gemalto.com"

Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com

This file is part of pyscard.

pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

pyscard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
from struct import unpack

# guid is ulong+ushort+ushort+uchar[8]; we need a map because bytes
# are swappted for the first three
map = {0: 3, 1: 2, 2: 1, 3: 0, 4: 5, 5: 4, 6: 7, 7: 6, 8: 8, 9: 9,
    10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15}

def  strToGUID(s):
    """Converts a GUID string into a list of bytes.

    >>> strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}')
    [103, 22, 79, 173, 117, 234, 36, 65, 132, 212, 100, 27, 59, 25, 124, 101]
    """
    l = []
    for i in unpack('x' + '2s' * 4 + 'x' + '2s2sx' * 3 + '2s' * 6 + 'x', s):
        l += [int(i, 16)]
    zr = []
    for i in xrange(len(l)):
        zr.append(l[map[i]])
    return zr

def GUIDToStr(g):
    """Converts a GUID sequence of bytes into a string.

    >>> GUIDToStr([103,22,79,173,  117,234,  36,65,
    ...            132, 212, 100, 27, 59, 25, 124, 101])
    '{AD4F1667-EA75-4124-84D4-641B3B197C65}'
    """
    zr = []
    for i in xrange(len(g)):
        zr.append(g[map[i]])
    return "{%2X%2X%2X%2X-%2X%2X-%2X%2X-%2X%2X-%2X%2X%2X%2X%2X%2X}" % tuple(zr)

if __name__ == "__main__":
    """Small sample illustrating the use of guid.py."""
    import smartcard.guid
    dummycardguid1 = strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}')
    print(dummycardguid1)
    print(GUIDToStr(dummycardguid1))

<commit_msg>Python3: use range() instead of xrange()<commit_after>"""smartcard.guid

Utility functions to handle GUIDs as strings or list of bytes

__author__ = "http://www.gemalto.com"

Copyright 2001-2012 gemalto
Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com

This file is part of pyscard.

pyscard is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

pyscard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with pyscard; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
from struct import unpack

# guid is ulong+ushort+ushort+uchar[8]; we need a map because bytes
# are swappted for the first three
map = {0: 3, 1: 2, 2: 1, 3: 0, 4: 5, 5: 4, 6: 7, 7: 6, 8: 8, 9: 9,
    10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15}

def  strToGUID(s):
    """Converts a GUID string into a list of bytes.

    >>> strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}')
    [103, 22, 79, 173, 117, 234, 36, 65, 132, 212, 100, 27, 59, 25, 124, 101]
    """
    l = []
    for i in unpack('x' + '2s' * 4 + 'x' + '2s2sx' * 3 + '2s' * 6 + 'x', s):
        l += [int(i, 16)]
    zr = []
    for i in range(len(l)):
        zr.append(l[map[i]])
    return zr

def GUIDToStr(g):
    """Converts a GUID sequence of bytes into a string.

    >>> GUIDToStr([103,22,79,173,  117,234,  36,65,
    ...            132, 212, 100, 27, 59, 25, 124, 101])
    '{AD4F1667-EA75-4124-84D4-641B3B197C65}'
    """
    zr = []
    for i in range(len(g)):
        zr.append(g[map[i]])
    return "{%2X%2X%2X%2X-%2X%2X-%2X%2X-%2X%2X-%2X%2X%2X%2X%2X%2X}" % tuple(zr)

if __name__ == "__main__":
    """Small sample illustrating the use of guid.py."""
    import smartcard.guid
    dummycardguid1 = strToGUID('{AD4F1667-EA75-4124-84D4-641B3B197C65}')
    print(dummycardguid1)
    print(GUIDToStr(dummycardguid1))

<|endoftext|>


<commit_before>from flask import Blueprint, jsonify
from flask import json
from flask import make_response
from flask import request
from flask_restful import Resource
from pymessenger.bot import Bot

from Norman.extensions import csrf_protect
from Norman.settings import DevConfig
from Norman.utils import response
from Norman.api.web import UserAPI

bot = Bot(DevConfig.FACEBOOK_SECRET_KEY)
blueprint = Blueprint('api', __name__, url_prefix='/api')

@blueprint.route('/', methods=['GET', 'POST'])
@csrf_protect.exempt
def helloworld():
    view_class = HelloWorld()
    if request.method == "GET":
        return view_class.get()
    else:
        return view_class.post()

class HelloWorld(Resource):
    def get(self):
        return jsonify({'hello': 'world'})

    def post(self):
        return jsonify({'method': 'POST'})

@blueprint.route('/webhook', methods=['GET', 'POST'])
@csrf_protect.exempt
def webhook():
    view_class = WebHook()
    if request.method == "GET":
        return view_class.get()
    else:
        return view_class.post()

class WebHook(Resource):
    def __init__(self):
        self.user_view = UserAPI()
    @staticmethod
    def get():
        args = request.args
        verify_token = 'python_rocks'
        if args.get('hub.mode') == 'subscribe' and args.get('hub.verify_token') == verify_token:
            return make_response(args.get('hub.challenge').strip("\n\""))
        else:
            return response.response_error('Failed validation. Make sure the validation tokens match', args)

    def post(self):
        data = request.get_json()
        for event in data['entry']:
            messaging = event['messaging']
            for action in messaging:
                if action.get('message'):
                    recipient_id = action['sender']['id']

                    if not self.user_view.validate_user(recipient_id):
                        message = "Hello, {0}".format(recipient_id)
                        return bot.send_text_message(recipient_id, message)
                return make_response(json.dumps({'success': True}), 200,
                                     {'ContentType': 'application/json'})

<commit_msg>Fixed Importss<commit_after>from flask import Blueprint, jsonify
from flask import json
from flask import make_response
from flask import request
from flask_restful import Resource
from pymessenger.bot import Bot

from Norman.extensions import csrf_protect
from Norman.settings import DevConfig
from Norman.utils import response
from Norman.api.web import UserAPI

bot = Bot(DevConfig.FACEBOOK_SECRET_KEY)
blueprint = Blueprint('api', __name__, url_prefix='/api')

@blueprint.route('/', methods=['GET', 'POST'])
@csrf_protect.exempt
def helloworld():
    view_class = HelloWorld()
    if request.method == "GET":
        return view_class.get()
    else:
        return view_class.post()

class HelloWorld(Resource):
    def get(self):
        return jsonify({'hello': 'world'})

    def post(self):
        return jsonify({'method': 'POST'})

@blueprint.route('/webhook', methods=['GET', 'POST'])
@csrf_protect.exempt
def webhook():
    view_class = WebHook()
    if request.method == "GET":
        return view_class.get()
    else:
        return view_class.post()

class WebHook(Resource):
    def __init__(self):
        self.user_view = UserAPI()
    @staticmethod
    def get():
        args = request.args
        verify_token = ''
        if args.get('hub.mode') == 'subscribe' and args.get('hub.verify_token') == verify_token:
            return make_response(args.get('hub.challenge').strip("\n\""))
        else:
            return response.response_error('Failed validation. Make sure the validation tokens match', args)

    def post(self):
        data = request.get_json()
        for event in data['entry']:
            messaging = event['messaging']
            for action in messaging:
                if action.get('message'):
                    recipient_id = action['sender']['id']

                    if not self.user_view.validate_user(recipient_id):
                        message = "Hello, {0}".format(recipient_id)
                        return bot.send_text_message(recipient_id, message)
                return make_response(json.dumps({'success': True}), 200,
                                     {'ContentType': 'application/json'})

<|endoftext|>


<commit_before>import logging
import os
import re

import bleach
import markdown as markdown_library

from django import template
from django.apps import apps
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.loader_tags import do_include
from django.utils.safestring import mark_safe

logger = logging.getLogger(__name__)

register = template.Library()

@register.filter(name='source_to_name')
def source_to_name(source):
    """
    Given 'go_viral', return 'GoViral'
    """
    try:
        return apps.get_app_config(source).verbose_name
    except:  # pylint: disable=bare-except
        return source

@register.filter(name='zip')
def zip_lists(a, b):
    """
    zip() the two arguments.
    """
    return zip(a, b)

class TryIncludeNode(template.Node):
    """
    A Node that instantiates an IncludeNode but wraps its render() in a
    try/except in case the template doesn't exist.
    """
    def __init__(self, parser, token):
        self.include_node = do_include(parser, token)

    def render(self, context):
        try:
            return self.include_node.render(context)
        except template.TemplateDoesNotExist:
            return ''

@register.tag
def try_include(parser, token):
    """
    Include the specified template but only if it exists.
    """
    return TryIncludeNode(parser, token)

@register.filter()
def markdown(value):
    """
    Translate markdown to a safe subset of HTML.
    """
    cleaned = bleach.clean(markdown_library.markdown(value),
                           tags=bleach.ALLOWED_TAGS +
                           ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])

    linkified = bleach.linkify(cleaned)

    return mark_safe(linkified)

@register.simple_tag(takes_context=True)
def next_page(context):
    """
    Get the next page for signup or login.

    The query string takes priority over the template variable and the default
    is an empty string.
    """
    if 'next' in context.request.GET:
        return context.request.GET['next']

    if 'next' in context.request.POST:
        return context.request.POST['next']

    if 'next' in context:
        return context['next']

    return ''

def slugify_url(url):
    """
    Turn '/study/connect_me/' into 'study-connect-me'.
    """
    return (url
            .lower()
            .strip('/')
            .replace(':', '-')
            .replace('/', '-')
            .replace('_', '-'))

def script_if_exists(slug):
    """
    Return a script tag if the given slug path exists.
    """
    fs_path = os.path.join(settings.BASE_DIR,
                           'build/js/bundle-{}.js'.format(slug))

    if os.path.exists(fs_path):
        return '<script src="{}js/bundle-{}.js"></script>'.format(
            settings.STATIC_URL, slug)

@register.simple_tag(takes_context=True)
def page_bundle(context):
    """
    Get the bundle path for a given page, first trying the view name and then
    the URL slug.
    """
    # for example, /study/connect/abc/ has a view_name of studies:connect
    name = slugify_url(context.request.resolver_match.view_name)
    script = script_if_exists(name)

    if script:
        return script

    path = slugify_url(context.request.path)
    script = script_if_exists(path)

    if script:
        return script

    if settings.DEBUG:
        return mark_safe('<!-- DEBUG: not found: "{}", "{}" -->'
                         .format(name, path))

    return ''

@register.simple_tag(takes_context=True)
def page_body_id(context):
    """
    Get the CSS class for a given page.
    """
    path = slugify_url(context.request.path)

    if not path:
        path = 'home'

    page_body_id_tag = 'page-' + path

    return page_body_id_tag

@register.simple_tag(takes_context=True)
def active(context, pattern_or_urlname):
    """
    Return 'active' if the given URL or pattern is active.
    """
    try:
        pattern = '^' + reverse(pattern_or_urlname)
    except NoReverseMatch:
        pattern = pattern_or_urlname

    path = context.request.path

    if re.search(pattern, path):
        return 'active'

    return ''

@register.filter
def join_and(value):
    """
    Given a list of strings, format them with commas and spaces, but
    with 'and' at the end.

    >>> join_and(['apples', 'oranges', 'pears'])
    "apples, oranges, and pears"
    """
    # convert numbers to strings
    value = [str(item) for item in value]

    if len(value) == 1:
        return value[0]

    if len(value) == 2:
        return '{} and {}'.format(value[0], value[1])

    # join all but the last element
    all_but_last = ', '.join(value[:-1])
    return '{}, and {}'.format(all_but_last, value[-1])
<commit_msg>mark template tag output as safe<commit_after>import logging
import os
import re

import bleach
import markdown as markdown_library

from django import template
from django.apps import apps
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template.loader_tags import do_include
from django.utils.safestring import mark_safe

logger = logging.getLogger(__name__)

register = template.Library()

@register.filter(name='source_to_name')
def source_to_name(source):
    """
    Given 'go_viral', return 'GoViral'
    """
    try:
        return apps.get_app_config(source).verbose_name
    except:  # pylint: disable=bare-except
        return source

@register.filter(name='zip')
def zip_lists(a, b):
    """
    zip() the two arguments.
    """
    return zip(a, b)

class TryIncludeNode(template.Node):
    """
    A Node that instantiates an IncludeNode but wraps its render() in a
    try/except in case the template doesn't exist.
    """
    def __init__(self, parser, token):
        self.include_node = do_include(parser, token)

    def render(self, context):
        try:
            return self.include_node.render(context)
        except template.TemplateDoesNotExist:
            return ''

@register.tag
def try_include(parser, token):
    """
    Include the specified template but only if it exists.
    """
    return TryIncludeNode(parser, token)

@register.filter()
def markdown(value):
    """
    Translate markdown to a safe subset of HTML.
    """
    cleaned = bleach.clean(markdown_library.markdown(value),
                           tags=bleach.ALLOWED_TAGS +
                           ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])

    linkified = bleach.linkify(cleaned)

    return mark_safe(linkified)

@register.simple_tag(takes_context=True)
def next_page(context):
    """
    Get the next page for signup or login.

    The query string takes priority over the template variable and the default
    is an empty string.
    """
    if 'next' in context.request.GET:
        return context.request.GET['next']

    if 'next' in context.request.POST:
        return context.request.POST['next']

    if 'next' in context:
        return context['next']

    return ''

def slugify_url(url):
    """
    Turn '/study/connect_me/' into 'study-connect-me'.
    """
    return (url
            .lower()
            .strip('/')
            .replace(':', '-')
            .replace('/', '-')
            .replace('_', '-'))

def script_if_exists(slug):
    """
    Return a script tag if the given slug path exists.
    """
    fs_path = os.path.join(settings.BASE_DIR,
                           'build/js/bundle-{}.js'.format(slug))

    if os.path.exists(fs_path):
        return '<script src="{}js/bundle-{}.js"></script>'.format(
            settings.STATIC_URL, slug)

@register.simple_tag(takes_context=True)
def page_bundle(context):
    """
    Get the bundle path for a given page, first trying the view name and then
    the URL slug.
    """
    # for example, /study/connect/abc/ has a view_name of studies:connect
    name = slugify_url(context.request.resolver_match.view_name)
    script = script_if_exists(name)

    if script:
        return script

    path = slugify_url(context.request.path)
    script = script_if_exists(path)

    if script:
        return mark_safe(script)

    if settings.DEBUG:
        return mark_safe('<!-- DEBUG: not found: "{}", "{}" -->'
                         .format(name, path))

    return ''

@register.simple_tag(takes_context=True)
def page_body_id(context):
    """
    Get the CSS class for a given page.
    """
    path = slugify_url(context.request.path)

    if not path:
        path = 'home'

    page_body_id_tag = 'page-' + path

    return page_body_id_tag

@register.simple_tag(takes_context=True)
def active(context, pattern_or_urlname):
    """
    Return 'active' if the given URL or pattern is active.
    """
    try:
        pattern = '^' + reverse(pattern_or_urlname)
    except NoReverseMatch:
        pattern = pattern_or_urlname

    path = context.request.path

    if re.search(pattern, path):
        return 'active'

    return ''

@register.filter
def join_and(value):
    """
    Given a list of strings, format them with commas and spaces, but
    with 'and' at the end.

    >>> join_and(['apples', 'oranges', 'pears'])
    "apples, oranges, and pears"
    """
    # convert numbers to strings
    value = [str(item) for item in value]

    if len(value) == 1:
        return value[0]

    if len(value) == 2:
        return '{} and {}'.format(value[0], value[1])

    # join all but the last element
    all_but_last = ', '.join(value[:-1])
    return '{}, and {}'.format(all_but_last, value[-1])
<|endoftext|>


<commit_before>"""CVR miner.

JSON file structure
-------------------
_id : ID of some form
_index :
_source : Actual data
_type: 'virksomhed', 'produktionsenhed', 'deltager' or 'meta'
fields

The 'meta' type appear only once and with the entry:
    {"_index":"cvr-permanent-prod-20151209",
     "_type":"meta",
     "_id":"1",
     "_score":1,
     "_source":{
       "NewestRetrievedFileTimestampForBeskaeftigelse":
         "2016-05-07T08:59:23.373+02:00"}}

Virksomheder
------------
aarsbeskaeftigelse

"""

import json

from pprint import pprint

JSONL_FILENAME = 'cvr-permanent.json'
# $ wc cvr-permanent.json
#    4721004   127333568 42796650397 cvr-permanent.json

def print_types(filename=JSONL_FILENAME):
    """Print entry types from JSON file.

    Iterate over entire file and print distinct entry types.

    This should produce:

    virksomhed
    produktionsenhed
    deltager
    meta

    Parameters
    ----------
    filename : str
        Filename for JSONL file

    """
    types = set()
    for line in open(filename):
        obj = json.loads(line)
        type_ = obj['_type']
        if type_ not in types:
            print(type_)
            types.add(type_)

def print_source_fields(filename=JSONL_FILENAME):
    """Print field values from _source fields in JSON file.

    This could produce:

    (u'Vrvirksomhed',)
    (u'VrproduktionsEnhed',)
    (u'Vrdeltagerperson',)
    (u'NewestRetrievedFileTimestampForBeskaeftigelse',)

    """
    fields_set = set()
    for line in open(filename):
        obj = json.loads(line)
        fields = tuple(obj['_source'].keys())
        if fields not in fields_set:
            print(fields)
            fields_set.add(fields)

def pretty_print(filename=JSONL_FILENAME):
    """Pretty print JSON lines."""
    for line in open('cvr-permanent.json'):
        obj = json.loads(line)
        pprint(obj)

def main():
    pretty_print()

if __name__ == '__main__':
    main()
<commit_msg>Delete cvrminer.py, code moved to submodules<commit_after><|endoftext|>


<commit_before>from django import forms
from django.utils.translation import ugettext as _

from openedx.features.marketplace.models import MarketplaceRequest

class MarketplaceRequestForm(forms.ModelForm):
    def __init__(self,*args, **kwargs):
        super(MarketplaceRequestForm,self).__init__(*args, **kwargs)
        self.label_suffix = ""
    class Meta:
        model = MarketplaceRequest
        fields = '__all__'
        widgets = {
            'user': forms.HiddenInput(),
            'organization': forms.Select(attrs={
                'style': 'pointer-events: none;'
            })
        }
        labels = {
            'organization': _('Organization Name*'),
            'organization_mission': _('Organization Mission*'),
            'organization_sector': _('Which sector is your organization working in?*'),
            'organizational_problems': _('Current Organizational Problems*'),
            'description': _('Brief Description of Challenges*'),
            'user_services': _('What help can you provide to other organizations?*'),
        }
        readonly_fields = ('organization',)
<commit_msg>LP-2000 update labels<commit_after>from django import forms
from django.utils.translation import ugettext as _

from openedx.features.marketplace.models import MarketplaceRequest

class MarketplaceRequestForm(forms.ModelForm):
    def __init__(self,*args, **kwargs):
        super(MarketplaceRequestForm,self).__init__(*args, **kwargs)
        self.label_suffix = ""
    class Meta:
        model = MarketplaceRequest
        fields = '__all__'
        widgets = {
            'user': forms.HiddenInput(),
            'organization': forms.Select(attrs={
                'style': 'pointer-events: none;'
            })
        }
        labels = {
            'organization': _('Organization Name*'),
            'organization_mission': _('Organization Mission*'),
            'organization_sector': _('Which sector is your organization working in?*'),
            'organizational_problems': _('Current Organizational Problems*'),
            'description': _('Brief Description of Challenges*'),
            'user_services': _('What help can you provide to other organizations?*'),
            'file': _('Additional Attachments (Optional)'),
        }
        readonly_fields = ('organization',)
<|endoftext|>


<commit_before>#
# Copyright (C) 2017 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods
from __future__ import absolute_import

import anyconfig.backend.cbor as TT
import tests.backend.common as TBC

class HasParserTrait(TBC.HasParserTrait):

    psr = TT.Parser()
    cnf = dict(a=0, b="bbb", c=5, sect0=dict(d=["x", "y", "z"]))
    cnf_s = TT.cbor.dumps(cnf)

class Test_10(TBC.Test_10_dumps_and_loads, HasParserTrait):

    load_options = dump_options = dict(sort_keys=False)

class Test_20(TBC.Test_20_dump_and_load, HasParserTrait):

    pass

# vim:sw=4:ts=4:et:
<commit_msg>change: do not run test code of cbor backend if it's not available<commit_after>#
# Copyright (C) 2017 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods
from __future__ import absolute_import

try:
    import anyconfig.backend.cbor as TT
except ImportError:
    TT = None

import tests.backend.common as TBC

if TT is not None:
    class HasParserTrait(TBC.HasParserTrait):

        psr = TT.Parser()
        cnf = dict(a=0, b="bbb", c=5, sect0=dict(d=["x", "y", "z"]))
        cnf_s = TT.cbor.dumps(cnf)

    class Test_10(TBC.Test_10_dumps_and_loads, HasParserTrait):

        load_options = dump_options = dict(sort_keys=False)

    class Test_20(TBC.Test_20_dump_and_load, HasParserTrait):

        pass

# vim:sw=4:ts=4:et:
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
import time

import openerp

_logger = logging.getLogger('ir_sequence')

class ir_sequence_type(openerp.osv.osv.osv):
    _name = 'ir.sequence.type'
    _order = 'name'
    _columns = {
        'name': openerp.osv.fields.char('Name', size=64, required=True),
        'code': openerp.osv.fields.char('Code', size=32, required=True),
    }

    _sql_constraints = [
        ('code_unique', 'unique(code)', '`code` must be unique.'),
    ]

def _code_get(self, cr, uid, context={}):
    cr.execute('select code, name from ir_sequence_type')
    return cr.fetchall()

class ir_sequence(openerp.osv.osv.osv):
    """ Sequence model.

    The sequence model allows to define and use so-called sequence objects.
    Such objects are used to generate unique identifiers in a transaction-safe
    way.

    """
    _name = 'ir.sequence'
    _order = 'name'
    _columns = {
        'name': openerp.osv.fields.char('Name', size=64, required=True),
        'code': openerp.osv.fields.selection(_code_get, 'Code', size=64),
        'implementation': openerp.osv.fields.selection( # TODO update the view
            [('standard', 'Standard'), ('no_gap', 'No gap')],
            'Implementation', required=True,
            help="Two sequence object implementations are offered: Standard "
            "and 'No gap'. The later is slower than the former but forbids any"
            " gap in the sequence (while they are possible in the former)."),
        'active': openerp.osv.fields.boolean('Active'),
        'prefix': openerp.osv.fields.char('Prefix', size=64, help="Prefix value of the record for the sequence"),
        'suffix': openerp.osv.fields.char('Suffix', size=64, help="Suffix value of the record for the sequence"),
        'number_next': openerp.osv.fields.integer('Next Number', required=True, help="Next number of this sequence"),
        'number_increment': openerp.osv.fields.integer('Increment Number', required=True, help="The next number of the sequence will be incremented by this number"),
        'padding' : openerp.osv.fields.integer('Number Padding', required=True, help="OpenERP will automatically adds some '0' on the left of the 'Next Number' to get the required padding size."),
        'company_id': openerp.osv.fields.many2one('res.company', 'Company'),
    }
    _defaults = {
        'implementation': 'standard',
        'active': True,
        'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.sequence', context=c),
        'number_increment': 1,
        'number_next': 1,
        'padding' : 0,
    }

    def init(self, cr):
        return # Don't do the following index yet.
        # CONSTRAINT/UNIQUE INDEX on (code, company_id)
        # /!\ The unique constraint 'unique_name_company_id' is not sufficient, because SQL92
        # only support field names in constraint definitions, and we need a function here:
        # we need to special-case company_id to treat all NULL company_id as equal, otherwise
        # we would allow duplicate (code, NULL) ir_sequences.
        cr.execute("""
            SELECT indexname FROM pg_indexes WHERE indexname =
            'ir_sequence_unique_code_company_id_idx'""")
        if not cr.fetchone():
            cr.execute("""
                CREATE UNIQUE INDEX ir_sequence_unique_code_company_id_idx
                ON ir_sequence (code, (COALESCE(company_id,-1)))""")

    def _create_sequence(self, cr, id, number_increment, number_next):
        """ Create a PostreSQL sequence.

        There is no access rights check.
        """
        assert isinstance(id, (int, long))
        sql = "CREATE SEQUENCE ir_sequence_%03d INCREMENT BY %%s START WITH %%s" % id
        cr.execute(sql, (number_increment, number_next))

    def _drop_sequence(self, cr, ids):
        """ Drop the PostreSQL sequence if it exists.

        There is no access rights check.
        """

        ids = ids if isinstance(ids, (list, tuple)) else [ids]
        assert all(isinstance(i, (int, long)) for i in ids), \
            "Only ids in (int, long) allowed."
        names = ','.join('ir_sequence_%03d' % i for i in ids)

        # RESTRICT is the default; it prevents dropping the sequence if an
        # object depends on it.
        cr.execute("DROP SEQUENCE IF EXISTS %s RESTRICT " % names)

    def _alter_sequence(self, cr, id, number_increment, number_next):
        """ Alter a PostreSQL sequence.

        There is no access rights check.
        """
        assert isinstance(id, (int, long))
        cr.execute("""
            ALTER SEQUENCE ir_sequence_%03d INCREMENT BY %%s RESTART WITH %%s
            """ % id, (number_increment, number_next))

    def create(self, cr, uid, values, context=None):
        """ Create a sequence, in implementation == standard a fast gaps-allowed PostgreSQL sequence is used.
        """
        values = self._add_missing_default_values(cr, uid, values, context)
        values['id'] = super(ir_sequence, self).create(cr, uid, values, context)
        if values['implementation'] == 'standard':
            f = self._create_sequence(cr, values['id'], values['number_increment'], values['number_next'])
        return values['id']

    def unlink(self, cr, uid, ids, context=None):
        super(ir_sequence, self).unlink(cr, uid, ids, context)
        self._drop_sequence(cr, ids)
        return True

    def write(self, cr, uid, ids, values, context=None):
        if not isinstance(ids, (list, tuple)):
            ids = [ids]
        new_implementation = values.get('implementation')
        rows = self.read(cr, uid, ids, ['implementation', 'number_increment', 'number_next'], context)
        super(ir_sequence, self).write(cr, uid, ids, values, context)

        for row in rows:
            # 4 cases: we test the previous impl. against the new one.
            if row['implementation'] == 'standard':
                i = values.get('number_increment', row['number_increment'])
                n = values.get('number_next', row['number_next'])
                if new_implementation in ('standard', None):
                    self._alter_sequence(cr, row['id'], i, n)
                else:
                    self._drop_sequence(cr, row['id'])
            else:
                if new_implementation in ('no_gap', None):
                    pass
                else:
                    self._create_sequence(cr, row['id'], i, n)

        return True

    def _interpolate(self, s, d):
        if s:
            return s % d
        return  ''

    def _interpolation_dict(self):
        t = time.localtime() # Actually, the server is always in UTC.
        return {
            'year': time.strftime('%Y', t),
            'month': time.strftime('%m', t),
            'day': time.strftime('%d', t),
            'y': time.strftime('%y', t),
            'doy': time.strftime('%j', t),
            'woy': time.strftime('%W', t),
            'weekday': time.strftime('%w', t),
            'h24': time.strftime('%H', t),
            'h12': time.strftime('%I', t),
            'min': time.strftime('%M', t),
            'sec': time.strftime('%S', t),
        }

    def _next(self, cr, uid, seq_ids, context=None):
        if not seq_ids:
            return False
        seq = self.read(cr, uid, seq_ids[:1], ['implementation','number_next','prefix','suffix','padding'])[0]
        if seq['implementation'] == 'standard':
            cr.execute("SELECT nextval('ir_sequence_%03d')" % seq['id'])
            seq['number_next'] = cr.fetchone()
        else:
            cr.execute("SELECT number_next FROM ir_sequence WHERE id=%s FOR UPDATE NOWAIT", (seq['id'],))
            cr.execute("UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s ", (seq['id'],))
        d = self._interpolation_dict()
        interpolated_prefix = self._interpolate(seq['prefix'], d)
        interpolated_suffix = self._interpolate(seq['suffix'], d)
        return interpolated_prefix + '%%0%sd' % seq['padding'] % seq['number_next'] + interpolated_suffix

    def next_by_id(self, cr, uid, sequence_id, context=None):
        """ Draw an interpolated string using the specified sequence."""
        self.check_read(cr, uid)
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
        ids = self.search(cr, uid, ['&',('id','=', sequence_id),('company_id','in',company_ids)])
        return self._next(cr, uid, ids, context)

    def next_by_code(self, cr, uid, sequence_code, context=None):
        """ Draw an interpolated string using the specified sequence."""
        self.check_read(cr, uid)
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
        ids = self.search(cr, uid, ['&',('code','=', sequence_code),('company_id','in',company_ids)])
        return self._next(cr, uid, ids, context)

    def get_id(self, cr, uid, sequence_code_or_id, code_or_id='id', context=None):
        """ Draw an interpolated string using the specified sequence.

        The sequence to use is specified by the ``sequence_code_or_id``
        argument, which can be a code or an id (as controlled by the
        ``code_or_id`` argument. This method is deprecated.
        """
        # TODO: bump up to warning after 6.1 release
        _logger.debug("ir_sequence.get() and ir_sequence.get_id() are deprecated. "
            "Please use ir_sequence.next_by_code() or ir_sequence.next_by_id().")
        if code_or_id == 'id':
            return self.next_by_id(cr, uid, sequence_code_or_id, context)
        else:
            return self.next_by_code(cr, uid, sequence_code_or_id, context)

    def get(self, cr, uid, code, context=None):
        """ Draw an interpolated string using the specified sequence.

        The sequence to use is specified by its code. This method is
        deprecated.
        """
        return self.get_id(cr, uid, code, 'code', context)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>[FIX]:UnboundLocalError<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
import time

import openerp

_logger = logging.getLogger('ir_sequence')

class ir_sequence_type(openerp.osv.osv.osv):
    _name = 'ir.sequence.type'
    _order = 'name'
    _columns = {
        'name': openerp.osv.fields.char('Name', size=64, required=True),
        'code': openerp.osv.fields.char('Code', size=32, required=True),
    }

    _sql_constraints = [
        ('code_unique', 'unique(code)', '`code` must be unique.'),
    ]

def _code_get(self, cr, uid, context={}):
    cr.execute('select code, name from ir_sequence_type')
    return cr.fetchall()

class ir_sequence(openerp.osv.osv.osv):
    """ Sequence model.

    The sequence model allows to define and use so-called sequence objects.
    Such objects are used to generate unique identifiers in a transaction-safe
    way.

    """
    _name = 'ir.sequence'
    _order = 'name'
    _columns = {
        'name': openerp.osv.fields.char('Name', size=64, required=True),
        'code': openerp.osv.fields.selection(_code_get, 'Code', size=64),
        'implementation': openerp.osv.fields.selection( # TODO update the view
            [('standard', 'Standard'), ('no_gap', 'No gap')],
            'Implementation', required=True,
            help="Two sequence object implementations are offered: Standard "
            "and 'No gap'. The later is slower than the former but forbids any"
            " gap in the sequence (while they are possible in the former)."),
        'active': openerp.osv.fields.boolean('Active'),
        'prefix': openerp.osv.fields.char('Prefix', size=64, help="Prefix value of the record for the sequence"),
        'suffix': openerp.osv.fields.char('Suffix', size=64, help="Suffix value of the record for the sequence"),
        'number_next': openerp.osv.fields.integer('Next Number', required=True, help="Next number of this sequence"),
        'number_increment': openerp.osv.fields.integer('Increment Number', required=True, help="The next number of the sequence will be incremented by this number"),
        'padding' : openerp.osv.fields.integer('Number Padding', required=True, help="OpenERP will automatically adds some '0' on the left of the 'Next Number' to get the required padding size."),
        'company_id': openerp.osv.fields.many2one('res.company', 'Company'),
    }
    _defaults = {
        'implementation': 'standard',
        'active': True,
        'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.sequence', context=c),
        'number_increment': 1,
        'number_next': 1,
        'padding' : 0,
    }

    def init(self, cr):
        return # Don't do the following index yet.
        # CONSTRAINT/UNIQUE INDEX on (code, company_id)
        # /!\ The unique constraint 'unique_name_company_id' is not sufficient, because SQL92
        # only support field names in constraint definitions, and we need a function here:
        # we need to special-case company_id to treat all NULL company_id as equal, otherwise
        # we would allow duplicate (code, NULL) ir_sequences.
        cr.execute("""
            SELECT indexname FROM pg_indexes WHERE indexname =
            'ir_sequence_unique_code_company_id_idx'""")
        if not cr.fetchone():
            cr.execute("""
                CREATE UNIQUE INDEX ir_sequence_unique_code_company_id_idx
                ON ir_sequence (code, (COALESCE(company_id,-1)))""")

    def _create_sequence(self, cr, id, number_increment, number_next):
        """ Create a PostreSQL sequence.

        There is no access rights check.
        """
        assert isinstance(id, (int, long))
        sql = "CREATE SEQUENCE ir_sequence_%03d INCREMENT BY %%s START WITH %%s" % id
        cr.execute(sql, (number_increment, number_next))

    def _drop_sequence(self, cr, ids):
        """ Drop the PostreSQL sequence if it exists.

        There is no access rights check.
        """

        ids = ids if isinstance(ids, (list, tuple)) else [ids]
        assert all(isinstance(i, (int, long)) for i in ids), \
            "Only ids in (int, long) allowed."
        names = ','.join('ir_sequence_%03d' % i for i in ids)

        # RESTRICT is the default; it prevents dropping the sequence if an
        # object depends on it.
        cr.execute("DROP SEQUENCE IF EXISTS %s RESTRICT " % names)

    def _alter_sequence(self, cr, id, number_increment, number_next):
        """ Alter a PostreSQL sequence.

        There is no access rights check.
        """
        assert isinstance(id, (int, long))
        cr.execute("""
            ALTER SEQUENCE ir_sequence_%03d INCREMENT BY %%s RESTART WITH %%s
            """ % id, (number_increment, number_next))

    def create(self, cr, uid, values, context=None):
        """ Create a sequence, in implementation == standard a fast gaps-allowed PostgreSQL sequence is used.
        """
        values = self._add_missing_default_values(cr, uid, values, context)
        values['id'] = super(ir_sequence, self).create(cr, uid, values, context)
        if values['implementation'] == 'standard':
            f = self._create_sequence(cr, values['id'], values['number_increment'], values['number_next'])
        return values['id']

    def unlink(self, cr, uid, ids, context=None):
        super(ir_sequence, self).unlink(cr, uid, ids, context)
        self._drop_sequence(cr, ids)
        return True

    def write(self, cr, uid, ids, values, context=None):
        if not isinstance(ids, (list, tuple)):
            ids = [ids]
        new_implementation = values.get('implementation')
        rows = self.read(cr, uid, ids, ['implementation', 'number_increment', 'number_next'], context)
        super(ir_sequence, self).write(cr, uid, ids, values, context)

        for row in rows:
            # 4 cases: we test the previous impl. against the new one.
            i = values.get('number_increment', row['number_increment'])
            n = values.get('number_next', row['number_next'])
            if row['implementation'] == 'standard':
                if new_implementation in ('standard', None):
                    self._alter_sequence(cr, row['id'], i, n)
                else:
                    self._drop_sequence(cr, row['id'])
            else:
                if new_implementation in ('no_gap', None):
                    pass
                else:
                    self._create_sequence(cr, row['id'], i, n)

        return True

    def _interpolate(self, s, d):
        if s:
            return s % d
        return  ''

    def _interpolation_dict(self):
        t = time.localtime() # Actually, the server is always in UTC.
        return {
            'year': time.strftime('%Y', t),
            'month': time.strftime('%m', t),
            'day': time.strftime('%d', t),
            'y': time.strftime('%y', t),
            'doy': time.strftime('%j', t),
            'woy': time.strftime('%W', t),
            'weekday': time.strftime('%w', t),
            'h24': time.strftime('%H', t),
            'h12': time.strftime('%I', t),
            'min': time.strftime('%M', t),
            'sec': time.strftime('%S', t),
        }

    def _next(self, cr, uid, seq_ids, context=None):
        if not seq_ids:
            return False
        seq = self.read(cr, uid, seq_ids[:1], ['implementation','number_next','prefix','suffix','padding'])[0]
        if seq['implementation'] == 'standard':
            cr.execute("SELECT nextval('ir_sequence_%03d')" % seq['id'])
            seq['number_next'] = cr.fetchone()
        else:
            cr.execute("SELECT number_next FROM ir_sequence WHERE id=%s FOR UPDATE NOWAIT", (seq['id'],))
            cr.execute("UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s ", (seq['id'],))
        d = self._interpolation_dict()
        interpolated_prefix = self._interpolate(seq['prefix'], d)
        interpolated_suffix = self._interpolate(seq['suffix'], d)
        return interpolated_prefix + '%%0%sd' % seq['padding'] % seq['number_next'] + interpolated_suffix

    def next_by_id(self, cr, uid, sequence_id, context=None):
        """ Draw an interpolated string using the specified sequence."""
        self.check_read(cr, uid)
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
        ids = self.search(cr, uid, ['&',('id','=', sequence_id),('company_id','in',company_ids)])
        return self._next(cr, uid, ids, context)

    def next_by_code(self, cr, uid, sequence_code, context=None):
        """ Draw an interpolated string using the specified sequence."""
        self.check_read(cr, uid)
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context) + [False]
        ids = self.search(cr, uid, ['&',('code','=', sequence_code),('company_id','in',company_ids)])
        return self._next(cr, uid, ids, context)

    def get_id(self, cr, uid, sequence_code_or_id, code_or_id='id', context=None):
        """ Draw an interpolated string using the specified sequence.

        The sequence to use is specified by the ``sequence_code_or_id``
        argument, which can be a code or an id (as controlled by the
        ``code_or_id`` argument. This method is deprecated.
        """
        # TODO: bump up to warning after 6.1 release
        _logger.debug("ir_sequence.get() and ir_sequence.get_id() are deprecated. "
            "Please use ir_sequence.next_by_code() or ir_sequence.next_by_id().")
        if code_or_id == 'id':
            return self.next_by_id(cr, uid, sequence_code_or_id, context)
        else:
            return self.next_by_code(cr, uid, sequence_code_or_id, context)

    def get(self, cr, uid, code, context=None):
        """ Draw an interpolated string using the specified sequence.

        The sequence to use is specified by its code. This method is
        deprecated.
        """
        return self.get_id(cr, uid, code, 'code', context)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>


<commit_before>import os
import re
import sys

try:
    from Cython.Distutils import build_ext
except ImportError:
    from setuptools.command.build_ext import build_ext

from distutils.extension import Extension
from distutils.sysconfig import get_config_vars, get_python_lib, get_python_version
from pkg_resources import Distribution

if sys.platform == 'darwin':
    config_vars = get_config_vars()
    config_vars['LDSHARED'] = config_vars['LDSHARED'].replace('-bundle', '')
    config_vars['SHLIB_EXT'] = '.so'

def is_pip_install():
    if "_" in os.environ and os.environ["_"].endswith("pip"):
        return True
    if "pip-egg-info" in sys.argv:
        return True
    if re.search("/pip-.*-build/", __file__):
        return True
    return False

class CyExtension(Extension):
    def __init__(self, *args, **kwargs):
        self._init_func = kwargs.pop("init_func", None)
        Extension.__init__(self, *args, **kwargs)

    def extend_includes(self, includes):
        self.include_dirs.extend(includes)

    def extend_macros(self, macros):
        self.define_macros.extend(macros)

    def extend_extra_objects(self, objs):
        self.extra_objects.extend(objs)

class cy_build_ext(build_ext):

    def _get_egg_name(self):
        ei_cmd = self.get_finalized_command("egg_info")
        return Distribution(
            None, None, ei_cmd.egg_name, ei_cmd.egg_version, get_python_version(),
            self.distribution.has_ext_modules() and self.plat_name).egg_name()

    def build_extension(self, ext):

        if isinstance(ext, CyExtension) and ext._init_func:
            ext._init_func(ext)

        if not self.inplace:
            ext.library_dirs.append(os.path.join(self.build_lib, "pysam"))

        if sys.platform == 'darwin':
            relative_module_path = ext.name.replace(".", os.sep) + get_config_vars()["SO"]

            if "develop" in sys.argv or "test" in sys.argv:
                # develop-mode and tests use local directory
                pkg_root = os.path.dirname(__file__)
                linker_path = os.path.join(pkg_root, relative_module_path)
            elif "bdist_wheel" in sys.argv or is_pip_install():
                # making a wheel, or pip is secretly involved
                linker_path = os.path.join("@rpath", relative_module_path)
            else:
                # making an egg: `python setup.py install` default behavior
                egg_name = '%s.egg' % self._get_egg_name()
                linker_path = os.path.join("@rpath", egg_name, relative_module_path)

            if not ext.extra_link_args:
                ext.extra_link_args = []
            ext.extra_link_args += ['-dynamiclib',
                                    '-rpath', get_python_lib(),
                                    '-Wl,-headerpad_max_install_names',
                                    '-Wl,-install_name,%s' % linker_path,
                                    '-Wl,-x']
        else:
            if not ext.extra_link_args:
                ext.extra_link_args = []

            ext.extra_link_args += ['-Wl,-rpath,$ORIGIN']

        build_ext.build_extension(self, ext)
<commit_msg>Use @loader_path for mac OS library paths<commit_after>import os
import re
import sys

try:
    from Cython.Distutils import build_ext
except ImportError:
    from setuptools.command.build_ext import build_ext

from distutils.extension import Extension
from distutils.sysconfig import get_config_vars, get_python_lib, get_python_version
from pkg_resources import Distribution

if sys.platform == 'darwin':
    config_vars = get_config_vars()
    config_vars['LDSHARED'] = config_vars['LDSHARED'].replace('-bundle', '')
    config_vars['SHLIB_EXT'] = '.so'

def is_pip_install():
    if "_" in os.environ and os.environ["_"].endswith("pip"):
        return True
    if "pip-egg-info" in sys.argv:
        return True
    if re.search("/pip-.*-build/", __file__):
        return True
    return False

class CyExtension(Extension):
    def __init__(self, *args, **kwargs):
        self._init_func = kwargs.pop("init_func", None)
        Extension.__init__(self, *args, **kwargs)

    def extend_includes(self, includes):
        self.include_dirs.extend(includes)

    def extend_macros(self, macros):
        self.define_macros.extend(macros)

    def extend_extra_objects(self, objs):
        self.extra_objects.extend(objs)

class cy_build_ext(build_ext):

    def _get_egg_name(self):
        ei_cmd = self.get_finalized_command("egg_info")
        return Distribution(
            None, None, ei_cmd.egg_name, ei_cmd.egg_version, get_python_version(),
            self.distribution.has_ext_modules() and self.plat_name).egg_name()

    def build_extension(self, ext):

        if isinstance(ext, CyExtension) and ext._init_func:
            ext._init_func(ext)

        if not self.inplace:
            ext.library_dirs.append(os.path.join(self.build_lib, "pysam"))

        if sys.platform == 'darwin':
            relative_module_path = ext.name.replace(".", os.sep) + get_config_vars()["SO"]
            library_path = os.path.join(
                "@loader_path", os.path.basename(relative_module_path)
            )

            if not ext.extra_link_args:
                ext.extra_link_args = []
            ext.extra_link_args += ['-dynamiclib',
                                    '-Wl,-headerpad_max_install_names',
                                    '-Wl,-install_name,%s' % library_path,
                                    '-Wl,-x']
        else:
            if not ext.extra_link_args:
                ext.extra_link_args = []

            ext.extra_link_args += ['-Wl,-rpath,$ORIGIN']

        build_ext.build_extension(self, ext)
<|endoftext|>


<commit_before>#!/usr/bin/python

# Copyright (c) 2016 IBM Corp.
# Copyright (c) 2016 Red Hat
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.

import datetime

class Console(object):
    def __enter__(self):
        self.logfile = open('/tmp/console.html', 'a', 0)
        return self

    def __exit__(self, etype, value, tb):
        self.logfile.close()

    def addLine(self, ln):
        ts = datetime.datetime.now()
        outln = '%s %s' % (str(ts), ln)
        self.logfile.write(outln)

def log(msg):
    if not isinstance(msg, list):
        msg = [msg]
    with Console() as console:
        for line in msg:
            console.addLine("[Zuul] %s\n" % line)

def main():
    module = AnsibleModule(
        argument_spec=dict(
            msg=dict(required=True, type='raw'),
        )
    )

    p = module.params
    log(p['msg'])
    module.exit_json(changed=True)

from ansible.module_utils.basic import *  # noqa

if __name__ == '__main__':
    main()
<commit_msg>Ansible launcher: add '|' to zuul_log<commit_after>#!/usr/bin/python

# Copyright (c) 2016 IBM Corp.
# Copyright (c) 2016 Red Hat
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.

import datetime

class Console(object):
    def __enter__(self):
        self.logfile = open('/tmp/console.html', 'a', 0)
        return self

    def __exit__(self, etype, value, tb):
        self.logfile.close()

    def addLine(self, ln):
        ts = datetime.datetime.now()
        outln = '%s | %s' % (str(ts), ln)
        self.logfile.write(outln)

def log(msg):
    if not isinstance(msg, list):
        msg = [msg]
    with Console() as console:
        for line in msg:
            console.addLine("[Zuul] %s\n" % line)

def main():
    module = AnsibleModule(
        argument_spec=dict(
            msg=dict(required=True, type='raw'),
        )
    )

    p = module.params
    log(p['msg'])
    module.exit_json(changed=True)

from ansible.module_utils.basic import *  # noqa

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>import collections
import itertools
import multiprocessing

import mcbench.xpath
from mcbench.models import Benchmark, Query
from mcbench import settings

def matches_in(xpath, file):
    xpath = mcbench.xpath.compile(xpath)
    xml = mcbench.xpath.parse_xml_filename(file.xml_path)
    return xpath.execute(xml)

def matching_lines(benchmark, xpath):
    lines = collections.defaultdict(lambda: {'m': [], 'xml': []})
    if xpath is None:
        return lines
    for file in benchmark.files:
        matches = matches_in(xpath, file)
        lines[file.name]['m'] = [match.get('line', 1) for match in matches]
        lines[file.name]['xml'] = [match.sourceline for match in matches]
    return lines

def _num_matches_worker((name, xpath)):
    benchmark = Benchmark(name=name)
    return sum(len(matches_in(xpath, file)) for file in benchmark.files)

def _map(benchmarks, query):
    pool = multiprocessing.Pool(processes=32)
    results = pool.map(_num_matches_worker,
                       ((b.name, query) for b in benchmarks))
    pool.close()
    return results

def compute_matches(xpath):
    benchmarks = Benchmark.all()
    results = _map(benchmarks, xpath)
    for benchmark, num_matches in itertools.izip(benchmarks, results):
        if num_matches:
            yield benchmark, num_matches

def get_matches(xpath):
    query = Query.find_by_xpath(xpath)
    if query is None:
        query = Query.create(xpath=xpath, name='')
        query.cache_matches(compute_matches(xpath))
    return list(query.get_cached_matches())
<commit_msg>Fix spurious RuntimeError. Closes #12.<commit_after>import collections
import itertools
import multiprocessing

import mcbench.xpath
from mcbench.models import Benchmark, Query
from mcbench import settings

def matches_in(xpath, file):
    xpath = mcbench.xpath.compile(xpath)
    xml = mcbench.xpath.parse_xml_filename(file.xml_path)
    return xpath.execute(xml)

def matching_lines(benchmark, xpath):
    lines = collections.defaultdict(lambda: {'m': [], 'xml': []})
    if xpath is None:
        return lines
    for file in benchmark.files:
        matches = matches_in(xpath, file)
        lines[file.name]['m'] = [match.get('line', 1) for match in matches]
        lines[file.name]['xml'] = [match.sourceline for match in matches]
    return lines

def _num_matches_worker((name, xpath)):
    benchmark = Benchmark(name=name)
    return sum(len(matches_in(xpath, file)) for file in benchmark.files)

def _map(benchmarks, query):
    pool = multiprocessing.Pool(processes=32)
    results = pool.map(_num_matches_worker,
                       ((b.name, query) for b in benchmarks))
    pool.close()
    pool.join()
    return results

def compute_matches(xpath):
    benchmarks = Benchmark.all()
    results = _map(benchmarks, xpath)
    for benchmark, num_matches in itertools.izip(benchmarks, results):
        if num_matches:
            yield benchmark, num_matches

def get_matches(xpath):
    query = Query.find_by_xpath(xpath)
    if query is None:
        query = Query.create(xpath=xpath, name='')
        query.cache_matches(compute_matches(xpath))
    return list(query.get_cached_matches())
<|endoftext|>


<commit_before>"""
Metrics for off-policy evaluation.
"""

POLICY_RETURNS = {
    'halfcheetah-medium' : 3985.8150261686337,
    'halfcheetah-random' : -199.26067391425954,
    'halfcheetah-expert' : 12330.945945279545,
    'hopper-medium' : 2260.1983114487352,
    'hopper-random' : 1257.9757846810203,
    'hopper-expert' : 3624.4696022560997,
    'walker2d-medium' : 2760.3310101980005,
    'walker2d-random' : 896.4751989935487,
    'walker2d-expert' : .005.89370727539,
}

def ranking_metric(policies):
    """
    Returns true if policies given are ranked properly, from
    highest to lowest returns.

    Args:
        policies: A list of policy string identifiers.
            Valid identifiers must be contained in POLICY_RETURNS.

    Returns:
        true if the returns of policies are monotonically decreasing.
    """
    return_values = [POLICY_RETURNS[policy_key] for policy_key in policies]
    return all(x>y for x, y in zip(return_values, return_values[1:]))

def return_mse_metric(policy, returns):
    """
    Returns the squared error in estimated returns.

    Args:
        policy (str): A policy string identifier.
        returns (float): Estimated returns
    """
    return (returns - POLICY_RETURNS[policy])**2

def policy_regret_metric(policy, expert_policies):
    """
    Returns the regret of the given policy against a set of expert policies.

    Args:
        policy (str): A policy string identifier.
        expert_policies (list[str]): A list of expert policies
    Returns:
        The regret, which is returns of the best expert minus the returns of the policy.
    """
    best_returns = max([POLICY_RETURNS[policy_key] for policy_key in policies])
    return best_returns - POLICY_RETURNS[best_guess_policy]
<commit_msg>typos<commit_after>"""
Metrics for off-policy evaluation.
"""

POLICY_RETURNS = {
    'halfcheetah-medium' : 3985.8150261686337,
    'halfcheetah-random' : -199.26067391425954,
    'halfcheetah-expert' : 12330.945945279545,
    'hopper-medium' : 2260.1983114487352,
    'hopper-random' : 1257.9757846810203,
    'hopper-expert' : 3624.4696022560997,
    'walker2d-medium' : 2760.3310101980005,
    'walker2d-random' : 896.4751989935487,
    'walker2d-expert' : 4005.89370727539,
}

def ranking_metric(policies):
    """
    Returns true if policies given are ranked properly, from
    highest to lowest value.

    Args:
        policies: A list of policy string identifiers.
            Valid identifiers must be contained in POLICY_RETURNS.

    Returns:
        True if the values of policies are monotonically decreasing.
    """
    return_values = [POLICY_RETURNS[policy_key] for policy_key in policies]
    return all(x>y for x, y in zip(return_values, return_values[1:]))

def value_error_metric(policy, value):
    """
    Returns the absolute error in estimated value.

    Args:
        policy (str): A policy string identifier.
        value (float): Estimated value
    """
    return abs(value - POLICY_RETURNS[policy])

def policy_regret_metric(policy, expert_policies):
    """
    Returns the regret of the given policy against a set of expert policies.

    Args:
        policy (str): A policy string identifier.
        expert_policies (list[str]): A list of expert policies
    Returns:
        The regret, which is value of the best expert minus the value of the policy.
    """
    best_returns = max([POLICY_RETURNS[policy_key] for policy_key in expert_policies])
    return best_returns - POLICY_RETURNS[policy]
<|endoftext|>


<commit_before>#!/usr/bin/env python2.7

# Based on previous work by
# Charles Menguy (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and Sander Marechal (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon19.py measures the temperature of the diskarray.
# uses moving averages

import os, sys, time, math, commands
from libdaemon import Daemon
from libsmart import SmartDisk

# BEWARE
# The disks identified here as `sda`, `sdb` etc. may not necessarily
# be called `/dev/sda`, `/dev/sdb` etc. on the system!!
sda = SmartDisk("/dev/sda -d ata",0)
sdb = SmartDisk("/dev/sdb -d ata",0)
sdc = SmartDisk("/dev/sdc -d ata",0)
sdd = SmartDisk("/dev/sdd -d ata",0)

DEBUG = False

class MyDaemon(Daemon):
  def run(self):
    sampleptr = 0
    cycles = 6
    SamplesPerCycle = 5
    samples = SamplesPerCycle * cycles

    datapoints = 4
    data = []

    sampleTime = 60
    cycleTime = samples * sampleTime
    # sync to whole minute
    waitTime = (cycleTime + sampleTime) - (time.time() % (cycleTime/cycles))
    if DEBUG:print "Waiting {0} s".format(int(waitTime))
    time.sleep(waitTime)
    while True:
      startTime = time.time()

      result = do_work().split(',')
      if DEBUG: print result

      data.append(map(float, result))
      if (len(data) > samples):data.pop(0)
      sampleptr = sampleptr + 1

      # report sample average
      if (sampleptr % SamplesPerCycle == 0):
        somma = map(sum,zip(*data))
        averages = [format(s / len(data), '.3f') for s in somma]
        if DEBUG:print averages
        do_report(averages)
        if (sampleptr == samples):
          sampleptr = 0

      waitTime = sampleTime - (time.time() - startTime) - (startTime%sampleTime)
      if (waitTime > 0):
        if DEBUG:print "Waiting {0} s".format(int(waitTime))
        time.sleep(waitTime)

def do_work():
  # 4 datapoints gathered here
  #
  sda.smart()
  sdb.smart()
  sdc.smart()
  sdd.smart()
  # disktemperature
  Tsda=sda.getdata('194')
  Tsdb=sdb.getdata('194')
  Tsdc=sdc.getdata('194')
  Tsdd=sdd.getdata('194')

  if DEBUG: print Tsda, Tsdb, Tsdc, Tsdd
  return '{0}, {1}, {2}, {3}'.format(Tsda, Tsdb, Tsdc, Tsdd)

def do_report(result):
  # Get the time and date in human-readable form and UN*X-epoch...
  outDate = commands.getoutput("date '+%F %H:%M:%S, %s'")
  result = ', '.join(map(str, result))
  flock = '/tmp/synodiagd/19.lock'
  lock(flock)
  f = file('/tmp/synodiagd/19-tempdisk.csv', 'a')
  f.write('{0}, {1}\n'.format(outDate, result) )
  f.close()
  unlock(flock)
  return

def lock(fname):
  open(fname, 'a').close()

def unlock(fname):
  if os.path.isfile(fname):
    os.remove(fname)

if __name__ == "__main__":
  daemon = MyDaemon('/tmp/synodiagd/19.pid')
  if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
      daemon.start()
    elif 'stop' == sys.argv[1]:
      daemon.stop()
    elif 'restart' == sys.argv[1]:
      daemon.restart()
    elif 'foreground' == sys.argv[1]:
      # assist with debugging.
      print "Debug-mode started. Use <Ctrl>+C to stop."
      DEBUG = True
      daemon.run()
    else:
      print "Unknown command"
      sys.exit(2)
    sys.exit(0)
  else:
    print "usage: %s start|stop|restart|foreground" % sys.argv[0]
    sys.exit(2)
<commit_msg>Revert "first report comes sooner"<commit_after>#!/usr/bin/env python2.7

# Based on previous work by
# Charles Menguy (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and Sander Marechal (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon19.py measures the temperature of the diskarray.
# uses moving averages

import os, sys, time, math, commands
from libdaemon import Daemon
from libsmart import SmartDisk

# BEWARE
# The disks identified here as `sda`, `sdb` etc. may not necessarily
# be called `/dev/sda`, `/dev/sdb` etc. on the system!!
sda = SmartDisk("/dev/sda -d ata",0)
sdb = SmartDisk("/dev/sdb -d ata",0)
sdc = SmartDisk("/dev/sdc -d ata",0)
sdd = SmartDisk("/dev/sdd -d ata",0)

DEBUG = False

class MyDaemon(Daemon):
  def run(self):
    sampleptr = 0
    cycles = 6
    SamplesPerCycle = 5
    samples = SamplesPerCycle * cycles

    datapoints = 4
    data = []

    sampleTime = 60
    cycleTime = samples * sampleTime
    # sync to whole minute
    waitTime = (cycleTime + sampleTime) - (time.time() % cycleTime)
    if DEBUG:print "Waiting {0} s".format(int(waitTime))
    time.sleep(waitTime)
    while True:
      startTime = time.time()

      result = do_work().split(',')
      if DEBUG: print result

      data.append(map(float, result))
      if (len(data) > samples):data.pop(0)
      sampleptr = sampleptr + 1

      # report sample average
      if (sampleptr % SamplesPerCycle == 0):
        somma = map(sum,zip(*data))
        averages = [format(s / len(data), '.3f') for s in somma]
        if DEBUG:print averages
        do_report(averages)
        if (sampleptr == samples):
          sampleptr = 0

      waitTime = sampleTime - (time.time() - startTime) - (startTime%sampleTime)
      if (waitTime > 0):
        if DEBUG:print "Waiting {0} s".format(int(waitTime))
        time.sleep(waitTime)

def do_work():
  # 4 datapoints gathered here
  #
  sda.smart()
  sdb.smart()
  sdc.smart()
  sdd.smart()
  # disktemperature
  Tsda=sda.getdata('194')
  Tsdb=sdb.getdata('194')
  Tsdc=sdc.getdata('194')
  Tsdd=sdd.getdata('194')

  if DEBUG: print Tsda, Tsdb, Tsdc, Tsdd
  return '{0}, {1}, {2}, {3}'.format(Tsda, Tsdb, Tsdc, Tsdd)

def do_report(result):
  # Get the time and date in human-readable form and UN*X-epoch...
  outDate = commands.getoutput("date '+%F %H:%M:%S, %s'")
  result = ', '.join(map(str, result))
  flock = '/tmp/synodiagd/19.lock'
  lock(flock)
  f = file('/tmp/synodiagd/19-tempdisk.csv', 'a')
  f.write('{0}, {1}\n'.format(outDate, result) )
  f.close()
  unlock(flock)
  return

def lock(fname):
  open(fname, 'a').close()

def unlock(fname):
  if os.path.isfile(fname):
    os.remove(fname)

if __name__ == "__main__":
  daemon = MyDaemon('/tmp/synodiagd/19.pid')
  if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
      daemon.start()
    elif 'stop' == sys.argv[1]:
      daemon.stop()
    elif 'restart' == sys.argv[1]:
      daemon.restart()
    elif 'foreground' == sys.argv[1]:
      # assist with debugging.
      print "Debug-mode started. Use <Ctrl>+C to stop."
      DEBUG = True
      daemon.run()
    else:
      print "Unknown command"
      sys.exit(2)
    sys.exit(0)
  else:
    print "usage: %s start|stop|restart|foreground" % sys.argv[0]
    sys.exit(2)
<|endoftext|>


<commit_before>"""
This module contains views used to serve static media if
msettings['SERVE_REMOTE'] == False. See mediasync.urls to see how
these are shimmed in.

The static_serve() function is where the party starts.
"""
from django.http import HttpResponse
from django.views.static import serve
from django.views.generic.simple import redirect_to
from mediasync import combine_files
from mediasync.conf import msettings

def combo_serve(request, path, client):
    """
    Handles generating a 'combo' file for the given path. This is similar to
    what happens when we upload to S3. Processors are applied, and we get
    the value that we would if we were serving from S3. This is a good way
    to make sure combo files work as intended before rolling out
    to production.
    """
    joinfile = path
    sourcefiles = msettings['JOINED'][path]
    # Generate the combo file as a string.
    combo_data, dirname = combine_files(joinfile, sourcefiles, client)

    if path.endswith('.css'):
        mime_type = 'text/css'
    elif joinfile.endswith('.js'):
        mime_type = 'application/javascript'

    return HttpResponse(combo_data, mimetype=mime_type)

def _form_key_str(path):
    """
    Given a URL path, massage it into a key we can perform a lookup on the
    MEDIASYNC['JOINED'] dict with.

    This mostly involves figuring into account the CSS_PATH and JS_PATH
    settings, if they have been set.
    """
    if path.endswith('.css'):
        media_path_prefix = msettings['CSS_PATH']
    elif path.endswith('.js'):
        media_path_prefix = msettings['JS_PATH']
    else:
        # This isn't a CSS/JS file, no combo for you.
        return None

    if media_path_prefix:
        # CS/JSS path prefix has been set. Factor that into the key lookup.
        if not media_path_prefix.endswith('/'):
            # We need to add this slash so we can lop it off the 'path'
            # variable, to match the value in the JOINED dict.
            media_path_prefix += '/'

        if path.startswith(media_path_prefix):
            # Given path starts with the CSS/JS media prefix. Lop this off
            # so we can perform a lookup in the JOINED dict.
            return path[len(media_path_prefix):]
        else:
            # Path is in a root dir, send along as-is.
            return path

    # No CSS/JS path prefix set. Keep it raw.
    return path

def _find_combo_match(path):
    """
    Calculate the key to check the MEDIASYNC['JOINED'] dict for, perform the
    lookup, and return the matching key string if a match is found. If no
    match is found, return None instead.
    """
    key_str = _form_key_str(path)
    if not key_str:
        # _form_key_str() says this isn't even a CSS/JS file.
        return None

    if not msettings['JOINED'].has_key(key_str):
        # No combo file match found. Must be an single file.
        return None
    else:
        # Combo match found, return the JOINED key.
        return key_str

def static_serve(request, path, client):
    """
    Given a request for a media asset, this view does the necessary wrangling
    to get the correct thing delivered to the user. This can also emulate the
    combo behavior seen when SERVE_REMOTE == False and EMULATE_COMBO == True.
    """
    if msettings['SERVE_REMOTE']:
        # We're serving from S3, redirect there.
        url = client.remote_media_url().strip('/') + '/%(path)s'
        return redirect_to(request, url, path=path)

    if not msettings['SERVE_REMOTE'] and msettings['EMULATE_COMBO']:
        # Combo emulation is on and we're serving media locally. Try to see if
        # the given path matches a combo file defined in the JOINED dict in
        # the MEDIASYNC settings dict.
        combo_match = _find_combo_match(path)
        if combo_match:
            # We found a combo file match. Combine it and serve the result.
            return combo_serve(request, combo_match, client)

    # No combo file, but we're serving locally. Use the standard (inefficient)
    # Django static serve view.
    resp = serve(request, path, document_root=client.media_root)
    resp.content = client.process(resp.content, resp['Content-Type'], path)
    return resp
<commit_msg>show indexes when media is served locally<commit_after>"""
This module contains views used to serve static media if
msettings['SERVE_REMOTE'] == False. See mediasync.urls to see how
these are shimmed in.

The static_serve() function is where the party starts.
"""
from django.http import HttpResponse
from django.views.static import serve
from django.views.generic.simple import redirect_to
from mediasync import combine_files
from mediasync.conf import msettings

def combo_serve(request, path, client):
    """
    Handles generating a 'combo' file for the given path. This is similar to
    what happens when we upload to S3. Processors are applied, and we get
    the value that we would if we were serving from S3. This is a good way
    to make sure combo files work as intended before rolling out
    to production.
    """
    joinfile = path
    sourcefiles = msettings['JOINED'][path]
    # Generate the combo file as a string.
    combo_data, dirname = combine_files(joinfile, sourcefiles, client)

    if path.endswith('.css'):
        mime_type = 'text/css'
    elif joinfile.endswith('.js'):
        mime_type = 'application/javascript'

    return HttpResponse(combo_data, mimetype=mime_type)

def _form_key_str(path):
    """
    Given a URL path, massage it into a key we can perform a lookup on the
    MEDIASYNC['JOINED'] dict with.

    This mostly involves figuring into account the CSS_PATH and JS_PATH
    settings, if they have been set.
    """
    if path.endswith('.css'):
        media_path_prefix = msettings['CSS_PATH']
    elif path.endswith('.js'):
        media_path_prefix = msettings['JS_PATH']
    else:
        # This isn't a CSS/JS file, no combo for you.
        return None

    if media_path_prefix:
        # CS/JSS path prefix has been set. Factor that into the key lookup.
        if not media_path_prefix.endswith('/'):
            # We need to add this slash so we can lop it off the 'path'
            # variable, to match the value in the JOINED dict.
            media_path_prefix += '/'

        if path.startswith(media_path_prefix):
            # Given path starts with the CSS/JS media prefix. Lop this off
            # so we can perform a lookup in the JOINED dict.
            return path[len(media_path_prefix):]
        else:
            # Path is in a root dir, send along as-is.
            return path

    # No CSS/JS path prefix set. Keep it raw.
    return path

def _find_combo_match(path):
    """
    Calculate the key to check the MEDIASYNC['JOINED'] dict for, perform the
    lookup, and return the matching key string if a match is found. If no
    match is found, return None instead.
    """
    key_str = _form_key_str(path)
    if not key_str:
        # _form_key_str() says this isn't even a CSS/JS file.
        return None

    if not msettings['JOINED'].has_key(key_str):
        # No combo file match found. Must be an single file.
        return None
    else:
        # Combo match found, return the JOINED key.
        return key_str

def static_serve(request, path, client):
    """
    Given a request for a media asset, this view does the necessary wrangling
    to get the correct thing delivered to the user. This can also emulate the
    combo behavior seen when SERVE_REMOTE == False and EMULATE_COMBO == True.
    """

    if msettings['SERVE_REMOTE']:
        # We're serving from S3, redirect there.
        url = client.remote_media_url().strip('/') + '/%(path)s'
        return redirect_to(request, url, path=path)

    if not msettings['SERVE_REMOTE'] and msettings['EMULATE_COMBO']:
        # Combo emulation is on and we're serving media locally. Try to see if
        # the given path matches a combo file defined in the JOINED dict in
        # the MEDIASYNC settings dict.
        combo_match = _find_combo_match(path)
        if combo_match:
            # We found a combo file match. Combine it and serve the result.
            return combo_serve(request, combo_match, client)

    # No combo file, but we're serving locally. Use the standard (inefficient)
    # Django static serve view.

    resp = serve(request, path, document_root=client.media_root, show_indexes=True)
    resp.content = client.process(resp.content, resp['Content-Type'], path)
    return resp
<|endoftext|>