<commit_before>#!/usr/bin/env python

# Copyright 2013 Quanta Research Cambridge, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import argparse
import inspect
import json
import sys
from testtools.testsuite import iterate_tests
try:
    from unittest import loader
except ImportError:
    # unittest in python 2.6 does not contain loader, so uses unittest2
    from unittest2 import loader

from tempest.openstack.common import log as logging
from tempest.stress import driver

LOG = logging.getLogger(__name__)

def discover_stress_tests(path="./", filter_attr=None, call_inherited=False):
    """Discovers all tempest tests and create action out of them
    """
    LOG.info("Start test discovery")
    tests = []
    testloader = loader.TestLoader()
    list = testloader.discover(path)
    for func in (iterate_tests(list)):
        attrs = []
        try:
            method_name = getattr(func, '_testMethodName')
            full_name = "%s.%s.%s" % (func.__module__,
                                      func.__class__.__name__,
                                      method_name)
            test_func = getattr(func, method_name)
            # NOTE(mkoderer): this contains a list of all type attributes
            attrs = getattr(test_func, "__testtools_attrs")
        except Exception:
            next
        if 'stress' in attrs:
            if filter_attr is not None and not filter_attr in attrs:
                continue
            class_setup_per = getattr(test_func, "st_class_setup_per")

            action = {'action':
                      "tempest.stress.actions.unit_test.UnitTest",
                      'kwargs': {"test_method": full_name,
                                 "class_setup_per": class_setup_per
                                 }
                      }
            if (not call_inherited and
                getattr(test_func, "st_allow_inheritance") is not True):
                class_structure = inspect.getmro(test_func.im_class)
                if test_func.__name__ not in class_structure[0].__dict__:
                    continue
            tests.append(action)
    return tests

def main(ns):
    result = 0
    if not ns.all:
        tests = json.load(open(ns.tests, 'r'))
    else:
        tests = discover_stress_tests(filter_attr=ns.type,
                                      call_inherited=ns.call_inherited)

    if ns.serial:
        for test in tests:
            step_result = driver.stress_openstack([test],
                                                  ns.duration,
                                                  ns.number,
                                                  ns.stop)
            # NOTE(mkoderer): we just save the last result code
            if (step_result != 0):
                result = step_result
    else:
        driver.stress_openstack(tests, ns.duration, ns.number, ns.stop)
    return result

parser = argparse.ArgumentParser(description='Run stress tests')
parser.add_argument('-d', '--duration', default=300, type=int,
                    help="Duration of test in secs")
parser.add_argument('-s', '--serial', action='store_true',
                    help="Trigger running tests serially")
parser.add_argument('-S', '--stop', action='store_true',
                    default=False, help="Stop on first error")
parser.add_argument('-n', '--number', type=int,
                    help="How often an action is executed for each process")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a', '--all', action='store_true',
                   help="Execute all stress tests")
parser.add_argument('-T', '--type',
                    help="Filters tests of a certain type (e.g. gate)")
parser.add_argument('-i', '--call-inherited', action='store_true',
                    default=False,
                    help="Call also inherited function with stress attribute")
group.add_argument('-t', "--tests", nargs='?',
                   help="Name of the file with test description")

if __name__ == "__main__":
    try:
        sys.exit(main(parser.parse_args()))
    except Exception:
        LOG.exception("Failure in the stress test framework")
        sys.exit(1)
<commit_msg>Fix stress runner exit issues<commit_after>#!/usr/bin/env python

# Copyright 2013 Quanta Research Cambridge, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

import argparse
import inspect
import json
import sys
from testtools.testsuite import iterate_tests
try:
    from unittest import loader
except ImportError:
    # unittest in python 2.6 does not contain loader, so uses unittest2
    from unittest2 import loader

from tempest.openstack.common import log as logging
from tempest.stress import driver

LOG = logging.getLogger(__name__)

def discover_stress_tests(path="./", filter_attr=None, call_inherited=False):
    """Discovers all tempest tests and create action out of them
    """
    LOG.info("Start test discovery")
    tests = []
    testloader = loader.TestLoader()
    list = testloader.discover(path)
    for func in (iterate_tests(list)):
        attrs = []
        try:
            method_name = getattr(func, '_testMethodName')
            full_name = "%s.%s.%s" % (func.__module__,
                                      func.__class__.__name__,
                                      method_name)
            test_func = getattr(func, method_name)
            # NOTE(mkoderer): this contains a list of all type attributes
            attrs = getattr(test_func, "__testtools_attrs")
        except Exception:
            next
        if 'stress' in attrs:
            if filter_attr is not None and not filter_attr in attrs:
                continue
            class_setup_per = getattr(test_func, "st_class_setup_per")

            action = {'action':
                      "tempest.stress.actions.unit_test.UnitTest",
                      'kwargs': {"test_method": full_name,
                                 "class_setup_per": class_setup_per
                                 }
                      }
            if (not call_inherited and
                getattr(test_func, "st_allow_inheritance") is not True):
                class_structure = inspect.getmro(test_func.im_class)
                if test_func.__name__ not in class_structure[0].__dict__:
                    continue
            tests.append(action)
    return tests

def main(ns):
    result = 0
    if not ns.all:
        tests = json.load(open(ns.tests, 'r'))
    else:
        tests = discover_stress_tests(filter_attr=ns.type,
                                      call_inherited=ns.call_inherited)

    if ns.serial:
        for test in tests:
            step_result = driver.stress_openstack([test],
                                                  ns.duration,
                                                  ns.number,
                                                  ns.stop)
            # NOTE(mkoderer): we just save the last result code
            if (step_result != 0):
                result = step_result
                if ns.stop:
                    return result
    else:
        result = driver.stress_openstack(tests,
                                         ns.duration,
                                         ns.number,
                                         ns.stop)
    return result

parser = argparse.ArgumentParser(description='Run stress tests')
parser.add_argument('-d', '--duration', default=300, type=int,
                    help="Duration of test in secs")
parser.add_argument('-s', '--serial', action='store_true',
                    help="Trigger running tests serially")
parser.add_argument('-S', '--stop', action='store_true',
                    default=False, help="Stop on first error")
parser.add_argument('-n', '--number', type=int,
                    help="How often an action is executed for each process")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a', '--all', action='store_true',
                   help="Execute all stress tests")
parser.add_argument('-T', '--type',
                    help="Filters tests of a certain type (e.g. gate)")
parser.add_argument('-i', '--call-inherited', action='store_true',
                    default=False,
                    help="Call also inherited function with stress attribute")
group.add_argument('-t', "--tests", nargs='?',
                   help="Name of the file with test description")

if __name__ == "__main__":
    try:
        sys.exit(main(parser.parse_args()))
    except Exception:
        LOG.exception("Failure in the stress test framework")
        sys.exit(1)
<|endoftext|>


<commit_before>
from typing import Any, Dict, Optional, List, Text
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _

from zerver.decorator import require_realm_admin, to_non_negative_int, to_not_negative_int_or_none
from zerver.lib.actions import (
    do_set_realm_message_editing,
    do_set_realm_authentication_methods,
    do_set_realm_notifications_stream,
    do_set_realm_property,
)
from zerver.lib.i18n import get_available_language_codes
from zerver.lib.request import has_request_variables, REQ, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.validator import check_string, check_dict, check_bool, check_int
from zerver.lib.streams import access_stream_by_id
from zerver.models import Realm, UserProfile

