<commit_before>import collections
import copy
import logging
import new
import traceback
import warnings

from operator import itemgetter

logger = logging.getLogger(__name__)

class EnumConstructionException(Exception):
    """
    Raised whenever there's an error in an Enum's declaration.
    """
    pass

class EnumLookupError(Exception):
    """
    Raised when an enum cannot be found by the specified method of lookup.
    """
    pass

class EnumComparisonWarning(Warning):
    """
    Raised when comparing an enum to a value of another type.
    """
    pass

def enum(**enums):
    """
    A basic enum implementation.

    Usage:
        >>> MY_ENUM = enum(FOO=1, BAR=2)
        >>> MY_ENUM.FOO
        1
        >>> MY_ENUM.BAR
        2
    """
    # Enum values must be hashable to support reverse lookup.
    if not all(isinstance(val, collections.Hashable) for val in enums.itervalues()):
        raise EnumConstructionException('All enum values must be hashable.')

    # Cheating by maintaining a copy of original dict for iteration b/c iterators are hard.
    # It must be a deepcopy because new.classobj() modifies the original.
    en = copy.deepcopy(enums)
    e = new.classobj('Enum', (), enums)
    e._dict = en
    e.choices = [(v, k) for k, v in sorted(en.iteritems(), key=itemgetter(1))]  # DEPRECATED
    e.get_id_by_label = e._dict.get
    e.get_label_by_id = dict([(v, k) for (k, v) in e._dict.items()]).get

    return e

class RichEnumValue(object):
    def __init__(self, canonical_name, display_name, *args, **kwargs):
        self.canonical_name = canonical_name
        self.display_name = display_name

    def __unicode__(self):
        return unicode(self.display_name)

    def __repr__(self):
        return unicode("%s - canonical_name: '%s'  display_name: '%s'" % (self.__class__.__name__,
                                                                          self.canonical_name,
                                                                          self.display_name))

    def __str__(self):
        return self.display_name

    def __hash__(self):
        return hash(self.canonical_name)

    def __cmp__(self, other):
        if other is None:
            return -1
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return -1
        return cmp(self.canonical_name, other.canonical_name)

    def __eq__(self, other):
        if other is None:
            return False
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return False
        return self.canonical_name == other.canonical_name

    def __ne__(self, other):
        return not (self.__eq__(other))

    def _warn_about_compare(self, other):
        warnings.warn(
            'Comparing a %s to a %s!' % (type(other), type(self)),
            EnumComparisonWarning,
            stacklevel=3)  # Complain about _warn_about_compare's caller's caller
        logger.debug(''.join(traceback.format_stack()))

    def choicify(self, value_field="canonical_name", display_field="display_name"):
        """
        DEPRECATED

        Returns a tuple that's compatible with Django's choices.
        https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
        """
        return (getattr(self, value_field), getattr(self, display_field))

class OrderedRichEnumValue(RichEnumValue):
    def __init__(self, index, canonical_name, display_name, *args, **kwargs):
        super(OrderedRichEnumValue, self).__init__(canonical_name, display_name, args, kwargs)
        if not isinstance(index, (int, long)):
            raise EnumConstructionException("Index must be an integer type, not: %s" % type(index))
        if index < 0:
            raise EnumConstructionException("Index cannot be a negative number")

        self.index = index

    def __repr__(self):
        return unicode("%s - idx: %d  canonical_name: '%s'  display_name: '%s'"
                       % (self.__class__.__name__,
                          self.index,
                          self.canonical_name,
                          self.display_name))

    def __cmp__(self, other):
        if other is None:
            return -1
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return -1
        return cmp(self.index, other.index)

    def __eq__(self, other):
        if other is None:
            return False
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return False
        return self.index == other.index

def _setup_members(cls_attrs, cls_parents, member_cls):
        members = []

        # Find qualified "EnumValue" attributes.
        # Field names must be UPPERCASE, not prefixed with _ ("internal"),
        # and values must be EnumValue-derived.
        last_type = None
        for attr_key, attr_value in cls_attrs.items():
            # Skip "internal" attributes
            if attr_key.startswith("_"):
                continue

            if not attr_key.isupper():  # Implementation desiderata
                continue

            if isinstance(attr_value, member_cls):
                members.append(attr_value)
            else:
                raise EnumConstructionException("Invalid attribute: %s" % attr_key)

            attr_type = type(attr_value)
            if last_type and attr_type != last_type:
                raise EnumConstructionException("Differing member types: have seen %s,"
                                                " encountered %s" % (last_type, attr_type))
            else:
                last_type = attr_type

        if cls_parents not in [(object, ), (_EnumMethods, )] and not members:
            raise EnumConstructionException(
                "Must specify at least one attribute when using RichEnum")

        return members

class _BaseRichEnumMetaclass(type):
    def __iter__(cls):
        for item in cls._MEMBERS:
            yield item

    def __len__(cls):
        return len(cls._MEMBERS)

    def __contains__(cls, item):
        # Check membership without comparing enum values to other types.
        members = cls.members()
        if not members:
            return False
        if not type(members[0]) == type(item):
            return False
        return (item in members)

class _RichEnumMetaclass(_BaseRichEnumMetaclass):
    def __new__(cls, cls_name, cls_parents, cls_attrs):
        members = _setup_members(cls_attrs, cls_parents, RichEnumValue)
        # Use tuple when possible when setting internal attributes to prevent modification
        cls_attrs['_MEMBERS'] = tuple(members)
        return super(_RichEnumMetaclass, cls).__new__(cls, cls_name, cls_parents, cls_attrs)

class _OrderedRichEnumMetaclass(_BaseRichEnumMetaclass):
    def __new__(cls, cls_name, cls_parents, cls_attrs):
        members = _setup_members(cls_attrs, cls_parents, OrderedRichEnumValue)
        members.sort(key=lambda x: x.index)

        # Use tuple when possible when setting internal attributes to prevent modification
        cls_attrs['_MEMBERS'] = tuple(members)

        # we want to validate that there are not two items at the same index, so lets do that here
        seen = set()
        for member in members:
            if member.index in seen:
                raise EnumConstructionException("Index already defined: %s." % (member.index))
            seen.add(member.index)

        return super(_OrderedRichEnumMetaclass, cls).__new__(cls, cls_name, cls_parents, cls_attrs)

    ############################################################################
    # Overwriting built-ins
    ############################################################################

    def get(self, key, default=None):
        return getattr(self, key, default)

class _EnumMethods(object):
    @classmethod
    def members(cls):
        return cls._MEMBERS  # pylint: disable=E1101

    @classmethod
    def lookup(cls, field, value):
        for member in cls:
            member_value = getattr(member, field)
            if member_value == value:
                return member
            if (
                not isinstance(member_value, str) and
                not isinstance(member_value, unicode) and
                isinstance(member_value, collections.Iterable) and
                value in member_value
            ):
                return member
        raise EnumLookupError('Could not find member matching %s = %s in enum %s'
                              % (field, value, cls)
                              )

    @classmethod
    def from_canonical(cls, canonical_name):
        return cls.lookup('canonical_name', canonical_name)

    @classmethod
    def from_display(cls, display_name):
        return cls.lookup('display_name', display_name)

    @classmethod
    def choices(cls, value_field='canonical_name', display_field='display_name'):
        """
        DEPRECATED

        Returns a list of 2-tuples to be used as an argument to Django Field.choices

        Implementation note: choices() can't be a property
        See:
            http://www.no-ack.org/2011/03/strange-behavior-with-properties-on.html
            http://utcc.utoronto.ca/~cks/space/blog/python/UsingMetaclass03
        """
        return [m.choicify(value_field=value_field, display_field=display_field) for m in cls.members()]

class RichEnum(_EnumMethods):
    """
    Enumeration that can represent a name for referencing (canonical_name) and
    a name for displaying (display_name).

    Usage:

        >>> class MyRichEnum(RichEnum):
        ...    FOO = RichEnumValue(canonical_name="foo", display_name="Foo")
        ...    BAR = RichEnumValue(canonical_name="bar", display_name="Bar")
        ...
        >>> MyRichEnum.FOO
        RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'
        >>> MyRichEnum.from_canonical("foo")
        RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'

    Notes:
        1) display_name can be a string that's marked for translation.
           We recommend that display name be a lazily translated string for
           RichEnums in constants files.
        2) Subclassing RichEnumValue is nice, that way when the RichEnumValue
           is logged/printed, it'll show your custom RichEnumValue and it'll be
           easier to differentiate between all of your different RichEnums.

   """
    __metaclass__ = _RichEnumMetaclass

class OrderedRichEnum(_EnumMethods):
    """
    Use OrderedRichEnum when you need a RichEnum with index-based
    access into the enum, e.g. OrderedRichEnumExample.from_index(0),
    and iteration over the enums sorted by index.

    If you don't need index-based access or sorted entries, please use regular RichEnum
    instead. It's more explicit.

    Usage:

        >>> class MyOrderedRichEnum(OrderedRichEnum):
        ...    FOO = OrderedRichEnumValue(index=1, canonical_name="foo", display_name="Foo")
        ...    BAR = OrderedRichEnumValue(index=2, canonical_name="bar", display_name="Bar")
        ...
        >>> MyOrderedRichEnum.FOO
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
        >>> MyOrderedRichEnum.from_canonical("foo")
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
        >>> MyOrderedRichEnum.from_index(1)
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
    """
    __metaclass__ = _OrderedRichEnumMetaclass

    @classmethod
    def from_index(cls, index):
        return cls.lookup('index', index)
