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

CopyRight = '''
/**************************************************************************
 *
 * Copyright 2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
'''

import sys

from u_format_parse import *
import u_format_pack

def layout_map(layout):
    return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()

def colorspace_map(colorspace):
    return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()

colorspace_channels_map = {
    'rgb': ['r', 'g', 'b', 'a'],
    'srgb': ['sr', 'sg', 'sb', 'a'],
    'zs': ['z', 's'],
    'yuv': ['y', 'u', 'v'],
}

type_map = {
    VOID:     "UTIL_FORMAT_TYPE_VOID",
    UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
    SIGNED:   "UTIL_FORMAT_TYPE_SIGNED",
    FIXED:    "UTIL_FORMAT_TYPE_FIXED",
    FLOAT:    "UTIL_FORMAT_TYPE_FLOAT",
}

def bool_map(value):
    if value:
        return "TRUE"
    else:
        return "FALSE"

swizzle_map = {
    SWIZZLE_X:    "UTIL_FORMAT_SWIZZLE_X",
    SWIZZLE_Y:    "UTIL_FORMAT_SWIZZLE_Y",
    SWIZZLE_Z:    "UTIL_FORMAT_SWIZZLE_Z",
    SWIZZLE_W:    "UTIL_FORMAT_SWIZZLE_W",
    SWIZZLE_0:    "UTIL_FORMAT_SWIZZLE_0",
    SWIZZLE_1:    "UTIL_FORMAT_SWIZZLE_1",
    SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE",
}

def write_format_table(formats):
    print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
    print
    # This will print the copyright message on the top of this file
    print CopyRight.strip()
    print
    print '#include "u_format.h"'
    print '#include "u_format_s3tc.h"'
    print '#include "u_format_rgtc.h"'
    print '#include "u_format_latc.h"'
    print

    u_format_pack.generate(formats)

    for format in formats:
        print 'const struct util_format_description'
        print 'util_format_%s_description = {' % (format.short_name(),)
        print "   %s," % (format.name,)
        print "   \"%s\"," % (format.name,)
        print "   \"%s\"," % (format.short_name(),)
        print "   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
        print "   %s," % (layout_map(format.layout),)
        print "   %u,\t/* nr_channels */" % (format.nr_channels(),)
        print "   %s,\t/* is_array */" % (bool_map(format.is_array()),)
        print "   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
        print "   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
        print "   {"
        for i in range(4):
            channel = format.channels[i]
            if i < 3:
                sep = ","
            else:
                sep = ""
            if channel.size:
                print "      {%s, %s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, sep, "xyzw"[i], channel.name)
            else:
                print "      {0, 0, 0}%s" % (sep,)
        print "   },"
        print "   {"
        for i in range(4):
            swizzle = format.swizzles[i]
            if i < 3:
                sep = ","
            else:
                sep = ""
            try:
                comment = colorspace_channels_map[format.colorspace][i]
            except (KeyError, IndexError):
                comment = 'ignored'
            print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
        print "   },"
        print "   %s," % (colorspace_map(format.colorspace),)
        if format.colorspace != ZS and format.channels[0].pure == False:
            print "   &util_format_%s_unpack_rgba_8unorm," % format.short_name()
            print "   &util_format_%s_pack_rgba_8unorm," % format.short_name()
            if format.layout == 's3tc' or format.layout == 'rgtc':
                print "   &util_format_%s_fetch_rgba_8unorm," % format.short_name()
            else:
                print "   NULL, /* fetch_rgba_8unorm */"
            print "   &util_format_%s_unpack_rgba_float," % format.short_name()
            print "   &util_format_%s_pack_rgba_float," % format.short_name()
            print "   &util_format_%s_fetch_rgba_float," % format.short_name()
        else:
            print "   NULL, /* unpack_rgba_8unorm */"
            print "   NULL, /* pack_rgba_8unorm */"
            print "   NULL, /* fetch_rgba_8unorm */"
            print "   NULL, /* unpack_rgba_float */"
            print "   NULL, /* pack_rgba_float */"
            print "   NULL, /* fetch_rgba_float */"
        if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE:
            print "   &util_format_%s_unpack_z_32unorm," % format.short_name()
            print "   &util_format_%s_pack_z_32unorm," % format.short_name()
            print "   &util_format_%s_unpack_z_float," % format.short_name()
            print "   &util_format_%s_pack_z_float," % format.short_name()
        else:
            print "   NULL, /* unpack_z_32unorm */"
            print "   NULL, /* pack_z_32unorm */"
            print "   NULL, /* unpack_z_float */"
            print "   NULL, /* pack_z_float */"
        if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE:
            print "   &util_format_%s_unpack_s_8uint," % format.short_name()
            print "   &util_format_%s_pack_s_8uint," % format.short_name()
        else:
            print "   NULL, /* unpack_s_8uint */"
            print "   NULL, /* pack_s_8uint */"
        if format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == UNSIGNED:
            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
            print "   &util_format_%s_pack_signed  /* pack_rgba_sint */" % format.short_name()
        elif format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == SIGNED:
            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
            print "   &util_format_%s_pack_signed  /* pack_rgba_sint */" % format.short_name()
        else:
            print "   NULL, /* unpack_rgba_uint */"
            print "   NULL, /* pack_rgba_uint */"
            print "   NULL, /* unpack_rgba_sint */"
            print "   NULL  /* pack_rgba_sint */"
        print "};"
        print

    print "const struct util_format_description *"
    print "util_format_description(enum pipe_format format)"
    print "{"
    print "   if (format >= PIPE_FORMAT_COUNT) {"
    print "      return NULL;"
    print "   }"
    print
    print "   switch (format) {"
    for format in formats:
        print "   case %s:" % format.name
        print "      return &util_format_%s_description;" % (format.short_name(),)
    print "   default:"
    print "      return NULL;"
    print "   }"
    print "}"
    print

def main():

    formats = []
    for arg in sys.argv[1:]:
        formats.extend(parse(arg))
    write_format_table(formats)

if __name__ == '__main__':
    main()
<commit_msg>util: Add missing initializer.<commit_after>#!/usr/bin/env python

CopyRight = '''
/**************************************************************************
 *
 * Copyright 2010 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
'''

import sys

from u_format_parse import *
import u_format_pack

def layout_map(layout):
    return 'UTIL_FORMAT_LAYOUT_' + str(layout).upper()

def colorspace_map(colorspace):
    return 'UTIL_FORMAT_COLORSPACE_' + str(colorspace).upper()

colorspace_channels_map = {
    'rgb': ['r', 'g', 'b', 'a'],
    'srgb': ['sr', 'sg', 'sb', 'a'],
    'zs': ['z', 's'],
    'yuv': ['y', 'u', 'v'],
}

type_map = {
    VOID:     "UTIL_FORMAT_TYPE_VOID",
    UNSIGNED: "UTIL_FORMAT_TYPE_UNSIGNED",
    SIGNED:   "UTIL_FORMAT_TYPE_SIGNED",
    FIXED:    "UTIL_FORMAT_TYPE_FIXED",
    FLOAT:    "UTIL_FORMAT_TYPE_FLOAT",
}

def bool_map(value):
    if value:
        return "TRUE"
    else:
        return "FALSE"

swizzle_map = {
    SWIZZLE_X:    "UTIL_FORMAT_SWIZZLE_X",
    SWIZZLE_Y:    "UTIL_FORMAT_SWIZZLE_Y",
    SWIZZLE_Z:    "UTIL_FORMAT_SWIZZLE_Z",
    SWIZZLE_W:    "UTIL_FORMAT_SWIZZLE_W",
    SWIZZLE_0:    "UTIL_FORMAT_SWIZZLE_0",
    SWIZZLE_1:    "UTIL_FORMAT_SWIZZLE_1",
    SWIZZLE_NONE: "UTIL_FORMAT_SWIZZLE_NONE",
}

