"""
    ${NAME}
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import zipfile

log = logging.getLogger(__name__)

class ResourceNotFound(KeyError):
    pass

class ResourceLoader(object):
    def __init__(self):
        super(ResourceLoader, self).__init__()
        self.zipFiles = []

    def addZipFile(self, zipPath):
        try:
            zf = zipfile.ZipFile(zipPath)
        except zipfile.BadZipfile as e:
            raise IOError("Could not read %s as a zip file." % zipPath)
        self.zipFiles.append(zf)

    def openStream(self, path):
        for zipFile in self.zipFiles:
            try:
                stream = zipFile.open(path)
                break
            except KeyError:  # Not found in zip file
                continue
        else:
            raise ResourceNotFound("Resource %s not found in search path" % path)

        return stream

    def blockModelPaths(self):
        for zf in self.zipFiles:
            for name in zf.namelist():
                if name.startswith("assets/minecraft/models/block"):
                    yield name

Add function to ResourceLoader for listing all block models

xxx only lists Vanilla models. haven't looked at mods with models yet.


# Copyright 2007 Pompeu Fabra University (Computational Imaging Laboratory), Barcelona, Spain. Web: www.cilab.upf.edu.
# This software is distributed WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#  Script to automate CSnake calls from cruise control

import sys
import csnGUIHandler
import csnGUIOptions
import csnGenerator

# Check command line arguments
if len(sys.argv) != 3:
    sys.exit("Error: not enough arguments. You need to provide an option and a configuration file.")

# Command line inputs
options_file = sys.argv[1]
config_file = sys.argv[2]

# Create GUI handler
handler = csnGUIHandler.Handler()
# Read options
options = csnGUIOptions.Options()
options.Load( options_file )
# Read settings
settings = csnGenerator.Settings()
settings.Load( config_file )
# Set the options
handler.SetOptions( options )
# Configure the project with the settings
if settings.instance == "thirdParty":
    res = handler.ConfigureThirdPartyFolder(settings)
else:
    res = handler.ConfigureProjectToBinFolder( settings, 1 )
sys.exit(res)

# Copyright 2007 Pompeu Fabra University (Computational Imaging Laboratory), Barcelona, Spain. Web: www.cilab.upf.edu.
# This software is distributed WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#  Script to automate CSnake calls from cruise control

import sys
import csnGUIHandler
import csnGUIOptions
import csnGenerator

# Check command line arguments
if len(sys.argv) != 3:
    sys.exit("Error: not enough arguments. You need to provide an option and a configuration file.")

# Command line inputs
options_file = sys.argv[1]
config_file = sys.argv[2]

# Create GUI handler
handler = csnGUIHandler.Handler()
# Read options
options = csnGUIOptions.Options()
options.Load( options_file )
# Read settings
settings = csnGenerator.Settings()
settings.Load( config_file )
# Set the options
handler.SetOptions( options )
# Configure the project with the settings
if settings.instance == "thirdParty":
    res = handler.ConfigureThirdPartyFolder(settings)
else:
    res = handler.ConfigureProjectToBinFolder( settings, 1 )
# exit with error if there was a problem
if res == false:
  sys.exit(1)

Exit with the proper value.

Exit with the proper value.

git-svn-id: a26c1b3dc012bc7b166f1b96505d8277332098eb@265 9ffc3505-93cb-cd4b-9e5d-8a77f6415fcf


__author__ = 'Gareth Coles'

import weakref

class Info(object):

    data = None

    core = None
    info = None

    def __init__(self, yaml_data, plugin_object=None):
        """

        :param yaml_data:
        :type yaml_data: dict
        :return:
        """

        self.data = yaml_data

        if plugin_object:
            self._plugin_object = weakref.ref(plugin_object)

        for key in yaml_data.keys():
            obj = yaml_data[key]

            if isinstance(obj, dict):
                setattr(self, key, Info(obj))
            else:
                setattr(self, key, obj)

        if self.core is not None:
            self.name = self.core.name
            self.module = self.core.module
            if hasattr(self.core, "dependencies"):
                self.dependencies = self.core.dependencies
            else:
                self.dependencies = []

        if self.info is not None:
            self.version = self.info.version
            self.description = self.info.description
            self.author = self.info.author
            self.website = self.info.website
            self.copyright = self.info.copyright

    @property
    def plugin_object(self):
        if hasattr(self, "_plugin_object"):
            return self._plugin_object()
        return None

    def get_module(self):
        if hasattr(self, "module"):
            return "plugins.%s" % self.module
        return None

    def set_plugin_object(self, obj):
        self._plugin_object = weakref.ref(obj)

__author__ = 'Gareth Coles'

import weakref

class Info(object):

    data = None

    core = None
    info = None

    def __init__(self, yaml_data, plugin_object=None):
        """

        :param yaml_data:
        :type yaml_data: dict
        :return:
        """

        self.data = yaml_data

        if plugin_object:
            self._plugin_object = weakref.ref(plugin_object)

        for key in yaml_data.keys():
            obj = yaml_data[key]

            if isinstance(obj, dict):
                setattr(self, key, Info(obj))
            else:
                setattr(self, key, obj)

        if self.core is not None:
            self.name = self.core.name
            self.module = self.core.module
            if hasattr(self.core, "dependencies"):
                self.dependencies = self.core.dependencies
            else:
                self.core.dependencies = []
                self.dependencies = []

        if self.info is not None:
            self.version = self.info.version
            self.description = self.info.description
            self.author = self.info.author
            self.website = self.info.website
            self.copyright = self.info.copyright

    @property
    def plugin_object(self):
        if hasattr(self, "_plugin_object"):
            return self._plugin_object()
        return None

    def get_module(self):
        if hasattr(self, "module"):
            return "plugins.%s" % self.module
        return None

    def set_plugin_object(self, obj):
        self._plugin_object = weakref.ref(obj)

Fix missing dependencies on core

Fix missing dependencies on core


from celery.task import task
from .models import MailerMessage

@task(name="tasks.send_mail")
def send_mail(pk):
    message = MailerMessage.objects.get(pk=pk)
    message._send()

@task()
def clear_sent_messages():
    from mailqueue.models import MailerMessage
    MailerMessage.objects.clear_sent_messages()

from celery.task import task
from .models import MailerMessage

@task(name="tasks.send_mail", default_retry_delay=5, max_retries=5)
def send_mail(pk):
    message = MailerMessage.objects.get(pk=pk)
    message._send()

    # Retry when message is not sent
    if not message.sent:
        send_mail.retry([message.pk,])

@task()
def clear_sent_messages():
    from mailqueue.models import MailerMessage
    MailerMessage.objects.clear_sent_messages()

Add retry to celery task

Add retry to celery task

Messages do not always get delivered. Built in a retry when message is not sent.
Max retry count could also be a setting.


from __future__ import (absolute_import, print_function, division)
from itertools import repeat
from mitmproxy.main import mitmdump
import os

from . import (cli, proxyswitch, say)

def main():
    parser = cli.args()
    args, extra_args = parser.parse_known_args()

    try:
        config = cli.config(args)
    except IOError as err:
        parser.error(err)
    except toml.core.TomlError as err:
        parser.error("Errors found in the config file:\n\n", err)

    mitm_args = cli.mitm_args(config)
    is_sudo = os.getuid() == 0

    if type(mitm_args) == Exception:
        parser.error(mitm_args.message)

    say.level(config["core"]["verbose"])

    try:
        if config["os"]["proxy-settings"]:
            if not is_sudo:
                parser.error("proxy-settings is enabled, please provide sudo in order to change the OSX proxy configuration.")

            proxyswitch.enable(config["core"]["host"],
                               str(config["core"]["port"]))

        mitmdump(mitm_args + extra_args)
    finally:
        if config["os"]["proxy-settings"] and is_sudo:
            proxyswitch.disable()

from __future__ import (absolute_import, print_function, division)
from itertools import repeat
from mitmproxy.main import mitmdump
import os

from . import (cli, proxyswitch, say)

def main():
    parser = cli.args()
    args, extra_args = parser.parse_known_args()

    try:
        config = cli.config(args)
    except IOError as err:
        parser.error(err)
    except toml.core.TomlError as err:
        parser.error("Errors found in the config file:\n\n", err)

    mitm_args = cli.mitm_args(config)
    is_sudo = os.getuid() == 0

    if type(mitm_args) == Exception:
        parser.error(mitm_args.message)

    say.level(config["core"]["verbose"])

    host= config["core"]["host"]
    port = config["core"]["port"]
    pid_filename = "/var/tmp/mastermind.{}{}.pid".format(host.replace('.', ''), port)

    try:
        if config["os"]["proxy-settings"]:
            if not is_sudo:
                parser.error("proxy-settings is enabled, please provide sudo in order to change the OSX proxy configuration.")

            proxyswitch.enable(host, str(port))

        with open(pid_filename, "w") as f:
            f.write(str(os.getpid()))

        mitmdump(mitm_args + extra_args)
    finally:
        os.remove(pid_filename)

        if config["os"]["proxy-settings"] and is_sudo:
            proxyswitch.disable()

Write PID to a file

Write PID to a file


#!/usr/bin/env python
"""
               #   # ####  ##### #   # ##### #   # #   #
               #  #  #   # #     ## ##  #  # #  ## #   #
               ###   ####  ####  # # #  #  # # # # #####
               #  #  #     #     #   #  #  # ##  # #   #
               #   # #     ##### #   # #   # #   # #   #

                   Kremlin Magical Everything System
               Glasnost Image Board and Boredom Inhibitor

"""

from kremlin import app

def main():
    print "Launching kremlin in development mode."
    print "--------------------------------------"
    app.run(debug=True)

if __name__ == '__main__':
    main()

#!/usr/bin/env python
"""
               #   # ####  ##### #   # ##### #   # #   #
               #  #  #   # #     ## ##  #  # #  ## #   #
               ###   ####  ####  # # #  #  # # # # #####
               #  #  #     #     #   #  #  # ##  # #   #
               #   # #     ##### #   # #   # #   # #   #

                   Kremlin Magical Everything System
               Glasnost Image Board and Boredom Inhibitor