@require_realm_admin
@has_request_variables
def update_realm(request, user_profile, name=REQ(validator=check_string, default=None),
                 description=REQ(validator=check_string, default=None),
                 restricted_to_domain=REQ(validator=check_bool, default=None),
                 invite_required=REQ(validator=check_bool, default=None),
                 invite_by_admins_only=REQ(validator=check_bool, default=None),
                 name_changes_disabled=REQ(validator=check_bool, default=None),
                 email_changes_disabled=REQ(validator=check_bool, default=None),
                 inline_image_preview=REQ(validator=check_bool, default=None),
                 inline_url_embed_preview=REQ(validator=check_bool, default=None),
                 create_stream_by_admins_only=REQ(validator=check_bool, default=None),
                 add_emoji_by_admins_only=REQ(validator=check_bool, default=None),
                 allow_message_editing=REQ(validator=check_bool, default=None),
                 mandatory_topics=REQ(validator=check_bool, default=None),
                 message_content_edit_limit_seconds=REQ(converter=to_non_negative_int, default=None),
                 allow_edit_history=REQ(validator=check_bool, default=None),
                 default_language=REQ(validator=check_string, default=None),
                 waiting_period_threshold=REQ(converter=to_non_negative_int, default=None),
                 authentication_methods=REQ(validator=check_dict([]), default=None),
                 notifications_stream_id=REQ(validator=check_int, default=None),
                 message_retention_days=REQ(converter=to_not_negative_int_or_none, default=None)):
    # type: (HttpRequest, UserProfile, Optional[str], Optional[str], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[int], Optional[bool], Optional[str], Optional[int], Optional[dict], Optional[int], Optional[int]) -> HttpResponse
    realm = user_profile.realm

    # Additional validation/error checking beyond types go here, so
    # the entire request can succeed or fail atomically.
    if default_language is not None and default_language not in get_available_language_codes():
        raise JsonableError(_("Invalid language '%s'" % (default_language,)))
    if description is not None and len(description) > 1000:
        return json_error(_("Realm description is too long."))
    if name is not None and len(name) > Realm.MAX_REALM_NAME_LENGTH:
        return json_error(_("Realm name is too long."))
    if authentication_methods is not None and True not in list(authentication_methods.values()):
        return json_error(_("At least one authentication method must be enabled."))

    # The user of `locals()` here is a bit of a code smell, but it's
    # restricted to the elements present in realm.property_types.
    #
    # TODO: It should be possible to deduplicate this function up
    # further by some more advanced usage of the
    # `REQ/has_request_variables` extraction.
    req_vars = {k: v for k, v in list(locals().items()) if k in realm.property_types}
    data = {}  # type: Dict[str, Any]

    for k, v in list(req_vars.items()):
        if v is not None and getattr(realm, k) != v:
            do_set_realm_property(realm, k, v)
            if isinstance(v, Text):
                data[k] = 'updated'
            else:
                data[k] = v

    # The following realm properties do not fit the pattern above
    # authentication_methods is not supported by the do_set_realm_property
    # framework because of its bitfield.
    if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
        do_set_realm_authentication_methods(realm, authentication_methods)
        data['authentication_methods'] = authentication_methods
    # The message_editing settings are coupled to each other, and thus don't fit
    # into the do_set_realm_property framework.
    if (allow_message_editing is not None and realm.allow_message_editing != allow_message_editing) or \
       (message_content_edit_limit_seconds is not None and
            realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds):
        if allow_message_editing is None:
            allow_message_editing = realm.allow_message_editing
        if message_content_edit_limit_seconds is None:
            message_content_edit_limit_seconds = realm.message_content_edit_limit_seconds
        do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds)
        data['allow_message_editing'] = allow_message_editing
        data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
    # Realm.notifications_stream is not a boolean, Text or integer field, and thus doesn't fit
    # into the do_set_realm_property framework.
    if notifications_stream_id is not None:
        if realm.notifications_stream is None or realm.notifications_stream.id != notifications_stream_id:
            new_notifications_stream = None
            if notifications_stream_id >= 0:
                (new_notifications_stream, recipient, sub) = access_stream_by_id(user_profile, notifications_stream_id)
            do_set_realm_notifications_stream(realm, new_notifications_stream, notifications_stream_id)
            data['notifications_stream_id'] = notifications_stream_id

    return json_success(data)
<commit_msg>mypy: Use Dict[Any,Any] for update_realm parameter.<commit_after>
from typing import Any, Dict, Optional, List, Text
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _

from zerver.decorator import require_realm_admin, to_non_negative_int, to_not_negative_int_or_none
from zerver.lib.actions import (
    do_set_realm_message_editing,
    do_set_realm_authentication_methods,
    do_set_realm_notifications_stream,
    do_set_realm_property,
)
from zerver.lib.i18n import get_available_language_codes
from zerver.lib.request import has_request_variables, REQ, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.validator import check_string, check_dict, check_bool, check_int
from zerver.lib.streams import access_stream_by_id
from zerver.models import Realm, UserProfile

@require_realm_admin
@has_request_variables
def update_realm(request, user_profile, name=REQ(validator=check_string, default=None),
                 description=REQ(validator=check_string, default=None),
                 restricted_to_domain=REQ(validator=check_bool, default=None),
                 invite_required=REQ(validator=check_bool, default=None),
                 invite_by_admins_only=REQ(validator=check_bool, default=None),
                 name_changes_disabled=REQ(validator=check_bool, default=None),
                 email_changes_disabled=REQ(validator=check_bool, default=None),
                 inline_image_preview=REQ(validator=check_bool, default=None),
                 inline_url_embed_preview=REQ(validator=check_bool, default=None),
                 create_stream_by_admins_only=REQ(validator=check_bool, default=None),
                 add_emoji_by_admins_only=REQ(validator=check_bool, default=None),
                 allow_message_editing=REQ(validator=check_bool, default=None),
                 mandatory_topics=REQ(validator=check_bool, default=None),
                 message_content_edit_limit_seconds=REQ(converter=to_non_negative_int, default=None),
                 allow_edit_history=REQ(validator=check_bool, default=None),
                 default_language=REQ(validator=check_string, default=None),
                 waiting_period_threshold=REQ(converter=to_non_negative_int, default=None),
                 authentication_methods=REQ(validator=check_dict([]), default=None),
                 notifications_stream_id=REQ(validator=check_int, default=None),
                 message_retention_days=REQ(converter=to_not_negative_int_or_none, default=None)):
    # type: (HttpRequest, UserProfile, Optional[str], Optional[str], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[int], Optional[bool], Optional[str], Optional[int], Optional[Dict[Any,Any]], Optional[int], Optional[int]) -> HttpResponse
    realm = user_profile.realm

    # Additional validation/error checking beyond types go here, so
    # the entire request can succeed or fail atomically.
    if default_language is not None and default_language not in get_available_language_codes():
        raise JsonableError(_("Invalid language '%s'" % (default_language,)))
    if description is not None and len(description) > 1000:
        return json_error(_("Realm description is too long."))
    if name is not None and len(name) > Realm.MAX_REALM_NAME_LENGTH:
        return json_error(_("Realm name is too long."))
    if authentication_methods is not None and True not in list(authentication_methods.values()):
        return json_error(_("At least one authentication method must be enabled."))

    # The user of `locals()` here is a bit of a code smell, but it's
    # restricted to the elements present in realm.property_types.
    #
    # TODO: It should be possible to deduplicate this function up
    # further by some more advanced usage of the
    # `REQ/has_request_variables` extraction.
    req_vars = {k: v for k, v in list(locals().items()) if k in realm.property_types}
    data = {}  # type: Dict[str, Any]

    for k, v in list(req_vars.items()):
        if v is not None and getattr(realm, k) != v:
            do_set_realm_property(realm, k, v)
            if isinstance(v, Text):
                data[k] = 'updated'
            else:
                data[k] = v

    # The following realm properties do not fit the pattern above
    # authentication_methods is not supported by the do_set_realm_property
    # framework because of its bitfield.
    if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
        do_set_realm_authentication_methods(realm, authentication_methods)
        data['authentication_methods'] = authentication_methods
    # The message_editing settings are coupled to each other, and thus don't fit
    # into the do_set_realm_property framework.
    if (allow_message_editing is not None and realm.allow_message_editing != allow_message_editing) or \
       (message_content_edit_limit_seconds is not None and
            realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds):
        if allow_message_editing is None:
            allow_message_editing = realm.allow_message_editing
        if message_content_edit_limit_seconds is None:
            message_content_edit_limit_seconds = realm.message_content_edit_limit_seconds
        do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds)
        data['allow_message_editing'] = allow_message_editing
        data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
    # Realm.notifications_stream is not a boolean, Text or integer field, and thus doesn't fit
    # into the do_set_realm_property framework.
    if notifications_stream_id is not None:
        if realm.notifications_stream is None or realm.notifications_stream.id != notifications_stream_id:
            new_notifications_stream = None
            if notifications_stream_id >= 0:
                (new_notifications_stream, recipient, sub) = access_stream_by_id(user_profile, notifications_stream_id)
            do_set_realm_notifications_stream(realm, new_notifications_stream, notifications_stream_id)
            data['notifications_stream_id'] = notifications_stream_id

    return json_success(data)
<|endoftext|>


<commit_before># coding: utf-8

#
# Copyright © 2017 weirdgiraffe <giraffe@cyberzoo.xyz>
#
# Distributed under terms of the MIT license.
#
import os
import pytest
import re
import requests
import seasonvar.parser as parser
from seasonvar.requester import Requester
from seasonvar import day_items, season_info, episodes
from datetime import datetime

@pytest.mark.online
def test_parse_main_page_dayblocks():
    req = Requester()
    main_page_html = req.main_page()
    dates = []
    for d, content in parser._main_page_dayblocks(main_page_html):
        dates.append(d)
        assert len(content) > 0
    assert len(dates) > 0

@pytest.mark.online
def test_parse_main_page_items_online():
    date = datetime.today()
    datestr = date.strftime('%d.%m.%Y')
    changes = list(day_items(datestr))
    assert len(changes) > 0
    for c in changes:
        assert 'url' in c
        assert c['url'] != ''
        assert 'name' in c
        assert c['name'] != ''
        assert 'changes' in c
        assert c['changes'] != ''

@pytest.mark.online
def test_parse_playlists_online():
    date = datetime.today()
    datestr = date.strftime('%d.%m.%Y')
    changes = list(day_items(datestr))
    assert len(changes) > 0
    c = changes[0]
    info = season_info(c['url'])
    assert info
    pl = list(info['playlist'])
    assert len(pl) > 0