def write_format_table(formats):
    print '/* This file is autogenerated by u_format_table.py from u_format.csv. Do not edit directly. */'
    print
    # This will print the copyright message on the top of this file
    print CopyRight.strip()
    print
    print '#include "u_format.h"'
    print '#include "u_format_s3tc.h"'
    print '#include "u_format_rgtc.h"'
    print '#include "u_format_latc.h"'
    print

    u_format_pack.generate(formats)

    for format in formats:
        print 'const struct util_format_description'
        print 'util_format_%s_description = {' % (format.short_name(),)
        print "   %s," % (format.name,)
        print "   \"%s\"," % (format.name,)
        print "   \"%s\"," % (format.short_name(),)
        print "   {%u, %u, %u},\t/* block */" % (format.block_width, format.block_height, format.block_size())
        print "   %s," % (layout_map(format.layout),)
        print "   %u,\t/* nr_channels */" % (format.nr_channels(),)
        print "   %s,\t/* is_array */" % (bool_map(format.is_array()),)
        print "   %s,\t/* is_bitmask */" % (bool_map(format.is_bitmask()),)
        print "   %s,\t/* is_mixed */" % (bool_map(format.is_mixed()),)
        print "   {"
        for i in range(4):
            channel = format.channels[i]
            if i < 3:
                sep = ","
            else:
                sep = ""
            if channel.size:
                print "      {%s, %s, %s, %u}%s\t/* %s = %s */" % (type_map[channel.type], bool_map(channel.norm), bool_map(channel.pure), channel.size, sep, "xyzw"[i], channel.name)
            else:
                print "      {0, 0, 0, 0}%s" % (sep,)
        print "   },"
        print "   {"
        for i in range(4):
            swizzle = format.swizzles[i]
            if i < 3:
                sep = ","
            else:
                sep = ""
            try:
                comment = colorspace_channels_map[format.colorspace][i]
            except (KeyError, IndexError):
                comment = 'ignored'
            print "      %s%s\t/* %s */" % (swizzle_map[swizzle], sep, comment)
        print "   },"
        print "   %s," % (colorspace_map(format.colorspace),)
        if format.colorspace != ZS and format.channels[0].pure == False:
            print "   &util_format_%s_unpack_rgba_8unorm," % format.short_name()
            print "   &util_format_%s_pack_rgba_8unorm," % format.short_name()
            if format.layout == 's3tc' or format.layout == 'rgtc':
                print "   &util_format_%s_fetch_rgba_8unorm," % format.short_name()
            else:
                print "   NULL, /* fetch_rgba_8unorm */"
            print "   &util_format_%s_unpack_rgba_float," % format.short_name()
            print "   &util_format_%s_pack_rgba_float," % format.short_name()
            print "   &util_format_%s_fetch_rgba_float," % format.short_name()
        else:
            print "   NULL, /* unpack_rgba_8unorm */"
            print "   NULL, /* pack_rgba_8unorm */"
            print "   NULL, /* fetch_rgba_8unorm */"
            print "   NULL, /* unpack_rgba_float */"
            print "   NULL, /* pack_rgba_float */"
            print "   NULL, /* fetch_rgba_float */"
        if format.colorspace == ZS and format.swizzles[0] != SWIZZLE_NONE:
            print "   &util_format_%s_unpack_z_32unorm," % format.short_name()
            print "   &util_format_%s_pack_z_32unorm," % format.short_name()
            print "   &util_format_%s_unpack_z_float," % format.short_name()
            print "   &util_format_%s_pack_z_float," % format.short_name()
        else:
            print "   NULL, /* unpack_z_32unorm */"
            print "   NULL, /* pack_z_32unorm */"
            print "   NULL, /* unpack_z_float */"
            print "   NULL, /* pack_z_float */"
        if format.colorspace == ZS and format.swizzles[1] != SWIZZLE_NONE:
            print "   &util_format_%s_unpack_s_8uint," % format.short_name()
            print "   &util_format_%s_pack_s_8uint," % format.short_name()
        else:
            print "   NULL, /* unpack_s_8uint */"
            print "   NULL, /* pack_s_8uint */"
        if format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == UNSIGNED:
            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
            print "   &util_format_%s_pack_signed  /* pack_rgba_sint */" % format.short_name()
        elif format.colorspace != ZS and format.channels[0].pure == True and format.channels[0].type == SIGNED:
            print "   &util_format_%s_unpack_unsigned, /* unpack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_pack_unsigned, /* pack_rgba_uint */" % format.short_name()
            print "   &util_format_%s_unpack_signed, /* unpack_rgba_sint */" % format.short_name()
            print "   &util_format_%s_pack_signed  /* pack_rgba_sint */" % format.short_name()
        else:
            print "   NULL, /* unpack_rgba_uint */"
            print "   NULL, /* pack_rgba_uint */"
            print "   NULL, /* unpack_rgba_sint */"
            print "   NULL  /* pack_rgba_sint */"
        print "};"
        print

    print "const struct util_format_description *"
    print "util_format_description(enum pipe_format format)"
    print "{"
    print "   if (format >= PIPE_FORMAT_COUNT) {"
    print "      return NULL;"
    print "   }"
    print
    print "   switch (format) {"
    for format in formats:
        print "   case %s:" % format.name
        print "      return &util_format_%s_description;" % (format.short_name(),)
    print "   default:"
    print "      return NULL;"
    print "   }"
    print "}"
    print

def main():

    formats = []
    for arg in sys.argv[1:]:
        formats.extend(parse(arg))
    write_format_table(formats)

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


<commit_before># coding: utf-8
"""
Amavis forms.
"""

from django import forms
from django.utils.translation import ugettext as _

from modoboa.lib import form_utils
from modoboa.parameters import forms as param_forms
from modoboa.parameters import tools as param_tools

from .models import Policy, Users

class DomainPolicyForm(forms.ModelForm):

    spam_subject_tag2_act = forms.BooleanField()

    class Meta:
        model = Policy
        fields = ('bypass_virus_checks', 'bypass_spam_checks',
                  'spam_tag2_level', 'spam_subject_tag2',
                  'spam_kill_level', 'bypass_banned_checks')
        widgets = {
            'bypass_virus_checks': form_utils.InlineRadioSelect(),
            'bypass_spam_checks': form_utils.InlineRadioSelect(),
            'spam_tag2_level': forms.TextInput(
                attrs={'class': 'form-control'}),
            'spam_kill_level': forms.TextInput(
                attrs={'class': 'form-control'}),
            'spam_subject_tag2': forms.TextInput(
                attrs={'class': 'form-control'}),
            'bypass_banned_checks': form_utils.InlineRadioSelect(),
        }

    def __init__(self, *args, **kwargs):
        if "instance" in kwargs:
            self.domain = kwargs["instance"]
            try:
                policy = Users.objects.get(
                    email="@%s" % self.domain.name).policy
                kwargs["instance"] = policy
            except (Users.DoesNotExist, Policy.DoesNotExist):
                del kwargs["instance"]
        super(DomainPolicyForm, self).__init__(*args, **kwargs)
        for field in self.fields.keys():
            self.fields[field].required = False

    def save(self, user, commit=True):
        policy = super(DomainPolicyForm, self).save(commit=False)
        for field in ['bypass_spam_checks', 'bypass_virus_checks',
                      'bypass_banned_checks']:
            if getattr(policy, field) == '':
                setattr(policy, field, None)

        if self.cleaned_data['spam_subject_tag2_act']:
            policy.spam_subject_tag2 = None

        if commit:
            policy.save()
            try:
                u = Users.objects.get(fullname=policy.policy_name)
            except Users.DoesNotExist:
                u = Users.objects.get(email="@%s" % self.domain.name)
                u.policy = policy
                policy.save()
        return policy

class LearningRecipientForm(forms.Form):
    """A form to select the recipient of a learning request."""

    recipient = forms.ChoiceField(
        label=None, choices=[]
    )
    ltype = forms.ChoiceField(
        label="", choices=[("spam", "spam"), ("ham", "ham")],
        widget=forms.widgets.HiddenInput
    )
    selection = forms.CharField(
        label="", widget=forms.widgets.HiddenInput)

    def __init__(self, user, *args, **kwargs):
        """Constructor."""
        super(LearningRecipientForm, self).__init__(*args, **kwargs)
        choices = []
        if user.role == "SuperAdmins":
            choices.append(("global", _("Global database")))
        conf = dict(param_tools.get_global_parameters("modoboa_amavis"))
        if conf["domain_level_learning"]:
            choices.append(("domain", _("Domain's database")))
        if conf["user_level_learning"]:
            choices.append(("user", _("User's database")))
        self.fields["recipient"].choices = choices