"""

from kremlin import app

def main():
    print "Kremlin Magical Everything System v 0.0.0-None"
    print "Copyright (c) Glasnost 2010-2011"
    print "-----------------------------------------------"
    print "RUNNING IN DEVELOPMENT MODE! ** NOT FOR PRODUCTION **"
    print "Connect to http://127.0.0.1:5000 to access."
    app.run(debug=True)

if __name__ == '__main__':
    main()

Add proper banner to dev launch script

Add proper banner to dev launch script


#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Ethan Zimmerman
# Copyright (c) 2014 Ethan Zimmerman
#
# License: MIT
#

"""This module exports the RamlCop plugin class."""

from SublimeLinter.lint import NodeLinter

class RamlCop(NodeLinter):

    """Provides an interface to raml-cop."""

    syntax = 'raml'
    cmd = 'raml-cop --no-color'
    version_requirement = '>= 1.0.0'
    regex = (
        r'^\[.+:(?P<line>\d+):(?P<col>\d+)\] '
        r'(?P<message>.+)'
    )
    line_col_base = (0, 0)
    tempfile_suffix = '-'

Remove empty line before class docstring


# Copyright (c) 2013 gocept gmbh & co. kg
# See also LICENSE.txt

import grokcore.component as grok
import zeit.cms.browser.preview
import zeit.magazin.interfaces

@grok.adapter(zeit.magazin.interfaces.IZMOContent, basestring)
@grok.implementer(zeit.cms.browser.interfaces.IPreviewURL)
def preview_url(content, preview_type):
    return zeit.cms.browser.preview.prefixed_url(
        'zmo-%s-prefix' % preview_type, content.uniqueId)

# XXX there also is a (basestring, basestring) variant of the adapter
# which is used by zeit.find to caluclate preview-urls for search results
# without looking up the content object first. What do we do about that?

Remove oddity marker, it's been resolved in zeit.find now


import itertools
from functools import partial
import math

from typing import Any, Callable, Iterable

from rechunker.types import CopySpec, StagedCopySpec, Executor

Thunk = Callable[[], None]

class PythonExecutor(Executor[Thunk]):
    """An execution engine based on Python loops.

    Supports copying between any arrays that implement ``__getitem__`` and
    ``__setitem__`` for tuples of ``slice`` objects.

    Execution plans for PythonExecutor are functions that accept no arguments.
    """

    def prepare_plan(self, specs: Iterable[StagedCopySpec]) -> Thunk:
        tasks = []
        for staged_copy_spec in specs:
            for copy_spec in staged_copy_spec.stages:
                tasks.append(partial(_direct_copy_array, copy_spec))
        return partial(_execute_all, tasks)

    def execute_plan(self, plan: Thunk):
        plan()

def _direct_copy_array(copy_spec: CopySpec) -> None:
    """Direct copy between zarr arrays."""
    source_array, target_array, chunks = copy_spec
    shape = source_array.shape
    ranges = [range(math.ceil(s / c)) for s, c in zip(shape, chunks)]
    for indices in itertools.product(*ranges):
        key = tuple(slice(c * i, c * (i + 1)) for i, c in zip(indices, chunks))
        target_array[key] = source_array[key]

def _execute_all(tasks: Iterable[Callable[[], Any]]) -> None:
    for task in tasks:
        task()

import itertools
from functools import partial
import math

from typing import Callable, Iterable

from rechunker.types import CopySpec, StagedCopySpec, Executor

# PythonExecutor represents delayed execution tasks as functions that require
# no arguments.
Task = Callable[[], None]

class PythonExecutor(Executor[Task]):
    """An execution engine based on Python loops.

    Supports copying between any arrays that implement ``__getitem__`` and
    ``__setitem__`` for tuples of ``slice`` objects.

    Execution plans for PythonExecutor are functions that accept no arguments.
    """

    def prepare_plan(self, specs: Iterable[StagedCopySpec]) -> Task:
        tasks = []
        for staged_copy_spec in specs:
            for copy_spec in staged_copy_spec.stages:
                tasks.append(partial(_direct_copy_array, copy_spec))
        return partial(_execute_all, tasks)

    def execute_plan(self, plan: Task):
        plan()

def _direct_copy_array(copy_spec: CopySpec) -> None:
    """Direct copy between zarr arrays."""
    source_array, target_array, chunks = copy_spec
    shape = source_array.shape
    ranges = [range(math.ceil(s / c)) for s, c in zip(shape, chunks)]
    for indices in itertools.product(*ranges):
        key = tuple(slice(c * i, c * (i + 1)) for i, c in zip(indices, chunks))
        target_array[key] = source_array[key]

def _execute_all(tasks: Iterable[Task]) -> None:
    for task in tasks:
        task()

Remove 'thunk' jargon from PythonExecutor


from django.views import debug

def _patch_django_debug_view():

    new_data = """

            View in Stackoverflow

    """

    replace_point = '
'
    replacement = new_data + replace_point

    # monkey patch the built-in template.
    debug.TECHNICAL_500_TEMPLATE = debug.TECHNICAL_500_TEMPLATE.replace(
        replace_point,
        replacement,
        1  # replace the first occurence
    )

class DjangoStackoverTraceMiddleware(object):

    def __init__(self):
        _patch_django_debug_view()

    def process_response(self, request, response):
        return response

from django.views import debug
from django.conf import settings

def get_search_link():
    default_choice = "stackoverflow"

    search_urls = {
        "stackoverflow": "http://stackoverflow.com/search?q=[python] or "
                         "[django]+{{ exception_value|force_escape }}",
        "googlesearch": "https://www.google.com.tr/#q=site:stackoverflow.com"
                        "+django+{{ exception_value|force_escape }}"
    }

    search_url = getattr(
        settings,
        'DJANGO_STACKOVERFLOW_TRACE_SEARCH_SITE',
        default_choice
    )

    return search_urls.get(search_url, search_urls[default_choice])

def _patch_django_debug_view():

    new_data = """

            View in Stackoverflow

    """ % get_search_link()

    replace_point = '
'
    replacement = new_data + replace_point

    # monkey patch the built-in template.
    debug.TECHNICAL_500_TEMPLATE = debug.TECHNICAL_500_TEMPLATE.replace(
        replace_point,
        replacement,
        1  # replace the first occurence
    )

class DjangoStackoverTraceMiddleware(object):

    def __init__(self):
        _patch_django_debug_view()

    def process_response(self, request, response):
        return response

Add a google search option

Add a google search option


from indra.databases import context_client

def test_get_protein_expression():
    res = context_client.get_protein_expression('EGFR', 'BT20_BREAST')
    assert(res is not None)
    assert(res.get('EGFR') is not None)
    assert(res['EGFR'].get('BT20_BREAST') is not None)
    assert(res['EGFR']['BT20_BREAST'] > 1000)

def test_get_mutations():
    res = context_client.get_mutations('BRAF', 'A375_SKIN')
    assert(res is not None)
    assert(res.get('BRAF') is not None)
    assert(res['BRAF'].get('A375_SKIN') is not None)
    assert(res['BRAF']['A375_SKIN'] == 1.0)

def test_send_request_bad():
    res = context_client.send_request('xyz', None)
    assert(res is None)

Remove deprecated context client test

Remove deprecated context client test


from django.db import models
from django import forms
from django.utils.translation import ugettext_lazy as _

from .backends import detect_backend, UnknownIdException, \
    UnknownBackendException

__all__ = ('EmbedVideoField', 'EmbedVideoFormField')

class EmbedVideoField(models.URLField):
    """
    Model field for embeded video. Descendant of
    :py:class:`django.db.models.URLField`.
    """

    def formfield(self, **kwargs):
        defaults = {'form_class': EmbedVideoFormField}
        defaults.update(kwargs)
        return super(EmbedVideoField, self).formfield(**defaults)

    def south_field_triple(self):
        from south.modelsinspector import introspector
        cls_name = '%s.%s' % (
            self.__class__.__module__,
            self.__class__.__name__
        )
        args, kwargs = introspector(self)
        return (cls_name, args, kwargs)

class EmbedVideoFormField(forms.URLField):
    """
    Form field for embeded video. Descendant of
    :py:class:`django.forms.URLField`
    """

    def validate(self, url):
        super(EmbedVideoFormField, self).validate(url)

        if url:
            try:
                detect_backend(url)
            except UnknownBackendException:
                raise forms.ValidationError(_(u'URL could not be recognized.'))
            except UnknownIdException:
                raise forms.ValidationError(_(u'ID of this video could not be \
                                                recognized.'))

        return url

from django.db import models
from django import forms
from django.utils.translation import ugettext_lazy as _

from .backends import detect_backend, UnknownIdException, \
    UnknownBackendException

__all__ = ('EmbedVideoField', 'EmbedVideoFormField')

class EmbedVideoField(models.URLField):
    """
    Model field for embeded video. Descendant of
    :py:class:`django.db.models.URLField`.
    """

    def formfield(self, **kwargs):
        defaults = {'form_class': EmbedVideoFormField}
        defaults.update(kwargs)
        return super(EmbedVideoField, self).formfield(**defaults)

    def south_field_triple(self):
        from south.modelsinspector import introspector
        cls_name = '%s.%s' % (
            self.__class__.__module__,
            self.__class__.__name__
        )
        args, kwargs = introspector(self)
        return (cls_name, args, kwargs)

class EmbedVideoFormField(forms.URLField):
    """
    Form field for embeded video. Descendant of
    :py:class:`django.forms.URLField`
    """

    def validate(self, url):
        # if empty url is not allowed throws an exception
        super(EmbedVideoFormField, self).validate(url)

        if not url:
            return

        try:
            detect_backend(url)
        except UnknownBackendException:
            raise forms.ValidationError(_(u'URL could not be recognized.'))
        except UnknownIdException:
            raise forms.ValidationError(_(u'ID of this video could not be \
                                            recognized.'))
        return url

Simplify validate method in FormField.

Simplify validate method in FormField.


"""Extensions of standard exceptions for PyWXSB events.

Yeah, I'd love this module to be named exceptions.py, but it can't
because the standard library has one of those, and we need to
reference it below.
"""

import exceptions

class PyWXSBException (exceptions.Exception):
    """Base class for exceptions that indicate a problem that the user should fix."""
    pass

class SchemaValidationError (PyWXSBException):
    """Raised when the XML hierarchy does not appear to be valid for an XML schema."""
    pass

class BadTypeValueError (PyWXSBException):
    """Raised when a value in an XML attribute does not conform to the simple type."""
    pass

class NotInNamespaceError (PyWXSBException):
    '''Raised when a name is referenced that is not defined in the appropriate namespace.'''
    __namespace = None
    __ncName = None

class BadPropertyError (PyWXSBException):
    """Raised when a schema component property is accessed on a component instance that does not define that property."""
    pass

class PyWXSBError (exceptions.Exception):
    """Base class for exceptions that indicate a problem that the user probably can't fix."""
    pass

class LogicError (PyWXSBError):
    """Raised when the code detects an implementation problem."""

class IncompleteImplementationError (LogicError):
    """Raised when a code branch is taken that has not yet been implemented."""

"""Extensions of standard exceptions for PyWXSB events.

Yeah, I'd love this module to be named exceptions.py, but it can't
because the standard library has one of those, and we need to
reference it below.
"""

import exceptions

class PyWXSBException (exceptions.Exception):
    """Base class for exceptions that indicate a problem that the user should fix."""
    pass

class SchemaValidationError (PyWXSBException):
    """Raised when the XML hierarchy does not appear to be valid for an XML schema."""
    pass

class BadTypeValueError (PyWXSBException):
    """Raised when a value in an XML attribute does not conform to the simple type."""
    pass

class NotInNamespaceError (PyWXSBException):
    '''Raised when a name is referenced that is not defined in the appropriate namespace.'''
    __namespace = None
    __ncName = None

class BadPropertyError (PyWXSBException):
    """Raised when a schema component property is accessed on a component instance that does not define that property."""
    pass

class BadDocumentError (PyWXSBException):
    """Raised when processing document content and an error is encountered."""
    pass

class PyWXSBError (exceptions.Exception):
    """Base class for exceptions that indicate a problem that the user probably can't fix."""
    pass

class LogicError (PyWXSBError):
    """Raised when the code detects an implementation problem."""

class IncompleteImplementationError (LogicError):
    """Raised when a code branch is taken that has not yet been implemented."""

Add an exception to throw when a document does have the expected structure


# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

from tensorflow.tensorboard.tensorboard import main

if __name__ == '__main__':
    sys.exit(main())

Add pylint indentation check to sanity and fix existing indentation Change: 132840696


from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

import editorFactory

if __name__ == "__main__":
    server = editorFactory.EditorFactory()
    TCP4ServerEndpoint(reactor, 4567).listen(server)
    reactor.run()

from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