@pytest.mark.online
@pytest.mark.parametrize('term, min_suggestions', [
    ('bone', 1),
    ('привет', 1),
    ('hhhhhhhhhhhhhhhhhhhпривет', 0),
])
def test_pase_latin_search_items_online(term, min_suggestions):
    req = Requester()
    search_response = req.search(term)
    suggestions = list(parser.search_items(search_response))
    assert len(suggestions) >= min_suggestions
    for i in suggestions:
        assert i['url'] is not None

@pytest.mark.skipif(os.getenv('TRAVIS', 'false') == 'true',
                    reason='TRAVIS could not access this CDN')
@pytest.mark.online
def test_play_online_episodes():
    info = season_info('/serial-13945-Horoshee_mesto.html')
    assert info is not None
    assert 'playlist' in info
    assert len(info['playlist']) > 0
    assert 'url' in info['playlist'][0]
    el = list(episodes(info['playlist'][0]['url']))
    assert len(el) > 0
    for e in el:
        assert re.match(r'.*\.(m3u8|mp4)$', e['url'])
        assert len(e['name']) != 0
        res = requests.head(e['url'])
        assert res.status_code == 200
<commit_msg>refactor test for online playlists<commit_after># coding: utf-8

#
# Copyright © 2017 weirdgiraffe <giraffe@cyberzoo.xyz>
#
# Distributed under terms of the MIT license.
#
import os
import pytest
import re
import requests
import seasonvar.parser as parser
from seasonvar.requester import Requester
from seasonvar import day_items, season_info, episodes
from datetime import datetime

@pytest.mark.online
def test_parse_main_page_dayblocks():
    req = Requester()
    main_page_html = req.main_page()
    dates = []
    for d, content in parser._main_page_dayblocks(main_page_html):
        dates.append(d)
        assert len(content) > 0
    assert len(dates) > 0

@pytest.mark.online
def test_parse_main_page_items_online():
    date = datetime.today()
    datestr = date.strftime('%d.%m.%Y')
    changes = list(day_items(datestr))
    assert len(changes) > 0
    for c in changes:
        assert 'url' in c
        assert c['url'] != ''
        assert 'name' in c
        assert c['name'] != ''
        assert 'changes' in c
        assert c['changes'] != ''

@pytest.mark.online
def test_parse_playlists_online():
    date = datetime.today()
    datestr = date.strftime('%d.%m.%Y')
    changes = list(day_items(datestr))
    assert len(changes) > 0
    info_found = 0
    pl_found = 0
    for c in changes[:3]:
        info = season_info(c['url'])
        if info:
            info_found += 1
            pl = list(info['playlist'])
            if len(pl) > 0:
                pl_found += 1
    assert info_found > 0
    assert pl_found > 0

@pytest.mark.online
@pytest.mark.parametrize('term, min_suggestions', [
    ('bone', 1),
    ('привет', 1),
    ('hhhhhhhhhhhhhhhhhhhпривет', 0),
])
def test_pase_latin_search_items_online(term, min_suggestions):
    req = Requester()
    search_response = req.search(term)
    suggestions = list(parser.search_items(search_response))
    assert len(suggestions) >= min_suggestions
    for i in suggestions:
        assert i['url'] is not None

@pytest.mark.skipif(os.getenv('TRAVIS', 'false') == 'true',
                    reason='TRAVIS could not access this CDN')
@pytest.mark.online
def test_play_online_episodes():
    info = season_info('/serial-13945-Horoshee_mesto.html')
    assert info is not None
    assert 'playlist' in info
    assert len(info['playlist']) > 0
    assert 'url' in info['playlist'][0]
    el = list(episodes(info['playlist'][0]['url']))
    assert len(el) > 0
    for e in el:
        assert re.match(r'.*\.(m3u8|mp4)$', e['url'])
        assert len(e['name']) != 0
        res = requests.head(e['url'])
        assert res.status_code == 200
<|endoftext|>


<commit_before>import csv
from datetime import datetime

from django.contrib import admin
from django.contrib.admin import util
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _

from . import app_settings
from .models import Project, ProjectAdmin, Pledge
from .utils import get_backer_model, use_default_backer_model

from .paypal.models import Preapproval, Payment

def export_as_csv(modeladmin, request, queryset):
    model = modeladmin.model
    response = HttpResponse(mimetype='text/csv')
    model_name = force_unicode(model._meta.verbose_name)
    timestamp = datetime.now().strftime('%d%m%y_%H%M')
    filename = '%s_export_%s.csv' % (model_name, timestamp)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    writer = csv.writer(response)
    field_names = modeladmin.list_display

    def get_label(field):
        label = util.label_for_field(field, model, modeladmin)
        return force_unicode(label).title().encode('utf-8')

    def serialize(field, obj):
        f, attr, value = util.lookup_field(field, obj, modeladmin)
        if f is not None:
            value = util.display_for_field(value, f)
        return force_unicode(value).encode('utf-8')

    writer.writerow([get_label(field) for field in field_names])

    for obj in queryset:
        writer.writerow([serialize(field, obj) for field in field_names])

    return response

export_as_csv.short_description = _('Export as csv')

class PledgeInlineAdmin(admin.TabularInline):
    model = Pledge
    extra = 0
    raw_id_fields = ('backer','project')
    feincms_inline = True

class DefaultBackerAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('_first_name', '_last_name', '_email', 'user__username', 'user__email')
    raw_id_fields = ['user']
    inlines = [PledgeInlineAdmin]
    actions = [export_as_csv]

if use_default_backer_model():
    BackerModel = get_backer_model()
    admin.site.register(BackerModel, DefaultBackerAdmin)

class RewardListFilter(admin.SimpleListFilter):

    title = _('Reward')
    parameter_name = 'reward'

    def lookups(self, request, model_admin):
        project_id = request.GET.get('project__id__exact', None)
        if  project_id:
            project = get_object_or_404(Project, pk=project_id)
            for reward in project.rewards.all():
                name = '{0} {1}'.format(reward.minimum, project.currency)
                yield (str(reward.pk), name)

    def queryset(self, request, queryset):
        value = self.value()

        if value:
            return queryset.filter(reward__id=value)

class PaypalFilter(admin.SimpleListFilter):

    title = _('Paypal status')
    parameter_name = 'paypal'

    def lookups(self, request, model_admin):
        return (
            ('approved', _('is approved')),
            ('paid', _('is paid'))
        )

    def queryset(self, request, queryset):
        value = self.value()

        if value == 'approved':
            approved = Preapproval.objects.filter(approved=True)
            approved_ids = approved.values_list('pledge__pk')
            return queryset.filter(pk__in=approved_ids)
        if value == 'paid':
            paid = Payment.objects.filter(status=Payment.COMPLETED)
            paid_ids = paid.values_list('preapproval__pledge__pk')
            return queryset.filter(pk__in=paid_ids)

class PledgeAdmin(admin.ModelAdmin):

    def username(self, pledge):
        if pledge.backer and pledge.backer.user:
            return pledge.backer.user.username
        else:
            return None
    username.short_description = _('username')

    def first_name(self, pledge):
        return pledge.backer.first_name
    first_name.short_description = _('first name')

    def last_name(self, pledge):
        return pledge.backer.last_name
    last_name.short_description = _('last name')

    def email(self, pledge):
        return pledge.backer.email
    email.short_description = _('email')

    def amount_display(self, pledge):
        return '%s %s' % (pledge.amount, pledge.currency)
    amount_display.short_description = _('amount')

    list_display = (
        'username',
        'email',
        'first_name',
        'last_name',
        'amount_display',
        'reward',
        'status'
    )

    list_display_links = (
        'username',
        'email',
    )

    search_fields = (
        'backer___first_name',
        'backer___last_name',
        'backer___email',
        'backer__user__username',
        'backer__user__first_name',
        'backer__user__last_name',
        'backer__user__email',
    )

    raw_id_fields = ('backer', 'project')
    list_filter = ('project', 'status', PaypalFilter, RewardListFilter)
    actions = [export_as_csv]

admin.site.register(Project, ProjectAdmin)
admin.site.register(Pledge, PledgeAdmin)<commit_msg>dont crash on empty backer<commit_after>import csv
from datetime import datetime

from django.contrib import admin
from django.contrib.admin import util
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _

from . import app_settings
from .models import Project, ProjectAdmin, Pledge
from .utils import get_backer_model, use_default_backer_model

from .paypal.models import Preapproval, Payment

def export_as_csv(modeladmin, request, queryset):
    model = modeladmin.model
    response = HttpResponse(mimetype='text/csv')
    model_name = force_unicode(model._meta.verbose_name)
    timestamp = datetime.now().strftime('%d%m%y_%H%M')
    filename = '%s_export_%s.csv' % (model_name, timestamp)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    writer = csv.writer(response)
    field_names = modeladmin.list_display

    def get_label(field):
        label = util.label_for_field(field, model, modeladmin)
        return force_unicode(label).title().encode('utf-8')

    def serialize(field, obj):
        f, attr, value = util.lookup_field(field, obj, modeladmin)
        if f is not None:
            value = util.display_for_field(value, f)
        return force_unicode(value).encode('utf-8')

    writer.writerow([get_label(field) for field in field_names])

    for obj in queryset:
        writer.writerow([serialize(field, obj) for field in field_names])

    return response