class ParametersForm(param_forms.AdminParametersForm):
    """Extension settings."""

    app = "modoboa_amavis"

    qsettings_sep = form_utils.SeparatorField(label=_("Quarantine settings"))

    max_messages_age = forms.IntegerField(
        label=_("Maximum message age"),
        initial=14,
        help_text=_(
            "Quarantine messages maximum age (in days) before deletion"
        )
    )

    sep1 = form_utils.SeparatorField(label=_("Messages releasing"))

    released_msgs_cleanup = form_utils.YesNoField(
        label=_("Remove released messages"),
        initial=False,
        help_text=_("Remove messages marked as released while cleaning up "
                    "the database")
    )

    am_pdp_mode = forms.ChoiceField(
        label=_("Amavis connection mode"),
        choices=[("inet", "inet"), ("unix", "unix")],
        initial="unix",
        help_text=_("Mode used to access the PDP server"),
        widget=form_utils.InlineRadioSelect(attrs={"type": "checkbox"})
    )

    am_pdp_host = forms.CharField(
        label=_("PDP server address"),
        initial="localhost",
        help_text=_("PDP server address (if inet mode)"),
        widget=forms.TextInput(attrs={"class": "form-control"})
    )

    am_pdp_port = forms.IntegerField(
        label=_("PDP server port"),
        initial=9998,
        help_text=_("PDP server port (if inet mode)")
    )

    am_pdp_socket = forms.CharField(
        label=_("PDP server socket"),
        initial="/var/amavis/amavisd.sock",
        help_text=_("Path to the PDP server socket (if unix mode)")
    )

    user_can_release = form_utils.YesNoField(
        label=_("Allow direct release"),
        initial=False,
        help_text=_("Allow users to directly release their messages")
    )

    self_service = form_utils.YesNoField(
        label=_("Enable self-service mode"),
        initial=False,
        help_text=_("Activate the 'self-service' mode")
    )

    notifications_sender = forms.EmailField(
        label=_("Notifications sender"),
        initial="notification@modoboa.org",
        help_text=_("The e-mail address used to send notitications")
    )

    lsep = form_utils.SeparatorField(label=_("Manual learning"))

    manual_learning = form_utils.YesNoField(
        label=_("Enable manual learning"),
        initial=True,
        help_text=_(
            "Allow super administrators to manually train Spamassassin"
        )
    )

    sa_is_local = form_utils.YesNoField(
        label=_("Is Spamassassin local?"),
        initial=True,
        help_text=_(
            "Tell if Spamassassin is running on the same server than modoboa"
        )
    )

    default_user = forms.CharField(
        label=_("Default user"),
        initial="amavis",
        help_text=_(
            "Name of the user owning the default bayesian database"
        )
    )

    spamd_address = forms.CharField(
        label=_("Spamd address"),
        initial="127.0.0.1",
        help_text=_("The IP address where spamd can be reached")
    )

    spamd_port = forms.IntegerField(
        label=_("Spamd port"),
        initial=783,
        help_text=_("The TCP port spamd is listening on")
    )

    domain_level_learning = form_utils.YesNoField(
        label=_("Enable per-domain manual learning"),
        initial=False,
        help_text=_(
            "Allow domain administrators to train Spamassassin "
            "(within dedicated per-domain databases)"
        )
    )

    user_level_learning = form_utils.YesNoField(
        label=_("Enable per-user manual learning"),
        initial=False,
        help_text=_(
            "Allow simple users to personally train Spamassassin "
            "(within a dedicated database)"
        )
    )

    visibility_rules = {
        "am_pdp_host": "am_pdp_mode=inet",
        "am_pdp_port": "am_pdp_mode=inet",
        "am_pdp_socket": "am_pdp_mode=unix",

        "sa_is_local": "manual_learning=yes",
        "default_user": "manual_learning=yes",
        "spamd_address": "sa_is_local=no",
        "spamd_port": "sa_is_local=no",
        "domain_level_learning": "manual_learning=yes",
        "user_level_learning": "manual_learning=yes"
    }

class UserSettings(param_forms.UserParametersForm):
    """Per-user settings."""

    app = "modoboa_amavis"

    dsep = form_utils.SeparatorField(label=_("Display"))

    messages_per_page = forms.IntegerField(
        initial=40,
        label=_("Number of displayed emails per page"),
        help_text=_("Set the maximum number of messages displayed in a page")
    )
<commit_msg>Updated visibility rules.<commit_after># coding: utf-8
"""
Amavis forms.
"""

from django import forms
from django.utils.translation import ugettext as _

from modoboa.lib import form_utils
from modoboa.parameters import forms as param_forms
from modoboa.parameters import tools as param_tools

from .models import Policy, Users

class DomainPolicyForm(forms.ModelForm):

    spam_subject_tag2_act = forms.BooleanField()

    class Meta:
        model = Policy
        fields = ('bypass_virus_checks', 'bypass_spam_checks',
                  'spam_tag2_level', 'spam_subject_tag2',
                  'spam_kill_level', 'bypass_banned_checks')
        widgets = {
            'bypass_virus_checks': form_utils.InlineRadioSelect(),
            'bypass_spam_checks': form_utils.InlineRadioSelect(),
            'spam_tag2_level': forms.TextInput(
                attrs={'class': 'form-control'}),
            'spam_kill_level': forms.TextInput(
                attrs={'class': 'form-control'}),
            'spam_subject_tag2': forms.TextInput(
                attrs={'class': 'form-control'}),
            'bypass_banned_checks': form_utils.InlineRadioSelect(),
        }

    def __init__(self, *args, **kwargs):
        if "instance" in kwargs:
            self.domain = kwargs["instance"]
            try:
                policy = Users.objects.get(
                    email="@%s" % self.domain.name).policy
                kwargs["instance"] = policy
            except (Users.DoesNotExist, Policy.DoesNotExist):
                del kwargs["instance"]
        super(DomainPolicyForm, self).__init__(*args, **kwargs)
        for field in self.fields.keys():
            self.fields[field].required = False

    def save(self, user, commit=True):
        policy = super(DomainPolicyForm, self).save(commit=False)
        for field in ['bypass_spam_checks', 'bypass_virus_checks',
                      'bypass_banned_checks']:
            if getattr(policy, field) == '':
                setattr(policy, field, None)

        if self.cleaned_data['spam_subject_tag2_act']:
            policy.spam_subject_tag2 = None

        if commit:
            policy.save()
            try:
                u = Users.objects.get(fullname=policy.policy_name)
            except Users.DoesNotExist:
                u = Users.objects.get(email="@%s" % self.domain.name)
                u.policy = policy
                policy.save()
        return policy

class LearningRecipientForm(forms.Form):
    """A form to select the recipient of a learning request."""

    recipient = forms.ChoiceField(
        label=None, choices=[]
    )
    ltype = forms.ChoiceField(
        label="", choices=[("spam", "spam"), ("ham", "ham")],
        widget=forms.widgets.HiddenInput
    )
    selection = forms.CharField(
        label="", widget=forms.widgets.HiddenInput)

    def __init__(self, user, *args, **kwargs):
        """Constructor."""
        super(LearningRecipientForm, self).__init__(*args, **kwargs)
        choices = []
        if user.role == "SuperAdmins":
            choices.append(("global", _("Global database")))
        conf = dict(param_tools.get_global_parameters("modoboa_amavis"))
        if conf["domain_level_learning"]:
            choices.append(("domain", _("Domain's database")))
        if conf["user_level_learning"]:
            choices.append(("user", _("User's database")))
        self.fields["recipient"].choices = choices