import editorFactory

if __name__ == "__main__":
    server = editorFactory.EditorFactory()
    TCP4ServerEndpoint(reactor, 4567).listen(server)
    print('Starting up...')
    reactor.run()

Print something at the start

Print something at the start


import pytest
import numpy

from ...neural._classes.model import Model

def test_mismatched_shapes_raise_ShapeError():
    X = numpy.ones((3, 4))
    model = Model(10, 5)
    with pytest.raises(ValueError):
        y = model.begin_training(X)

import pytest
import numpy

from ...neural._classes.model import Model
from ...exceptions import UndefinedOperatorError, DifferentLengthError
from ...exceptions import ExpectedTypeError, ShapeMismatchError

def test_mismatched_shapes_raise_ShapeError():
    X = numpy.ones((3, 4))
    model = Model(10, 5)
    with pytest.raises(ShapeMismatchError):
        y = model.begin_training(X, X)

Update test and import errors

Update test and import errors


__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "Martin Paul Eve & Andy Byers"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from django.utils.translation import ugettext_lazy as _

from utils.setting_handler import get_plugin_setting
from core.homepage_elements.about import plugin_settings

def yield_homepage_element_context(request, homepage_elements):
    if homepage_elements is not None and homepage_elements.filter(name='About').exists():

        try:
            title = get_plugin_setting(
                plugin_settings.get_self(),
                'about_title',
                request.journal,
            )
            title_value = title.value if title.value else ''
        except IndexError:
            title_value = _('About this Journal')

        return {
            'about_content': request.journal.description,
            'title_value': title_value,
        }
    else:
        return {}

__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "Martin Paul Eve & Andy Byers"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from django.utils.translation import ugettext_lazy as _

from utils.setting_handler import get_plugin_setting
from core.homepage_elements.about import plugin_settings

def yield_homepage_element_context(request, homepage_elements):
    if homepage_elements is not None and homepage_elements.filter(name='About').exists():

        try:
            title = get_plugin_setting(
                plugin_settings.get_self(),
                'about_title',
                request.journal,
            )
            title_value = title.value if title.value else ''
        except AttributeError:
            title_value = _('About this Journal')

        return {
            'about_content': request.journal.description,
            'title_value': title_value,
        }
    else:
        return {}

Swap IndexError for AttributeError as a result of the swap from HVAD to MT


from functools import wraps

from ratelimit.exceptions import Ratelimited
from ratelimit.helpers import is_ratelimited

def registration_ratelimit(ip=True, block=False, method=['POST'], field=None, rate='1/h',
                           skip_if=None, keys=None):
    def decorator(fn):
        @wraps(fn)
        def _wrapped(request, *args, **kw):
            request.limited = getattr(request, 'limited', False)
            if skip_if is None or not skip_if(request):
                ratelimited = is_ratelimited(request=request, increment=False,
                                             ip=ip, method=method, field=field,
                                             rate=rate, keys=keys)
                if ratelimited and block:
                    raise Ratelimited()
            return_val, success = fn(request, *args, **kw)
            if success:
                is_ratelimited(request=request, increment=True, ip=ip,
                               method=method, field=field, rate=rate, keys=keys)
            return return_val
        return _wrapped
    return decorator

Use custom decorator to allow ratelimiting only on successful POST - prevents blocking form errors


"""The testing directory contains a small set of imaging files to be used
for doctests only.  More thorough tests and example data will be stored in
a nipy-data-suite to be created later and downloaded separately.

Examples
--------

>>> from neuroimaging.testing import funcfile
>>> from neuroimaging.core.image import image
>>> img = image.load(funcfile)
>>> img.shape
(20, 2, 20, 20)

Notes
-----
BUG: anatomical.nii.gz is a copy of functional.nii.gz.  This is a place-holder
    until we build a proper anatomical test image.

"""

import os

#__all__ = ['funcfile', 'anatfile']

# Discover directory path
filepath = os.path.abspath(__file__)
basedir = os.path.dirname(filepath)

funcfile = os.path.join(basedir, 'functional.nii.gz')
anatfile = os.path.join(basedir, 'anatomical.nii.gz')

from numpy.testing import *
import decorators as dec
from nose.tools import assert_true, assert_false

Add some nose.tools to testing imports.

Add some nose.tools to testing imports.


from optparse import make_option
from django.core.management.base import BaseCommand
from zephyr.models import Realm, UserProfile
import simplejson

def dump():
    pointers = []
    for u in UserProfile.objects.select_related("user__email").all():
        pointers.append((u.user.email, u.pointer))
    file("dumped-pointers", "w").write(simplejson.dumps(pointers) + "\n")

def restore(change):
    for (email, pointer) in simplejson.loads(file("dumped-pointers").read()):
        u = UserProfile.objects.get(user__email=email)
        print "%s: pointer %s => %s" % (email, u.pointer, pointer)
        if change:
            u.pointer = pointer
            u.save()

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--restore', default=False, action='store_true'),
        make_option('--dry-run', '-n', default=False, action='store_true'),)

    def handle(self, *args, **options):
        if options["restore"]:
            restore(change=not options['dry_run'])
        else:
            dump()

from optparse import make_option
from django.core.management.base import BaseCommand
from zephyr.models import Realm, UserProfile
import simplejson

def dump():
    pointers = []
    for u in UserProfile.objects.select_related("user__email").all():
        pointers.append((u.user.email, u.pointer))
    file("dumped-pointers", "w").write(simplejson.dumps(pointers) + "\n")

def restore(change):
    for (email, pointer) in simplejson.loads(file("dumped-pointers").read()):
        u = UserProfile.objects.get(user__email__iexact=email)
        print "%s: pointer %s => %s" % (email, u.pointer, pointer)
        if change:
            u.pointer = pointer
            u.save()

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--restore', default=False, action='store_true'),
        make_option('--dry-run', '-n', default=False, action='store_true'),)

    def handle(self, *args, **options):
        if options["restore"]:
            restore(change=not options['dry_run'])
        else:
            dump()

Fix email case issues when restoring user pointers.

(imported from commit 84d3288dffc1cb010d8cd2a749fe71aa2a4d0df3)


import sys
if sys.version_info[0] < 3 and sys.version_info[1] < 4:
    raise Exception('Python 3.4 is required to use this package.')

from .ignitiondelayexp import ExperimentalIgnitionDelay
from .compare_to_sim import CompareToSimulation
from .volume_trace import VolumeTraceBuilder
from .nonreactive import NonReactiveExperiments

__all__ = [
    'ExperimentalIgnitionDelay',
    'CompareToSimulation',
    'VolumeTraceBuilder',
    'NonReactiveExperiments',
]

Enforce Python >= 3.4 on import of the package

Python 3.4 is required for the pathlib module


from __future__ import unicode_literals
import pytest

from ...symbols import POS, VERB, VerbForm_inf
from ...tokens import Doc
from ...vocab import Vocab
from ...lemmatizer import Lemmatizer

@pytest.fixture
def index():
    return {'verb': {}}

@pytest.fixture
def exceptions():
    return {'verb': {}}

@pytest.fixture
def rules():
    return {"verb": [["ed", "e"]]}

@pytest.fixture
def lemmatizer(index, exceptions, rules):
    return Lemmatizer(index, exceptions, rules)

@pytest.fixture
def tag_map():
    return {'VB': {POS: VERB, 'morph': VerbForm_inf}}

@pytest.fixture
def vocab(lemmatizer, tag_map):
    return Vocab(lemmatizer=lemmatizer, tag_map=tag_map)

def test_not_lemmatize_base_forms(vocab, lemmatizer):
    doc = Doc(vocab, words=["Do", "n't", "feed", "the", "dog"])
    feed = doc[2]
    feed.tag_ = u'VB'
    assert feed.text == u'feed'
    assert feed.lemma_ == u'feed'

from __future__ import unicode_literals
import pytest

from ...symbols import POS, VERB, VerbForm_inf
from ...tokens import Doc
from ...vocab import Vocab
from ...lemmatizer import Lemmatizer

@pytest.fixture
def index():
    return {'verb': {}}

@pytest.fixture
def exceptions():
    return {'verb': {}}

@pytest.fixture
def rules():
    return {"verb": [["ed", "e"]]}

@pytest.fixture
def lemmatizer(index, exceptions, rules):
    return Lemmatizer(index, exceptions, rules)

@pytest.fixture
def tag_map():
    return {'VB': {POS: VERB, 'morph': VerbForm_inf}}

@pytest.fixture
def vocab(lemmatizer, tag_map):
    return Vocab(lemmatizer=lemmatizer, tag_map=tag_map)

def test_not_lemmatize_base_forms(vocab):
    doc = Doc(vocab, words=["Do", "n't", "feed", "the", "dog"])
    feed = doc[2]
    feed.tag_ = u'VB'
    assert feed.text == u'feed'
    assert feed.lemma_ == u'feed'

Remove unnecessary argument in test

Remove unnecessary argument in test


# -*- coding: utf-8 -*-
import randspectra as rs
import sys
import os.path as path

snapnum=sys.argv[1]
sim=sys.argv[2]
#base="/n/hernquistfs1/mvogelsberger/projects/GFM/Production/Cosmo/Cosmo"+str(sim)+"_V6/L25n512/output/"
#savedir="/n/home11/spb/scratch/Cosmo/Cosmo"+str(sim)+"_V6_512/snapdir_"+str(snapnum).rjust(3,'0')
base=path.expanduser("~/data/Cosmo/Cosmo"+str(sim)+"_V6/L25n512")
halo = rs.RandSpectra(snapnum, base)
halo.get_observer_tau("Si",2)
halo.get_col_density("H",1)
#halo.get_tau("H",1,1)
halo.get_col_density("Z",-1)
halo.get_col_density("H",-1)
halo.save_file()

# -*- coding: utf-8 -*-
import randspectra as rs
import sys
import os.path as path

snapnum=sys.argv[1]
sim=sys.argv[2]
#base="/n/hernquistfs1/mvogelsberger/projects/GFM/Production/Cosmo/Cosmo"+str(sim)+"_V6/L25n512/output/"
#savedir="/n/home11/spb/scratch/Cosmo/Cosmo"+str(sim)+"_V6_512/snapdir_"+str(snapnum).rjust(3,'0')
base=path.expanduser("~/data/Cosmo/Cosmo"+str(sim)+"_V6/L25n512")
halo = rs.RandSpectra(snapnum, base)
halo.save_file()
halo.get_observer_tau("Si",2)
halo.get_col_density("H",1)
#halo.get_tau("H",1,1)
halo.get_col_density("Z",-1)
halo.get_col_density("H",-1)
halo.save_file()

Handle the case where the savefile already exists by moving it out of the way


"""Project-wide test configuration, including fixutres that can be
used by any module.

Example test: ::

    def test_my_renderer(fakefile):
        assert my_renderer(fakefile) == '..expected result..'

"""
import io
import pytest

@pytest.fixture
def fakefile():
    return io.BytesIO(b'foo')

"""Project-wide test configuration, including fixutres that can be
used by any module.

Example test: ::

    def test_my_renderer(fakefile):
        assert my_renderer(fakefile) == '..expected result..'

"""
import pytest
import mock

@pytest.fixture
def fakefile():
    """A simple file-like object."""
    return mock.Mock(spec=file)

Make fakefile a mock instead of an io object

Makes it possible to mutate attributes, e.g. the name,
for tests


from django.conf import settings
from django.contrib.sites.models import Site
from settings import DeploymentType