export_as_csv.short_description = _('Export as csv')

class PledgeInlineAdmin(admin.TabularInline):
    model = Pledge
    extra = 0
    raw_id_fields = ('backer','project')
    feincms_inline = True

class DefaultBackerAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('_first_name', '_last_name', '_email', 'user__username', 'user__email')
    raw_id_fields = ['user']
    inlines = [PledgeInlineAdmin]
    actions = [export_as_csv]

if use_default_backer_model():
    BackerModel = get_backer_model()
    admin.site.register(BackerModel, DefaultBackerAdmin)

class RewardListFilter(admin.SimpleListFilter):

    title = _('Reward')
    parameter_name = 'reward'

    def lookups(self, request, model_admin):
        project_id = request.GET.get('project__id__exact', None)
        if  project_id:
            project = get_object_or_404(Project, pk=project_id)
            for reward in project.rewards.all():
                name = '{0} {1}'.format(reward.minimum, project.currency)
                yield (str(reward.pk), name)

    def queryset(self, request, queryset):
        value = self.value()

        if value:
            return queryset.filter(reward__id=value)

class PaypalFilter(admin.SimpleListFilter):

    title = _('Paypal status')
    parameter_name = 'paypal'

    def lookups(self, request, model_admin):
        return (
            ('approved', _('is approved')),
            ('paid', _('is paid'))
        )

    def queryset(self, request, queryset):
        value = self.value()

        if value == 'approved':
            approved = Preapproval.objects.filter(approved=True)
            approved_ids = approved.values_list('pledge__pk')
            return queryset.filter(pk__in=approved_ids)
        if value == 'paid':
            paid = Payment.objects.filter(status=Payment.COMPLETED)
            paid_ids = paid.values_list('preapproval__pledge__pk')
            return queryset.filter(pk__in=paid_ids)

class PledgeAdmin(admin.ModelAdmin):

    def username(self, pledge):
        if pledge.backer and pledge.backer.user:
            return pledge.backer.user.username
        else:
            return _('(None)')
    username.short_description = _('username')

    def first_name(self, pledge):
        if pledge.backer:
            return pledge.backer.first_name
        else:
            return _('(None)')
    first_name.short_description = _('first name')

    def last_name(self, pledge):
        if pledge.backer:
            return pledge.backer.last_name
        else:
            return _('(None)')
    last_name.short_description = _('last name')

    def email(self, pledge):
        if pledge.backer:
            return pledge.backer.email
        else:
            return _('(None)')
    email.short_description = _('email')

    def amount_display(self, pledge):
        return '%s %s' % (pledge.amount, pledge.currency)
    amount_display.short_description = _('amount')

    list_display = (
        'username',
        'email',
        'first_name',
        'last_name',
        'amount_display',
        'reward',
        'status'
    )

    list_display_links = (
        'username',
        'email',
    )

    search_fields = (
        'backer___first_name',
        'backer___last_name',
        'backer___email',
        'backer__user__username',
        'backer__user__first_name',
        'backer__user__last_name',
        'backer__user__email',
    )

    raw_id_fields = ('backer', 'project')
    list_filter = ('project', 'status', PaypalFilter, RewardListFilter)
    actions = [export_as_csv]

admin.site.register(Project, ProjectAdmin)
admin.site.register(Pledge, PledgeAdmin)<|endoftext|>


<commit_before>
from django import forms
from django.utils.encoding import smart_unicode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.template.loader import render_to_string

from .models import Project, Pledge, Reward
from .utils import get_backer_model, format_html
from .widgets import BootstrapRadioSelect
from .app_settings import ALLOW_ANONYMOUS_PLEDGES

class BackProjectForm(forms.ModelForm):

    amount = forms.IntegerField(label=_('amount'))

    class Meta:
        model = Pledge
        exclude = ('backer', 'status') if ALLOW_ANONYMOUS_PLEDGES else \
                  ('backer', 'status', 'anonymously')
        widgets = {
            'project': forms.widgets.HiddenInput,
            'reward': BootstrapRadioSelect,
        }

    def __init__(self, *args, **kwargs):
        self.project = kwargs.pop('project')

        initial = kwargs.get('initial', {})
        initial.update({'project': self.project})

        if 'instance' in kwargs:
            initial['amount'] = int(kwargs['instance'].amount)

        kwargs['initial'] = initial

        super(BackProjectForm, self).__init__(*args, **kwargs)

        self.fields['reward'].queryset = self.project.rewards.all()
        self.fields['reward'].label = _('reward')
        self.fields['reward'].empty_label = _('No reward')
        self.fields['reward'].label_from_instance = self.label_for_reward

        #print self.fields['amount'].__dict__

    def label_for_reward(self, reward):
        return render_to_string('zipfelchappe/reward_option.html', {
            'reward': reward,
            'project': self.project
        })

    def clean(self):
        cleaned_data = super(BackProjectForm, self).clean()

        amount = cleaned_data.get('amount')
        reward = cleaned_data.get('reward')

        if reward and amount and reward.minimum > amount:
            raise forms.ValidationError(_('Amount is too small for reward!'))

        if reward and not reward.is_available:
            raise forms.ValidationError(
                _('Sorry, this reward is not available anymore.')
            )

        return cleaned_data

    class Media:
        js = (
            "zipfelchappe/js/loading_wall.js",
            "zipfelchappe/js/project_back_form.js",
        )

class AuthenticatedBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user', '_first_name', '_last_name', '_email')

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class RegisterUserForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'username')

    def __init__(self, *args, **kwargs):
        super(RegisterUserForm, self).__init__(*args, **kwargs)
        for field in ('first_name', 'last_name', 'email'):
            self.fields[field].required=True

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class RegisterBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user', '_first_name', '_last_name', '_email')

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class UserlessBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user')

    def __init__(self, *args, **kwargs):
        super(UserlessBackerForm, self).__init__(*args, **kwargs)
        for field in ('_first_name', '_last_name', '_email'):
            self.fields[field].required=True

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)
<commit_msg>more form validation<commit_after>
from django import forms
from django.utils.encoding import smart_unicode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.template.loader import render_to_string

from .models import Project, Pledge, Reward
from .utils import get_backer_model, format_html
from .widgets import BootstrapRadioSelect
from .app_settings import ALLOW_ANONYMOUS_PLEDGES

class BackProjectForm(forms.ModelForm):

    amount = forms.IntegerField(label=_('amount'),
        widget=forms.widgets.TextInput(attrs={'maxlength':'4'}))

    class Meta:
        model = Pledge
        exclude = ('backer', 'status') if ALLOW_ANONYMOUS_PLEDGES else \
                  ('backer', 'status', 'anonymously')
        widgets = {
            'project': forms.widgets.HiddenInput,
            'reward': BootstrapRadioSelect,
        }

    def __init__(self, *args, **kwargs):
        self.project = kwargs.pop('project')

        initial = kwargs.get('initial', {})
        initial.update({'project': self.project})

        if 'instance' in kwargs:
            initial['amount'] = int(kwargs['instance'].amount)

        kwargs['initial'] = initial

        super(BackProjectForm, self).__init__(*args, **kwargs)

        self.fields['reward'].queryset = self.project.rewards.all()
        self.fields['reward'].label = _('reward')
        self.fields['reward'].empty_label = _('No reward')
        self.fields['reward'].label_from_instance = self.label_for_reward

    def label_for_reward(self, reward):
        return render_to_string('zipfelchappe/reward_option.html', {
            'reward': reward,
            'project': self.project
        })

    def clean_amount(self):
        amount = self.cleaned_data['amount']

        if amount < 5:
            raise forms.ValidationError(_('Please, just 5 bucks!'))

        if amount > 2000:
            raise forms.ValidationError(_('Sorry, 2000 is the maximal amount'))

        return amount

    def clean(self):
        cleaned_data = super(BackProjectForm, self).clean()

        amount = cleaned_data.get('amount')
        reward = cleaned_data.get('reward')

        if reward and amount and reward.minimum > amount:
            raise forms.ValidationError(_('Amount is too small for reward!'))

        if reward and not reward.is_available:
            raise forms.ValidationError(
                _('Sorry, this reward is not available anymore.')
            )

        return cleaned_data

    class Media:
        js = (
            "zipfelchappe/js/loading_wall.js",
            "zipfelchappe/js/project_back_form.js",
        )

class AuthenticatedBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user', '_first_name', '_last_name', '_email')

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class RegisterUserForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'username')

    def __init__(self, *args, **kwargs):
        super(RegisterUserForm, self).__init__(*args, **kwargs)
        for field in ('first_name', 'last_name', 'email'):
            self.fields[field].required=True

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class RegisterBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user', '_first_name', '_last_name', '_email')

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)

class UserlessBackerForm(forms.ModelForm):

    class Meta:
        model = get_backer_model()
        exclude = ('user')

    def __init__(self, *args, **kwargs):
        super(UserlessBackerForm, self).__init__(*args, **kwargs)
        for field in ('_first_name', '_last_name', '_email'):
            self.fields[field].required=True

    class Media:
        js = ("zipfelchappe/js/loading_wall.js",)