class ParametersForm(param_forms.AdminParametersForm):
    """Extension settings."""

    app = "modoboa_amavis"

    qsettings_sep = form_utils.SeparatorField(label=_("Quarantine settings"))

    max_messages_age = forms.IntegerField(
        label=_("Maximum message age"),
        initial=14,
        help_text=_(
            "Quarantine messages maximum age (in days) before deletion"
        )
    )

    sep1 = form_utils.SeparatorField(label=_("Messages releasing"))

    released_msgs_cleanup = form_utils.YesNoField(
        label=_("Remove released messages"),
        initial=False,
        help_text=_("Remove messages marked as released while cleaning up "
                    "the database")
    )

    am_pdp_mode = forms.ChoiceField(
        label=_("Amavis connection mode"),
        choices=[("inet", "inet"), ("unix", "unix")],
        initial="unix",
        help_text=_("Mode used to access the PDP server"),
        widget=form_utils.InlineRadioSelect(attrs={"type": "checkbox"})
    )

    am_pdp_host = forms.CharField(
        label=_("PDP server address"),
        initial="localhost",
        help_text=_("PDP server address (if inet mode)"),
        widget=forms.TextInput(attrs={"class": "form-control"})
    )

    am_pdp_port = forms.IntegerField(
        label=_("PDP server port"),
        initial=9998,
        help_text=_("PDP server port (if inet mode)")
    )

    am_pdp_socket = forms.CharField(
        label=_("PDP server socket"),
        initial="/var/amavis/amavisd.sock",
        help_text=_("Path to the PDP server socket (if unix mode)")
    )

    user_can_release = form_utils.YesNoField(
        label=_("Allow direct release"),
        initial=False,
        help_text=_("Allow users to directly release their messages")
    )

    self_service = form_utils.YesNoField(
        label=_("Enable self-service mode"),
        initial=False,
        help_text=_("Activate the 'self-service' mode")
    )

    notifications_sender = forms.EmailField(
        label=_("Notifications sender"),
        initial="notification@modoboa.org",
        help_text=_("The e-mail address used to send notitications")
    )

    lsep = form_utils.SeparatorField(label=_("Manual learning"))

    manual_learning = form_utils.YesNoField(
        label=_("Enable manual learning"),
        initial=True,
        help_text=_(
            "Allow super administrators to manually train Spamassassin"
        )
    )

    sa_is_local = form_utils.YesNoField(
        label=_("Is Spamassassin local?"),
        initial=True,
        help_text=_(
            "Tell if Spamassassin is running on the same server than modoboa"
        )
    )

    default_user = forms.CharField(
        label=_("Default user"),
        initial="amavis",
        help_text=_(
            "Name of the user owning the default bayesian database"
        )
    )

    spamd_address = forms.CharField(
        label=_("Spamd address"),
        initial="127.0.0.1",
        help_text=_("The IP address where spamd can be reached")
    )

    spamd_port = forms.IntegerField(
        label=_("Spamd port"),
        initial=783,
        help_text=_("The TCP port spamd is listening on")
    )

    domain_level_learning = form_utils.YesNoField(
        label=_("Enable per-domain manual learning"),
        initial=False,
        help_text=_(
            "Allow domain administrators to train Spamassassin "
            "(within dedicated per-domain databases)"
        )
    )

    user_level_learning = form_utils.YesNoField(
        label=_("Enable per-user manual learning"),
        initial=False,
        help_text=_(
            "Allow simple users to personally train Spamassassin "
            "(within a dedicated database)"
        )
    )

    visibility_rules = {
        "am_pdp_host": "am_pdp_mode=inet",
        "am_pdp_port": "am_pdp_mode=inet",
        "am_pdp_socket": "am_pdp_mode=unix",

        "sa_is_local": "manual_learning=True",
        "default_user": "manual_learning=True",
        "spamd_address": "sa_is_local=False",
        "spamd_port": "sa_is_local=False",
        "domain_level_learning": "manual_learning=True",
        "user_level_learning": "manual_learning=True"
    }

class UserSettings(param_forms.UserParametersForm):
    """Per-user settings."""

    app = "modoboa_amavis"

    dsep = form_utils.SeparatorField(label=_("Display"))

    messages_per_page = forms.IntegerField(
        initial=40,
        label=_("Number of displayed emails per page"),
        help_text=_("Set the maximum number of messages displayed in a page")
    )
<|endoftext|>


<commit_before># Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: dan@reciprocitylabs.com
# Maintained By: miha@reciprocitylabs.com

from datetime import date
from collections import OrderedDict
from sqlalchemy import orm
from sqlalchemy import not_

from ggrc import db
from ggrc.login import get_current_user
from ggrc.models.associationproxy import association_proxy
from ggrc.models.computed_property import computed_property
from ggrc.models.context import HasOwnContext
from ggrc.models.person import Person
from ggrc.models.reflection import AttributeInfo
from ggrc.models.mixins import (
    deferred, Base, Titled, Slugged, Described, Timeboxed, Stateful,
    CustomAttributable
)
from ggrc.models.reflection import PublishOnly
from ggrc_gdrive_integration.models.object_folder import Folderable
from ggrc_workflows.models.cycle import Cycle
from ggrc_basic_permissions.models import UserRole

class Workflow(CustomAttributable, HasOwnContext, Timeboxed, Described, Titled,
               Slugged, Stateful, Base, db.Model):
  __tablename__ = 'workflows'
  _title_uniqueness = False

  VALID_STATES = [u"Draft", u"Active", u"Inactive"]

  VALID_FREQUENCIES = [
      "one_time",
      "weekly",
      "monthly",
      "quarterly",
      "annually"
  ]

  @classmethod
  def default_frequency(cls):
    return 'one_time'

  @orm.validates('frequency')
  def validate_frequency(self, key, value):
    if value is None:
      value = self.default_frequency()
    if value not in self.VALID_FREQUENCIES:
      message = u"Invalid state '{}'".format(value)
      raise ValueError(message)
    return value

  notify_on_change = deferred(
      db.Column(db.Boolean, default=False, nullable=False), 'Workflow')
  notify_custom_message = deferred(
      db.Column(db.Text, nullable=True), 'Workflow')

  frequency = deferred(
      db.Column(db.String, nullable=True, default=default_frequency),
      'Workflow'
  )

  object_approval = deferred(
      db.Column(db.Boolean, default=False, nullable=False), 'Workflow')

  recurrences = db.Column(db.Boolean, default=False, nullable=False)

  workflow_people = db.relationship(
      'WorkflowPerson', backref='workflow', cascade='all, delete-orphan')
  people = association_proxy(
      'workflow_people', 'person', 'WorkflowPerson')

  task_groups = db.relationship(
      'TaskGroup', backref='workflow', cascade='all, delete-orphan')

  cycles = db.relationship(
      'Cycle', backref='workflow', cascade='all, delete-orphan')

  next_cycle_start_date = db.Column(db.Date, nullable=True)

  non_adjusted_next_cycle_start_date = db.Column(db.Date, nullable=True)

  @computed_property
  def workflow_state(self):
    return WorkflowState.get_state(self.cycles)

  _sanitize_html = [
      'notify_custom_message',
  ]

  _publish_attrs = [
      'workflow_people',
      PublishOnly('people'),
      'task_groups',
      'frequency',
      'notify_on_change',
      'notify_custom_message',
      'cycles',
      'object_approval',
      'recurrences',
      PublishOnly('next_cycle_start_date'),
      PublishOnly('non_adjusted_next_cycle_start_date'),
      PublishOnly('workflow_state'),
  ]

  _aliases = {
      "frequency": {
          "display_name": "Frequency",
          "mandatory": True,
      },
      "notify_custom_message": "Custom email message",
      "notify_on_change": "Force real-time email updates",
      "workflow_owner": {
          "display_name": "Manager",
          "type": AttributeInfo.Type.USER_ROLE,
          "mandatory": True,
          "filter_by": "_filter_by_workflow_owner",
      },
      "workflow_member": {
          "display_name": "Member",
          "type": AttributeInfo.Type.USER_ROLE,
          "filter_by": "_filter_by_workflow_member",
      },
      "workflow_mapped": {
          "display_name": "No Access",
          "type": AttributeInfo.Type.USER_ROLE,
          "filter_by": "_filter_by_no_access",
      },
      "status": None,
      "start_date": None,
      "end_date": None,
  }

  @classmethod
  def _filter_by_workflow_owner(cls, predicate):
    return cls._filter_by_role("WorkflowOwner", predicate)

  @classmethod
  def _filter_by_workflow_member(cls, predicate):
    return cls._filter_by_role("WorkflowMember", predicate)

  @classmethod
  def _filter_by_no_access(cls, predicate):
    from ggrc_workflows.models import WorkflowPerson
    is_no_access = not_(UserRole.query.filter(
        (UserRole.person_id == Person.id) &
        (UserRole.context_id == cls.context_id)
    ).exists())
    return WorkflowPerson.query.filter(
        (cls.id == WorkflowPerson.workflow_id) & is_no_access
    ).join(Person).filter(
        (predicate(Person.name) | predicate(Person.email))
    ).exists()

  def copy(self, _other=None, **kwargs):
    columns = [
        'title', 'description', 'notify_on_change', 'notify_custom_message',
        'frequency', 'end_date', 'start_date'
    ]
    target = self.copy_into(_other, columns, **kwargs)
    return target

  def copy_task_groups(self, target, **kwargs):
    for task_group in self.task_groups:
      obj = task_group.copy(
          workflow=target,
          context=target.context,
          clone_people=kwargs.get("clone_people", False),
          clone_objects=kwargs.get("clone_objects", False),
          modified_by=get_current_user(),
      )
      target.task_groups.append(obj)

      if(kwargs.get("clone_tasks", False)):
        task_group.copy_tasks(
            obj,
            clone_people=kwargs.get("clone_people", False),
            clone_objects=kwargs.get("clone_objects", True)
        )

    return target