<commit_msg>Suppress type comparison warning in RichEnum.lookup.<commit_after>import collections
import copy
import logging
import new
import traceback
import warnings

from operator import itemgetter

logger = logging.getLogger(__name__)

class EnumConstructionException(Exception):
    """
    Raised whenever there's an error in an Enum's declaration.
    """
    pass

class EnumLookupError(Exception):
    """
    Raised when an enum cannot be found by the specified method of lookup.
    """
    pass

class EnumComparisonWarning(Warning):
    """
    Raised when comparing an enum to a value of another type.
    """
    pass

def enum(**enums):
    """
    A basic enum implementation.

    Usage:
        >>> MY_ENUM = enum(FOO=1, BAR=2)
        >>> MY_ENUM.FOO
        1
        >>> MY_ENUM.BAR
        2
    """
    # Enum values must be hashable to support reverse lookup.
    if not all(isinstance(val, collections.Hashable) for val in enums.itervalues()):
        raise EnumConstructionException('All enum values must be hashable.')

    # Cheating by maintaining a copy of original dict for iteration b/c iterators are hard.
    # It must be a deepcopy because new.classobj() modifies the original.
    en = copy.deepcopy(enums)
    e = new.classobj('Enum', (), enums)
    e._dict = en
    e.choices = [(v, k) for k, v in sorted(en.iteritems(), key=itemgetter(1))]  # DEPRECATED
    e.get_id_by_label = e._dict.get
    e.get_label_by_id = dict([(v, k) for (k, v) in e._dict.items()]).get

    return e

class RichEnumValue(object):
    def __init__(self, canonical_name, display_name, *args, **kwargs):
        self.canonical_name = canonical_name
        self.display_name = display_name

    def __unicode__(self):
        return unicode(self.display_name)

    def __repr__(self):
        return unicode("%s - canonical_name: '%s'  display_name: '%s'" % (self.__class__.__name__,
                                                                          self.canonical_name,
                                                                          self.display_name))

    def __str__(self):
        return self.display_name

    def __hash__(self):
        return hash(self.canonical_name)

    def __cmp__(self, other):
        if other is None:
            return -1
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return -1
        return cmp(self.canonical_name, other.canonical_name)

    def __eq__(self, other):
        if other is None:
            return False
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return False
        return self.canonical_name == other.canonical_name

    def __ne__(self, other):
        return not (self.__eq__(other))

    def _warn_about_compare(self, other):
        warnings.warn(
            'Comparing a %s to a %s!' % (type(other), type(self)),
            EnumComparisonWarning,
            stacklevel=3)  # Complain about _warn_about_compare's caller's caller
        logger.debug(''.join(traceback.format_stack()))

    def choicify(self, value_field="canonical_name", display_field="display_name"):
        """
        DEPRECATED

        Returns a tuple that's compatible with Django's choices.
        https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
        """
        return (getattr(self, value_field), getattr(self, display_field))

class OrderedRichEnumValue(RichEnumValue):
    def __init__(self, index, canonical_name, display_name, *args, **kwargs):
        super(OrderedRichEnumValue, self).__init__(canonical_name, display_name, args, kwargs)
        if not isinstance(index, (int, long)):
            raise EnumConstructionException("Index must be an integer type, not: %s" % type(index))
        if index < 0:
            raise EnumConstructionException("Index cannot be a negative number")

        self.index = index

    def __repr__(self):
        return unicode("%s - idx: %d  canonical_name: '%s'  display_name: '%s'"
                       % (self.__class__.__name__,
                          self.index,
                          self.canonical_name,
                          self.display_name))

    def __cmp__(self, other):
        if other is None:
            return -1
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return -1
        return cmp(self.index, other.index)

    def __eq__(self, other):
        if other is None:
            return False
        if not isinstance(other, type(self)):
            self._warn_about_compare(other)
            return False
        return self.index == other.index

def _setup_members(cls_attrs, cls_parents, member_cls):
        members = []

        # Find qualified "EnumValue" attributes.
        # Field names must be UPPERCASE, not prefixed with _ ("internal"),
        # and values must be EnumValue-derived.
        last_type = None
        for attr_key, attr_value in cls_attrs.items():
            # Skip "internal" attributes
            if attr_key.startswith("_"):
                continue

            if not attr_key.isupper():  # Implementation desiderata
                continue

            if isinstance(attr_value, member_cls):
                members.append(attr_value)
            else:
                raise EnumConstructionException("Invalid attribute: %s" % attr_key)

            attr_type = type(attr_value)
            if last_type and attr_type != last_type:
                raise EnumConstructionException("Differing member types: have seen %s,"
                                                " encountered %s" % (last_type, attr_type))
            else:
                last_type = attr_type

        if cls_parents not in [(object, ), (_EnumMethods, )] and not members:
            raise EnumConstructionException(
                "Must specify at least one attribute when using RichEnum")

        return members

class _BaseRichEnumMetaclass(type):
    def __iter__(cls):
        for item in cls._MEMBERS:
            yield item

    def __len__(cls):
        return len(cls._MEMBERS)

    def __contains__(cls, item):
        # Check membership without comparing enum values to other types.
        members = cls.members()
        if not members:
            return False
        if not type(members[0]) == type(item):
            return False
        return (item in members)

class _RichEnumMetaclass(_BaseRichEnumMetaclass):
    def __new__(cls, cls_name, cls_parents, cls_attrs):
        members = _setup_members(cls_attrs, cls_parents, RichEnumValue)
        # Use tuple when possible when setting internal attributes to prevent modification
        cls_attrs['_MEMBERS'] = tuple(members)
        return super(_RichEnumMetaclass, cls).__new__(cls, cls_name, cls_parents, cls_attrs)

class _OrderedRichEnumMetaclass(_BaseRichEnumMetaclass):
    def __new__(cls, cls_name, cls_parents, cls_attrs):
        members = _setup_members(cls_attrs, cls_parents, OrderedRichEnumValue)
        members.sort(key=lambda x: x.index)

        # Use tuple when possible when setting internal attributes to prevent modification
        cls_attrs['_MEMBERS'] = tuple(members)

        # we want to validate that there are not two items at the same index, so lets do that here
        seen = set()
        for member in members:
            if member.index in seen:
                raise EnumConstructionException("Index already defined: %s." % (member.index))
            seen.add(member.index)

        return super(_OrderedRichEnumMetaclass, cls).__new__(cls, cls_name, cls_parents, cls_attrs)

    ############################################################################
    # Overwriting built-ins
    ############################################################################

    def get(self, key, default=None):
        return getattr(self, key, default)

class _EnumMethods(object):
    @classmethod
    def members(cls):
        return cls._MEMBERS  # pylint: disable=E1101

    @classmethod
    def lookup(cls, field, value):
        for member in cls:
            member_value = getattr(member, field)

            # We filter warnings below; in the case where the field is a list
            # of RichEnumValues and the value we're looking for is a RichEnum,
            # this generates a warning due to mismatched type comparison even
            # though the calling pattern is acceptable.
            # n.b. There's likely a cleaner way to fix this but I was unsure if
            # doing so would break the semantics of this method.
            with warnings.catch_warnings():
                warnings.simplefilter('ignore')
                if member_value == value:
                    return member

            if (
                not isinstance(member_value, str) and
                not isinstance(member_value, unicode) and
                isinstance(member_value, collections.Iterable) and
                value in member_value
            ):
                return member
        raise EnumLookupError('Could not find member matching %s = %s in enum %s'
                              % (field, value, cls)
                              )

    @classmethod
    def from_canonical(cls, canonical_name):
        return cls.lookup('canonical_name', canonical_name)

    @classmethod
    def from_display(cls, display_name):
        return cls.lookup('display_name', display_name)

    @classmethod
    def choices(cls, value_field='canonical_name', display_field='display_name'):
        """
        DEPRECATED

        Returns a list of 2-tuples to be used as an argument to Django Field.choices

        Implementation note: choices() can't be a property
        See:
            http://www.no-ack.org/2011/03/strange-behavior-with-properties-on.html
            http://utcc.utoronto.ca/~cks/space/blog/python/UsingMetaclass03
        """
        return [m.choicify(value_field=value_field, display_field=display_field) for m in cls.members()]

class RichEnum(_EnumMethods):
    """
    Enumeration that can represent a name for referencing (canonical_name) and
    a name for displaying (display_name).

    Usage:

        >>> class MyRichEnum(RichEnum):
        ...    FOO = RichEnumValue(canonical_name="foo", display_name="Foo")
        ...    BAR = RichEnumValue(canonical_name="bar", display_name="Bar")
        ...
        >>> MyRichEnum.FOO
        RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'
        >>> MyRichEnum.from_canonical("foo")
        RichEnumValue - canonical_name: 'foo'  display_name: 'Foo'

    Notes:
        1) display_name can be a string that's marked for translation.
           We recommend that display name be a lazily translated string for
           RichEnums in constants files.
        2) Subclassing RichEnumValue is nice, that way when the RichEnumValue
           is logged/printed, it'll show your custom RichEnumValue and it'll be
           easier to differentiate between all of your different RichEnums.

   """
    __metaclass__ = _RichEnumMetaclass