<|endoftext|>


<commit_before>import events
import requests
from errors import PanoplyException
import traceback

# abstract DataSource object
class DataSource(events.Emitter):

    source = None
    options = None

    # data source constructor
    def __init__(self, source, options = {}, events = {}):
        super(DataSource, self).__init__(events)

        if 'destination' not in source:
            source['destination'] = source['type']

        self.source = source
        self.options = options

    # log a message
    def log(self, *msgs):
        if "logger" in self.options:
            self.options["logger"](msgs)
        else:
            print(msgs)

    def progress(self, loaded, total, msg=''):
        self.fire("progress", {
            "loaded": loaded,
            "total": total,
            "msg": msg
        })

# a decorator used to invalidate the access_token for oauth based data sources
# this should be used on every method in the data source that fetches data
# from the server (controlled by this oauth), and that needs to be invalidated
def invalidate_token(refresh_url, callback=None,
                     keys=['access_token', 'refresh_token']):
    def _invalidate_token(f):
        def wrapper(*args):
            self = args[0]
            try:
                return f(*args)
            except:
                try:
                    self.log("Reinvalidating access_token...")
                    self.source[keys[0]] = None

                    # get a new token from refresh_url
                    token = self.source.get(keys[1])
                    r = requests.post(
                        refresh_url,
                        data = dict(self.options['refresh'], **{keys[1]:token}))
                    self.source[keys[0]] = r.json()[keys[0]]

                    # save the new token in the database
                    changes = {keys[0]: self.source[keys[0]]}
                    self.fire("source-change", changes)

                    # notify the callback that a new token was issued
                    if callback:
                        if callable(callback):
                            _callback = callback
                        else:
                            _callback = getattr(self, callback)
                        _callback(self.source.get(keys[0]))

                    return f(*args)
                except:
                    # make sure the real exception is logged
                    self.log(traceback.format_exc())
                    self.log("Error: Access token can't be invalidated." +
                          " The user would have to re-authenticate")
                    # raise a non-retryable exception
                    raise PanoplyException(
                        'access token could not be refreshed',
                        retryable=False)
        return wrapper
    return _invalidate_token
<commit_msg>fix - source tests might fail if "type" was not defined for the source<commit_after>import events
import requests
from errors import PanoplyException
import traceback

# abstract DataSource object
class DataSource(events.Emitter):

    source = None
    options = None

    # data source constructor
    def __init__(self, source, options = {}, events = {}):
        super(DataSource, self).__init__(events)

        if 'destination' not in source:
            source['destination'] = source.get('type')

        self.source = source
        self.options = options

    # log a message
    def log(self, *msgs):
        if "logger" in self.options:
            self.options["logger"](msgs)
        else:
            print(msgs)

    def progress(self, loaded, total, msg=''):
        self.fire("progress", {
            "loaded": loaded,
            "total": total,
            "msg": msg
        })

# a decorator used to invalidate the access_token for oauth based data sources
# this should be used on every method in the data source that fetches data
# from the server (controlled by this oauth), and that needs to be invalidated
def invalidate_token(refresh_url, callback=None,
                     keys=['access_token', 'refresh_token']):
    def _invalidate_token(f):
        def wrapper(*args):
            self = args[0]
            try:
                return f(*args)
            except:
                try:
                    self.log("Reinvalidating access_token...")
                    self.source[keys[0]] = None

                    # get a new token from refresh_url
                    token = self.source.get(keys[1])
                    r = requests.post(
                        refresh_url,
                        data = dict(self.options['refresh'], **{keys[1]:token}))
                    self.source[keys[0]] = r.json()[keys[0]]

                    # save the new token in the database
                    changes = {keys[0]: self.source[keys[0]]}
                    self.fire("source-change", changes)

                    # notify the callback that a new token was issued
                    if callback:
                        if callable(callback):
                            _callback = callback
                        else:
                            _callback = getattr(self, callback)
                        _callback(self.source.get(keys[0]))

                    return f(*args)
                except:
                    # make sure the real exception is logged
                    self.log(traceback.format_exc())
                    self.log("Error: Access token can't be invalidated." +
                          " The user would have to re-authenticate")
                    # raise a non-retryable exception
                    raise PanoplyException(
                        'access token could not be refreshed',
                        retryable=False)
        return wrapper
    return _invalidate_token
<|endoftext|>


<commit_before>from __future__ import print_function

import os

import yaml

config = yaml.safe_load(r'''tempdir: C:\judgetemp
runtime:
  awk: C:\Program Files\Git\usr\bin\awk.exe
  csc: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
  cl.exe: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe
  go: C:\go\bin\go.exe
  java: C:\Program Files (x86)\Java\jre7\bin\java.exe
  javac: C:\Program Files (x86)\Java\jdk1.7.0\bin\javac.exe
  java8: C:\Program Files (x86)\Java\jre8\bin\java.exe
  javac8: C:\Program Files (x86)\Java\jdk1.8.0\bin\javac.exe
  perl: C:\Perl\bin\perl.exe
  python: C:\Python27\Python.exe
  python3: C:\Python35\Python.exe
  ruby19: C:\Ruby193\bin\ruby.exe
  ruby2: C:\Ruby21\bin\ruby.exe
  sed: C:\Program Files\Git\usr\bin\sed.exe
  vbc: C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe

  go_env:
    PATH: C:\go\bin
''')

runtime = config['runtime']

try:
    gcc_bin = next(dir for dir, dirs, files in os.walk(r'C:\mingw-w64') if 'gcc.exe' in files)
except StopIteration:
    pass
else:
    runtime['gcc_env'] = {'PATH': gcc_bin}
    runtime['gcc'] = os.path.join(gcc_bin, 'gcc.exe')
    runtime['g++'] = runtime['g++11'] = os.path.join(gcc_bin, 'g++.exe')
    runtime['gfortran'] = os.path.join(gcc_bin, 'gfortran.exe')

print(yaml.safe_dump(config))
<commit_msg>No longer test Java 7 on Appveyor; #287<commit_after>from __future__ import print_function

import os

import yaml

config = yaml.safe_load(r'''tempdir: C:\judgetemp
runtime:
  awk: C:\Program Files\Git\usr\bin\awk.exe
  csc: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
  cl.exe: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe
  go: C:\go\bin\go.exe
  java8: C:\Program Files (x86)\Java\jre8\bin\java.exe
  javac8: C:\Program Files (x86)\Java\jdk1.8.0\bin\javac.exe
  perl: C:\Perl\bin\perl.exe
  python: C:\Python27\Python.exe
  python3: C:\Python35\Python.exe
  ruby19: C:\Ruby193\bin\ruby.exe
  ruby2: C:\Ruby21\bin\ruby.exe
  sed: C:\Program Files\Git\usr\bin\sed.exe
  vbc: C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe

  go_env:
    PATH: C:\go\bin
''')

runtime = config['runtime']

try:
    gcc_bin = next(dir for dir, dirs, files in os.walk(r'C:\mingw-w64') if 'gcc.exe' in files)
except StopIteration:
    pass
else:
    runtime['gcc_env'] = {'PATH': gcc_bin}
    runtime['gcc'] = os.path.join(gcc_bin, 'gcc.exe')
    runtime['g++'] = runtime['g++11'] = os.path.join(gcc_bin, 'g++.exe')
    runtime['gfortran'] = os.path.join(gcc_bin, 'gfortran.exe')

print(yaml.safe_dump(config))
<|endoftext|>


<commit_before>from unitinspector	import unitinspector
from netcdfcopier 	import netcdfcopier
from netCDF4 import *

class standardizer(object):

	def __init__(self, srcpath, dstpath, file, mode='a'):

		self.__srcpath = srcpath
		self.__dstpath = dstpath
		self.__file = file
		self.__mode = mode
		self.__rgrp = None

		self.__blacklist = []

		self.__oldlat   = None # for rollback reasons
		self.__oldlon   = None # for rollback reasons
		self.__oldtime  = None # for rollback reasons
		self.__oldlev   = None # for rollback reasons

#	open the netcdf file
	def __netcdfOpen(self,path,file,mode):

		try:
			self.__rgrp = Dataset(path+file,mode)
		except:
			print "File couldn't be opened"

# rollback the changes to the dimensions' names
	def __rollback(self):

		if self.__oldlat is not None:
			self.__rgrp.renameDimension('lat',self.__oldlat)
			self.__rgrp.renameVariable('lat',self.__oldlat)

		if self.__oldlon is not None:
			self.__rgrp.renameDimension('lon',self.__oldlon)
			self.__rgrp.renameVariable('lon',self.__oldlon)

		if self.__oldtime is not None:
			self.__rgrp.renameDimension('time',self.__oldtime)
			self.__rgrp.renameVariable('time',self.__oldtime)

		if self.__oldlev is not None:
			self.__rgrp.renameDimension('level',self.__oldlevel)
			self.__rgrp.renameVariable('level',self.__oldlevel)