class WorkflowState(object):

  _publish_attrs = [PublishOnly('workflow_state')]
  _update_attrs = []
  _stub_attrs = []

  @classmethod
  def get_state(cls, objs):
    priority_states = OrderedDict([
        # The first True state will be returned
        ("Overdue", False),
        ("InProgress", False),
        ("Finished", False),
        ("Assigned", False),
        ("Verified", False)
    ])

    for obj in objs:
      today = date.today()
      cycle = obj if isinstance(obj, Cycle) else obj.cycle
      if not cycle.is_current:
        continue
      for task in obj.cycle_task_group_object_tasks:
        if task.end_date and \
           task.end_date <= today and \
           task.status != "Verified":
          priority_states["Overdue"] = True
      if not obj.status:
        priority_states["Assigned"] = True
      priority_states[obj.status] = True

    for state in priority_states.keys():
      if priority_states[state]:
        return state

    return None

  @computed_property
  def workflow_state(self):
    return WorkflowState.get_state(self.cycle_task_group_objects)

  @classmethod
  def eager_query(cls):

    query = super(WorkflowState, cls).eager_query()
    return query.options(
        orm.subqueryload('cycle_task_group_objects')
        .undefer_group('CycleTaskGroupObject_complete'),
        orm.subqueryload_all('cycle_task_group_objects.cycle'),
    )

# TODO: This makes the Workflow module dependant on Gdrive. It is not pretty.
Workflow.__bases__ = (Folderable,) + Workflow.__bases__
Workflow.late_init_folderable()
<commit_msg>Clean up imports in Workflow model<commit_after># Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: dan@reciprocitylabs.com
# Maintained By: miha@reciprocitylabs.com

from datetime import date
from collections import OrderedDict
from sqlalchemy import orm
from sqlalchemy import not_

from ggrc import db
from ggrc.login import get_current_user
from ggrc.models.associationproxy import association_proxy
from ggrc.models.computed_property import computed_property
from ggrc.models.context import HasOwnContext
from ggrc.models.person import Person
from ggrc.models.reflection import AttributeInfo
from ggrc.models.mixins import (
    deferred, Base, Titled, Slugged, Described, Timeboxed, Stateful,
    CustomAttributable
)
from ggrc.models.reflection import PublishOnly
from ggrc_gdrive_integration.models.object_folder import Folderable
from ggrc_workflows.models.cycle import Cycle
from ggrc_workflows.models.workflow_person import WorkflowPerson
from ggrc_basic_permissions.models import UserRole

class Workflow(CustomAttributable, HasOwnContext, Timeboxed, Described, Titled,
               Slugged, Stateful, Base, db.Model):
  __tablename__ = 'workflows'
  _title_uniqueness = False

  VALID_STATES = [u"Draft", u"Active", u"Inactive"]

  VALID_FREQUENCIES = [
      "one_time",
      "weekly",
      "monthly",
      "quarterly",
      "annually"
  ]

  @classmethod
  def default_frequency(cls):
    return 'one_time'

  @orm.validates('frequency')
  def validate_frequency(self, key, value):
    if value is None:
      value = self.default_frequency()
    if value not in self.VALID_FREQUENCIES:
      message = u"Invalid state '{}'".format(value)
      raise ValueError(message)
    return value

  notify_on_change = deferred(
      db.Column(db.Boolean, default=False, nullable=False), 'Workflow')
  notify_custom_message = deferred(
      db.Column(db.Text, nullable=True), 'Workflow')

  frequency = deferred(
      db.Column(db.String, nullable=True, default=default_frequency),
      'Workflow'
  )

  object_approval = deferred(
      db.Column(db.Boolean, default=False, nullable=False), 'Workflow')

  recurrences = db.Column(db.Boolean, default=False, nullable=False)

  workflow_people = db.relationship(
      'WorkflowPerson', backref='workflow', cascade='all, delete-orphan')
  people = association_proxy(
      'workflow_people', 'person', 'WorkflowPerson')

  task_groups = db.relationship(
      'TaskGroup', backref='workflow', cascade='all, delete-orphan')

  cycles = db.relationship(
      'Cycle', backref='workflow', cascade='all, delete-orphan')

  next_cycle_start_date = db.Column(db.Date, nullable=True)

  non_adjusted_next_cycle_start_date = db.Column(db.Date, nullable=True)

  @computed_property
  def workflow_state(self):
    return WorkflowState.get_state(self.cycles)

  _sanitize_html = [
      'notify_custom_message',
  ]

  _publish_attrs = [
      'workflow_people',
      PublishOnly('people'),
      'task_groups',
      'frequency',
      'notify_on_change',
      'notify_custom_message',
      'cycles',
      'object_approval',
      'recurrences',
      PublishOnly('next_cycle_start_date'),
      PublishOnly('non_adjusted_next_cycle_start_date'),
      PublishOnly('workflow_state'),
  ]

  _aliases = {
      "frequency": {
          "display_name": "Frequency",
          "mandatory": True,
      },
      "notify_custom_message": "Custom email message",
      "notify_on_change": "Force real-time email updates",
      "workflow_owner": {
          "display_name": "Manager",
          "type": AttributeInfo.Type.USER_ROLE,
          "mandatory": True,
          "filter_by": "_filter_by_workflow_owner",
      },
      "workflow_member": {
          "display_name": "Member",
          "type": AttributeInfo.Type.USER_ROLE,
          "filter_by": "_filter_by_workflow_member",
      },
      "workflow_mapped": {
          "display_name": "No Access",
          "type": AttributeInfo.Type.USER_ROLE,
          "filter_by": "_filter_by_no_access",
      },
      "status": None,
      "start_date": None,
      "end_date": None,
  }

  @classmethod
  def _filter_by_workflow_owner(cls, predicate):
    return cls._filter_by_role("WorkflowOwner", predicate)

  @classmethod
  def _filter_by_workflow_member(cls, predicate):
    return cls._filter_by_role("WorkflowMember", predicate)

  @classmethod
  def _filter_by_no_access(cls, predicate):
    is_no_access = not_(UserRole.query.filter(
        (UserRole.person_id == Person.id) &
        (UserRole.context_id == cls.context_id)
    ).exists())
    return WorkflowPerson.query.filter(
        (cls.id == WorkflowPerson.workflow_id) & is_no_access
    ).join(Person).filter(
        (predicate(Person.name) | predicate(Person.email))
    ).exists()

  def copy(self, _other=None, **kwargs):
    columns = [
        'title', 'description', 'notify_on_change', 'notify_custom_message',
        'frequency', 'end_date', 'start_date'
    ]
    target = self.copy_into(_other, columns, **kwargs)
    return target

  def copy_task_groups(self, target, **kwargs):
    for task_group in self.task_groups:
      obj = task_group.copy(
          workflow=target,
          context=target.context,
          clone_people=kwargs.get("clone_people", False),
          clone_objects=kwargs.get("clone_objects", False),
          modified_by=get_current_user(),
      )
      target.task_groups.append(obj)

      if(kwargs.get("clone_tasks", False)):
        task_group.copy_tasks(
            obj,
            clone_people=kwargs.get("clone_people", False),
            clone_objects=kwargs.get("clone_objects", True)
        )

    return target