class OrderedRichEnum(_EnumMethods):
    """
    Use OrderedRichEnum when you need a RichEnum with index-based
    access into the enum, e.g. OrderedRichEnumExample.from_index(0),
    and iteration over the enums sorted by index.

    If you don't need index-based access or sorted entries, please use regular RichEnum
    instead. It's more explicit.

    Usage:

        >>> class MyOrderedRichEnum(OrderedRichEnum):
        ...    FOO = OrderedRichEnumValue(index=1, canonical_name="foo", display_name="Foo")
        ...    BAR = OrderedRichEnumValue(index=2, canonical_name="bar", display_name="Bar")
        ...
        >>> MyOrderedRichEnum.FOO
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
        >>> MyOrderedRichEnum.from_canonical("foo")
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
        >>> MyOrderedRichEnum.from_index(1)
        OrderedRichEnumValue - idx: 1  canonical_name: 'foo'  display_name: 'Foo'
    """
    __metaclass__ = _OrderedRichEnumMetaclass

    @classmethod
    def from_index(cls, index):
        return cls.lookup('index', index)
<|endoftext|>


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

from __future__ import print_function

desc = '''Generate HTML output to visualize optimization records from the YAML files
generated with -fsave-optimization-record and -fdiagnostics-show-hotness.

The tools requires PyYAML to be installed.'''

import yaml
import argparse
import os.path
import subprocess
import shutil

parser = argparse.ArgumentParser(description=desc)
parser.add_argument('yaml_files', nargs='+')
parser.add_argument('output_dir')
args = parser.parse_args()