#	main function
	def createNetcdf(self):

		ncp  = netcdfcopier()

		self.__netcdfOpen(self.__srcpath,self.__file,self.__mode)

		geo = self.__checkUnits()
		if geo:
			# if there is any variable left
			if [v for v in self.__rgrp.variables.values() if not set(v.dimensions).intersection(self.__blacklist)]:
				srcpath = self.__srcpath+self.__file
				dstpath = self.__dstpath+self.__file
				dst = ncp.copy(srcpath,dstpath,dbl=self.__blacklist)

		self.__rollback()

		return dst

#	set the convention for the dimentions involved
	def __checkUnits(self):

		lat = False
		lon = False

		ui = unitinspector()

		for dimension in  self.__rgrp.dimensions.values():

			#if the dimension is present as variable as well (which it should)
			if ( dimension.name in [i.name for i in self.__rgrp.variables.values()]):

				#try to load the correspondent variable
				try:
					var = self.__rgrp.get_variables_by_attributes(name = dimension.name)[0]

				except IndexError:
					pass

				# otherwise change the name of the dimension and of the associated variable
				else:

					if ui.verifyLat(var):

						lat = True

						if var.name != 'lat':
							self.__oldlat = var.name
							self.__rgrp.renameDimension(var.name,'lat')
							self.__rgrp.renameVariable(var.name,'lat')

					elif ui.verifyLon(var):

						lon = True

						if var.name != 'lon':
							self.__oldlon = var.name
							self.rgrp.renameDimension(var.name,'lon')
							self.rgrp.renameVariable(var.name,'lon')

					elif ui.verifyTime(var):

						if var.name != 'time':
							self.__oldtime = var.name
							self.rgrp.renameDimension(var.name,'time')
							self.rgrp.renameVariable(var.name,'time')

					elif ui.verifyLevel(var):

						if var.name != 'level':
							self.__oldlev = var.name
							self.rgrp.renameDimension(var.name,'level')
							self.rgrp.renameVariable(var.name,'level')

					else:
						self.__blacklist.append(var.name)	# if the dimension is not among lat/lon/time/lev
			else:
				self.__blacklist.append(dimension.name)			# if the dimension is has not a variable describing it

		return lat and lon

if __name__ == '__main__':

	std = standardizer("../../rawnc/", "../../testednc/", "test_echam_spectral.nc")
	std.start()<commit_msg>refactoring<commit_after>from unitinspector	import UnitInspector
from netcdfcopier 	import NetcdfCopier
from netCDF4 import *

class standardizer(object):

	def __init__(self, srcpath, dstpath, file, mode='a'):

		self.__srcpath = srcpath
		self.__dstpath = dstpath
		self.__file = file
		self.__mode = mode
		self.__rgrp = None

		self.__blacklist = []

		self.__oldlat   = None # for rollback reasons
		self.__oldlon   = None # for rollback reasons
		self.__oldtime  = None # for rollback reasons
		self.__oldlev   = None # for rollback reasons

#	open the netcdf file
	def __netcdfOpen(self,path,file,mode):

		try:
			self.__rgrp = Dataset(path+file,mode)
		except:
			print "File couldn't be opened"

# rollback the changes to the dimensions' names
	def __rollback(self):

		if self.__oldlat is not None:
			self.__rgrp.renameDimension('lat',self.__oldlat)
			self.__rgrp.renameVariable('lat',self.__oldlat)

		if self.__oldlon is not None:
			self.__rgrp.renameDimension('lon',self.__oldlon)
			self.__rgrp.renameVariable('lon',self.__oldlon)

		if self.__oldtime is not None:
			self.__rgrp.renameDimension('time',self.__oldtime)
			self.__rgrp.renameVariable('time',self.__oldtime)

		if self.__oldlev is not None:
			self.__rgrp.renameDimension('level',self.__oldlevel)
			self.__rgrp.renameVariable('level',self.__oldlevel)

#	main function
	def createNetcdf(self):

		ncp  = netcdfcopier()

		self.__netcdfOpen(self.__srcpath,self.__file,self.__mode)

		geo = self.__checkUnits()
		if geo:
			# if there is any variable left
			if [v for v in self.__rgrp.variables.values() if not set(v.dimensions).intersection(self.__blacklist)]:
				srcpath = self.__srcpath+self.__file
				dstpath = self.__dstpath+self.__file
				dst = ncp.copy(srcpath,dstpath,dbl=self.__blacklist)

		self.__rollback()

		return dst

#	set the convention for the dimentions involved
	def __checkUnits(self):

		lat = False
		lon = False

		ui = unitinspector()

		for dimension in  self.__rgrp.dimensions.values():

			#if the dimension is present as variable as well (which it should)
			if ( dimension.name in [i.name for i in self.__rgrp.variables.values()]):

				#try to load the correspondent variable
				try:
					var = self.__rgrp.get_variables_by_attributes(name = dimension.name)[0]

				except IndexError:
					pass

				# otherwise change the name of the dimension and of the associated variable
				else:

					if ui.verifyLat(var):

						lat = True

						if var.name != 'lat':
							self.__oldlat = var.name
							self.__rgrp.renameDimension(var.name,'lat')
							self.__rgrp.renameVariable(var.name,'lat')

					elif ui.verifyLon(var):

						lon = True

						if var.name != 'lon':
							self.__oldlon = var.name
							self.rgrp.renameDimension(var.name,'lon')
							self.rgrp.renameVariable(var.name,'lon')

					elif ui.verifyTime(var):

						if var.name != 'time':
							self.__oldtime = var.name
							self.rgrp.renameDimension(var.name,'time')
							self.rgrp.renameVariable(var.name,'time')

					elif ui.verifyLevel(var):

						if var.name != 'level':
							self.__oldlev = var.name
							self.rgrp.renameDimension(var.name,'level')
							self.rgrp.renameVariable(var.name,'level')

					else:
						self.__blacklist.append(var.name)	# if the dimension is not among lat/lon/time/lev
			else:
				self.__blacklist.append(dimension.name)			# if the dimension is has not a variable describing it

		return lat and lon

if __name__ == '__main__':

	std = standardizer("../../rawnc/", "../../testednc/", "test_echam_spectral.nc")
	std.start()<|endoftext|>


<commit_before>from __future__ import division
import numpy as np
from collections import defaultdict
from copy import deepcopy

from util.text import progprint_xrange

class ParallelTempering(object):
    def __init__(self,model,temperatures):
        temperatures = [1.] + list(sorted(temperatures))
        self.models = [deepcopy(model) for T in temperatures]
        for m,T in zip(self.models,temperatures):
            m.temperature = T

        self.swapcounts = defaultdict(int)
        self.itercount = 0

    @property
    def unit_temp_model(self):
        return self.models[0]

    @property
    def temperatures(self):
        return [m.temperature for m in self.models]

    @property
    def energies(self):
        return [m.energy for m in self.models]

    def step(self,intermediate_resamples):
        for m in self.models:
            for itr in xrange(intermediate_resamples):
                m.resample_model()

        triples = zip(self.models,self.energies,self.temperatures)
        for (M1,E1,T1), (M2,E2,T2) in zip(triples[:-1],triples[1:]):
            swap_logprob = min(0., (E1-E2)*(1./T1 - 1./T2))
            if np.log(np.random.random()) < swap_logprob:
                print 'swap at %0.3f (%0.2f,%0.2f,%0.3f, %0.3f)' % (np.exp(swap_logprob),T1,T2,E1,E2)
                M1.swap_sample_with(M2)
                self.swapcounts[(T1,T2)] += 1
            else:
                print 'no swap at delta E = %0.3f' % (E1-E2)

        self.itercount += 1

    def run(self,niter,intermediate_resamples):
        samples = []
        for itr in progprint_xrange(niter):
            self.step(intermediate_resamples)

<commit_msg>add printing and profiling to parallel_tempering<commit_after>from __future__ import division
import numpy as np
from collections import defaultdict
from copy import deepcopy

from util.text import progprint_xrange

from pyhsmm.util.profiling import line_profiled
PROFILING = True
PRINTING = True

class ParallelTempering(object):
    def __init__(self,model,temperatures):
        temperatures = [1.] + list(sorted(temperatures))
        self.models = [deepcopy(model) for T in temperatures]
        for m,T in zip(self.models,temperatures):
            m.temperature = T

        self.swapcounts = defaultdict(int)
        self.itercount = 0

    @property
    def unit_temp_model(self):
        return self.models[0]

    @property
    def temperatures(self):
        return [m.temperature for m in self.models]

    @property
    def energies(self):
        return [m.energy for m in self.models]

    @line_profiled
    def step(self,intermediate_resamples):
        for m in self.models:
            for itr in xrange(intermediate_resamples):
                m.resample_model()

        triples = zip(self.models,self.energies,self.temperatures)
        for (M1,E1,T1), (M2,E2,T2) in zip(triples[:-1],triples[1:]):
            swap_logprob = min(0., (E1-E2)*(1./T1 - 1./T2))
            if np.log(np.random.random()) < swap_logprob:
                M1.swap_sample_with(M2)
                self.swapcounts[(T1,T2)] += 1

                if PRINTING:
                    print 'SWAP at %0.3f%% (Egap = %0.3f)' \
                            % (100*np.exp(swap_logprob),E1-E2)

            elif PRINTING:
                print 'no swap at %0.3f%% (Egap = %0.3f)' \
                        % (100*np.exp(swap_logprob),E1-E2)

        self.itercount += 1

    def run(self,niter,intermediate_resamples):
        samples = []
        for itr in progprint_xrange(niter):
            self.step(intermediate_resamples)