class WorkflowState(object):

  _publish_attrs = [PublishOnly('workflow_state')]
  _update_attrs = []
  _stub_attrs = []

  @classmethod
  def get_state(cls, objs):
    priority_states = OrderedDict([
        # The first True state will be returned
        ("Overdue", False),
        ("InProgress", False),
        ("Finished", False),
        ("Assigned", False),
        ("Verified", False)
    ])

    for obj in objs:
      today = date.today()
      cycle = obj if isinstance(obj, Cycle) else obj.cycle
      if not cycle.is_current:
        continue
      for task in obj.cycle_task_group_object_tasks:
        if task.end_date and \
           task.end_date <= today and \
           task.status != "Verified":
          priority_states["Overdue"] = True
      if not obj.status:
        priority_states["Assigned"] = True
      priority_states[obj.status] = True

    for state in priority_states.keys():
      if priority_states[state]:
        return state

    return None

  @computed_property
  def workflow_state(self):
    return WorkflowState.get_state(self.cycle_task_group_objects)

  @classmethod
  def eager_query(cls):

    query = super(WorkflowState, cls).eager_query()
    return query.options(
        orm.subqueryload('cycle_task_group_objects')
        .undefer_group('CycleTaskGroupObject_complete'),
        orm.subqueryload_all('cycle_task_group_objects.cycle'),
    )

# TODO: This makes the Workflow module dependant on Gdrive. It is not pretty.
Workflow.__bases__ = (Folderable,) + Workflow.__bases__
Workflow.late_init_folderable()
<|endoftext|>


<commit_before>from __future__ import division
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import sys
import pylab
import file_loader as load

#works, but also a work in progress.

def oscillation_period(density_list):
    peaklist = []
    index_position_pairs = list(enumerate(density_list))
    for i in index_position_pairs:
        if (abs(i[1]-max(density_list)) < .01*max(density_list)):
            peaklist += [i[0]]
    return (peaklist[0], peaklist[1])

NflE = load.data(protein="NflE")
NflD = load.data(protein="NflD")
nATP = load.data(protein="nATP")
nE = load.data(protein="nE")
nADP = load.data(protein="nADP")
Nd = load.data(protein="Nd")
Nde = load.data(protein="Nde")

proteins = [NflE, NflD]
sub_proteins_D = [nATP, nADP, Nd, Nde]
sub_proteins_E = [nE, Nde]

for p in proteins:
    #pick a relatively arbitrary box divider
    divider_left = round(p.datashape[1]/3)
    divider_right = round(p.datashape[1]*2/3)
    p.proteins_left = []
    p.proteins_mid = []
    p.proteins_right = []

    #collect the total number of proteins in each box, at each time step
    for file in p.dataset:
        left_sum = 0
        mid_sum = 0
        right_sum = 0
        for i in range(p.datashape[0]-1):
            for j in range(p.datashape[1]-1):
                if j <= divider_left:
                    left_sum += file[i][j]
                elif divider_left < j <= divider_right:
                    mid_sum += file[i][j]
                else:
                    right_sum += file[i][j]
        p.proteins_left += [left_sum]
        p.proteins_mid += [mid_sum]
        p.proteins_right += [right_sum]

    #gather the individual proteins to stick on the same graph as NflD or NflE.
    if p==NflE:
        for individual_proteins in sub_proteins_E:
            #the following 3 lines convert densities to counts (i.e. nATP to NATP) so the graphs make sense
            dV = 1
            if individual_proteins == nE:
                dV = load.dx**3
            individual_proteins.proteins_left = []
            individual_proteins.proteins_mid = []
            individual_proteins.proteins_right = []

            for file in individual_proteins.dataset:
                left_sum = 0
                mid_sum = 0
                right_sum = 0
                for i in range(individual_proteins.datashape[0]-1):
                    for j in range(individual_proteins.datashape[1]-1):
                        if j <= divider_left:
                            left_sum += file[i][j]
                        elif divider_left < j <= divider_right:
                            mid_sum += file[i][j]
                        else:
                            right_sum += file[i][j]
                individual_proteins.proteins_left += [left_sum*dV]
                individual_proteins.proteins_mid += [mid_sum*dV]
                individual_proteins.proteins_right += [right_sum*dV]
        NflE.lines = [nE.proteins_left, Nde.proteins_left, \
                          nE.proteins_mid, Nde.proteins_mid, \
                          nE.proteins_right, Nde.proteins_right]

    if p==NflD:
        for individual_proteins in sub_proteins_D:
            dV = 1
            if (individual_proteins == nATP) or (individual_proteins == nADP):
                dV = load.dx**3
            individual_proteins.proteins_left = []
            individual_proteins.proteins_mid = []
            individual_proteins.proteins_right = []

            for file in individual_proteins.dataset:
                left_sum = 0
                mid_sum = 0
                right_sum = 0
                for i in range(individual_proteins.datashape[0]-1):
                    for j in range(individual_proteins.datashape[1]-1):
                        if j <= divider_left:
                            left_sum += file[i][j]
                        elif divider_left < j <= divider_right:
                            mid_sum += file[i][j]
                        else:
                            right_sum += file[i][j]
                individual_proteins.proteins_left += [left_sum*dV]
                individual_proteins.proteins_mid += [mid_sum*dV]
                individual_proteins.proteins_right += [right_sum*dV]
        NflD.lines = [nATP.proteins_left, nADP.proteins_left, Nd.proteins_left, Nde.proteins_left, \
                          nATP.proteins_mid, nADP.proteins_mid, Nd.proteins_mid, Nde.proteins_mid, \
                          nATP.proteins_right, nADP.proteins_right, Nd.proteins_right, Nde.proteins_right]

    #calculate the period
    (start, end) = (0, -1)
    if "-all" in sys.argv:
        (start, end) = oscillation_period(p.proteins_mid)

    #plot them and save the figures.
    plt.figure()
    plt.xlabel("Time")
    plt.ylabel("%s count"%p.protein)
    plt.title("%s count vs time"%p.protein)

    plot_lines = [p.lines[0]]
    for i in range(1,len(p.lines)):
        plot_lines += [[j+k for (j,k) in zip(plot_lines[i-1], p.lines[i])]]

    #the list comprehensions used below are just one-liner ways to add the elements in two lists
    lowest_line = p.proteins_left
    middle_line = [i+j for (i,j) in zip(lowest_line,p.proteins_mid)]
    top_line = [i+j for (i,j) in zip(middle_line,p.proteins_right)]

    time_axis = list(np.arange(0,len(p.proteins_left)*5,5))

    plt.fill_between(time_axis[start:end],0,lowest_line[start:end],alpha=0.5)
    plt.fill_between(time_axis[start:end],lowest_line[start:end],middle_line[start:end],alpha=0.5,facecolor="green")
    plt.fill_between(time_axis[start:end],middle_line[start:end],top_line[start:end],alpha=0.5,facecolor="red")

    plt.plot(time_axis[start:end],lowest_line[start:end],color="blue",linewidth=0.5)
    plt.plot(time_axis[start:end],middle_line[start:end],color="green",linewidth=0.5)
    plt.plot(time_axis[start:end],top_line[start:end],color="red",linewidth=0.5)

    all_string = ""
    if "-all" in sys.argv:
        for line in plot_lines:
            plt.plot(time_axis[start:end],line[start:end],color="black",linewidth=0.5)
        all_string = "-all"

    plt.xlim(time_axis[start],max(time_axis[start:end]))
    plt.ylim(0,1.4*max(top_line[start:end]))
    #plt.legend(["left density", "middle density", "right density"],loc="best")
    plt.savefig(load.print_string("box-plot-yes"+all_string,p))
<commit_msg>fixed typo<commit_after>from __future__ import division
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import sys
import pylab
import file_loader as load

#works, but also a work in progress.

def oscillation_period(density_list):
    peaklist = []
    index_position_pairs = list(enumerate(density_list))
    for i in index_position_pairs:
        if (abs(i[1]-max(density_list)) < .01*max(density_list)):
            peaklist += [i[0]]
    return (peaklist[0], peaklist[1])