def default(request):
    context = {}
    context['DEPLOYMENT'] = settings.DEPLOYMENT
    context['current_site'] = Site.objects.get_current()
    if settings.DEPLOYMENT != DeploymentType.PRODUCTION:
        context['GIT_COMMIT'] = settings.GIT_COMMIT
    context['site_email'] = settings.CONTACT_EMAIL
    if request.is_secure():
        context['protocol'] = 'https://'
    else:
        context['protocol'] = 'http://'
    context['current_site_url'] = (context['protocol'] +
            context['current_site'].domain)
    return context

def ssl_media(request):
    if request.is_secure():
        ssl_media_url = settings.MEDIA_URL.replace('http://','https://')
    else:
        ssl_media_url = settings.MEDIA_URL
    return {'MEDIA_URL': ssl_media_url}

from django.conf import settings
from django.contrib.sites.models import Site
from settings import DeploymentType

def default(request):
    context = {}
    context['DEPLOYMENT'] = settings.DEPLOYMENT
    context['current_site'] = Site.objects.get_current()
    if settings.DEPLOYMENT != DeploymentType.PRODUCTION:
        context['GIT_COMMIT'] = settings.GIT_COMMIT
    context['site_email'] = settings.CONTACT_EMAIL
    if request.is_secure():
        context['protocol'] = 'https://'
    else:
        context['protocol'] = 'http://'
    context['current_site_url'] = (context['protocol'] +
            context['current_site'].domain)
    return context

def profile(request):
    context = {}
    if request.user.is_authenticated():
        context['profile'] = request.user.get_profile()
    return context

def ssl_media(request):
    if request.is_secure():
        ssl_media_url = settings.MEDIA_URL.replace('http://','https://')
    else:
        ssl_media_url = settings.MEDIA_URL
    return {'MEDIA_URL': ssl_media_url}

Add a context processor that adds the UserProfile to each context.


"""Test praw.util.cache."""

from .. import UnitTest

from praw.util.cache import cachedproperty

class TestCachedProperty(UnitTest):
    class Klass:
        @cachedproperty
        def nine(self):
            """Return 9."""
            return 9

        def ten(self):
            return 10

        ten = cachedproperty(ten, doc="Return 10.")

    def test_get(self):
        klass = self.Klass()
        assert "nine" not in klass.__dict__
        assert klass.nine == 9
        assert "nine" in klass.__dict__

    def test_repr(self):
        klass = self.Klass()
        assert repr(klass.nine) == "9"

        property_repr = repr(self.Klass.nine)
        assert property_repr.startswith("<cachedproperty <function")

    def test_doc(self):
        assert self.Klass.nine.__doc__ == "Return 9."
        assert self.Klass.ten.__doc__ == "Return 10."

"""Test praw.util.cache."""

from .. import UnitTest

from praw.util.cache import cachedproperty

class TestCachedProperty(UnitTest):
    class Klass:
        @cachedproperty
        def nine(self):
            """Return 9."""
            return 9

        def ten(self):
            return 10

        ten = cachedproperty(ten, doc="Return 10.")

    def test_get(self):
        klass = self.Klass()
        assert "nine" not in klass.__dict__
        assert klass.nine == 9
        assert "nine" in klass.__dict__
        assert "ten" not in klass.__dict__
        assert klass.ten == 10
        assert "ten" in klass.__dict__

    def test_repr(self):
        klass = self.Klass()
        assert repr(klass.nine) == "9"

        property_repr = repr(self.Klass.nine)
        assert property_repr.startswith("<cachedproperty <function")

        assert repr(klass.ten) == "10"

        property_repr = repr(self.Klass.ten)
        assert property_repr.startswith("<cachedproperty <function")

    def test_doc(self):
        assert self.Klass.nine.__doc__ == "Return 9."
        assert self.Klass.ten.__doc__ == "Return 10."

Test for property ten as well

Test for property ten as well


from django.conf.urls import url
from django.views import generic

from . import views

app_name = 'main'
urlpatterns = [
    url(r'^$', views.AboutView.as_view(), name='about'),
    url(r'^chas/$', views.AboutChasView.as_view(), name='chas'),
    url(r'^evan/$', views.AboutEvanView.as_view(), name='evan'),
]

from django.urls import include, path

from . import views

app_name = 'main'
urlpatterns = [
    path('', views.AboutView.as_view(), name='about'),
    path('chas/', views.AboutChasView.as_view(), name='chas'),
    path('evan/', views.AboutEvanView.as_view(), name='evan'),
]

Move some urlpatterns to DJango 2.0 preferred method


import unittest

from firmant.db.relations import schema

class TestSchemaLoad(unittest.TestCase):

    def testLoad(self):
        if schema('loader-works') != 'SCHEMA LOAD WORKING PROPERLY':
            self.fail()

suite = unittest.TestLoader().loadTestsFromTestCase(TestSchemaLoad)

import unittest

from firmant.db.relations import schema

class TestSchemaLoad(unittest.TestCase):

    def testLoad(self):
        self.assertEqual(schema('loader-works'), 'SCHEMA LOAD WORKING PROPERLY')

suite = unittest.TestLoader().loadTestsFromTestCase(TestSchemaLoad)

Update the schema load test.

Update the schema load test.

It now has 100% coverage if the tests pass.


def transform_scalars(dataset):
    from tomviz import utils
    import numpy as np

    #----USER SPECIFIED VARIABLES-----#
    ###XRANGE###
    ###YRANGE###
    ###ZRANGE###
    #---------------------------------#

    data_bs = utils.get_array(dataset) #get data as numpy array

    if data_bs is None: #Check if data exists
        raise RuntimeError("No data array found!")

    for i in range(ZRANGE[0],ZRANGE[1]):
        a = data_bs[:,:,i] - np.average(data_bs[XRANGE[0]:XRANGE[1],YRANGE[0]:YRANGE[1],i])
        data_bs[:,:,i] = a

    utils.set_array(dataset, data_bs)

def transform_scalars(dataset):
    from tomviz import utils
    import numpy as np

    #----USER SPECIFIED VARIABLES-----#
    ###XRANGE###
    ###YRANGE###
    ###ZRANGE###
    #---------------------------------#

    data_bs = utils.get_array(dataset) #get data as numpy array

    data_bs = data_bs.astype(np.float32) #change tilt series type to float

    if data_bs is None: #Check if data exists
        raise RuntimeError("No data array found!")

    for i in range(ZRANGE[0],ZRANGE[1]):
        a = data_bs[:,:,i] - np.average(data_bs[XRANGE[0]:XRANGE[1],YRANGE[0]:YRANGE[1],i])
        data_bs[:,:,i] = a

    utils.set_array(dataset, data_bs)

Change tilt series type to float in manual background sub.


from nose.tools import *
from webtest_plus import TestApp

from tests.base import OsfTestCase
from tests.factories import AuthUserFactory
from website.app import init_app
from website.addons.twofactor.tests import _valid_code

app = init_app(
    routes=True,
    set_backends=False,
    settings_module='website.settings',
)

class TestViews(OsfTestCase):
    def setUp(self):
        super(TestViews, self).setUp()
        self.user = AuthUserFactory()
        self.user.add_addon('twofactor')
        self.user_settings = self.user.get_addon('twofactor')

        self.app = TestApp(app)

    def test_confirm_code(self):
        # Send a valid code to the API endpoint for the user settings.
        res = self.app.post_json(
            '/api/v1/settings/twofactor/',
            {'code': _valid_code(self.user_settings.totp_secret)},
            auth=self.user.auth
        )

        # reload the user settings object from the DB
        self.user_settings.reload()

        assert_true(self.user_settings.is_confirmed)
        assert_equal(res.status_code, 200)

from nose.tools import *
from webtest.app import AppError
from webtest_plus import TestApp

from tests.base import OsfTestCase
from tests.factories import AuthUserFactory
from website.app import init_app
from website.addons.twofactor.tests import _valid_code

app = init_app(
    routes=True,
    set_backends=False,
    settings_module='website.settings',
)

class TestViews(OsfTestCase):
    def setUp(self):
        super(TestViews, self).setUp()
        self.user = AuthUserFactory()
        self.user.add_addon('twofactor')
        self.user_settings = self.user.get_addon('twofactor')

        self.app = TestApp(app)

    def test_confirm_code(self):
        # Send a valid code to the API endpoint for the user settings.
        res = self.app.post_json(
            '/api/v1/settings/twofactor/',
            {'code': _valid_code(self.user_settings.totp_secret)},
            auth=self.user.auth
        )

        # reload the user settings object from the DB
        self.user_settings.reload()

        assert_true(self.user_settings.is_confirmed)
        assert_equal(res.status_code, 200)

    def test_confirm_code_failure(self):
        with assert_raises(AppError) as error:
            res = self.app.post_json(
                '/api/v1/settings/twofactor/',
                {'code': '000000'},
                auth=self.user.auth
            )

            assert_in('403 FORBIDDEN', error.message)

        # reload the user settings object from the DB
        self.user_settings.reload()

        assert_false(self.user_settings.is_confirmed)

Add test for failure to confirm 2FA code


import cherrypy
import os
import raatest

from conary import dbstore
from conary.server import schema

class webPluginTest(raatest.webTest):
    def __init__(
        self, module = None, init = True, preInit = None, preConst = None):

        def func(rt):
            cherrypy.root.servicecfg.pluginDirs = [os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "raaplugins"))]
            if preInit:
                preInit(rt)

        return raatest.webTest.__init__(
            self, module=module, init=init, preInit=func, preConst=preConst)

import cherrypy
import os
import raatest

from conary import dbstore
from conary.server import schema

class webPluginTest(raatest.webTest):
    def __init__(
        self, module = None, init = True, preInit = None, preConst = None):

        def func(rt):
            cherrypy.root.servicecfg.pluginDirs = [os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "raaplugins"))]
            if preInit:
                preInit(rt)

        return raatest.webTest.__init__(
            self, module=module, init=init, preInit=func, preConst=preConst)

Fix relative path to plugin directories (RBL-2737)


from django.shortcuts import render
from django.contrib import messages

# Create your views here.

def test(request):
    messages.debug(request, 'This is a debug alert')
    messages.info(request, 'This is an info alert')
    messages.success(request, 'This is a success alert')
    messages.warning(request, 'This is a warning alert')
    messages.error(request, 'This is an error alert')

    return render(request, 'alerts/test.html')

from django.shortcuts import render
from django.contrib import messages

# Create your views here.

def test(request):
    messages.set_level(request, messages.DEBUG)

    messages.debug(request, 'This is a debug alert')
    messages.info(request, 'This is an info alert')
    messages.success(request, 'This is a success alert')
    messages.warning(request, 'This is a warning alert')
    messages.error(request, 'This is an error alert')

    return render(request, 'alerts/test.html')

Set message level to DEBUG in test view

Set message level to DEBUG in test view


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lib.login import Login

def pychat():
    login = Login
    login()

if __name__ == '__main__':
    pychat()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lib.login import Login
from lib.client import Client
from Tkinter import *

class PyChat(object):

    def __init__(self):
        self.root = Tk()
        self.root.geometry("300x275+400+100")

    def login(self):
        self.login = Login(self.root, self.create_client)

    def run(self):
        self.root.mainloop()

    def create_client(self):
        credentials = self.login.login_credentials()
        credentials['root'] = self.root
        self.reset()
        self.client = Client(**credentials)

    def reset(self):
        for element in self.root.winfo_children():
            element.destroy()