<|endoftext|>


<commit_before>#!/usr/bin/python
# encoding: utf-8
"""

@author: xuk1
@license: (C) Copyright 2013-2017
@contact: kai.a.xu@intel.com
@file: parallelprocessing.py
@time: 9/15/2017 15:43
@desc:

"""
from multiprocessing import Pool
from functools import partial
from component.factory import AttribFactory
import os
from cluster import Cluster
import pandas as pd
from utils.commonOps import get_paths
import time
import sys

def get_node_attrib_data_by_time(attrib, start, end, file_path):
    """
    Get data of a given attribute within a given time period
    :param attrib: input attribute
    :param start: list of start timestamp
    :param end: list of end timestamp, should be the same length of start
    :return: dict that contains avg value and all raw data of all the timestamp pair
    """
    if attrib.lower() in AttribFactory.node_attrib.keys():
        attrib_file = file_path + os.sep + AttribFactory.node_attrib[attrib.lower()]
        if os.path.isfile(attrib_file):
            return AttribFactory.create_attrib(attrib, attrib_file).get_data_by_time(start, end)
        else:
            print 'node does not have attribute {0}'.format(attrib)
            exit(-1)
    else:
        print 'Node does not have attrib: {0} defined, defined attributions are: {1}, will exit...' \
            .format(attrib, AttribFactory.node_attrib.keys())
        exit(-1)

def get_cluster_attrib_data(pat_path, start, end, save_raw, attrib):
    pat_path = pat_path + os.sep + 'instruments'
    if os.path.exists(pat_path):
        nodes = get_paths(pat_path)
    else:
        print 'Path: {0} does not exist, will exit...'.format(pat_path)
        exit(-1)

    pool = Pool(4)
    func = partial(get_node_attrib_data_by_time, attrib, start, end)
    p = pool.map(func, nodes)

    tmp_avg = pd.DataFrame()
    tmp_all = pd.DataFrame()

    for i in range(len(p)):
        tmp_avg = tmp_avg.append(p[i][0])
        if save_raw:
            tmp_all = tmp_all.append(p[i][1])
    if save_raw:
        raw_path = pat_path + os.sep + attrib + '.csv'
        tmp_all.index = pd.to_datetime(tmp_all.index, unit='s')
        tmp_all.to_csv(raw_path, sep=',')

    avg = pd.DataFrame()
    for i in range(len(start)):
        avg = avg.append(tmp_avg.loc[i].mean(axis=0), ignore_index=True)
    return avg

def get_cluster_data_by_time(pat_path, start, end, save_raw):
    cluster = Cluster(pat_path)
    attrib = cluster.attrib
    nodes = cluster.nodes
    cluster_avg = {}

    for attr in attrib:
        cluster_avg[attr] = get_cluster_attrib_data(pat_path, start, end, save_raw, attr)
    # print cluster_avg
    return cluster_avg

if __name__ == '__main__':
    # pat_path = 'C:\\Users\\xuk1\PycharmProjects\\tmp_data\pat_spark163_1TB_r1'
    tic = time.time()
    pat_path = 'C:\\Users\\xuk1\\PycharmProjects\\tmp_data\\pat_cdh511_HoS_27workers_2699v4_72vcores_PCIe_30T_4S_r1'
    # pat_path = sys.argv[1]
    # nodes = Cluster(pat_path).nodes
    # attrib = 'cpu'
    start = [0, 1505148392, 1505148392, 1505148392]
    end = [0, 1505272675, 1505272675, 1505272675]
    # get_cluster_attrib_data(nodes, attrib, start, end)

    get_cluster_data_by_time(pat_path, start, end, False)
    toc = time.time()
    print 'Processing elapsed time: {0}'.format(toc - tic)
<commit_msg>add "1-%idle" to the result<commit_after>#!/usr/bin/python
# encoding: utf-8
"""

@author: xuk1
@license: (C) Copyright 2013-2017
@contact: kai.a.xu@intel.com
@file: parallelprocessing.py
@time: 9/15/2017 15:43
@desc:

"""
from multiprocessing import Pool
from functools import partial
from component.factory import AttribFactory
import os
from cluster import Cluster
import pandas as pd
from utils.commonOps import get_paths
import time
import sys

def get_node_attrib_data_by_time(attrib, start, end, file_path):
    """
    Get data of a given attribute within a given time period
    :param attrib: input attribute
    :param start: list of start timestamp
    :param end: list of end timestamp, should be the same length of start
    :return: dict that contains avg value and all raw data of all the timestamp pair
    """
    if attrib.lower() in AttribFactory.node_attrib.keys():
        attrib_file = file_path + os.sep + AttribFactory.node_attrib[attrib.lower()]
        if os.path.isfile(attrib_file):
            return AttribFactory.create_attrib(attrib, attrib_file).get_data_by_time(start, end)
        else:
            print 'node does not have attribute {0}'.format(attrib)
            exit(-1)
    else:
        print 'Node does not have attrib: {0} defined, defined attributions are: {1}, will exit...' \
            .format(attrib, AttribFactory.node_attrib.keys())
        exit(-1)

def get_cluster_attrib_data(pat_path, start, end, save_raw, attrib):
    pat_path = pat_path + os.sep + 'instruments'
    if os.path.exists(pat_path):
        nodes = get_paths(pat_path)
    else:
        print 'Path: {0} does not exist, will exit...'.format(pat_path)
        exit(-1)

    pool = Pool(4)
    func = partial(get_node_attrib_data_by_time, attrib, start, end)
    p = pool.map(func, nodes)

    tmp_avg = pd.DataFrame()
    tmp_all = pd.DataFrame()

    for i in range(len(p)):
        tmp_avg = tmp_avg.append(p[i][0])
        if save_raw:
            tmp_all = tmp_all.append(p[i][1])
    if save_raw:
        raw_path = pat_path + os.sep + attrib + '.csv'
        tmp_all.index = pd.to_datetime(tmp_all.index, unit='s')
        tmp_all.to_csv(raw_path, sep=',')

    avg = pd.DataFrame()
    for i in range(len(start)):
        avg = avg.append(tmp_avg.loc[i].mean(axis=0), ignore_index=True)
    return avg

def get_cluster_data_by_time(pat_path, start, end, save_raw):
    cluster = Cluster(pat_path)
    attrib = cluster.attrib
    nodes = cluster.nodes
    cluster_avg = {}

    for attr in attrib:
        cluster_avg[attr] = get_cluster_attrib_data(pat_path, start, end, save_raw, attr)
    if 'cpu' in cluster_avg.keys():
        cluster_avg['cpu'].insert(0, '1-%idle', 100-cluster_avg['cpu']['%idle'])  # add column '1-%idle' to the result
    return cluster_avg

if __name__ == '__main__':
    pat_path = 'C:\\Users\\xuk1\PycharmProjects\\tmp_data\pat_spark163_1TB_r1'
    tic = time.time()
    # pat_path = 'C:\\Users\\xuk1\\PycharmProjects\\tmp_data\\pat_cdh511_HoS_27workers_2699v4_72vcores_PCIe_30T_4S_r1'
    # pat_path = sys.argv[1]
    # nodes = Cluster(pat_path).nodes
    # attrib = 'cpu'
    # start = [0, 1505148392, 1505148392, 1505148392]
    # end = [0, 1505272675, 1505272675, 1505272675]
    start = [0, 1487687161, 1487687176]
    end = [0, 1487687170, 1487687185]
    # get_cluster_attrib_data(nodes, attrib, start, end)

    get_cluster_data_by_time(pat_path, start, end, False)
    toc = time.time()
    print 'Processing elapsed time: {0}'.format(toc - tic)
<|endoftext|>


<commit_before>from brian.stdunits import *
from brian.units import *

F = 1
N_SUBPOP = 2
INTERCO_RATE = 0
INTERCO_STRENGTH = 0