NflE = load.data(protein="NflE")
NflD = load.data(protein="NflD")
nATP = load.data(protein="nATP")
nE = load.data(protein="nE")
nADP = load.data(protein="nADP")
Nd = load.data(protein="Nd")
Nde = load.data(protein="Nde")

proteins = [NflE, NflD]
sub_proteins_D = [nATP, nADP, Nd, Nde]
sub_proteins_E = [nE, Nde]

for p in proteins:
    #pick a relatively arbitrary box divider
    divider_left = round(p.datashape[1]/3)
    divider_right = round(p.datashape[1]*2/3)
    p.proteins_left = []
    p.proteins_mid = []
    p.proteins_right = []

    #collect the total number of proteins in each box, at each time step
    for file in p.dataset:
        left_sum = 0
        mid_sum = 0
        right_sum = 0
        for i in range(p.datashape[0]-1):
            for j in range(p.datashape[1]-1):
                if j <= divider_left:
                    left_sum += file[i][j]
                elif divider_left < j <= divider_right:
                    mid_sum += file[i][j]
                else:
                    right_sum += file[i][j]
        p.proteins_left += [left_sum]
        p.proteins_mid += [mid_sum]
        p.proteins_right += [right_sum]

    #gather the individual proteins to stick on the same graph as NflD or NflE.
    if p==NflE:
        for individual_proteins in sub_proteins_E:
            #the following 3 lines convert densities to counts (i.e. nATP to NATP) so the graphs make sense
            dV = 1
            if individual_proteins == nE:
                dV = load.dx**3
            individual_proteins.proteins_left = []
            individual_proteins.proteins_mid = []
            individual_proteins.proteins_right = []

            for file in individual_proteins.dataset:
                left_sum = 0
                mid_sum = 0
                right_sum = 0
                for i in range(individual_proteins.datashape[0]-1):
                    for j in range(individual_proteins.datashape[1]-1):
                        if j <= divider_left:
                            left_sum += file[i][j]
                        elif divider_left < j <= divider_right:
                            mid_sum += file[i][j]
                        else:
                            right_sum += file[i][j]
                individual_proteins.proteins_left += [left_sum*dV]
                individual_proteins.proteins_mid += [mid_sum*dV]
                individual_proteins.proteins_right += [right_sum*dV]
        NflE.lines = [nE.proteins_left, Nde.proteins_left, \
                          nE.proteins_mid, Nde.proteins_mid, \
                          nE.proteins_right, Nde.proteins_right]

    if p==NflD:
        for individual_proteins in sub_proteins_D:
            dV = 1
            if (individual_proteins == nATP) or (individual_proteins == nADP):
                dV = load.dx**3
            individual_proteins.proteins_left = []
            individual_proteins.proteins_mid = []
            individual_proteins.proteins_right = []

            for file in individual_proteins.dataset:
                left_sum = 0
                mid_sum = 0
                right_sum = 0
                for i in range(individual_proteins.datashape[0]-1):
                    for j in range(individual_proteins.datashape[1]-1):
                        if j <= divider_left:
                            left_sum += file[i][j]
                        elif divider_left < j <= divider_right:
                            mid_sum += file[i][j]
                        else:
                            right_sum += file[i][j]
                individual_proteins.proteins_left += [left_sum*dV]
                individual_proteins.proteins_mid += [mid_sum*dV]
                individual_proteins.proteins_right += [right_sum*dV]
        NflD.lines = [nATP.proteins_left, nADP.proteins_left, Nd.proteins_left, Nde.proteins_left, \
                          nATP.proteins_mid, nADP.proteins_mid, Nd.proteins_mid, Nde.proteins_mid, \
                          nATP.proteins_right, nADP.proteins_right, Nd.proteins_right, Nde.proteins_right]

    #calculate the period
    (start, end) = (0, -1)
    if ("-all" in sys.argv) or ("-short" in sys.argv):
        (start, end) = oscillation_period(p.proteins_mid)

    #plot them and save the figures.
    plt.figure()
    plt.xlabel("Time")
    plt.ylabel("%s count"%p.protein)
    plt.title("%s count vs time"%p.protein)

    plot_lines = [p.lines[0]]
    for i in range(1,len(p.lines)):
        plot_lines += [[j+k for (j,k) in zip(plot_lines[i-1], p.lines[i])]]

    #the list comprehensions used below are just one-liner ways to add the elements in two lists
    lowest_line = p.proteins_left
    middle_line = [i+j for (i,j) in zip(lowest_line,p.proteins_mid)]
    top_line = [i+j for (i,j) in zip(middle_line,p.proteins_right)]

    time_axis = list(np.arange(0,len(p.proteins_left)*5,5))

    plt.fill_between(time_axis[start:end],0,lowest_line[start:end],alpha=0.5)
    plt.fill_between(time_axis[start:end],lowest_line[start:end],middle_line[start:end],alpha=0.5,facecolor="green")
    plt.fill_between(time_axis[start:end],middle_line[start:end],top_line[start:end],alpha=0.5,facecolor="red")

    plt.plot(time_axis[start:end],lowest_line[start:end],color="blue",linewidth=0.5)
    plt.plot(time_axis[start:end],middle_line[start:end],color="green",linewidth=0.5)
    plt.plot(time_axis[start:end],top_line[start:end],color="red",linewidth=0.5)

    all_string = ""
    if "-all" in sys.argv:
        for line in plot_lines:
            plt.plot(time_axis[start:end],line[start:end],color="black",linewidth=0.5)
        all_string = "-all"

    short_string = ""
    if "-short" in sys.argv:
        short_string = "-short"

    plt.xlim(time_axis[start],max(time_axis[start:end]))
    plt.ylim(0,1.4*max(top_line[start:end]))
    #plt.legend(["left density", "middle density", "right density"],loc="best")
    plt.savefig(load.print_string("box-plot"+short_string+all_string,p))
<|endoftext|>


<commit_before># Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function

import re
import os
import tempfile
import subprocess

import six

from .. import environment
from ..console import log
from .. import util

WIN = (os.name == "nt")