if __name__ == '__main__':
    pychat = PyChat()
    pychat.login()
    pychat.run()

Rework to have a central root window controlled from a top class


'''
Turn a Python script into Intel HEX format to be concatenated at the
end of the MicroPython firmware.hex.  A simple header is added to the
script.
'''

import sys
import struct
import binascii

# read script body
with open(sys.argv[1], "rb") as f:
    data = f.read()

# add header, pad to multiple of 16 bytes
data = b'MP' + struct.pack('<H', len(data)) + data
data = data + bytes(16 - len(data) % 16)
assert len(data) <= 0x2000

# convert to .hex format
addr = 0x3e000 # magic start address in flash
for i in range(0, len(data), 16):
    chunk = data[i:min(i + 16, len(data))]
    chunk = struct.pack('>BHB', len(chunk), addr & 0xffff, 0) + chunk
    checksum = (-(sum(data))) & 0xff
    hexline = ':%s%02X' % (str(binascii.hexlify(chunk), 'utf8').upper(), checksum)
    print(hexline)
    addr += 16

'''
Turn a Python script into Intel HEX format to be concatenated at the
end of the MicroPython firmware.hex.  A simple header is added to the
script.
'''

import sys
import struct
import binascii

# read script body
with open(sys.argv[1], "rb") as f:
    data = f.read()

# add header, pad to multiple of 16 bytes
data = b'MP' + struct.pack('<H', len(data)) + data
data = data + bytes(16 - len(data) % 16)
assert len(data) <= 0x2000

# convert to .hex format
addr = 0x3e000 # magic start address in flash
for i in range(0, len(data), 16):
    chunk = data[i:min(i + 16, len(data))]
    chunk = struct.pack('>BHB', len(chunk), addr & 0xffff, 0) + chunk
    checksum = (-(sum(chunk))) & 0xff
    hexline = ':%s%02X' % (str(binascii.hexlify(chunk), 'utf8').upper(), checksum)
    print(hexline)
    addr += 16

Fix bug in heexlifyscript.py when computing checksum.


#!/usr/bin/env python
# -*- coding: latin-1 -*-

"""Unit tests."""

import unittest
from daily_parser import url_from_args

class TestDailyParser(unittest.TestCase):

    """Testing methods from daily_parser."""

    def test_url_from_args(self):
        output = url_from_args(2014, 1)
        expected = 'https://dons.wikimedia.fr/journal/2014-01'
        self.assertEqual(output, expected)

#!/usr/bin/env python
# -*- coding: latin-1 -*-

"""Unit tests."""

import unittest
from daily_parser import url_from_args, DonationsParser

class TestDailyParser(unittest.TestCase):

    """Testing methods from daily_parser."""

    def test_url_from_args(self):
        output = url_from_args(2014, 1)
        expected = 'https://dons.wikimedia.fr/journal/2014-01'
        self.assertEqual(output, expected)

class TestDonationsParser(unittest.TestCase):

    """Testing DonationsParser class."""

    def setUp(self):
        self.donations_parser = DonationsParser(2014, 01)
        donations_data = {
            '01': {'sum': 370, 'avg': 46.25, 'quantity': 8},
            '02': {'sum': 5682, 'avg': 132.14, 'quantity': 43}
        }
        self.donations_parser.donations = donations_data

    def test_get_csv(self):
        expected = """'day', 'sum', 'quantity', 'avg'
'2014-01-01', 370, 8, 46.25
'2014-01-02', 5682, 43, 132.14
"""
        output = self.donations_parser.get_csv()
        self.assertEqual(output, expected)

Add unit test for DonationsParser.get_csv


from flask import Blueprint, render_template, url_for, redirect, flash
from flask.ext.login import login_required, logout_user, current_user, login_user
from .forms import LoginForm
from school.config import FLASH_SUCCESS, FLASH_INFO, FLASH_WARNING

frontend = Blueprint('frontend', __name__)

@frontend.route('/login', methods=["GET", "POST"])
def login():
    if current_user.is_authenticated():  # user is already logged in
        flash("You are already logged in", FLASH_WARNING)
        return redirect(url_for('user.index'))

    form = LoginForm()
    if form.validate_on_submit():
        flash('Successfully logged in as %s' % form.user.username, FLASH_SUCCESS)
        login_user(form.user)

        return form.redirect("user.index")

    return render_template('frontend/index.html', form=form)

@frontend.route('/logout')
@login_required
def logout():
    logout_user()
    flash('You have been logged out.', FLASH_INFO)

    return redirect(url_for("frontend.login"))

from flask import Blueprint, render_template, url_for, redirect, flash
from flask.ext.login import login_required, logout_user, current_user, login_user
from .forms import LoginForm
from school.config import FLASH_SUCCESS, FLASH_INFO, FLASH_WARNING

frontend = Blueprint('frontend', __name__)

@frontend.route('/login', methods=["GET", "POST"])
def login():
    if current_user.is_authenticated():  # user is already logged in
        flash("You are already logged in", FLASH_WARNING)
        return redirect(url_for('user.index'))

    form = LoginForm()
    if form.validate_on_submit():
        login_user(form.user)

        return form.redirect("user.index")

    return render_template('frontend/index.html', form=form)

@frontend.route('/logout')
@login_required
def logout():
    logout_user()
    flash('You have been logged out.', FLASH_INFO)

    return redirect(url_for("frontend.login"))

Remove flash success message when logging in.


import pytest

@pytest.mark.xfail
def test_missing_file(audiomgr):
    sound = audiomgr.get_sound('/not/a/valid/file.ogg')
    assert sound is None

import pytest

def test_missing_file(audiomgr):
    sound = audiomgr.get_sound('/not/a/valid/file.ogg')
    assert str(sound).startswith('NullAudioSound')

tests: Update audio test to recognize missing sounds as NullAudioSound


# -*- coding: utf-8 -*-
import logging

__version__ = VERSION = '0.0.1'
__project__ = PROJECT = 'django-misc'

log = logging.getLogger( __name__ )

# -*- coding: utf-8 -*-
import logging

__version__ = VERSION = '0.0.2'
__project__ = PROJECT = 'django-misc'

log = logging.getLogger( __name__ )

Change version to 0.0.2 to update pypi repo


"""
Test the Prime Finder class

Still working on getting dependency injection working.
"""

import unittest
from primes.Primes import PrimeFinder
#from primes.Primes import PrimeGenerator

class PrimeFinderTests(unittest.TestCase):
    def test_find_prime(self):
        prime_finder = PrimeFinder.PrimeFinder(PrimeGenerator.PrimeGenerator())
        self.assertEqual(prime_finder.find_prime(6), 13)

"""
Test the Prime Finder class

Still working on getting dependency injection working.
Injecting the Generator into the Finder allows for many possibilities.
From the testing perspective this would allow me to inject a mock object
for the Generator that returns a set value speeding up the testing of the
Prime Finder class.
"""

import unittest
from primes.Primes import PrimeFinder
#from primes.Primes import PrimeGenerator

class PrimeFinderTests(unittest.TestCase):
    def test_find_prime(self):
        prime_finder = PrimeFinder.PrimeFinder(PrimeGenerator.PrimeGenerator())
        self.assertEqual(prime_finder.find_prime(6), 13)

Add additional comments regarding Dependency Injection


MSG_INFO    = 0x01
MSG_WARNING = 0x02
MSG_ERROR   = 0x04
MSG_VERBOSE = 0x08
MSG_ALL     = MSG_INFO | MSG_WARNING | MSG_ERROR | MSG_VERBOSE

def logi(msg):
    print("[INFO] " + msg)
def logv(msg):
    print("[VERBOSE] " + msg)
def logw(msg):
    print("[WARNING] " + msg)
def loge(msg):
    print("[ERROR] " + msg)

class Logger(object):
    def __init__(self):
        self.logger_level = MSG_ALL
    def info(self, msg):
        if self.logger_level & MSG_INFO:
            logi(msg)
    def warning(self, msg):
        if self.logger_level & MSG_WARNING:
            logw(msg)
    def error(self, msg):
        if self.logger_level & MSG_ERROR:
            loge(msg)
    def verbose(self, msg):
        if self.logger_level & MSG_VERBOSE:
            logv(msg)

MSG_INFO    = 0x01
MSG_WARNING = 0x02
MSG_ERROR   = 0x04
MSG_VERBOSE = 0x08
MSG_ALL     = MSG_INFO | MSG_WARNING | MSG_ERROR | MSG_VERBOSE

def logi(msg):
    print("[INFO] " + msg)
def logv(msg):
    print("[VERBOSE] " + msg)
def logw(msg):
    print("[WARNING] " + msg)
def loge(msg):
    print("\033[1;31m[ERROR] " + msg + "\033[m")

class Logger(object):
    def __init__(self):
        self.logger_level = MSG_ALL
    def info(self, msg):
        if self.logger_level & MSG_INFO:
            logi(msg)
    def warning(self, msg):
        if self.logger_level & MSG_WARNING:
            logw(msg)
    def error(self, msg):
        if self.logger_level & MSG_ERROR:
            loge(msg)
    def verbose(self, msg):
        if self.logger_level & MSG_VERBOSE:
            logv(msg)

Add color for error message.

Add color for error message.


import os
import json
import time
import socket
from subprocess import call

import requests

from test import test_activity
from build import compile_bundle

# Fixes a weird bug... it might create some though :P
os.environ['http_proxy'] = ''

# HOST = 'http://localhost:5001'
HOST = 'http://aslo-bot-master.sugarlabs.org'

print 'Waiting for 1st task'

while True:
    r = requests.get(HOST + '/task')
    if r.status_code == 404:
        time.sleep(7)
        continue
    task = r.json()

    print 'Got new task'
    call(['git', 'clone', 'https://www.github.com/' + task['gh'],
          'dl'])
    result = test_activity(task['bundle_id'], task['gh'])

    data = {'result': result, 'file': compile_bundle(),
            'bundle_id': task['bundle_id'], 'task_id': task['task_id']}

    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    r = requests.post(HOST + '/done',
                      data=json.dumps(data), headers=headers)

    call(['rm', '-rf', 'dl'])

    print 'Mined 1 activity:', task['bundle_id'], task['gh']

import os
import json
import time
import socket
from subprocess import call

import requests

from test import test_activity
from build import compile_bundle

# Fixes a weird bug... it might create some though :P
os.environ['http_proxy'] = ''

# HOST = 'http://localhost:5001'
HOST = 'http://aslo-bot-master.sugarlabs.org'

print 'Waiting for 1st task'

while True:
    try:
        r = requests.get(HOST + '/task')
    except requests.exceptions.ConnectionError, e:
        continue
    if r.status_code == 404:
        time.sleep(7)
        continue
    task = r.json()

    print 'Got new task'
    call(['git', 'clone', 'https://www.github.com/' + task['gh'],
          'dl'])
    result = test_activity(task['bundle_id'], task['gh'])

    data = {'result': result, 'file': compile_bundle(),
            'bundle_id': task['bundle_id'], 'task_id': task['task_id']}

    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    r = requests.post(HOST + '/done',
                      data=json.dumps(data), headers=headers)

    call(['rm', '-rf', 'dl'])

    print 'Mined 1 activity:', task['bundle_id'], task['gh']

Stop bots from randomly disconnecting

Stop bots from randomly disconnecting


import os
import logging