p = subprocess.Popen(['c++filt', '-n'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def demangle(name):
    p.stdin.write(name + '\n')
    return p.stdout.readline().rstrip()

class Remark(yaml.YAMLObject):
    max_hotness = 0

    @property
    def File(self):
        return self.DebugLoc['File']

    @property
    def Line(self):
        return int(self.DebugLoc['Line'])

    @property
    def Column(self):
        return self.DebugLoc['Column']

    @property
    def DebugLocString(self):
        return "{}:{}:{}".format(self.File, self.Line, self.Column)

    @property
    def DemangledFunctionName(self):
        return demangle(self.Function)

    @classmethod
    def make_link(cls, File, Line):
        return "{}#L{}".format(SourceFileRenderer.html_file_name(File), Line)

    @property
    def Link(self):
        return Remark.make_link(self.File, self.Line)

    def getArgString(self, mapping):
        mapping = mapping.copy()
        dl = mapping.get('DebugLoc')
        if dl:
            del mapping['DebugLoc']

        assert(len(mapping) == 1)
        (key, value) = mapping.items()[0]

        if key == 'Caller' or key == 'Callee':
            value = demangle(value)

        if dl and key != 'Caller':
            return "{}".format(
                Remark.make_link(dl['File'], dl['Line']), value)
        else:
            return value

    @property
    def message(self):
        # Args is a list of mappings (dictionaries)
        values = [self.getArgString(mapping) for mapping in self.Args]
        return "".join(values)

    @property
    def RelativeHotness(self):
        return int(round(self.Hotness * 100 / Remark.max_hotness))

class Analysis(Remark):
    yaml_tag = '!Analysis'

    @property
    def color(self): return "white"

class AnalysisFPCommute(Analysis):
    yaml_tag = '!AnalysisFPCommute'

class AnalysisAliasing(Analysis):
    yaml_tag = '!AnalysisAliasing'

class Passed(Remark):
    yaml_tag = '!Passed'

    @property
    def color(self): return "green"

class Missed(Remark):
    yaml_tag = '!Missed'

    @property
    def color(self): return "red"

class SourceFileRenderer:
    def __init__(self, filename):
        self.source_stream = open(filename)
        self.stream = open(os.path.join(args.output_dir, SourceFileRenderer.html_file_name(filename)), 'w')

    def render_source_line(self, linenum, line):
        print('''

{linenum}

{line}

'''.format(**locals()), file=self.stream)

    def render_inline_remarks(self, r):
        print('''

{r.RelativeHotness}%
{r.Pass}
{r.message}

'''.format(**locals()), file=self.stream)

    def render(self, line_remarks):
        print('''

<link rel='stylesheet' type='text/css' href='style.css'>

Line
Hotness
Optimization
Source

''', file=self.stream)
        for (linenum, line) in enumerate(self.source_stream.readlines(), start=1):
            self.render_source_line(linenum, line)
            for remark in line_remarks.get(linenum, []):
                self.render_inline_remarks(remark)
        print('''

''', file=self.stream)

    @classmethod
    def html_file_name(cls, filename):
        return filename.replace('/', '_') + ".html"

class IndexRenderer:
    def __init__(self):
        self.stream = open(os.path.join(args.output_dir, 'index.html'), 'w')

    def render_entry(self, r):
        print('''

{r.DebugLocString}
{r.RelativeHotness}%
{r.DemangledFunctionName}
{r.Pass}

'''.format(**locals()), file=self.stream)

    def render(self, all_remarks):
        print('''

<link rel='stylesheet' type='text/css' href='style.css'>

Source Location
Hotness
Function
Pass

''', file=self.stream)
        for remark in all_remarks:
            self.render_entry(remark)
        print('''

''', file=self.stream)

all_remarks = []
file_remarks  = dict()

for input_file in args.yaml_files:
    f = open(input_file)
    docs = yaml.load_all(f)
    for remark in docs:
        if hasattr(remark, 'Hotness'):
            file_remarks.setdefault(remark.File, dict()).setdefault(remark.Line, []).append(remark);
            all_remarks.append(remark)
            Remark.max_hotness = max(Remark.max_hotness, remark.Hotness)

all_remarks = sorted(all_remarks, key=lambda r: r.Hotness, reverse=True)

if not os.path.exists(args.output_dir):
    os.mkdir(args.output_dir)

for (filename, remarks) in file_remarks.iteritems():
    SourceFileRenderer(filename).render(remarks)

IndexRenderer().render(all_remarks)

shutil.copy(os.path.join(os.path.dirname(os.path.realpath(__file__)), "style.css"), args.output_dir)
<commit_msg>[opt-viewer] Avoid division by zero<commit_after>#!/usr/bin/env python2.7

from __future__ import print_function

desc = '''Generate HTML output to visualize optimization records from the YAML files
generated with -fsave-optimization-record and -fdiagnostics-show-hotness.

The tools requires PyYAML to be installed.'''

import yaml
import argparse
import os.path
import subprocess
import shutil

parser = argparse.ArgumentParser(description=desc)
parser.add_argument('yaml_files', nargs='+')
parser.add_argument('output_dir')
args = parser.parse_args()

p = subprocess.Popen(['c++filt', '-n'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def demangle(name):
    p.stdin.write(name + '\n')
    return p.stdout.readline().rstrip()

class Remark(yaml.YAMLObject):
    max_hotness = 1

    @property
    def File(self):
        return self.DebugLoc['File']

    @property
    def Line(self):
        return int(self.DebugLoc['Line'])

    @property
    def Column(self):
        return self.DebugLoc['Column']

    @property
    def DebugLocString(self):
        return "{}:{}:{}".format(self.File, self.Line, self.Column)

    @property
    def DemangledFunctionName(self):
        return demangle(self.Function)

    @classmethod
    def make_link(cls, File, Line):
        return "{}#L{}".format(SourceFileRenderer.html_file_name(File), Line)

    @property
    def Link(self):
        return Remark.make_link(self.File, self.Line)

    def getArgString(self, mapping):
        mapping = mapping.copy()
        dl = mapping.get('DebugLoc')
        if dl:
            del mapping['DebugLoc']

        assert(len(mapping) == 1)
        (key, value) = mapping.items()[0]

        if key == 'Caller' or key == 'Callee':
            value = demangle(value)

        if dl and key != 'Caller':
            return "{}".format(
                Remark.make_link(dl['File'], dl['Line']), value)
        else:
            return value

    @property
    def message(self):
        # Args is a list of mappings (dictionaries)
        values = [self.getArgString(mapping) for mapping in self.Args]
        return "".join(values)

    @property
    def RelativeHotness(self):
        return int(round(self.Hotness * 100 / Remark.max_hotness))

class Analysis(Remark):
    yaml_tag = '!Analysis'

    @property
    def color(self): return "white"

class AnalysisFPCommute(Analysis):
    yaml_tag = '!AnalysisFPCommute'

class AnalysisAliasing(Analysis):
    yaml_tag = '!AnalysisAliasing'

class Passed(Remark):
    yaml_tag = '!Passed'

    @property
    def color(self): return "green"

class Missed(Remark):
    yaml_tag = '!Missed'

    @property
    def color(self): return "red"

class SourceFileRenderer:
    def __init__(self, filename):
        self.source_stream = open(filename)
        self.stream = open(os.path.join(args.output_dir, SourceFileRenderer.html_file_name(filename)), 'w')

    def render_source_line(self, linenum, line):
        print('''

{linenum}

{line}

'''.format(**locals()), file=self.stream)

    def render_inline_remarks(self, r):
        print('''

{r.RelativeHotness}%
{r.Pass}
{r.message}

'''.format(**locals()), file=self.stream)

    def render(self, line_remarks):
        print('''

<link rel='stylesheet' type='text/css' href='style.css'>

Line
Hotness
Optimization
Source

''', file=self.stream)
        for (linenum, line) in enumerate(self.source_stream.readlines(), start=1):
            self.render_source_line(linenum, line)
            for remark in line_remarks.get(linenum, []):
                self.render_inline_remarks(remark)
        print('''

''', file=self.stream)

    @classmethod
    def html_file_name(cls, filename):
        return filename.replace('/', '_') + ".html"

class IndexRenderer:
    def __init__(self):
        self.stream = open(os.path.join(args.output_dir, 'index.html'), 'w')

    def render_entry(self, r):
        print('''

{r.DebugLocString}
{r.RelativeHotness}%
{r.DemangledFunctionName}
{r.Pass}

'''.format(**locals()), file=self.stream)

    def render(self, all_remarks):
        print('''

<link rel='stylesheet' type='text/css' href='style.css'>

Source Location
Hotness
Function
Pass

''', file=self.stream)
        for remark in all_remarks:
            self.render_entry(remark)
        print('''

''', file=self.stream)

all_remarks = []
file_remarks  = dict()

for input_file in args.yaml_files:
    f = open(input_file)
    docs = yaml.load_all(f)
    for remark in docs:
        if hasattr(remark, 'Hotness'):
            file_remarks.setdefault(remark.File, dict()).setdefault(remark.Line, []).append(remark);
            all_remarks.append(remark)
            Remark.max_hotness = max(Remark.max_hotness, remark.Hotness)

all_remarks = sorted(all_remarks, key=lambda r: r.Hotness, reverse=True)

if not os.path.exists(args.output_dir):
    os.mkdir(args.output_dir)

for (filename, remarks) in file_remarks.iteritems():
    SourceFileRenderer(filename).render(remarks)

IndexRenderer().render(all_remarks)

shutil.copy(os.path.join(os.path.dirname(os.path.realpath(__file__)), "style.css"), args.output_dir)
<|endoftext|>


<commit_before># coding: utf-8

from flask import abort, Blueprint, redirect, render_template, request
from htmlmin.minify import html_minify
from random import choice
from sqlalchemy import desc
from whiskyton import app, db
from whiskyton.models import Whisky, Correlation

site = Blueprint('site', __name__)

@site.route('/')
def index():
    return html_minify(render_template('home.html'))

@site.route('/search', methods=['GET', 'POST'])
def search():
    whisky = Whisky(distillery=request.args['s'])
    row = Whisky.query.filter_by(slug=whisky.get_slug()).first()
    if row is None:
        return render_template('404.html', slug=request.args['s'])
    else:
        return redirect('/' + str(row.slug))

@site.route('/<whisky_slug>')
def whisky_page(whisky_slug):

    # error page if whisky doesn't exist
    reference = Whisky.query.filter_by(slug=whisky_slug).first()
    if reference is None:
        return abort(404)

    # load correlations
    else:

        # query
        whiskies = Correlation.query\
            .add_entity(Whisky)\
            .filter(Correlation.reference == reference.id)\
            .filter(Correlation.r > 0.5)\
            .join(Whisky, Correlation.whisky == Whisky.id)\
            .order_by(desc(Correlation.r))\
            .limit(9)

        # if query succeeded
        if whiskies is not None:

            # page views count
            reference.views += 1
            db.session.add(reference)
            db.session.commit()

            # build result
            title = 'Whiskies for %s lovers | %s' % (reference.distillery,
                                                     app.config['MAIN_TITLE'])
            return html_minify(render_template(
                'whiskies.html',
                main_title=title,
                whiskies=whiskies,
                reference=reference,
                count=whiskies.count(),
                result_page=True
            ))

        # if queries fail, return 404
        else:
            return abort(404)

@site.route('/w/<whisky_id>')
def search_id(whisky_id):
    reference = Whisky.query.filter_by(id=whisky_id).first()
    if reference is None:
        return abort(404)
    else:
        return redirect('/' + reference.slug)

@site.errorhandler(404)
def page_not_found(error):
    return html_minify(render_template('404.html', error=error)), 404

@site.context_processor
def inject_main_vars():

    # get a random whisky (proportional to the page views)
    whisky_ids = list()
    for whisky in db.session.query(Whisky.id, Whisky.views).all():
        whisky.views = whisky.views if whisky.views else 1
        whisky_ids.extend([whisky.id] * whisky.views)
    random_whisky = choice(whisky_ids)

    # return useful variables
    return {
        'main_title': app.config['MAIN_TITLE'],
        'headline': app.config['HEADLINE'],
        'remote_scripts': app.config['GOOGLE_ANALYTICS'],
        'random_one': Whisky.query.get(random_whisky)
    }
<commit_msg>Remove inline if/else statement<commit_after># coding: utf-8

from flask import abort, Blueprint, redirect, render_template, request
from htmlmin.minify import html_minify
from random import choice
from sqlalchemy import desc
from whiskyton import app, db
from whiskyton.models import Whisky, Correlation

site = Blueprint('site', __name__)

@site.route('/')
def index():
    return html_minify(render_template('home.html'))

@site.route('/search', methods=['GET', 'POST'])
def search():
    whisky = Whisky(distillery=request.args['s'])
    row = Whisky.query.filter_by(slug=whisky.get_slug()).first()
    if row is None:
        return render_template('404.html', slug=request.args['s'])
    else:
        return redirect('/' + str(row.slug))

@site.route('/<whisky_slug>')
def whisky_page(whisky_slug):

    # error page if whisky doesn't exist
    reference = Whisky.query.filter_by(slug=whisky_slug).first()
    if reference is None:
        return abort(404)

    # load correlations
    else:

        # query
        whiskies = Correlation.query\
            .add_entity(Whisky)\
            .filter(Correlation.reference == reference.id)\
            .filter(Correlation.r > 0.5)\
            .join(Whisky, Correlation.whisky == Whisky.id)\
            .order_by(desc(Correlation.r))\
            .limit(9)

        # if query succeeded
        if whiskies is not None:

            # page views count
            reference.views += 1
            db.session.add(reference)
            db.session.commit()

            # build result
            title = 'Whiskies for %s lovers | %s' % (reference.distillery,
                                                     app.config['MAIN_TITLE'])
            return html_minify(render_template(
                'whiskies.html',
                main_title=title,
                whiskies=whiskies,
                reference=reference,
                count=whiskies.count(),
                result_page=True
            ))

        # if queries fail, return 404
        else:
            return abort(404)

@site.route('/w/<whisky_id>')
def search_id(whisky_id):
    reference = Whisky.query.filter_by(id=whisky_id).first()
    if reference is None:
        return abort(404)
    else:
        return redirect('/' + reference.slug)

@site.errorhandler(404)
def page_not_found(error):
    return html_minify(render_template('404.html', error=error)), 404

@site.context_processor
def inject_main_vars():

    # get a random whisky (proportional to the page views)
    whisky_ids = list()
    for whisky in db.session.query(Whisky.id, Whisky.views).all():
        weight = whisky.views
        if not weight:
            weight = 1
        whisky_ids.extend([whisky.id] * weight)
    random_whisky = choice(whisky_ids)

    # return useful variables
    return {
        'main_title': app.config['MAIN_TITLE'],
        'headline': app.config['HEADLINE'],
        'remote_scripts': app.config['GOOGLE_ANALYTICS'],
        'random_one': Whisky.query.get(random_whisky)
    }
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.

Python port was written by Xavier Grangier for Recrutae

Gravity.com licenses this file
to you 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 os
import re
import string
from goose.utils import FileHelper
from goose.utils.encoding import smart_unicode
from goose.utils.encoding import smart_str
from goose.utils.encoding import DjangoUnicodeDecodeError

TABSSPACE = re.compile(r'[\s\t]+')

def innerTrim(value):
    if isinstance(value, (unicode, str)):
        # remove tab and white space
        value = re.sub(TABSSPACE, ' ', value)
        value = ''.join(value.splitlines())
        return value.strip()
    return ''

def encodeValue(value):
    string_org = value
    try:
        value = smart_unicode(value)
    except (UnicodeEncodeError, DjangoUnicodeDecodeError):
        value = smart_str(value)
    except:
        value = string_org
    return value

class WordStats(object):

    def __init__(self):
        # total number of stopwords or
        # good words that we can calculate
        self.stop_word_count = 0

        # total number of words on a node
        self.word_count = 0

        # holds an actual list
        # of the stop words we found
        self.stop_words = []

    def get_stop_words(self):
        return self.stop_words

    def set_stop_words(self, words):
        self.stop_words = words

    def get_stopword_count(self):
        return self.stop_word_count

    def set_stopword_count(self, wordcount):
        self.stop_word_count = wordcount

    def get_word_count(self):
        return self.word_count

    def set_word_count(self, cnt):
        self.word_count = cnt

class StopWords(object):

    PUNCTUATION = re.compile("[^\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lo}\\p{Nd}\\p{Pc}\\s]")
    TRANS_TABLE = string.maketrans('', '')
    _cached_stop_words = {}

    def __init__(self, language='en'):
        # TODO replace 'x' with class
        # to generate dynamic path for file to load
        if not language in self._cached_stop_words:
            path = os.path.join('text', 'stopwords-%s.txt' % language)
            self._cached_stop_words[language] = set(FileHelper.loadResourceFile(path).splitlines())
        self.STOP_WORDS = self._cached_stop_words[language]

    def remove_punctuation(self, content):
        # code taken form
        # http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
        if isinstance(content, unicode):
            content = content.encode('utf-8')
        return content.translate(self.TRANS_TABLE, string.punctuation)

    def candiate_words(self, stripped_input):
        return stripped_input.split(' ')

    def get_stopword_count(self, content):
        if not content:
            return WordStats()
        ws = WordStats()
        stripped_input = self.remove_punctuation(content)
        candiate_words = self.candiate_words(stripped_input)
        overlapping_stopwords = []
        c = 0
        for w in candiate_words:
            c += 1
            if w.lower() in self.STOP_WORDS:
                overlapping_stopwords.append(w.lower())

        ws.set_word_count(c)
        ws.set_stopword_count(len(overlapping_stopwords))
        ws.set_stop_words(overlapping_stopwords)
        print vars(ws)
        return hgws

class StopWordsArabic(StopWords):
    """
    Arabic stopwords class
    """
    def __init__(self, language='ar'):
        # force zh languahe code
        super(StopWordsArabic, self).__init__(language='ar')

    def candiate_words(self, stripped_input):
        # jieba build a tree that takes sometime
        # avoid building the tree if we don't use
        # chinese language
        from tashaphyne import ArabicLightStemmer
        stem = ArabicLightStemmer()
        candiate_words = stem.token_pat.split(stripped_input)
        print candiate_words
        return candiate_words

class StopWordsChinese(StopWords):
    """
    Chinese segmentation
    """
    def __init__(self, language='zh'):
        # force zh languahe code
        super(StopWordsChinese, self).__init__(language='zh')

    def candiate_words(self, stripped_input):
        # jieba build a tree that takes sometime
        # avoid building the tree if we don't use
        # chinese language
        import jieba
        return jieba.cut(stripped_input, cut_all=True)
<commit_msg>StopWord class refactor<commit_after># -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.

Python port was written by Xavier Grangier for Recrutae

Gravity.com licenses this file
to you 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 os
import re
import string
from goose.utils import FileHelper
from goose.utils.encoding import smart_unicode
from goose.utils.encoding import smart_str
from goose.utils.encoding import DjangoUnicodeDecodeError

TABSSPACE = re.compile(r'[\s\t]+')

def innerTrim(value):
    if isinstance(value, (unicode, str)):
        # remove tab and white space
        value = re.sub(TABSSPACE, ' ', value)
        value = ''.join(value.splitlines())
        return value.strip()
    return ''

def encodeValue(value):
    string_org = value
    try:
        value = smart_unicode(value)
    except (UnicodeEncodeError, DjangoUnicodeDecodeError):
        value = smart_str(value)
    except:
        value = string_org
    return value

class WordStats(object):

    def __init__(self):
        # total number of stopwords or
        # good words that we can calculate
        self.stop_word_count = 0

        # total number of words on a node
        self.word_count = 0

        # holds an actual list
        # of the stop words we found
        self.stop_words = []

    def get_stop_words(self):
        return self.stop_words

    def set_stop_words(self, words):
        self.stop_words = words

    def get_stopword_count(self):
        return self.stop_word_count

    def set_stopword_count(self, wordcount):
        self.stop_word_count = wordcount

    def get_word_count(self):
        return self.word_count

    def set_word_count(self, cnt):
        self.word_count = cnt

class StopWords(object):

    PUNCTUATION = re.compile("[^\\p{Ll}\\p{Lu}\\p{Lt}\\p{Lo}\\p{Nd}\\p{Pc}\\s]")
    TRANS_TABLE = string.maketrans('', '')
    _cached_stop_words = {}

    def __init__(self, language='en'):
        # TODO replace 'x' with class
        # to generate dynamic path for file to load
        if not language in self._cached_stop_words:
            path = os.path.join('text', 'stopwords-%s.txt' % language)
            self._cached_stop_words[language] = set(FileHelper.loadResourceFile(path).splitlines())
        self.STOP_WORDS = self._cached_stop_words[language]

    def remove_punctuation(self, content):
        # code taken form
        # http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
        if isinstance(content, unicode):
            content = content.encode('utf-8')
        return content.translate(self.TRANS_TABLE, string.punctuation)

    def candiate_words(self, stripped_input):
        return stripped_input.split(' ')

    def get_stopword_count(self, content):
        if not content:
            return WordStats()
        ws = WordStats()
        stripped_input = self.remove_punctuation(content)
        candiate_words = self.candiate_words(stripped_input)
        overlapping_stopwords = []
        c = 0
        for w in candiate_words:
            c += 1
            if w.lower() in self.STOP_WORDS:
                overlapping_stopwords.append(w.lower())

        ws.set_word_count(c)
        ws.set_stopword_count(len(overlapping_stopwords))
        ws.set_stop_words(overlapping_stopwords)
        return hgws

class StopWordsChinese(StopWords):
    """
    Chinese segmentation
    """
    def __init__(self, language='zh'):
        # force zh languahe code
        super(StopWordsChinese, self).__init__(language='zh')

    def candiate_words(self, stripped_input):
        # jieba build a tree that takes sometime
        # avoid building the tree if we don't use
        # chinese language
        import jieba
        return jieba.cut(stripped_input, cut_all=True)
<|endoftext|>


<commit_before>from event import Signal

def _overview(data, width):
    density = len(data) / float(width)
    if density < 1:
        density = 1
    left = 0
    end = len(data)
    res = []
    while int(round(left)) < end:
        i = int(round(left))
        res.append(data[i])
        left = left + density
    return res

class Graph(object):
    """Computes how a sound must be displayed on the screen.

    When an audio file is displayed on the screen, several frames are
    condensed in one column of pixels. This object computes what to display on
    the screen, according to zooming and position in the sound.

    """
    def __init__(self, sound):
        self.changed = Signal()
        self._width = 100
        self.set_sound(sound)

    def set_sound(self, sound):
        self._sound = sound
        self._view_start = 0
        self._view_end = len(self._sound._data)
        self._sound.changed.connect(self.update)
        self.changed()

    def update(self):
        "called when sound has changed."
        self._adjust_view()
        self.changed()

    def view_starts_at(self, value):
        "Moves the view start and keep the view length"
        l = self._view_end - self._view_start
        self._view_start = value
        self._view_end = value + l
        self.changed()

    def get_info(self):
        "Returns information about the graph: length, view start, view end"
        length = len(self._sound._data)
        start = self._view_start
        end = self._view_end
        return (length, start, end)

    def get_density(self):
        "Number of frames per pixel."
        d = (self._view_end - self._view_start) / float(self._width)
        if d == 0:
            # empty sound
            d = 1
        return d

    def _zoom(self, point, factor):
        """Expand or shrink view according to factor.

        0 < factor < 1     zoom in
            factor = 1     unchanged
        1 < factor < +Inf  zoom out

        The zoom factor is relative to the current zoom, ie.::

            self._zoom(x, n)
            self._zoom(x, m)

        is equivalent to::

            self._zoom(x, n * m)

        """
        l = self._view_end - self._view_start
        l = l * factor
        self._view_start = point - l * 0.5
        self._view_end = point + l * 0.5
        self._adjust_view()
        self.changed()

    def zoom_in(self):
        "Make view twice smaller, centering on the middle of the view."
        mid = self._view_start + (self._view_end - self._view_start) * 0.5
        self._zoom(mid, 0.5)

    def zoom_out(self):
        "Make view twice larger, centering on the middle of the view."
        mid = self._view_start + (self._view_end - self._view_start) * 0.5
        self._zoom(mid, 2)

    def zoom_fit(self):
        "Fit everything in the view."
        self._view_start = 0
        self._view_end = len(self._sound._data)
        self.changed()

    def _scroll(self, factor):
        """Shift the view.

        A negative factor shift the view to the left, a positive one
        to the right. The absolute value of the factor determines the
        length of the shift, relative to the view length. For example:
        0.1 is 10%, O.5 is one half, 1.0 is 100%.

        """
        l = self._view_end - self._view_start
        l = int(l * factor)
        self._view_start += l
        self._view_end += l
        self._adjust_view()
        self.changed()

    def scroll_left(self):
        self._scroll(-0.20)

    def scroll_right(self):
        self._scroll(0.20)

    def set_width(self, width):
        "Set the number of values the graph must have."
        self._width = int(width)
        self._adjust_view()
        self.changed()

    def get_values(self):
        "Return the graph values."
        width = self._width
        start, end = [int(round(v)) for v in self._view_start, self._view_end]
        visible = self._sound._data[start:end]
        o = _overview(visible, width)
        return o

    def _adjust_view(self):
        """Adjust view boundaries.

        After scrolling or zooming, the view must be adjusted. Indeed,
        it must not start before 0 or end after the sound. Also, the
        view must contain enough values to fit in the width.

        """
        width = self._width

        # When the zoom is too strong and the width cannot be filled,
        # zoom out.
        view_length = self._view_end - self._view_start
        if view_length < width:
            self._view_start -= (width - view_length) * 0.5
            self._view_end = self._view_start + width

        # Check and adjust visible bounds.
        if self._view_start < 0:
            self._view_end += -self._view_start
            self._view_start = 0
        elif self._view_end > len(self._sound._data):
            self._view_start -= (self._view_end - len(self._sound._data))
            self._view_end = len(self._sound._data)

        # Ultimate check on bounds.
        if self._view_start < 0:
            self._view_start = 0
        if self._view_end > len(self._sound._data):
            self._view_end = len(self._sound._data)

def test_overview():
    b = xrange(1000000000)
    print _overview(b, 30)

def test_Graph():
    c = Graph()
    c.set_data(range(1000))
    c.set_width(200)
    o = c.get_values()

    c = Graph()
    class Foo:
        def foo(self):
            print "Changed."
    f = Foo()
    c = Graph()
    c.changed.connect(f.foo)
    c.set_data(range(1000))

def test_zoom():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)

    g.set_width(4)
    g._zoom(point=1.5, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g._zoom(point=0, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g._zoom(point=6, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(2)
    g.set_data(data)
    g._zoom(point=1.5, factor=0.5)
    o = g.get_values()
    assert o == [2, 3]

    g._zoom(point=1.5, factor=0.5)
    g.set_width(4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(2)
    g._zoom(point=0, factor=0.5)
    o = g.get_values()
    assert o == [1, 2]

    g.set_width(4)
    g._zoom(point=0, factor=0.25)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(4)
    g._zoom(point=4, factor=4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(3)
    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5)
    o = g.get_values()
    assert o == [2, 3, 4]

    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5)
    g._zoom(point=2, factor=0.5)
    start, end = g._view_start, g._view_end

    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5 * 0.5)
    assert (start, end) == (g._view_start, g._view_end)

def test_zoom_in():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)

    g.set_width(2)
    g.zoom_in()
    o = g.get_values()
    assert o == [2, 3]

    g.zoom_out()
    g.set_width(4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

def test_scroll():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)
    g.set_width(4)

    g.scroll_right()
    length, start, end = g.get_info()
    assert length == 4
    assert start == 0
    assert end == 4

if __name__ == "__main__":
    test_overview()
    test_Graph()
    test_zoom()
    test_zoom_in()
    test_scroll()
<commit_msg>fix bug in get_density()<commit_after>from event import Signal

def _overview(data, width):
    density = len(data) / float(width)
    if density < 1:
        density = 1
    left = 0
    end = len(data)
    res = []
    while int(round(left)) < end:
        i = int(round(left))
        res.append(data[i])
        left = left + density
    return res

class Graph(object):
    """Computes how a sound must be displayed on the screen.

    When an audio file is displayed on the screen, several frames are
    condensed in one column of pixels. This object computes what to display on
    the screen, according to zooming and position in the sound.

    """
    def __init__(self, sound):
        self.changed = Signal()
        self._width = 100
        self.set_sound(sound)

    def set_sound(self, sound):
        self._sound = sound
        self._view_start = 0
        self._view_end = len(self._sound._data)
        self._sound.changed.connect(self.update)
        self.changed()

    def update(self):
        "called when sound has changed."
        self._adjust_view()
        self.changed()

    def view_starts_at(self, value):
        "Moves the view start and keep the view length"
        l = self._view_end - self._view_start
        self._view_start = value
        self._view_end = value + l
        self.changed()

    def get_info(self):
        "Returns information about the graph: length, view start, view end"
        length = len(self._sound._data)
        start = self._view_start
        end = self._view_end
        return (length, start, end)

    def get_density(self):
        "Number of frames per pixel."
        number_frames_view = (self._view_end - self._view_start)
        if number_frames_view < self._width:
            # the sound is too small to fill the width
            d = 1
        else:
            d = float(number_frames_view) / self._width
        return d

    def _zoom(self, point, factor):
        """Expand or shrink view according to factor.

        0 < factor < 1     zoom in
            factor = 1     unchanged
        1 < factor < +Inf  zoom out

        The zoom factor is relative to the current zoom, ie.::

            self._zoom(x, n)
            self._zoom(x, m)

        is equivalent to::

            self._zoom(x, n * m)

        """
        l = self._view_end - self._view_start
        l = l * factor
        self._view_start = point - l * 0.5
        self._view_end = point + l * 0.5
        self._adjust_view()
        self.changed()

    def zoom_in(self):
        "Make view twice smaller, centering on the middle of the view."
        mid = self._view_start + (self._view_end - self._view_start) * 0.5
        self._zoom(mid, 0.5)

    def zoom_out(self):
        "Make view twice larger, centering on the middle of the view."
        mid = self._view_start + (self._view_end - self._view_start) * 0.5
        self._zoom(mid, 2)

    def zoom_fit(self):
        "Fit everything in the view."
        self._view_start = 0
        self._view_end = len(self._sound._data)
        self.changed()

    def _scroll(self, factor):
        """Shift the view.

        A negative factor shift the view to the left, a positive one
        to the right. The absolute value of the factor determines the
        length of the shift, relative to the view length. For example:
        0.1 is 10%, O.5 is one half, 1.0 is 100%.

        """
        l = self._view_end - self._view_start
        l = int(l * factor)
        self._view_start += l
        self._view_end += l
        self._adjust_view()
        self.changed()

    def scroll_left(self):
        self._scroll(-0.20)

    def scroll_right(self):
        self._scroll(0.20)

    def set_width(self, width):
        "Set the number of values the graph must have."
        self._width = int(width)
        self._adjust_view()
        self.changed()

    def get_values(self):
        "Return the graph values."
        width = self._width
        start, end = [int(round(v)) for v in self._view_start, self._view_end]
        visible = self._sound._data[start:end]
        o = _overview(visible, width)
        return o

    def _adjust_view(self):
        """Adjust view boundaries.

        After scrolling or zooming, the view must be adjusted. Indeed,
        it must not start before 0 or end after the sound. Also, the
        view must contain enough values to fit in the width.

        """
        width = self._width

        # When the zoom is too strong and the width cannot be filled,
        # zoom out.
        view_length = self._view_end - self._view_start
        if view_length < width:
            self._view_start -= (width - view_length) * 0.5
            self._view_end = self._view_start + width

        # Check and adjust visible bounds.
        if self._view_start < 0:
            self._view_end += -self._view_start
            self._view_start = 0
        elif self._view_end > len(self._sound._data):
            self._view_start -= (self._view_end - len(self._sound._data))
            self._view_end = len(self._sound._data)

        # Ultimate check on bounds.
        if self._view_start < 0:
            self._view_start = 0
        if self._view_end > len(self._sound._data):
            self._view_end = len(self._sound._data)

def test_overview():
    b = xrange(1000000000)
    print _overview(b, 30)

def test_Graph():
    c = Graph()
    c.set_data(range(1000))
    c.set_width(200)
    o = c.get_values()

    c = Graph()
    class Foo:
        def foo(self):
            print "Changed."
    f = Foo()
    c = Graph()
    c.changed.connect(f.foo)
    c.set_data(range(1000))

def test_zoom():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)

    g.set_width(4)
    g._zoom(point=1.5, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g._zoom(point=0, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g._zoom(point=6, factor=1)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(2)
    g.set_data(data)
    g._zoom(point=1.5, factor=0.5)
    o = g.get_values()
    assert o == [2, 3]

    g._zoom(point=1.5, factor=0.5)
    g.set_width(4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(2)
    g._zoom(point=0, factor=0.5)
    o = g.get_values()
    assert o == [1, 2]

    g.set_width(4)
    g._zoom(point=0, factor=0.25)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(4)
    g._zoom(point=4, factor=4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

    g.set_width(3)
    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5)
    o = g.get_values()
    assert o == [2, 3, 4]

    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5)
    g._zoom(point=2, factor=0.5)
    start, end = g._view_start, g._view_end

    data = [1, 2, 3, 4, 5]
    g.set_data(data)
    g._zoom(point=2, factor=0.5 * 0.5)
    assert (start, end) == (g._view_start, g._view_end)

def test_zoom_in():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)

    g.set_width(2)
    g.zoom_in()
    o = g.get_values()
    assert o == [2, 3]

    g.zoom_out()
    g.set_width(4)
    o = g.get_values()
    assert o == [1, 2, 3, 4]

def test_scroll():
    data = [1, 2, 3, 4]
    g = Graph()
    g.set_data(data)
    g.set_width(4)

    g.scroll_right()
    length, start, end = g.get_info()
    assert length == 4
    assert start == 0
    assert end == 4

if __name__ == "__main__":
    test_overview()
    test_Graph()
    test_zoom()
    test_zoom_in()
    test_scroll()
<|endoftext|>


<commit_before># This file is part of the Indico plugins.
# Copyright (C) 2020 CERN and ENEA
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.

from __future__ import unicode_literals

import random
import string

from requests.exceptions import HTTPError

from indico.core.db import db
from indico.modules.users.models.emails import UserEmail
from indico.modules.users.models.users import User
from indico.modules.vc.exceptions import VCRoomError, VCRoomNotFoundError
from indico.util.date_time import now_utc

from indico_vc_zoom import _
from indico_vc_zoom.api import ZoomIndicoClient

def find_enterprise_email(user):
    """Find a user's first e-mail address which can be used by the Zoom API.

    :param user: the `User` in question
    :return: the e-mail address if it exists, otherwise `None`
    """
    from indico_vc_zoom.plugin import ZoomPlugin
    providers = [auth.strip() for auth in ZoomPlugin.settings.get('email_domains').split(',')]
    result = UserEmail.query.filter(
        UserEmail.user == user,
        ~User.is_blocked,
        ~User.is_deleted,
        db.or_(UserEmail.email.ilike("%%@{}".format(provider)) for provider in providers)
    ).join(User).first()
    return result.email if result else None

def gen_random_password():
    """Generate a random 10-character-long alphanumeric string."""
    return ''.join(random.sample(string.ascii_lowercase + string.ascii_uppercase + string.digits, 10))

def fetch_zoom_meeting(vc_room, client=None, is_webinar=False):
    """Fetch a Zoom meeting from the Zoom API.

    :param vc_room: The `VCRoom` object
    :param client: a `ZoomIndicoClient` object, otherwise a fresh one will be created
    :param is_webinar: whether the call concerns a webinar (used to call the correct endpoint)
    """
    try:
        client = client or ZoomIndicoClient()
        if is_webinar:
            return client.get_webinar(vc_room.data['zoom_id'])
        return client.get_meeting(vc_room.data['zoom_id'])
    except HTTPError as e:
        if e.response.status_code in {400, 404}:
            # Indico will automatically mark this room as deleted
            raise VCRoomNotFoundError(_("This room has been deleted from Zoom"))
        else:
            from indico_vc_zoom.plugin import ZoomPlugin
            ZoomPlugin.logger.exception('Error getting Zoom Room: %s', e.response.content)
            raise VCRoomError(_("Problem fetching room from Zoom. Please contact support if the error persists."))

def update_zoom_meeting(zoom_id, changes, is_webinar=False):
    """Update a meeting which already exists in the Zoom API.

    :param zoom_id: ID of the meeting
    :param changes: dictionary with new attribute values
    :param is_webinar: whether the call concerns a webinar (used to call the correct endpoint)
    """
    client = ZoomIndicoClient()
    try:
        if is_webinar:
            client.update_webinar(zoom_id, changes)
        else:
            client.update_meeting(zoom_id, changes)
    except HTTPError as e:
        from indico_vc_zoom.plugin import ZoomPlugin
        ZoomPlugin.logger.exception("Error updating meeting '%s': %s", zoom_id, e.response.content)
        raise VCRoomError(_("Can't update meeting. Please contact support if the error persists."))

def get_schedule_args(obj):
    """Create a dictionary with scheduling information from an Event/Contribution/SessionBlock.

    :param obj: An `Event`, `Contribution` or `SessionBlock`
    """
    duration = obj.end_dt - obj.start_dt

    if obj.start_dt < now_utc():
        return {}

    return {
        'start_time': obj.start_dt,
        'duration': duration.total_seconds() / 60,
    }
<commit_msg>VC/Zoom: generate number-only passwords<commit_after># This file is part of the Indico plugins.
# Copyright (C) 2020 CERN and ENEA
#
# The Indico plugins are free software; you can redistribute
# them and/or modify them under the terms of the MIT License;
# see the LICENSE file for more details.

from __future__ import unicode_literals

import random
import string

from requests.exceptions import HTTPError

from indico.core.db import db
from indico.modules.users.models.emails import UserEmail
from indico.modules.users.models.users import User
from indico.modules.vc.exceptions import VCRoomError, VCRoomNotFoundError
from indico.util.date_time import now_utc

from indico_vc_zoom import _
from indico_vc_zoom.api import ZoomIndicoClient

def find_enterprise_email(user):
    """Find a user's first e-mail address which can be used by the Zoom API.

    :param user: the `User` in question
    :return: the e-mail address if it exists, otherwise `None`
    """
    from indico_vc_zoom.plugin import ZoomPlugin
    providers = [auth.strip() for auth in ZoomPlugin.settings.get('email_domains').split(',')]
    result = UserEmail.query.filter(
        UserEmail.user == user,
        ~User.is_blocked,
        ~User.is_deleted,
        db.or_(UserEmail.email.ilike("%%@{}".format(provider)) for provider in providers)
    ).join(User).first()
    return result.email if result else None

def gen_random_password():
    """Generate a random 8-character-long numeric string."""
    return ''.join(random.choice(string.digits) for _ in range(8))

def fetch_zoom_meeting(vc_room, client=None, is_webinar=False):
    """Fetch a Zoom meeting from the Zoom API.

    :param vc_room: The `VCRoom` object
    :param client: a `ZoomIndicoClient` object, otherwise a fresh one will be created
    :param is_webinar: whether the call concerns a webinar (used to call the correct endpoint)
    """
    try:
        client = client or ZoomIndicoClient()
        if is_webinar:
            return client.get_webinar(vc_room.data['zoom_id'])
        return client.get_meeting(vc_room.data['zoom_id'])
    except HTTPError as e:
        if e.response.status_code in {400, 404}:
            # Indico will automatically mark this room as deleted
            raise VCRoomNotFoundError(_("This room has been deleted from Zoom"))
        else:
            from indico_vc_zoom.plugin import ZoomPlugin
            ZoomPlugin.logger.exception('Error getting Zoom Room: %s', e.response.content)
            raise VCRoomError(_("Problem fetching room from Zoom. Please contact support if the error persists."))

def update_zoom_meeting(zoom_id, changes, is_webinar=False):
    """Update a meeting which already exists in the Zoom API.

    :param zoom_id: ID of the meeting
    :param changes: dictionary with new attribute values
    :param is_webinar: whether the call concerns a webinar (used to call the correct endpoint)
    """
    client = ZoomIndicoClient()
    try:
        if is_webinar:
            client.update_webinar(zoom_id, changes)
        else:
            client.update_meeting(zoom_id, changes)
    except HTTPError as e:
        from indico_vc_zoom.plugin import ZoomPlugin
        ZoomPlugin.logger.exception("Error updating meeting '%s': %s", zoom_id, e.response.content)
        raise VCRoomError(_("Can't update meeting. Please contact support if the error persists."))

def get_schedule_args(obj):
    """Create a dictionary with scheduling information from an Event/Contribution/SessionBlock.

    :param obj: An `Event`, `Contribution` or `SessionBlock`
    """
    duration = obj.end_dt - obj.start_dt

    if obj.start_dt < now_utc():
        return {}

    return {
        'start_time': obj.start_dt,
        'duration': duration.total_seconds() / 60,
    }
<|endoftext|>


<commit_before>def calculateEWS(RR, SpO2, Temp, SBP, HR, ConciousLevel, SupO2):
    try:
        intRR = int(RR)
    except:
        intRR = 0
    try:
        intSpO2 = int(SpO2)
    except:
        intSpO2 = 0
    try:
        intTemp = int(Temp)
    except:
        intTemp = 0
    try:
        intSBP = int(SBP)
    except:
        intSBP = 0
    try:
        intHR = int(HR)
    except:
        intHR = 0
    try:
        intConciousLevel = int(ConciousLevel)
    except:
        intConciousLevel = 4
    try:
        intSupO2 = int(SupO2)
    except:
        intSupO2 = 1
    if (8 <= intRR >= 25):
         RREWS = 3
    else:
        if (intRR >= 21):
            RREWS = 2
        else:
            if (11 >= intRR):
                RREWS = 1
            else:
                RREWS = 0
    if (intSpO2 <=91):
        SpO2EWS = 3
    else:
        if (intSpO2 <=93):
            SpO2EWS = 2
        else:
            if (intSpO2 <=95):
                SpO2EWS = 1
            else:
                SpO2EWS = 0
    EWS = RREWS + SpO2EWS
    return EWS<commit_msg>finalised basics of EWS function<commit_after>def calculateEWS(RR, SpO2, Temp, SBP, HR, ConciousLevel, SupO2):
    try:
        intRR = int(RR)
    except:
        intRR = 0
    try:
        intSpO2 = int(SpO2)
    except:
        intSpO2 = 0
    try:
        flTemp = float(Temp)
    except:
        flTemp = 0
    try:
        intSBP = int(SBP)
    except:
        intSBP = 0
    try:
        intHR = int(HR)
    except:
        intHR = 0
    try:
        intConciousLevel = int(ConciousLevel)
    except:
        intConciousLevel = 4
    try:
        intSupO2 = int(SupO2)
    except:
        intSupO2 = 1
    if (8 <= intRR >= 25):
         RREWS = 3
    else:
        if (intRR >= 21):
            RREWS = 2
        else:
            if (11 >= intRR):
                RREWS = 1
            else:
                RREWS = 0
    if (intSpO2 <= 91):
        SpO2EWS = 3
    else:
        if (intSpO2 <= 93):
            SpO2EWS = 2
        else:
            if (intSpO2 <= 95):
                SpO2EWS = 1
            else:
                SpO2EWS = 0
    if (35.0 >= flTemp)
        tempEWS = 3
    else:
        if (39.1 <= flTemp):
            tempEWS = 2
        else:
            if (36.0 >= flTemp >= 38.1):
                tempEWS = 1
            else:
                tempEWS = 0
    if (90 >= intSBP >= 220)
        SBPEWS = 3
    else:
        if (100 >= intSBP)
            SBPEWS = 2
        else:
            if (110 >= intSBP):
                SBPEWS = 1
            else:
                SBPEWS = 0
    if (40 >= intHR >=131):
        HREWS = 3
    else:
        if (intHR >=111):
            HREWS = 2
        else:
            if (50 >= intHR >= 91):
                HREWS = 1
            else:
                HREWS = 0
    if (intConciousLevel != 0):
        conciousLevelEWS = 3
    else:
        conciousLevelEWS = 0
    if (intSupO2 != 0):
        supO2EWS = 2
    else:
        supO2EWS = 0
    EWS = RREWS + SpO2EWS + tempEWS + SBPEWS + HREWS + conciousLevelEWS + supO2EWS
    return EWS<|endoftext|>


<commit_before>import os
import time
from collections import OrderedDict

import goethe

MODELS_PATH = 'models'
DATA_PATH = 'data/leipzig'

# Size
sizeconf = OrderedDict([('sg', [0, 1]),
                        ('size', [100, 300, 400, 600]),
                        ('iter', [5])])
sizelimits = [5_000_000]

# Iterations
iterconf = OrderedDict([('sg', [0, 1]),
                        ('size', [300]),
                        ('iter', [1, 5, 10, 20])])
iterlimits = [5_000_000]

# N
nconf = OrderedDict([('sg', [0, 1]),
                     ('size', [300]),
                     ('iter', [5])])
nlimits = [1_000_000, 5_000_000, 10_000_000, 20_000_000]

# Window
windowconf = OrderedDict([('sg', [0, 1]),
                     ('window', [5, 10, 20]),
                     ('iter', [5])])
windowlimits = [1_000_000]

#
nconf = OrderedDict([('sg', [0, 1]),
                     ('size', [300]),
                     ('iter', [5])])
nlimits = [1_000_000, 5_000_000, 10_000_000, 20_000_000]

configurations = [('Window', windowconf, windowlimits)]

if __name__ == '__main__':
    for confname, conf, limits in configurations:
        models = goethe.eval.gridsearch(DATA_PATH, conf, limits, MODELS_PATH)
        print(time.strftime(f'\n%Y-%m-%d %H:%M\t{confname.upper()}'))
        for model, name in models:
            model.save(os.path.join(MODELS_PATH, name))
            print(f"{time.strftime('%Y-%m-%d %H:%M')}\t{name}")
<commit_msg>Remove gridsearch.py<commit_after><|endoftext|>


<commit_before>__version__ = "2019.12.0"
__versionfull__ = __version__
<commit_msg>Bump app version to 2019.12.1<commit_after>__version__ = "2019.12.1"
__versionfull__ = __version__
<|endoftext|>


<commit_before>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   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 windmill
from urlparse import urlparse

import os

windmill.browser_registry = {}

def get_firefox_controller():
    """Get the firefox browser object"""
    import mozrunner
    from mozrunner import global_settings
    import simplesettings

    mozrunner_settings = simplesettings.initialize_settings(global_settings, mozrunner,
                                                  local_env_variable=mozrunner.settings_env)

    for key, value in mozrunner.settings.items():
        if not windmill.settings.has_key(key):
            windmill.settings[key] = value

    test_url = windmill.get_test_url(windmill.settings['TEST_URL'])

    if windmill.settings['INSTALL_FIREBUG']:
        windmill.settings['MOZILLA_PLUGINS'] = [os.path.join(os.path.dirname(__file__), os.path.pardir, 'xpi', 'firebug-1.2.1-fx.xpi')]

    windmill.settings['MOZILLA_PREFERENCES'].update( {
        'extensions.chromebug.openalways' : True,
        'extensions.chromebug.showIntroduction' : False,
        'general.warnOnAboutConfig' : False,
        'extensions.venkman.enableChromeFilter' : False,
        # Get rid of default browser check
        "browser.shell.checkDefaultBrowser": False,
        # Suppress authentication confirmations
        "network.http.phishy-userpass-length": 255,
        # Disable pop-up blocking
        "browser.allowpopups": True,
        "dom.disable_open_during_load": False,
        # Open links in new windows (Firefox 2.0)
        "browser.link.open_external": 2,
        "browser.link.open_newwindow": 2,
        # Configure local proxy
        "network.proxy.http": 'localhost',
        "network.proxy.http_port": windmill.settings['SERVER_HTTP_PORT'],
        "network.proxy.no_proxies_on": "",
        "network.proxy.type": 1,
        #"network.http.proxy.pipelining" : True,
        "network.http.max-connections": 10,
        "network.http.max-connections-per-server": 8,
#        "network.http.max-persistent-connections-per-proxy": 2,
#        "network.http.max-persistent-connections-per-server": 2,
        "network.http.pipelining.maxrequests": 10,

        # Turn off favicon requests, no need for even more requests
        "browser.chrome.favicons": False,

        "startup.homepage_override_url": test_url,
        "browser.startup.homepage": test_url,
        "startup.homepage_welcome_url": "",
        # Disable security warnings
        "security.warn_submit_insecure": False,
        "security.warn_submit_insecure.show_once": False,
        "security.warn_entering_secure": False,
        "security.warn_entering_secure.show_once": False,
        "security.warn_entering_weak": False,
        "security.warn_entering_weak.show_once": False,
        "security.warn_leaving_secure": False,
        "security.warn_leaving_secure.show_once": False,
        "security.warn_viewing_mixed": False,
        "security.warn_viewing_mixed.show_once": False,
        # Disable cache
        "browser.cache.disk.enable": False,
        "browser.sessionstore.resume_from_crash": False,
        # self.user_pref('"browser.cache.memory.enable", false')
        # Disable "do you want to remember this password?"
        "signon.rememberSignons": False,
        "dom.max_script_run_time": 20,
        } )

    windmill.settings['MOZILLA_CMD_ARGS'] = [test_url]

    controller = mozrunner.get_moz_from_settings(windmill.settings)

    return controller

def get_ie_controller():
    """Get the IE browser object"""
    import ie
    browser = ie.InternetExplorer()
    return browser

def get_safari_controller():
    """Get the Safari browser object"""
    import safari
    browser = safari.Safari()
    return browser

def get_chrome_controller():
    """Get the Safari browser object"""
    import chrome
    browser = chrome.Chrome()
    return browser
    <commit_msg>Disabling OSCP validation in FF.<commit_after>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   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 windmill
from urlparse import urlparse

import os

windmill.browser_registry = {}

def get_firefox_controller():
    """Get the firefox browser object"""
    import mozrunner
    from mozrunner import global_settings
    import simplesettings

    mozrunner_settings = simplesettings.initialize_settings(global_settings, mozrunner,
                                                  local_env_variable=mozrunner.settings_env)

    for key, value in mozrunner.settings.items():
        if not windmill.settings.has_key(key):
            windmill.settings[key] = value

    test_url = windmill.get_test_url(windmill.settings['TEST_URL'])

    if windmill.settings['INSTALL_FIREBUG']:
        windmill.settings['MOZILLA_PLUGINS'] = [os.path.join(os.path.dirname(__file__), os.path.pardir, 'xpi', 'firebug-1.2.1-fx.xpi')]

    windmill.settings['MOZILLA_PREFERENCES'].update( {
        'extensions.chromebug.openalways' : True,
        'extensions.chromebug.showIntroduction' : False,
        'general.warnOnAboutConfig' : False,
        'extensions.venkman.enableChromeFilter' : False,
        # Get rid of default browser check
        "browser.shell.checkDefaultBrowser": False,
        # Suppress authentication confirmations
        "network.http.phishy-userpass-length": 255,
        # Disable pop-up blocking
        "browser.allowpopups": True,
        "dom.disable_open_during_load": False,
        # Open links in new windows (Firefox 2.0)
        "browser.link.open_external": 2,
        "browser.link.open_newwindow": 2,
        # Configure local proxy
        "network.proxy.http": 'localhost',
        "network.proxy.http_port": windmill.settings['SERVER_HTTP_PORT'],
        "network.proxy.no_proxies_on": "",
        "network.proxy.type": 1,
        #"network.http.proxy.pipelining" : True,
        "network.http.max-connections": 10,
        "network.http.max-connections-per-server": 8,
#        "network.http.max-persistent-connections-per-proxy": 2,
#        "network.http.max-persistent-connections-per-server": 2,
        "network.http.pipelining.maxrequests": 10,

        # Turn off favicon requests, no need for even more requests
        "browser.chrome.favicons": False,

        "startup.homepage_override_url": test_url,
        "browser.startup.homepage": test_url,
        "startup.homepage_welcome_url": "",
        # Disable security warnings
        "security.warn_submit_insecure": False,
        "security.warn_submit_insecure.show_once": False,
        "security.warn_entering_secure": False,
        "security.warn_entering_secure.show_once": False,
        "security.warn_entering_weak": False,
        "security.warn_entering_weak.show_once": False,
        "security.warn_leaving_secure": False,
        "security.warn_leaving_secure.show_once": False,
        "security.warn_viewing_mixed": False,
        "security.warn_viewing_mixed.show_once": False,
        # Disable cache
        "browser.cache.disk.enable": False,
        "browser.sessionstore.resume_from_crash": False,
        # self.user_pref('"browser.cache.memory.enable", false')
        # Disable "do you want to remember this password?"
        "signon.rememberSignons": False,
        "dom.max_script_run_time": 20,
        # Disable OSCP validation, breaks through proxy.
        "security.OCSP.enabled":0,
        } )

    windmill.settings['MOZILLA_CMD_ARGS'] = [test_url]

    controller = mozrunner.get_moz_from_settings(windmill.settings)

    return controller

def get_ie_controller():
    """Get the IE browser object"""
    import ie
    browser = ie.InternetExplorer()
    return browser

def get_safari_controller():
    """Get the Safari browser object"""
    import safari
    browser = safari.Safari()
    return browser

def get_chrome_controller():
    """Get the Safari browser object"""
    import chrome
    browser = chrome.Chrome()
    return browser
    <|endoftext|>