class Conda(environment.Environment):
    """
    Manage an environment using conda.

    Dependencies are installed using ``conda``.  The benchmarked
    project is installed using ``pip`` (since ``conda`` doesn't have a
    method to install from an arbitrary ``setup.py``).
    """
    tool_name = "conda"
    _matches_cache = {}

    def __init__(self, conf, python, requirements):
        """
        Parameters
        ----------
        conf : Config instance

        python : str
            Version of Python.  Must be of the form "MAJOR.MINOR".

        requirements : dict
            Dictionary mapping a PyPI package name to a version
            identifier string.
        """
        self._python = python
        self._requirements = requirements
        self._conda_channels = conf.conda_channels
        super(Conda, self).__init__(conf, python, requirements)

    @classmethod
    def matches(cls, python):
        # Calling conda can take a long time, so remember the result
        if python not in cls._matches_cache:
            cls._matches_cache[python] = cls._matches(python)
        return cls._matches_cache[python]

    @classmethod
    def _matches(cls, python):
        if not re.match(r'^[0-9].*$', python):
            # The python name should be a version number
            return False

        try:
            conda = util.which('conda')
        except IOError:
            return False
        else:
            # This directory never gets created, since we're just
            # doing a dry run below.  All it needs to be is something
            # that doesn't already exist.
            path = os.path.join(tempfile.gettempdir(), 'check')

            # Check that the version number is valid
            try:
                util.check_call([
                    conda,
                    'create',
                    '--yes',
                    '-p',
                    path,
                    'python={0}'.format(python),
                    '--dry-run'], display_error=False, dots=False)
            except util.ProcessError:
                return False
            else:
                return True

    def _setup(self):
        try:
            conda = util.which('conda')
        except IOError as e:
            raise util.UserError(str(e))

        log.info("Creating conda environment for {0}".format(self.name))

        # create a temporary environment.yml file
        # and use that to generate the env for benchmarking
        env_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
        try:
            env_file.write('name: {0}\n'
                           'channels:\n'.format(self.name))
            env_file.writelines(('   - %s\n' % ch for ch in self._conda_channels))
            env_file.write('dependencies:\n'
                           '   - python={0}\n'
                           '   - wheel\n'
                           '   - pip\n'.format(self._python))

            # categorize & write dependencies based on pip vs. conda
            conda_args, pip_args = self._get_requirements(conda)
            env_file.writelines(('   - %s\n' % s for s in conda_args))
            if pip_args:
                # and now specify the packages that are to be installed in
                # the pip subsection
                env_file.write('   - pip:\n')
                env_file.writelines(('     - %s\n' % s for s in pip_args))

            env_file.close()

            util.check_output([conda] + ['env', 'create', '-f', env_file.name,
                                         '-p', self._path, '--force'])
        except Exception as exc:
            if os.path.isfile(env_file.name):
                with open(env_file.name, 'r') as f:
                    text = f.read()
                log.info("conda env create failed: in {} with:\n{}".format(self._path, text))
            raise
        finally:
            os.unlink(env_file.name)

    def _get_requirements(self, conda):
        if self._requirements:
            # retrieve and return all conda / pip dependencies
            conda_args = []
            pip_args = []

            for key, val in six.iteritems(self._requirements):
                if key.startswith('pip+'):
                    if val:
                        pip_args.append("{0}=={1}".format(key[4:], val))
                    else:
                        pip_args.append(key[4:])
                else:
                    if val:
                        conda_args.append("{0}={1}".format(key, val))
                    else:
                        conda_args.append(key)

            return conda_args, pip_args
        else:
            return [], []

    def _run_pip(self, args, **kwargs):
        # Run pip via python -m pip, so that it works on Windows when
        # upgrading pip itself, and avoids shebang length limit on Linux
        return self.run_executable('python', ['-mpip'] + list(args), **kwargs)

    def install(self, package):
        log.info("Installing into {0}".format(self.name))
        self._run_pip(['install', package], timeout=self._install_timeout)

    def uninstall(self, package):
        log.info("Uninstalling from {0}".format(self.name))
        self._run_pip(['uninstall', '-y', package],
                      timeout=self._install_timeout,
                      valid_return_codes=None)

    def run(self, args, **kwargs):
        log.debug("Running '{0}' in {1}".format(' '.join(args), self.name))
        return self.run_executable('python', args, **kwargs)

    def run_executable(self, executable, args, **kwargs):
        # Conda doesn't guarantee that user site directories are excluded
        kwargs["env"] = dict(kwargs.pop("env", os.environ),
                             PYTHONNOUSERSITE=str("True"))
        return super(Conda, self).run_executable(executable, args, **kwargs)
<commit_msg>Replace `util.which('conda')` with _find_conda()<commit_after># Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function

import re
import os
import tempfile
import subprocess

import six

from .. import environment
from ..console import log
from .. import util

WIN = (os.name == "nt")

def _find_conda():
    """Find the conda executable robustly across conda versions.

    Returns
    -------
    conda : str
        Path to the conda executable.

    Raises
    ------
    IOError
        If the executable cannot be found in either the CONDA_EXE environment
        variable or in the PATH.

    Notes
    -----
    In POSIX platforms in conda >= 4.4, conda can be set up as a bash function
    rather than an executable. (This is to enable the syntax
    ``conda activate env-name``.) In this case, the environment variable
    ``CONDA_EXE`` contains the path to the conda executable. In other cases,
    we use standard search for the appropriate name in the PATH.

    See https://github.com/airspeed-velocity/asv/issues/645 for more details.
    """
    if 'CONDA_EXE' in os.environ:
        conda = os.environ['CONDA_EXE']
    else:
        conda = util.which('conda')
    return conda

class Conda(environment.Environment):
    """
    Manage an environment using conda.

    Dependencies are installed using ``conda``.  The benchmarked
    project is installed using ``pip`` (since ``conda`` doesn't have a
    method to install from an arbitrary ``setup.py``).
    """
    tool_name = "conda"
    _matches_cache = {}

    def __init__(self, conf, python, requirements):
        """
        Parameters
        ----------
        conf : Config instance

        python : str
            Version of Python.  Must be of the form "MAJOR.MINOR".

        requirements : dict
            Dictionary mapping a PyPI package name to a version
            identifier string.
        """
        self._python = python
        self._requirements = requirements
        self._conda_channels = conf.conda_channels
        super(Conda, self).__init__(conf, python, requirements)

    @classmethod
    def matches(cls, python):
        # Calling conda can take a long time, so remember the result
        if python not in cls._matches_cache:
            cls._matches_cache[python] = cls._matches(python)
        return cls._matches_cache[python]

    @classmethod
    def _matches(cls, python):
        if not re.match(r'^[0-9].*$', python):
            # The python name should be a version number
            return False

        try:
            conda = _find_conda()
        except IOError:
            return False
        else:
            # This directory never gets created, since we're just
            # doing a dry run below.  All it needs to be is something
            # that doesn't already exist.
            path = os.path.join(tempfile.gettempdir(), 'check')

            # Check that the version number is valid
            try:
                util.check_call([
                    conda,
                    'create',
                    '--yes',
                    '-p',
                    path,
                    'python={0}'.format(python),
                    '--dry-run'], display_error=False, dots=False)
            except util.ProcessError:
                return False
            else:
                return True

    def _setup(self):
        try:
            conda = _find_conda()
        except IOError as e:
            raise util.UserError(str(e))

        log.info("Creating conda environment for {0}".format(self.name))

        # create a temporary environment.yml file
        # and use that to generate the env for benchmarking
        env_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
        try:
            env_file.write('name: {0}\n'
                           'channels:\n'.format(self.name))
            env_file.writelines(('   - %s\n' % ch for ch in self._conda_channels))
            env_file.write('dependencies:\n'
                           '   - python={0}\n'
                           '   - wheel\n'
                           '   - pip\n'.format(self._python))

            # categorize & write dependencies based on pip vs. conda
            conda_args, pip_args = self._get_requirements(conda)
            env_file.writelines(('   - %s\n' % s for s in conda_args))
            if pip_args:
                # and now specify the packages that are to be installed in
                # the pip subsection
                env_file.write('   - pip:\n')
                env_file.writelines(('     - %s\n' % s for s in pip_args))

            env_file.close()

            util.check_output([conda] + ['env', 'create', '-f', env_file.name,
                                         '-p', self._path, '--force'])
        except Exception as exc:
            if os.path.isfile(env_file.name):
                with open(env_file.name, 'r') as f:
                    text = f.read()
                log.info("conda env create failed: in {} with:\n{}".format(self._path, text))
            raise
        finally:
            os.unlink(env_file.name)

    def _get_requirements(self, conda):
        if self._requirements:
            # retrieve and return all conda / pip dependencies
            conda_args = []
            pip_args = []

            for key, val in six.iteritems(self._requirements):
                if key.startswith('pip+'):
                    if val:
                        pip_args.append("{0}=={1}".format(key[4:], val))
                    else:
                        pip_args.append(key[4:])
                else:
                    if val:
                        conda_args.append("{0}={1}".format(key, val))
                    else:
                        conda_args.append(key)

            return conda_args, pip_args
        else:
            return [], []

    def _run_pip(self, args, **kwargs):
        # Run pip via python -m pip, so that it works on Windows when
        # upgrading pip itself, and avoids shebang length limit on Linux
        return self.run_executable('python', ['-mpip'] + list(args), **kwargs)

    def install(self, package):
        log.info("Installing into {0}".format(self.name))
        self._run_pip(['install', package], timeout=self._install_timeout)

    def uninstall(self, package):
        log.info("Uninstalling from {0}".format(self.name))
        self._run_pip(['uninstall', '-y', package],
                      timeout=self._install_timeout,
                      valid_return_codes=None)

    def run(self, args, **kwargs):
        log.debug("Running '{0}' in {1}".format(' '.join(args), self.name))
        return self.run_executable('python', args, **kwargs)

    def run_executable(self, executable, args, **kwargs):
        # Conda doesn't guarantee that user site directories are excluded
        kwargs["env"] = dict(kwargs.pop("env", os.environ),
                             PYTHONNOUSERSITE=str("True"))
        return super(Conda, self).run_executable(executable, args, **kwargs)
<|endoftext|>