def logger_setup(name):
    loglevel = ''
    try:
        if os.environ['PB_LOGLEVEL'] == 'DEBUG':
            loglevel = logging.DEBUG

        if os.environ['PB_LOGLEVEL'] == 'INFO':
            loglevel = logging.INFO

        if os.environ['PB_LOGLEVEL'] == 'WARN':
            loglevel = logging.WARN
    except:
        loglevel = logging.DEBUG

    # from https://docs.python.org/2/howto/logging.html#configuring-logging
    # set up new logger for this file
    logger = logging.getLogger(name)
    logger.setLevel(loglevel)

    # console handler for logging
    conLog = logging.StreamHandler()
    conLog.setLevel(loglevel)

    # formatter
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # format console logs using formatter
    conLog.setFormatter(formatter)

    # add console logging transport to logger
    logger.addHandler(conLog)

    return logger

import os
import logging

def logger_setup(name):
    loglevel = ''
    try:
        if os.environ['PB_LOGLEVEL'] == 'DEBUG':
            loglevel = logging.DEBUG

        if os.environ['PB_LOGLEVEL'] == 'INFO':
            loglevel = logging.INFO

        if os.environ['PB_LOGLEVEL'] == 'WARN':
            loglevel = logging.WARN
    except:
        loglevel = logging.DEBUG

    # from https://docs.python.org/2/howto/logging.html#configuring-logging
    # set up new logger for this file
    logger = logging.getLogger(name)
    logger.setLevel(loglevel)

    # formatter
    formatter = logging.Formatter('PID: %(process)d - %(asctime)s - %(name)s - %(levelname)s - %(funcName)s - %(message)s')

    # console handler for logging
    conLog = logging.StreamHandler()
    conLog.setLevel(loglevel)
    # format console logs using formatter
    conLog.setFormatter(formatter)

    # log to file handler
    fileLog = logging.FileHandler('pairing-bot.log', encoding='utf-8')
    fileLog.setLevel(logging.DEBUG)
    # format console logs using formatter
    fileLog.setFormatter(formatter)

    # add console logging transport to logger
    logger.addHandler(conLog)

    # add file transport to logger
    logger.addHandler(fileLog)

    return logger

Add file transport and update log msg formatting to include pid and fn name


from django.conf.urls import patterns, url
from .views import FaqQuestionList, FaqCategoryDetail, FaqQuestionDetail

urlpatterns = patterns('',
    url(r'^$', FaqQuestionList.as_view(), name='faqquestion_index'),
    url(r'^(?P<slug>[^/]+)/$', FaqCategoryDetail.as_view(), name='faqcategory_detail'),
    url(r'^(?P<cat_slug>[^/]+)/(?P<slug>[^/]+)/$', FaqQuestionDetail.as_view(), name='faqquestion_detail'),
)

from django.conf.urls import url
from .views import FaqQuestionList, FaqCategoryDetail, FaqQuestionDetail

urlpatterns = [
    url(r'^$', FaqQuestionList.as_view(), name='faqquestion_index'),
    url(r'^(?P<slug>[^/]+)/$', FaqCategoryDetail.as_view(), name='faqcategory_detail'),
    url(r'^(?P<cat_slug>[^/]+)/(?P<slug>[^/]+)/$', FaqQuestionDetail.as_view(), name='faqquestion_detail'),
]

Fix Django 1.9 warnings about patterns('', ..)


import floppyforms.__future__ as floppyforms
from django.test import TestCase

from django_backend.backend.base.forms import BaseBackendForm

from .models import OneFieldModel

class OneFieldForm(BaseBackendForm):
    class Meta:
        model = OneFieldModel
        exclude = ()

class BaseBackendFormTests(TestCase):
    def test_has_superform_metaclass(self):
        from django_superform.forms import SuperModelFormMetaclass

        self.assertTrue(
            issubclass(BaseBackendForm.__metaclass__, SuperModelFormMetaclass))

    def test_has_floppyforms_metaclass(self):
        from floppyforms.__future__.models import ModelFormMetaclass

        self.assertTrue(
            issubclass(BaseBackendForm.__metaclass__, ModelFormMetaclass))

    def test_model_field_is_using_floppyforms_widget(self):
        form = OneFieldForm()
        self.assertTrue(
            isinstance(form.fields['chars'].widget, floppyforms.TextInput))

import floppyforms.__future__ as floppyforms
from django.test import TestCase

from django_backend.forms import BaseBackendForm

from .models import OneFieldModel

class OneFieldForm(BaseBackendForm):
    class Meta:
        model = OneFieldModel
        exclude = ()

class BaseBackendFormTests(TestCase):
    def test_has_superform_metaclass(self):
        from django_superform.forms import SuperModelFormMetaclass

        self.assertTrue(
            issubclass(BaseBackendForm.__metaclass__, SuperModelFormMetaclass))

    def test_has_floppyforms_metaclass(self):
        from floppyforms.__future__.models import ModelFormMetaclass

        self.assertTrue(
            issubclass(BaseBackendForm.__metaclass__, ModelFormMetaclass))

    def test_model_field_is_using_floppyforms_widget(self):
        form = OneFieldForm()
        self.assertTrue(
            isinstance(form.fields['chars'].widget, floppyforms.TextInput))

Fix forms import in tests

Fix forms import in tests


import tempfile
import tee

from process import Process
from exit import err_exit

class SCP(object):
    """Secure copy and FTP abstraction."""

    def __init__(self, process=None):
        self.process = process or Process()

    def run_scp(self, distfile, location):
        if not self.process.quiet:
            print 'scp-ing to %(location)s' % locals()
        rc = self.process.os_system(
            'scp "%(distfile)s" "%(location)s"' % locals())
        if rc != 0:
            err_exit('scp failed')
        return rc

    def run_sftp(self, distfile, location):
        if not self.process.quiet:
            print 'sftp-ing to %(location)s' % locals()
        with tempfile.NamedTemporaryFile(prefix='sftp-') as file:
            file.write('put "%(distfile)s"\n' % locals())
            file.write('quit\n')
            file.flush()
            cmdfile = file.name
            rc, lines = self.process.popen(
                'sftp -b "%(cmdfile)s" "%(location)s"' % locals(),
                echo=tee.StartsWith('Uploading'))
            if rc != 0:
                err_exit('sftp failed')
            return rc

import tempfile
import tee

from os.path import split

from process import Process
from chdir import ChdirStack
from exit import err_exit

class SCP(object):
    """Secure copy and FTP abstraction."""

    def __init__(self, process=None):
        self.process = process or Process()
        self.dirstack = ChdirStack()

    def run_scp(self, distfile, location):
        if not self.process.quiet:
            print 'scp-ing to %(location)s' % locals()
        rc = self.process.os_system(
            'scp "%(distfile)s" "%(location)s"' % locals())
        if rc != 0:
            err_exit('scp failed')
        return rc

    def run_sftp(self, distfile, location):
        if not self.process.quiet:
            print 'sftp-ing to %(location)s' % locals()
        dir, distfile = split(distfile)
        self.dirstack.push(dir)
        try:
            with tempfile.NamedTemporaryFile(prefix='sftp-') as file:
                file.write('put "%(distfile)s"\n' % locals())
                file.write('bye\n')
                file.flush()
                cmdfile = file.name
                rc, lines = self.process.popen(
                    'sftp -b "%(cmdfile)s" "%(location)s"' % locals(),
                    echo=tee.StartsWith('Uploading'))
                if rc != 0:
                    err_exit('sftp failed')
                return rc
        finally:
            self.dirstack.pop()

Change to dist dir before uploading to lose the absolute path.


import datetime

from celery import task
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

from django.db.models import Q

from thing.models import APIKey, TaskState

# ---------------------------------------------------------------------------
# Periodic task to perform database table cleanup
@task(name='thing.table_cleaner')
def table_cleaner():
    utcnow = datetime.datetime.utcnow()

    queued_timeout = utcnow - datetime.timedelta(minutes=120)

    # Build a QuerySet to find broken tasks
    taskstates = TaskState.objects.filter(state=TaskState.QUEUED_STATE, mod_time__lte=queued_timeout)
    for ts in taskstates:
        logger.warn('[table_cleaner] Stuck task: %d | %d | %s | %s', ts.id, ts.keyid, ts.parameter, ts.url)

    count = taskstates.update(mod_time=utcnow, next_time=utcnow, state=TaskState.READY_STATE)
    if count > 0:
        logger.warn('[table_cleaner] Reset %d broken task(s)', count)

    # Build a QuerySet to find tasks that refer to no longer existent keys
    taskstates = TaskState.objects.exclude(
        Q(keyid=-1)
        |
        Q(keyid__in=APIKey.objects.values('keyid'))
    )
    taskstates.delete()

# ---------------------------------------------------------------------------

import datetime

from celery import task
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

from django.db.models import Q

from thing.models import APIKey, TaskState

# ---------------------------------------------------------------------------
# Periodic task to perform database table cleanup
@task(name='thing.table_cleaner')
def table_cleaner():
    utcnow = datetime.datetime.utcnow()

    queued_timeout = utcnow - datetime.timedelta(minutes=120)

    # Build a QuerySet to find broken tasks
    taskstates = TaskState.objects.filter(state=TaskState.QUEUED_STATE, mod_time__lte=queued_timeout)
    for ts in taskstates:
        logger.warn('[table_cleaner] Stuck task: %d | %d | %s | %s', ts.id, ts.keyid, ts.parameter, ts.url)

    count = taskstates.update(mod_time=utcnow, next_time=utcnow, state=TaskState.READY_STATE)
    if count > 0:
        logger.warn('[table_cleaner] Reset %d broken task(s)', count)

    # Build a QuerySet to find tasks that refer to no longer existent keys
    taskstates = TaskState.objects.exclude(
        Q(keyid=-1)
        |
        Q(keyid__in=APIKey.objects.filter(valid=True).values('keyid'))
    )
    taskstates.delete()

# ---------------------------------------------------------------------------

Change thing.tasks.table_cleaner to delete TaskState objects for any invalid APIKeys


Grid-building functions
-----------------------

:func:`pixels_to_radius` and :func:`pixels_to_phi` were renamed to
:func:`radial_grid` and :func:`angle_grid` respectively. The name and order
of their arguments was also changed: see their docstring or API docs for
details. Importantly, the orientation of the output of angle grid has been
changed. Again, see the docstring for details.

DOC: Add change in angle_grid orientation to API changes.


# coding: utf-8
from unittest import TestCase
from chipy8 import Memory

class TestMemory(TestCase):
    def setUp(self):
        self.memory = Memory()

    def test_write(self):
        'Write a byte to memory then read it.'
        address = 0x200
        self.memory.write_byte(0x200, 0x01)
        self.assertEqual(0x01, self.memory.read_byte(0x200))

    def test_load(self):
        'Load a stream of bytes to memory starting on an address.'
        address = 0x200
        self.memory.load(0x200, [0x01, 0x02, 0x03])
        self.assertEqual(0x01, self.memory.read_byte(address))
        self.assertEqual(0x02, self.memory.read_byte(address + 1))
        self.assertEqual(0x03, self.memory.read_byte(address + 2))

# coding: utf-8
from unittest import TestCase
from chipy8 import Memory

class TestMemory(TestCase):
    def setUp(self):
        self.memory = Memory()

    def test_write(self):
        'Write a byte to memory then read it.'
        address = 0x200
        self.memory.write_byte(address, 0x01)
        self.assertEqual(0x01, self.memory.read_byte(address))

    def test_load(self):
        'Load a stream of bytes to memory starting on an address.'
        address = 0x200
        self.memory.load(address, [0x01, 0x02, 0x03])
        self.assertEqual(0x01, self.memory.read_byte(address))
        self.assertEqual(0x02, self.memory.read_byte(address + 1))
        self.assertEqual(0x03, self.memory.read_byte(address + 2))