PARAMETERS = {
'Common':
    {'simu_dt'    : 0.05*msecond,
    'simu_length' : 2000*msecond,
    'N_subpop'    : N_SUBPOP,
    'N_mitral'    : N_SUBPOP*50*F,
    'inter_conn_rate'     : {0: {1: INTERCO_RATE},
                             1: {0: INTERCO_RATE}},
    'inter_conn_strength' : {0: {1: INTERCO_STRENGTH},
                             1: {0: INTERCO_STRENGTH}}
    },
'Input':
    {'tau_Ein'  : 3*msecond,
    'g_Ein0'    : 1.*siemens*meter**-2,
    'sigma_Ein' : .05*siemens*meter**-2
    },
'InputOscillation':
    {'f'    : 2*Hz,
    'C'    : 1  # Must be set to 1 for oscillation
    },
'Mitral':
    {'C_m'      : 0.08*farad*meter**-2,
    'g_L'       : 0.87*siemens*meter**-2,
    'E_L'       : -64.5*mvolt,
    'V_r'       : -74*mvolt,
    'V_t'       : -62*mvolt,
    't_refract' : 0.2*msecond
    },
'Granule':
    {'C_m' : 0.01*farad*meter**-2,
    'g_L'  : 0.83*siemens*meter**-2,
    'E_L'  : -70*mvolt,
    'g_SD' : 1*siemens*meter**-2,
    'g_DS' : 300*siemens*meter**-2
    },
'Synapse':
    {'V_E'     : 0*mvolt,
    'V_act_E'  : 0*mvolt,
    'g_E'      : 3.5*siemens*meter**-2/F,
    'sigma_E'  : 0.01*mvolt,
    'alpha_E'  : 10*msecond**-1,
    'beta_E'   : 1./3*msecond**-1,

    'V_I'      : -80*mvolt,
    'V_act_I'  : -66.4*mvolt,
    'g_I'      : 10*siemens*meter**-2,
    'sigma_I'  : 0.4*mvolt,
    'alpha_I'  : 5*msecond**-1,
    'beta_I'   : 1./10*msecond**-1
    },
}
<commit_msg>Change default beta parameters<commit_after>from brian.stdunits import *
from brian.units import *

F = 1
N_SUBPOP = 2
INTERCO_RATE = 0
INTERCO_STRENGTH = 0

PARAMETERS = {
'Common':
    {'simu_dt'    : 0.05*msecond,
    'simu_length' : 2000*msecond,
    'N_subpop'    : N_SUBPOP,
    'N_mitral'    : N_SUBPOP*100*F,
    'inter_conn_rate'     : {0: {1: INTERCO_RATE},
                             1: {0: INTERCO_RATE}},
    'inter_conn_strength' : {0: {1: INTERCO_STRENGTH},
                             1: {0: INTERCO_STRENGTH}}
    },
'Input':
    {'tau_Ein'  : 3*msecond,
    'g_Ein0'    : 2.*siemens*meter**-2,
    'sigma_Ein' : .035*siemens*meter**-2
    },
'InputOscillation':
    {'f'    : 2*Hz,
    'C'    : 1  # Must be set to 1 for oscillation
    },
'Mitral':
    {'C_m'      : 0.08*farad*meter**-2,
    'g_L'       : 0.87*siemens*meter**-2,
    'E_L'       : -64.5*mvolt,
    'V_r'       : -74*mvolt,
    'V_t'       : -62*mvolt,
    't_refract' : 0.2*msecond
    },
'Granule':
    {'C_m' : 0.01*farad*meter**-2,
    'g_L'  : 0.83*siemens*meter**-2,
    'E_L'  : -70*mvolt,
    'g_SD' : 1*siemens*meter**-2,
    'g_DS' : 300*siemens*meter**-2
    },
'Synapse':
    {'V_E'     : 0*mvolt,
    'V_act_E'  : 0*mvolt,
    'g_E'      : 3.5*siemens*meter**-2/F,
    'sigma_E'  : 0.01*mvolt,
    'alpha_E'  : 10*msecond**-1,
    'beta_E'   : 1./3*msecond**-1,

    'V_I'      : -80*mvolt,
    'V_act_I'  : -66.4*mvolt,
    'g_I'      : 20*siemens*meter**-2,
    'sigma_I'  : 0.4*mvolt,
    'alpha_I'  : 5*msecond**-1,
    'beta_I'   : 1./10*msecond**-1
    },
}
<|endoftext|>


<commit_before>import re

from django import forms

FIELD_TYPES = [
    (forms.URLField, "url"),
    (forms.EmailField, "email"),
    (forms.IntegerField, "digits"),
    (forms.DecimalField, "number"),
    (forms.FloatField, "number"),
]

FIELD_ATTRS = [
    ("min_length", "minlength"),
    ("max_length", "maxlength"),
    ("min_value", "min"),
    ("max_value", "max"),
]

def update_widget_attrs(field):
    attrs = field.widget.attrs
    if field.required:
        attrs["data-required"] = "true"

        error_message = field.error_messages.get('required', None)
        if error_message:
            attrs["data-required-message"] = error_message

    if isinstance(field, forms.RegexField):
        attrs.update({"data-regexp": field.regex.pattern})

        error_message = field.error_messages.get('invalid', None)
        if error_message:
            attrs["data-regexp-message"] = error_message

        if field.regex.flags & re.IGNORECASE:
            attrs.update({"data-regexp-flag": "i"})
    if isinstance(field, forms.MultiValueField):
        for subfield in field.fields:
            update_widget_attrs(subfield)

    # Set data-* attributes for parsley based on Django field attributes
    for attr, data_attr, in FIELD_ATTRS:
        if getattr(field, attr, None):
            attrs["data-{0}".format(data_attr)] = getattr(field, attr)

            error_message = field.error_messages.get(attr, None)
            if error_message:
                attrs["data-{0}-message".format(data_attr)] = error_message

    # Set data-type attribute based on Django field instance type
    for klass, field_type in FIELD_TYPES:
        if isinstance(field, klass):
            attrs["data-type"] = field_type

            error_message = field.error_messages.get('invalid', None)
            if error_message:
                attrs["data-type-{0}-message".format(field_type)] = error_message

def parsleyfy(klass):
    "A decorator to add data-* attributes to your form.fields"
    old_init = klass.__init__

    def new_init(self, *args, **kwargs):
        old_init(self, *args, **kwargs)
        for _, field in self.fields.items():
            update_widget_attrs(field)
        extras = getattr(getattr(self, 'Meta', None), 'parsley_extras', {})
        for field_name, data in extras.items():
            for key, value in data.items():
                if field_name not in self.fields:
                    continue
                attrs = self.fields[field_name].widget.attrs
                if key == 'equalto':
                    # Use HTML id for data-equalto
                    value = '#' + self[value].id_for_label
                if isinstance(value, bool):
                    value = "true" if value else "false"
                attrs['data-%s' % key] = value
    klass.__init__ = new_init

    try:
        klass.Media.js += ("parsley/js/parsley-standalone.min.js",)
    except AttributeError:
        class Media:
            js = (
                "parsley/js/parsley-standalone.min.js",
            )
        klass.Media = Media

    return klass
<commit_msg>Issue #29 - Parsley 1.2 support.<commit_after>import re

from django import forms

FIELD_TYPES = [
    (forms.URLField, "url"),
    (forms.EmailField, "email"),
    (forms.IntegerField, "digits"),
    (forms.DecimalField, "number"),
    (forms.FloatField, "number"),
]

FIELD_ATTRS = [
    ("min_length", "minlength"),
    ("max_length", "maxlength"),
    ("min_value", "min"),
    ("max_value", "max"),
]

def update_widget_attrs(field):
    attrs = field.widget.attrs
    if field.required:
        attrs["required"] = "required"

        error_message = field.error_messages.get('required', None)
        if error_message:
            attrs["parsley-error-message"] = error_message

    if isinstance(field, forms.RegexField):
        attrs.update({"parsley-regexp": field.regex.pattern})

        error_message = field.error_messages.get('invalid', None)
        if error_message:
            attrs["parsley-error-message"] = error_message

        if field.regex.flags & re.IGNORECASE:
            attrs.update({"data-regexp-flag": "i"})
    if isinstance(field, forms.MultiValueField):
        for subfield in field.fields:
            update_widget_attrs(subfield)

    # Set data-* attributes for parsley based on Django field attributes
    for attr, data_attr, in FIELD_ATTRS:
        if getattr(field, attr, None):
            attrs["parsley-{0}".format(data_attr)] = getattr(field, attr)

            error_message = field.error_messages.get(attr, None)
            if error_message:
                attrs["parsley-{0}-message".format(data_attr)] = error_message

    # Set data-type attribute based on Django field instance type
    for klass, field_type in FIELD_TYPES:
        if isinstance(field, klass):
            attrs["parsley-type"] = field_type

            error_message = field.error_messages.get('invalid', None)
            if error_message:
                attrs["parsley-type-{0}-message".format(field_type)] = error_message

def parsleyfy(klass):
    "A decorator to add data-* attributes to your form.fields"
    old_init = klass.__init__

    def new_init(self, *args, **kwargs):
        old_init(self, *args, **kwargs)
        for _, field in self.fields.items():
            update_widget_attrs(field)
        extras = getattr(getattr(self, 'Meta', None), 'parsley_extras', {})
        for field_name, data in extras.items():
            for key, value in data.items():
                if field_name not in self.fields:
                    continue
                attrs = self.fields[field_name].widget.attrs
                if key == 'equalto':
                    # Use HTML id for data-equalto
                    value = '#' + self[value].id_for_label
                if isinstance(value, bool):
                    value = "true" if value else "false"
                attrs['parsley-%s' % key] = value
    klass.__init__ = new_init

    try:
        klass.Media.js += ("parsley/js/parsley-standalone.min.js",)
    except AttributeError:
        class Media:
            js = (
                "parsley/js/parsley-standalone.min.js",
            )
        klass.Media = Media

    return klass
<|endoftext|>