Clarify values used in tests.

Clarify values used in tests.


#!/usr/bin/env python

from pyhmsa.datafile import DataFile
from pyhmsa.type.language import langstr
datafile = DataFile()

author = langstr('Fyodor Dostoyevsky', {'ru': u'Фёдор Миха́йлович Достое́вский'})
datafile.header.author = author

print(datafile.header.author.alternatives['ru']) # Returns ...

#!/usr/bin/env python

from pyhmsa.datafile import DataFile
from pyhmsa.type.language import langstr
datafile = DataFile()

author = langstr('Wilhelm Conrad Roentgen', {'de': u'Wilhelm Conrad Röntgen'})
datafile.header.author = author

print(datafile.header.author.alternatives['de']) # Returns ...

Replace name in alternative language to prevent compilation problems with LaTeX


__version__ = "0.1.3"

__title__ = "gh2md"
__description__ = "Export Github repository issues to markdown files: https://github.com/mattduck/gh2md."
__doc__ = __description__
__uri__ = "https://github.com/mattduck/gh2md"

__author__ = "Matthew Duck"
__email__ = "matt@mattduck.com"

__license__ = "MIT"
__copyright__ = "Copyright (c) 2017 Matthew Duck"

__version__ = "0.1.4dev"

__title__ = "gh2md"
__description__ = "Export Github repository issues to markdown files: https://github.com/mattduck/gh2md."
__doc__ = __description__
__uri__ = "https://github.com/mattduck/gh2md"

__author__ = "Matthew Duck"
__email__ = "matt@mattduck.com"

__license__ = "MIT"
__copyright__ = "Copyright (c) 2017 Matthew Duck"

Bump to next dev version

Bump to next dev version

This is really just to confirm that Travis is still building correctly.


# Licensed under an MIT open source license - see LICENSE

'''
Test functions for SCF
'''

from unittest import TestCase

import numpy as np
import numpy.testing as npt

from ..statistics import SCF, SCF_Distance
from ._testing_data import \
    dataset1, dataset2, computed_data, computed_distances

class testSCF(TestCase):

    def setUp(self):
        self.dataset1 = dataset1
        self.dataset2 = dataset2

    def test_SCF_method(self):
        self.tester = SCF(dataset1["cube"], size=11)
        self.tester.run()

        assert np.allclose(self.tester.scf_surface, computed_data['scf_val'])

    def test_SCF_distance(self):
        self.tester_dist = \
            SCF_Distance(dataset1["cube"],
                         dataset2["cube"], size=11).distance_metric()
        npt.assert_almost_equal(self.tester_dist.distance,
                                computed_distances['scf_distance'])

# Licensed under an MIT open source license - see LICENSE

'''
Test functions for SCF
'''

from unittest import TestCase

import numpy as np
import numpy.testing as npt
from scipy.ndimage import zoom

from ..statistics import SCF, SCF_Distance
from ._testing_data import \
    dataset1, dataset2, computed_data, computed_distances

class testSCF(TestCase):

    def test_SCF_method(self):
        self.tester = SCF(dataset1["cube"], size=11)
        self.tester.run()

        assert np.allclose(self.tester.scf_surface, computed_data['scf_val'])

    def test_SCF_distance(self):
        self.tester_dist = \
            SCF_Distance(dataset1["cube"],
                         dataset2["cube"], size=11).distance_metric()
        npt.assert_almost_equal(self.tester_dist.distance,
                                computed_distances['scf_distance'])

    def test_SCF_regrid_distance(self):
        hdr = dataset1["cube"][1].copy()
        hdr["CDELT2"] = 0.5 * hdr["CDELT2"]
        hdr["CDELT1"] = 0.5 * hdr["CDELT1"]
        cube = zoom(dataset1["cube"][0], (1, 2, 2))

        self.tester_dist_zoom = \
            SCF_Distance([cube, hdr], dataset1["cube"],
                         size=11).distance_metric(verbose=True)

        fid_dist = 0.02

        assert self.tester_dist_zoom < fid_dist

Add simple SCF test for unequal grids

Add simple SCF test for unequal grids


__version_info__ = (0, 4, 1)
__version__ = '.'.join(map(str, __version_info__))

__version_info__ = (0, 5, 0)
__version__ = '.'.join(map(str, __version_info__))

Bump version to 0.5 cuz feature

Bump version to 0.5 cuz feature


from django.template import Library
from django_prices.templatetags import prices_i18n

from .. import exchange_currency

register = Library()

@register.simple_tag  # noqa
def gross_in_currency(price, currency):  # noqa
    converted_price = exchange_currency(price, currency)
    return prices_i18n.gross(converted_price)

@register.simple_tag  # noqa
def net_in_currency(price, currency):  # noqa
    converted_price = exchange_currency(price, currency)
    return prices_i18n.net(converted_price)

@register.simple_tag  # noqa
def tax_in_currency(price, currency):  # noqa
    converted_price = exchange_currency(price, currency)
    return prices_i18n.tax(converted_price)

@register.simple_tag
def discount_amount_in_currency(discount, price, currency):
    price = exchange_currency(price, to_currency=currency)
    discount_amount = exchange_currency(discount.amount, to_currency=currency)
    discount.amount = discount_amount
    return (price | discount) - price

Add templatetag for converting discounts between currencies


"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from django.contrib.auth.models import User
from django.core.mail import get_connection
from django.test import TestCase
from inviter.utils import invite
import shortuuid

class InviteTest(TestCase):
    def setUp(self):
        self.inviter = User.objects.create(username = shortuuid.uuid())
        self.existing = User.objects.create(username = shortuuid.uuid(),
            email = 'existing@example.com')
        self.backend = get_connection('django.core.mail.backends.locmem.EmailBackend')

    def test_inviting(self):
        user = invite("foo@example.com", self.inviter)
        self.assertFalse(user.is_active)
        self.assertEqual(1, len(self.backend.emails))
        self.assertEqual(3, User.objects.count())

        user = invite("foo@example.com", self.inviter)
        self.assertFalse(user.is_active)
        self.assertEqual(2, len(self.backend.emails))
        self.assertEqual(3, User.objects.count())

        user = invite("existing@example.com", self.inviter)
        self.assertTrue(user.is_active)
        self.assertEqual(2, len(self.backend.emails))
        self.assertEqual(3, User.objects.count())

"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""
from django.contrib.auth.models import User
from django.core.mail import outbox
from django.test import TestCase
from inviter.utils import invite
import shortuuid

class InviteTest(TestCase):
    def setUp(self):
        self.inviter = User.objects.create(username = shortuuid.uuid())
        self.existing = User.objects.create(username = shortuuid.uuid(),
            email = 'existing@example.com')
        self.outbox = outbox

    def test_inviting(self):
        user = invite("foo@example.com", self.inviter)
        self.assertFalse(user.is_active)
        self.assertEqual(1, len(self.outbox))
        self.assertEqual(3, User.objects.count())

        user = invite("foo@example.com", self.inviter)
        self.assertFalse(user.is_active)
        self.assertEqual(2, len(self.outbox))
        self.assertEqual(3, User.objects.count())

        user = invite("existing@example.com", self.inviter)
        self.assertTrue(user.is_active)
        self.assertEqual(2, len(self.outbox))
        self.assertEqual(3, User.objects.count())

Test fix to check django.core.mail.outbox


# coding: utf8
from __future__ import unicode_literals

# These exceptions are used to add NORM values based on a token's ORTH value.
# Individual languages can also add their own exceptions and overwrite them -
# for example, British vs. American spelling in English.

# Norms are only set if no alternative is provided in the tokenizer exceptions.
# Note that this does not change any other token attributes. Its main purpose
# is to normalise the word representations so that equivalent tokens receive
# similar representations. For example: $ and € are very different, but they're
# both currency symbols. By normalising currency symbols to $, all symbols are
# seen as similar, no matter how common they are in the training data.

BASE_NORMS = {
    "'s": "'s",
    "'S": "'s",
    "’s": "'s",
    "’S": "'s",
    "’": "'",
    "‘": "'",
    "´": "'",
    "`": "'",
    "”": '"',
    "“": '"',
    "''": '"',
    "``": '"',
    "´´": '"',
    "„": '"',
    "»": '"',
    "«": '"',
    "…": "...",
    "—": "-",
    "–": "-",
    "--": "-",
    "---": "-",
    "€": "$",
    "£": "$",
    "¥": "$",
    "฿": "$",
    "US$": "$",
    "C$": "$",
    "A$": "$"
}

# coding: utf8
from __future__ import unicode_literals

# These exceptions are used to add NORM values based on a token's ORTH value.
# Individual languages can also add their own exceptions and overwrite them -
# for example, British vs. American spelling in English.

# Norms are only set if no alternative is provided in the tokenizer exceptions.
# Note that this does not change any other token attributes. Its main purpose
# is to normalise the word representations so that equivalent tokens receive
# similar representations. For example: $ and € are very different, but they're
# both currency symbols. By normalising currency symbols to $, all symbols are
# seen as similar, no matter how common they are in the training data.

BASE_NORMS = {
    "'s": "'s",
    "'S": "'s",
    "’s": "'s",
    "’S": "'s",
    "’": "'",
    "‘": "'",
    "´": "'",
    "`": "'",
    "”": '"',
    "“": '"',
    "''": '"',
    "``": '"',
    "´´": '"',
    "„": '"',
    "»": '"',
    "«": '"',
    "‘‘": '"',
    "’’": '"',
    "？": "?",
    "！": "!",
    "，": ",",
    "；": ";",
    "：": ":",
    "。": ".",
    "।": ".",
    "…": "...",
    "—": "-",
    "–": "-",
    "--": "-",
    "---": "-",
    "——": "-",
    "€": "$",
    "£": "$",
    "¥": "$",
    "฿": "$",
    "US$": "$",
    "C$": "$",
    "A$": "$"
}

Update base norm exceptions with more unicode characters

e.g. unicode variations of punctuation used in Chinese


__title__ = 'pylsdj'
__version__ = '1.1.0'
__build__ = 0x010100
__author__ = 'Alex Rasmussen'
__license__ = 'MIT'
__copyright__ = 'Copyright 2014 Alex Rasmussen'

import bread_spec
import chain
import clock
import consts
import filepack
from instrument import Instrument
from phrase import Phrase
from project import load_lsdsng, load_srm, Project
from savfile import SAVFile
from song import Song, Sequence
from speech_instrument import Word, SpeechInstrument
from synth import Synth
from table import Table

Make imports a little more intuitive.

Make imports a little more intuitive.


#
## PyMoira client library
##
## This file contains the more abstract methods which allow user to work with
## lists and list members.
#

import protocol
import utils
import datetime
from errors import *

class Filesys(object):
    info_query_description = (
        ('label', str),
        ('type', str),
        ('machine', str),
        ('name', str),
        ('mountpoint', str),
        ('access_mode', str),
        ('description', str),
        ('owner_user', str),
        ('owner_group', str),
        ('create', bool),
        ('type', str),
        ('lastmod_datetime', datetime.datetime),
        ('lastmod_by', str),
        ('lastmod_with', str),
    )

    def __init__(self, client, name):
        self.client = client
        self.name = name

    def loadInfo(self):
        """Loads the information about the list from the server into the object."""

        response, = self.client.query( 'get_filesys_by_label', (self.name, ), version = 14 )
        result = utils.responseToDict(self.info_query_description, response)
        self.__dict__.update(result)

#
## PyMoira client library
##
## This file contains the more abstract methods which allow user to work with
## lists and list members.
#

import protocol
import utils
import datetime
from errors import *

class Filesys(object):
    info_query_description = (
        ('label', str),
        ('type', str),
        ('machine', str),
        ('name', str),
        ('mountpoint', str),
        ('access_mode', str),
        ('description', str),
        ('owner_user', str),
        ('owner_group', str),
        ('create', bool),
        ('locker_type', str),
        ('lastmod_datetime', datetime.datetime),
        ('lastmod_by', str),
        ('lastmod_with', str),
    )

    def __init__(self, client, name):
        self.client = client
        self.name = name

    def loadInfo(self):
        """Loads the information about the list from the server into the object."""

        response, = self.client.query( 'get_filesys_by_label', (self.name, ), version = 14 )
        result = utils.responseToDict(self.info_query_description, response)
        self.__dict__.update(result)

Fix a name collision for two types of types.


import distutils.sysconfig
from glob import glob
import os
from os.path import join as pjoin, basename
from shutil import copy
from sys import platform

def main():
    """
    Copy HDF5 DLLs into installed h5py package
    """
    # This is the function Tox also uses to locate site-packages (Apr 2019)
    sitepackagesdir = distutils.sysconfig.get_python_lib(plat_specific=True)
    print("site packages dir:", sitepackagesdir)

    hdf5_path = os.environ.get("HDF5_DIR")
    print("HDF5_DIR", hdf5_path)

    # HDF5_DIR is not set when we're testing wheels; these should already have
    # the necessary libraries bundled in.
    if platform.startswith('win') and hdf5_path is not None:
        for f in glob(pjoin(hdf5_path, 'lib/*.dll')):
            copy(f, pjoin(sitepackagesdir, 'h5py', basename(f)))
            print("Copied", f)

    print("In installed h5py:", os.listdir(pjoin(sitepackagesdir, 'h5py')))

if __name__ == '__main__':
    main()

import distutils.sysconfig
from glob import glob
import os
from os.path import join as pjoin, basename
from shutil import copy
from sys import platform

def main():
    """
    Copy HDF5 DLLs into installed h5py package
    """
    # This is the function Tox also uses to locate site-packages (Apr 2019)
    sitepackagesdir = distutils.sysconfig.get_python_lib(plat_specific=True)
    print("site packages dir:", sitepackagesdir)

    hdf5_path = os.environ.get("HDF5_DIR")
    print("HDF5_DIR", hdf5_path)

    # HDF5_DIR is not set when we're testing wheels; these should already have
    # the necessary libraries bundled in.
    if platform.startswith('win') and hdf5_path is not None:
        for f in glob(pjoin(hdf5_path, 'lib/*.dll')):
            copy(f, pjoin(sitepackagesdir, 'h5py', basename(f)))
            print("Copied", f)

        zlib_root = os.environ.get("ZLIB_ROOT")
        if zlib_root:
            f = pjoin(zlib_root, 'bin_release', 'zlib.dll')
            copy(f, pjoin(sitepackagesdir, 'h5py', 'zlib.dll'))
            print("Copied", f)

    print("In installed h5py:", os.listdir(pjoin(sitepackagesdir, 'h5py')))

if __name__ == '__main__':
    main()

Copy zlib.dll into Windows h5py installed from source


VERSION = '1.0.0'

def configure(**settings):
    from .settings import Settings
    for key, value in settings.items():
        Settings.setattr(key, value)

VERSION = '1.0.0'

from .models import \
    Omrade, \
    Sted

def configure(**settings):
    from .settings import Settings
    for key, value in settings.items():
        Settings.setattr(key, value)

Add relevant models to turbasen module

Add relevant models to turbasen module


from twisted.internet import protocol, reactor

factory = protocol.ServerFactory()
factory.protocol = protocol.Protocol

reactor.listenTCP(8000, factory)
reactor.run()

from twisted.internet import endpoints, protocol, reactor

class UpperProtocol(protocol.Protocol):
    pass

factory = protocol.ServerFactory()
factory.protocol = UpperProtocol

endpoints.serverFromString(reactor, 'tcp:8000').listen(factory)
reactor.run()

Convert to endpoints API and use custom protocol class


# Copyright 2016 Google Inc. All Rights Reserved.
#
# 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 os

import mock
import pytest

PROJECT = os.environ['GCLOUD_PROJECT']

@pytest.fixture
def api_client_inject_project_id():
    """Patches all googleapiclient requests to replace 'YOUR_PROJECT_ID' with
    the project ID."""
    import googleapiclient.http

    old_execute = googleapiclient.http.HttpRequest.execute

    def new_execute(self, http=None, num_retries=0):
        self.uri = self.uri.replace('YOUR_PROJECT_ID', PROJECT)
        return old_execute(self, http=http, num_retries=num_retries)

    with mock.patch(
            'googleapiclient.http.HttpRequest.execute',
            new=new_execute):
        yield

# Copyright 2016 Google Inc. All Rights Reserved.
#
# 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 os

import mock
import pytest

PROJECT = 'python-docs-samples'

@pytest.fixture
def api_client_inject_project_id():
    """Patches all googleapiclient requests to replace 'YOUR_PROJECT_ID' with
    the project ID."""
    import googleapiclient.http

    old_execute = googleapiclient.http.HttpRequest.execute

    def new_execute(self, http=None, num_retries=0):
        self.uri = self.uri.replace('YOUR_PROJECT_ID', PROJECT)
        return old_execute(self, http=http, num_retries=num_retries)

    with mock.patch(
            'googleapiclient.http.HttpRequest.execute',
            new=new_execute):
        yield

Set the Project in code

Set the Project in code


from urllib.parse import parse_qs
from channels import Group
from channels.sessions import channel_session

@channel_session
def ws_add(message):
    message.reply_channel.send({"accept": True})
    qs = parse_qs(message['query_string'])
    workflow_pk = qs['workflow_pk'][0]
    message.channel_session['workflow_pk'] = workflow_pk
    Group("workflow-{}".format(workflow_pk)).add(message.reply_channel)

@channel_session
def ws_disconnect(message):
    workflow_pk = message.channel_session['workflow_pk']
    Group("workflow-{}".format(workflow_pk)).discard(message.reply_channel)

from urllib.parse import parse_qs
from channels import Group
from channels.sessions import channel_session

@channel_session
def ws_add(message):
    message.reply_channel.send({"accept": True})
    qs = parse_qs(message['query_string'])
    workflow_pk = qs[b'workflow_pk'][0].decode('utf-8')
    message.channel_session['workflow_pk'] = workflow_pk
    Group("workflow-{}".format(workflow_pk)).add(message.reply_channel)

@channel_session
def ws_disconnect(message):
    workflow_pk = message.channel_session['workflow_pk']
    Group("workflow-{}".format(workflow_pk)).discard(message.reply_channel)

Fix query string problem of comparing byte strings and unicode strings in py3


"""Information used for locating nipy test data.

Nipy uses a set of test data that is installed separately.  The test
data should be located in the directory ``~/.nipy/tests/data``.

Install the data in your home directory from the data repository::
  $ mkdir -p .nipy/tests/data
  $ svn co http://neuroimaging.scipy.org/svn/ni/data/trunk/fmri .nipy/tests/data

"""
from os.path import expanduser, exists, join

from neuroimaging.io.datasource import Repository

# data directory should be: $HOME/.nipy/tests/data
datapath = expanduser(join('~', '.nipy', 'tests', 'data'))

if not exists(datapath):
    raise IOError, 'Nipy data directory is not found!'

repository = Repository(datapath)

"""
Nipy uses a set of test data that is installed separately.  The test
data should be located in the directory ``~/.nipy/tests/data``.

Install the data in your home directory from the data repository::
  $ mkdir -p .nipy/tests/data
  $ svn co http://neuroimaging.scipy.org/svn/ni/data/trunk/fmri .nipy/tests/data

"""

# Fernando pointed out that we should wrap the test data into a
# tarball and write a pure python function to grab the data for people
# instead of using svn.  Users may not have svn and the mkdir may not
# work on Windows.

from os.path import expanduser, exists, join

from neuroimaging.io.datasource import Repository

# data directory should be: $HOME/.nipy/tests/data
datapath = expanduser(join('~', '.nipy', 'tests', 'data'))

if not exists(datapath):
    msg = 'Nipy data directory is not found!\n%s' % __doc__
    raise IOError(msg)

repository = Repository(datapath)

Extend error message regarding missing test data.


import json

from buffer.models.profile import PATHS, Profile

class Profiles(list):

  def __init__(self, api, *args, **kwargs):
    super(Profiles, self).__init__(*args, **kwargs)

    self.api = api

  def all(self):
    response = self.api.get(url=PATHS['GET_PROFILES'], parser=json.loads)

    for raw_profile in response:
      self.append(Profile(self.api, raw_profile))

    return self

  def filter(self, **kwargs):

    if not len(self):
      self.all()

    new_list = filter(lambda item: [True for arg in kwargs if item[arg] == kwargs[arg]] != [], self)

    return Profiles(self.api, new_list)

import json

from buffer.models.profile import PATHS, Profile

class Profiles(list):
  '''
    Manage profiles
      + all     -> get all the profiles from buffer
      + filter  -> wrapper for list filtering
  '''

  def __init__(self, api, *args, **kwargs):
    super(Profiles, self).__init__(*args, **kwargs)

    self.api = api

  def all(self):
    '''
      Get all network profiles
    '''

    response = self.api.get(url=PATHS['GET_PROFILES'], parser=json.loads)

    for raw_profile in response:
      self.append(Profile(self.api, raw_profile))

    return self

  def filter(self, **kwargs):
    '''
      Based on some criteria, filter the profiles and return a new Profiles
      Manager containing only the chosen items

      If the manager doen't have any items, get all the profiles from Buffer
    '''

    if not len(self):
      self.all()

    new_list = filter(lambda item: [True for arg in kwargs if item[arg] == kwargs[arg]] != [], self)

    return Profiles(self.api, new_list)

Write documentation for profile mananger


from os import path
import shutil
import tempfile
from sh import hovercraft
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from waliki.models import Page

def slides(request, slug):
    page = get_object_or_404(Page, slug=slug)
    outpath = tempfile.mkdtemp()
    try:
        infile = page.abspath
        template = path.join(path.dirname(path.realpath(__file__)), 'template')
        hovercraft('-t', template, infile, outpath)
        with open(path.join(outpath, 'index.html')) as f:
            content = f.read()
    finally:
        shutil.rmtree(outpath)
    return HttpResponse(content)

from os import path
import shutil
import tempfile
from sh import hovercraft
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from waliki.models import Page
from waliki.acl import permission_required

@permission_required('view_page')
def slides(request, slug):
    page = get_object_or_404(Page, slug=slug)
    outpath = tempfile.mkdtemp()
    try:
        infile = page.abspath
        template = path.join(path.dirname(path.realpath(__file__)), 'template')
        hovercraft('-t', template, infile, outpath)
        with open(path.join(outpath, 'index.html')) as f:
            content = f.read()
    finally:
        shutil.rmtree(outpath)
    return HttpResponse(content)

Add permission check to slide urls.

Add permission check to slide urls.