from cloudbot import hook

@hook.command("bbmstaff")
def bbmStaff(text, message, chan):
    if chan in ("#bbm-bots", "#bbm-dev", "#bbm-packs", "#builtbrokenmodding", "#builtbroken"):
        message("Owners: Dmodoomsirius, DarkGuardsman");
        message("textureArtist: Morton0000");
        message("Developers: Snow, Hennamann")
    else:
        message("Command can only be run in Official BBM Channels. Join #bbm-bots to run the command.")

@hook.command("sponsor")
def sponsor(text, message, chan):
    if chan in ("#BBM-bots", "#BBM-Dev", "#BBM-Packs", "#BuiltBrokenModding", "#BuiltBroken"):
                    message("BuiltBroken servers both Beta test servers and build server");
                    message("is sponsored by Akliz.");
                    message("http://www.akliz.net/bbm")
    message("Command can only be run in Official BBM Channels. Join #bbm-bots to run the command.")

from cloudbot import hook

bbmChannels = ["#bbm-bots","#bbm-dev","#builtbroken","#builtbrokenmodding","#bbm-packs","#icbm","#artillects "]

@hook.command("bbmstaff")
def bbmStaff(text, message, chan):
    if any(x in chan for x in bbmChannels):
            message("Owners: Dmodoomsirius, DarkGuardsman");
            #message("Texture Artist: Morton0000");
            message("Senior Developers: Kolatra")
            message("Junior Developers: Kolatra, shobu9, TheCowGod, Hennamann")
    else:
            message("Command can only be run in Official BBM Channels. Join #bbm-bots to run the command.")

@hook.command("sponsor")
def sponsor(text, message, chan):
    if any(x in chan for x in bbmChannels):
            message("BuildBrokenModding servers both Beta test servers and build server");
            message("is sponsored by Akliz.");
            message("http://www.akliz.net/bbm")
    else:
            message("Command can only be run in Official BBM Channels. Join #bbm-bots to run the command.")

@hook.command("bbmchan")
def chans(text, message):
    message("The official BuiltBroken Channels are: " + " , ".join(bbmChannels))

@hook.command("bbmhelp")
def bbmhelp(text, message):
    message("If you are looking for who is the staff type .bbmstaff")
    message ("if you are looking for our sponsors please type .sponsor")
    message("If you are looking for our official channels please do .bbmchan")

Update and add more commands.

Update and add more commands.


# -*- coding: utf8 -*-
from django.contrib.auth.models import AbstractUser
from django.utils.functional import cached_property
from social.apps.django_app.default.models import UserSocialAuth

from frigg.helpers import github

class User(AbstractUser):

    @cached_property
    def github_token(self):
        try:
            return self.social_auth.get(provider='github').extra_data['access_token']
        except UserSocialAuth.DoesNotExist:
            return

    def save(self, *args, **kwargs):
        create = hasattr(self, 'id')
        super(User, self).save(*args, **kwargs)
        if create:
            self.update_repo_permissions()

    def update_repo_permissions(self):
        if self.github_token:
            github.update_repo_permissions(self)

# -*- coding: utf8 -*-
from django.contrib.auth.models import AbstractUser
from django.utils.functional import cached_property
from social.apps.django_app.default.models import UserSocialAuth

from frigg.helpers import github

class User(AbstractUser):

    @cached_property
    def github_token(self):
        try:
            return self.social_auth.get(provider='github').extra_data['access_token']
        except UserSocialAuth.DoesNotExist:
            return

    def save(self, *args, **kwargs):
        create = not hasattr(self, 'id')
        super(User, self).save(*args, **kwargs)
        if create:
            self.update_repo_permissions()

    def update_repo_permissions(self):
        if self.github_token:
            github.update_repo_permissions(self)

Fix update permission on user

Fix update permission on user

Only run it when user is created, not when it they are saved


'''
Update EVLA pipeline variables to the current system.
'''

def update_paths(pipe_dict, ms_path, pipepath):

    pipe_dict['ms_active'] = ms_path
    pipe_dict['SDM_name'] = ms_path+".ms"
    pipe_dict['pipepath'] = pipepath

    return pipe_dict

if __name__ == '__main__':

    import sys

    pipe_var_file = str(sys.argv[1])

    ms_path = str(sys.argv[2])

    pipepath = str(sys.argv[3])

    import shelve

    pipe_dict = shelve.open(pipe_var_file, writeback=True)

    pipe_dict = update_paths(pipe_dict, ms_path, pipepath)

    pipe_dict.flush()

    pipe_dict.close()

'''
Update EVLA pipeline variables to the current system.
'''

def update_paths(pipe_dict, ms_path, pipepath):

    pipe_dict['ms_active'] = ms_path
    pipe_dict['SDM_name'] = ms_path+".ms"
    pipe_dict['pipepath'] = pipepath

    return pipe_dict

if __name__ == '__main__':

    import sys

    pipe_var_file = str(sys.argv[5])

    ms_path = str(sys.argv[6])

    pipepath = str(sys.argv[7])

    import shelve

    pipe_dict = shelve.open(pipe_var_file, writeback=True)

    pipe_dict = update_paths(pipe_dict, ms_path, pipepath)

    pipe_dict.flush()

    pipe_dict.close()

Change cmd line args to reflect change to CASA


import sys

import django
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import DebugPanel

class VersionDebugPanel(DebugPanel):
    """
    Panel that displays the Django version.
    """
    name = 'Version'
    template = 'debug_toolbar/panels/versions.html'
    has_content = True

    def nav_title(self):
        return _('Versions')

    def nav_subtitle(self):
        return 'Django %s' % django.get_version()

    def url(self):
        return ''

    def title(self):
        return _('Versions')

    def process_response(self, request, response):
        versions = {}
        versions['Python'] = '%d.%d.%d' % sys.version_info[:3]
        for app in settings.INSTALLED_APPS + ['django']:
            name = app.split('.')[-1].replace('_', ' ').capitalize()
            __import__(app)
            app = sys.modules[app]
            if hasattr(app, 'get_version'):
                get_version = app.get_version
                if callable(get_version):
                    version = get_version()
                else:
                    version = get_version
            elif hasattr(app, 'VERSION'):
                version = app.VERSION
            elif hasattr(app, '__version__'):
                version = app.__version__
            else:
                continue
            if isinstance(version, (list, tuple)):
                version = '.'.join(str(o) for o in version)
            versions[name] = version

        self.record_stats({
            'versions': versions,
            'paths': sys.path,
        })

import sys

import django
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.panels import DebugPanel

class VersionDebugPanel(DebugPanel):
    """
    Panel that displays the Django version.
    """
    name = 'Version'
    template = 'debug_toolbar/panels/versions.html'
    has_content = True

    def nav_title(self):
        return _('Versions')

    def nav_subtitle(self):
        return 'Django %s' % django.get_version()

    def url(self):
        return ''

    def title(self):
        return _('Versions')

    def process_response(self, request, response):
        versions = {}
        versions['Python'] = '%d.%d.%d' % sys.version_info[:3]
        for app in list(settings.INSTALLED_APPS) + ['django']:
            name = app.split('.')[-1].replace('_', ' ').capitalize()
            __import__(app)
            app = sys.modules[app]
            if hasattr(app, 'get_version'):
                get_version = app.get_version
                if callable(get_version):
                    version = get_version()
                else:
                    version = get_version
            elif hasattr(app, 'VERSION'):
                version = app.VERSION
            elif hasattr(app, '__version__'):
                version = app.__version__
            else:
                continue
            if isinstance(version, (list, tuple)):
                version = '.'.join(str(o) for o in version)
            versions[name] = version

        self.record_stats({
            'versions': versions,
            'paths': sys.path,
        })

Convert settings.INSTALLED_APPS to list before concatenating django.

According to the Django documentation settings.INSTALLED_APPS is a
tuple. To go for sure that only list + list are concatenated,
settings.INSTALLED_APPS is converted to list type before adding
['django'].


# see https://gist.github.com/malthe/704910
import imp
import sys

def create_modules(module_path):
    path = ""
    module = None
    for element in module_path.split('.'):
        path += element

        try:
            module = __import__(path)
        except ImportError:
            new = imp.new_module(path)
            if module is not None:
                setattr(module, element, new)
            module = new

        sys.modules[path] = module
        __import__(path)

        path += "."

    return module

def stub(module_path, class_name, base_class, meta_class=type):
    module = create_modules(module_path)
    cls = meta_class(class_name, (base_class, ), {})
    setattr(module, class_name, cls)

def skip_pre315(portal):
    # Hack prevent out-of-date upgrading
    # Related: PR #1484
    # https://github.com/bikalabs/Bika-LIMS/pull/1484
    qi = portal.portal_quickinstaller
    info = qi.upgradeInfo('bika.lims')
    if info['installedVersion'] > '315':
        return True
    return False

Add return False to be sure all works as expected


from buildbot.process import buildstep
from buildbot.process.buildstep import SUCCESS, FAILURE, EXCEPTION
from buildbot.process.properties import Properties
from twisted.internet import defer

from ..travisyml import TravisYml

class ConfigurableStep(buildstep.LoggingBuildStep):

    """
    Base class for a step which can be tuned by changing settings in .travis.yml
    """

    @defer.inlineCallbacks
    def getStepConfig(self):
        log = self.addLog(".travis.yml")
        cmd = self.cmd = buildstep.RemoteShellCommand(workdir="build", command=["cat", ".travis.yml"])
        cmd.useLog(log, False, "stdio")
        yield self.runCommand(cmd)
        self.cmd = None
        if cmd.rc != 0:
            raise buildstep.BuildStepFailed()

        config = TravisYml()
        config.parse(log.getText())

        defer.returnValue(config)

from buildbot.process import buildstep
from buildbot.process.buildstep import SUCCESS, FAILURE, EXCEPTION
from buildbot.process.properties import Properties
from twisted.internet import defer

from ..travisyml import TravisYml

class ConfigurableStep(buildstep.LoggingBuildStep):

    """
    Base class for a step which can be tuned by changing settings in .travis.yml
    """

    @defer.inlineCallbacks
    def getStepConfig(self):
        config = TravisYml()

        struct = self.build.getProperty(".travis.yml", None)
        if struct:
            config.parse(struct)
            defer.returnValue(config)

        log = self.addLog(".travis.yml")
        cmd = self.cmd = buildstep.RemoteShellCommand(workdir="build", command=["cat", ".travis.yml"])
        cmd.useLog(log, False, "stdio")
        yield self.runCommand(cmd)
        self.cmd = None
        if cmd.rc != 0:
            raise buildstep.BuildStepFailed()

        config = TravisYml()
        config.parse(log.getText())

        self.build.setProperty(".travis.yml", config.config, ".VCS")

        defer.returnValue(config)

Save .travis.yml into build properties

Save .travis.yml into build properties


from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'promgen.settings')

app = Celery('promgen')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

# Because of the way that celery is de-coupled, it can be quite difficult to
# get an instance of 'self' when converting a class method into a celery task.
# as a way to get around this, we can mimick a @task(bind=True) call so that we
# get a self instance to the Celery task, and then we can set a __klass__
# attribute that we can use to get to our other class functions
def wrap_send(cls):
    if hasattr(cls, '_send'):
        print('Wrapping ', cls)
        cls._send = app.task(cls._send, bind=True, lazy=False)
        cls._send.__klass__ = cls()
    return cls

from __future__ import absolute_import, unicode_literals

import logging
import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'promgen.settings')
logger = logging.getLogger(__name__)

app = Celery('promgen')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

# Because of the way that celery is de-coupled, it can be quite difficult to
# get an instance of 'self' when converting a class method into a celery task.
# as a way to get around this, we can mimick a @task(bind=True) call so that we
# get a self instance to the Celery task, and then we can set a __klass__
# attribute that we can use to get to our other class functions
def wrap_send(cls):
    if hasattr(cls, '_send'):
        logger.debug('Wrapping %s', cls)
        cls._send = app.task(cls._send, bind=True, lazy=False)
        cls._send.__klass__ = cls()
    return cls

Swap print for logging statement

Swap print for logging statement


# coding: utf-8
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Tests for Packet.
"""
import os
import json
import pytest

from laniakea.core.providers.packet import PacketManager

@pytest.fixture
def packet():
    with open(os.path.join(os.getcwd(), 'laniakea/examples/packet.json')) as fo:
        conf = json.loads(fo.read())
    return PacketManager(conf)

def test_list_projects(packet):
    for plan in packet.list_projects():
        assert hasattr(plan, 'name')
        assert hasattr(plan, 'id')

# coding: utf-8
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Tests for Packet.
"""
import os
import json
import pytest

from laniakea.core.providers.packet import PacketManager

#@pytest.fixture
#def packet():
#    with open(os.path.join(os.getcwd(), 'laniakea/examples/packet.json')) as fo:
#        conf = json.loads(fo.read())
#    return PacketManager(conf)

#def test_list_projects(packet):
#    for plan in packet.list_projects():
#        assert hasattr(plan, 'name')
#        assert hasattr(plan, 'id')

Disable dummy Packet test temporarily

Disable dummy Packet test temporarily


import mr_streams as ms
import unittest

# :::: auxilary functions ::::
def sum_reduction(x,y):
    return x + y

class TestMisc(unittest.TestCase):

    def test_sum_reduce(self):
        _ = ms.stream([1,2,3,4,5]).reduce(sum_reduction)
        assert _ is 15

    def test_reduce_with_one_element(self):
        _ = ms.stream([1]).reduce(sum_reduction)
        assert _ is 1

    def test_empty_reduce(self):
        try:
            _ = ms.stream([]).reduce(sum_reduction)
        except ms.IllegalStreamOperationException:
            pass

import mr_streams as ms
import unittest

# :::: auxilary functions ::::
def sum_reduction(x,y):
    return x + y

class TestMisc(unittest.TestCase):

    def test_sum_reduce(self):
        _ = ms.stream([1,2,3,4,5]).reduce(sum_reduction)
        assert _ is 15

    def test_initializer(self):
        _ = ms.stream([1]).reduce(sum_reduction, initializer= 1)
        assert _ is 2

    def test_reduce_with_one_element(self):
        _ = ms.stream([1]).reduce(sum_reduction)
        assert _ is 1

    def test_empty_reduce(self):
        try:
            _ = ms.stream([]).reduce(sum_reduction)
        except ms.IllegalStreamOperationException:
            pass

Refactor reduce to handle edge-case streams of length 0 and 1.


from romanesco.format import dict_to_vtkarrays, dict_to_vtkrow
import vtk

vtk_builder = vtk.vtkMutableDirectedGraph()
node_fields = input["node_fields"]
edge_fields = input["edge_fields"]
dict_to_vtkarrays(input["node_data"], node_fields, vtk_builder.GetVertexData())
if "children" in input and len(input["children"]) > 0:
    dict_to_vtkarrays(input["children"][0]["edge_data"], edge_fields,
                      vtk_builder.GetEdgeData())

def process_node(vtknode, node):
    if "children" in node:
        for n in node["children"]:
            vtkchild = vtk_builder.AddVertex()
            vtkparentedge = vtk_builder.AddGraphEdge(vtknode, vtkchild).GetId()
            dict_to_vtkrow(n["node_data"], vtk_builder.GetVertexData())
            if "edge_data" in n:
                dict_to_vtkrow(n["edge_data"], vtk_builder.GetEdgeData())
            process_node(vtkchild, n)
vtk_builder.AddVertex()
dict_to_vtkrow(input["node_data"], vtk_builder.GetVertexData())
process_node(0, input)
output = vtk.vtkTree()
output.ShallowCopy(vtk_builder)

from romanesco.format import dict_to_vtkarrays, dict_to_vtkrow
import vtk

vtk_builder = vtk.vtkMutableDirectedGraph()
node_fields = input["node_fields"]
edge_fields = input["edge_fields"]
dict_to_vtkarrays(input["node_data"], node_fields, vtk_builder.GetVertexData())
if "children" in input and len(input["children"]) > 0:
    dict_to_vtkarrays(input["children"][0]["edge_data"], edge_fields,
                      vtk_builder.GetEdgeData())

def process_node(vtknode, node):
    if "children" in node:
        for n in node["children"]:
            vtkchild = vtk_builder.AddVertex()
            vtkparentedge = vtk_builder.AddGraphEdge(vtknode, vtkchild).GetId()
            dict_to_vtkrow(n["node_data"], vtk_builder.GetVertexData())
            dict_to_vtkrow(n["edge_data"], vtk_builder.GetEdgeData())
            process_node(vtkchild, n)
vtk_builder.AddVertex()
dict_to_vtkrow(input["node_data"], vtk_builder.GetVertexData())
process_node(0, input)
output = vtk.vtkTree()
output.ShallowCopy(vtk_builder)

Revert "tolerate missing edge data"

Revert "tolerate missing edge data"

This reverts commit 93f1f6b24b7e8e61dbbfebe500048db752bc9fed.


from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from getpaid.backends import PaymentProcessorBase

class PaymentProcessor(PaymentProcessorBase):
    BACKEND = 'getpaid.backends.paymill'
    BACKEND_NAME = _('Paymill')
    BACKEND_ACCEPTED_CURRENCY = ('EUR', 'CZK', 'DKK', 'HUF', 'ISK', 'ILS', 'LVL',
        'CHF', 'NOK', 'PLN', 'SEK', 'TRY', 'GBP', )

    def get_gateway_url(self, request):
        return reverse('getpaid-paymill-authorization', kwargs={'pk' : self.payment.pk}), "GET", {}

from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from getpaid.backends import PaymentProcessorBase

class PaymentProcessor(PaymentProcessorBase):
    BACKEND = 'getpaid.backends.paymill'
    BACKEND_NAME = _('Paymill')
    BACKEND_ACCEPTED_CURRENCY = ('EUR', 'CZK', 'DKK', 'HUF', 'ISK', 'ILS', 'LVL',
        'CHF', 'NOK', 'PLN', 'SEK', 'TRY', 'GBP', 'USD', )

    def get_gateway_url(self, request):
        return reverse('getpaid-paymill-authorization', kwargs={'pk' : self.payment.pk}), "GET", {}

Add USD to supported currencies in Paymill backend

USD was not listed as a supported currency in the init file. This was causing 403 Forbidden errors which were hard to debug, because the Paymill backend simply didn't show up in the payment form and the only error was about unsupported backend.


VERSION = (0, 1)

# Dynamically calculate the version based on VERSION tuple
if len(VERSION)>2 and VERSION[2] is not None:
    str_version = "%d.%d_%s" % VERSION[:3]
else:
    str_version = "%d.%d" % VERSION[:2]

__version__ = str_version

Add versioning information to eve_api

Add versioning information to eve_api


"""
The FVCOM Python toolbox (PyFvcom)

"""

__version__ = '1.2'
__author__ = 'Pierre Cazenave'
__credits__ = ['Pierre Cazenave']
__license__ = 'MIT'
__maintainer__ = 'Pierre Cazenave'
__email__ = 'pica@pml.ac.uk'

import inspect
from warnings import warn
# Import numpy so we have it across the board.
import numpy as np

# Import everything!
from PyFVCOM import buoy_tools
from PyFVCOM import cst_tools
from PyFVCOM import ctd_tools
from PyFVCOM import grid_tools
from PyFVCOM import img2xyz
from PyFVCOM import ll2utm
from PyFVCOM import ocean_tools
from PyFVCOM import stats_tools
from PyFVCOM import tide_tools
from PyFVCOM import tidal_ellipse
from PyFVCOM import process_results
from PyFVCOM import read_results

# External TAPPY now instead of my bundled version. Requires my forked version
# of TAPPY from https://github.com/pwcazenave/tappy or
# http://gitlab.em.pml.ac.uk/pica/tappy.
from tappy import tappy

# For backwards-compatibility.
process_FVCOM_results = process_results
read_FVCOM_results = read_results

"""
The FVCOM Python toolbox (PyFvcom)

"""

__version__ = '1.2'
__author__ = 'Pierre Cazenave'
__credits__ = ['Pierre Cazenave']
__license__ = 'MIT'
__maintainer__ = 'Pierre Cazenave'
__email__ = 'pica@pml.ac.uk'

import inspect
from warnings import warn
# Import numpy so we have it across the board.
import numpy as np

# Import everything!
from PyFVCOM import buoy_tools
from PyFVCOM import cst_tools
from PyFVCOM import ctd_tools
from PyFVCOM import grid_tools
from PyFVCOM import ll2utm
from PyFVCOM import ocean_tools
from PyFVCOM import stats_tools
from PyFVCOM import tide_tools
from PyFVCOM import tidal_ellipse
from PyFVCOM import process_results
from PyFVCOM import read_results

# External TAPPY now instead of my bundled version. Requires my forked version
# of TAPPY from https://github.com/pwcazenave/tappy or
# http://gitlab.em.pml.ac.uk/pica/tappy.
from tappy import tappy

# For backwards-compatibility.
process_FVCOM_results = process_results
read_FVCOM_results = read_results

Remove the (dodgy) function to convert from an image to data.


# -*- coding: utf-8 -*-

import warnings

from .settings import *  # noqa: F403,F401

# Handle system warning as log messages
warnings.simplefilter("once")

for handler in LOGGING.get("handlers", []):
    LOGGING["handlers"][handler]["level"] = "CRITICAL"
for logger in LOGGING.get("loggers", []):
    LOGGING["loggers"][logger]["level"] = "CRITICAL"

mysql_db = DATABASES["default"]
DEFAULT_DB = {
    "ENGINE": "django.db.backends.sqlite3",
    "NAME": ":memory:",
    "TEST": {"MIGRATE": False},
}
if os.environ.get("DB_TYPE") == "mysql":
    print("Using MySQL Backend!")
    DEFAULT_DB = mysql_db

DATABASES = {
    "default": DEFAULT_DB,
}

Unify and simplify for tests.

Unify and simplify for tests.


from sqlalchemy.ext.declarative import declared_attr
from blog.lib.database import db

class ModelMixin(object):
    """A base mixin for all models."""

    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()

    def __str__(self):
        return '<{} (id={})>'.format(self.__class__.__name__, self.id_)

    def __repr__(self):
        return str(self)

    id_ = db.Column('id', db.Integer, primary_key=True)

    def get_dictionary(self):
        d = {}
        for column in self.__table__.columns:
            key = 'id_' if column.key == 'id' else column.key
            d[key] = getattr(self, key)
        return d

    def update(self, d):
        for column in self.__table__.columns:
            if column.key == 'id_':
                continue
            setattr(
                self, column.key, d.get(
                    column.key, getattr(self, column.key)
                )
            )

from sqlalchemy.ext.declarative import declared_attr
from blog.lib.database import db

class ModelMixin(object):
    """A base mixin for all models."""

    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()

    def __str__(self):
        return '<{} (id={})>'.format(self.__class__.__name__, self.id_)

    def __repr__(self):
        return str(self)

    id_ = db.Column('id', db.Integer, primary_key=True)

    def get_dictionary(self):
        d = {}
        for column in self.__table__.columns:
            if column.key == 'id':
                d['id'] = getattr(self, 'id_')
            else:
                d[column.key] = getattr(self, column.key)
        return d

    def update(self, d):
        for column in self.__table__.columns:
            if column.key == 'id_':
                continue
            setattr(
                self, column.key, d.get(
                    column.key, getattr(self, column.key)
                )
            )

Return "id" key to front instead of "id_".


import tornado.ioloop
import tornado.web
import requests

host = 'localhost'
waybackPort = '8080'
archiveConfigFile = '/Applications/WAIL.app/config/archive.json'

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        iwa = isWaybackAccessible()
        print iwa
        self.write(iwa)

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

def isWaybackAccessible():
    try:
      r = requests.get('http://' + host + ':' + waybackPort)
      with open(archiveConfigFile, 'r') as myfile:
        data=myfile.read()
      return data
    except requests.exceptions.ConnectionError as e:
      return ''

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

import tornado.ioloop
import tornado.web
import requests

host = 'localhost'
waybackPort = '8080'
# Use a separate JSON file that only queries the local WAIL instance for MemGator
archiveConfigFile = '/Applications/WAIL.app/config/archive.json'

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        iwa = isWaybackAccessible()
        print iwa
        self.write(iwa)

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

def isWaybackAccessible():
    try:
      r = requests.get('http://' + host + ':' + waybackPort)
      with open(archiveConfigFile, 'r') as myfile:
        data=myfile.read()
      return data
    except requests.exceptions.ConnectionError as e:
      return ''

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Add comment to justify separate JSON file existence


def print_header():
    import sys
    from dxtbx.format.Registry import Registry

    # this will do the lookup for every frame - this is strictly not needed
    # if all frames are from the same instrument

    for arg in sys.argv[1:]:
        format = Registry.find(arg)
        i = format(arg)
        print 'Beam:'
        print i.get_beam()
        print 'Goniometer:'
        print i.get_goniometer()
        print 'Detector:'
        print i.get_detector()
        print 'Scan:'
        print i.get_scan()
        print 'Total Counts:'
        print sum(i.get_raw_data())

if __name__ == '__main__':
    print_header()

def print_header():
    import sys
    from dxtbx.format.Registry import Registry

    # this will do the lookup for every frame - this is strictly not needed
    # if all frames are from the same instrument

    for arg in sys.argv[1:]:
        format = Registry.find(arg)
        print 'Using header reader: %s' % format.__name__
        i = format(arg)
        print 'Beam:'
        print i.get_beam()
        print 'Goniometer:'
        print i.get_goniometer()
        print 'Detector:'
        print i.get_detector()
        print 'Scan:'
        print i.get_scan()
        print 'Total Counts:'
        print sum(i.get_raw_data())

if __name__ == '__main__':
    print_header()

Print the Format class used

Print the Format class used


import sys
try:
    basestring
except:
    basestring = str  # Python3

PY2 = sys.version_info[0] == 2
if not PY2:
    text_type = str
    binary_type = bytes
    string_types = (str,)
    integer_types = (int,)
else:
    text_type = unicode
    binary_type = str
    string_types = basestring
    integer_types = (int, long)

try:
    # avoid RemovedInDjango19Warning by using lru_cache where available
    from django.utils.lru_cache import lru_cache as memoize
except:
    from django.utils.functional import memoize

import sys
try:
    basestring
except:
    basestring = str  # Python3

PY2 = sys.version_info[0] == 2
if not PY2:
    text_type = str
    binary_type = bytes
    string_types = (str,)
    integer_types = (int,)
else:
    text_type = unicode
    binary_type = str
    string_types = basestring
    integer_types = (int, long)

try:
    # avoid RemovedInDjango19Warning by using lru_cache where available
    from django.utils.lru_cache import lru_cache
    def memoize(function, *args):
        return lru_cache()(function)
except:
    from django.utils.functional import memoize

Fix lru_cache import as memoize

Fix lru_cache import as memoize

Thanks to @jcomeauictx for the heads up


from lxml.html import fromstring as lxml_from_string
from unidecode import unidecode

UNSUBSCRIBE_MARKERS = [

  # English
  "unsub", "blacklist", "opt-out", "opt out",

  # French
  "desinscription", "desinscrire", "desabonner", "desabonnement",
  "ne souhaitez plus", "ne plus recevoir", "cesser de recevoir"

]

def FindUnsubscribeLink(message):

  unsubscribe_link = None
  unsubscribe_links = []

  for part in message.walk():
    if part.get_content_type() == 'text/html':
      html = part.get_payload(decode=True)

      doc = lxml_from_string(html)

      for element, attribute, link, pos in doc.iterlinks():
        link_content = unidecode(element.text_content()).lower()
        link = link.lower()

        unsubscribe_links.append((repr(link_content)[0:100], link[0:100]))
        for pattern in UNSUBSCRIBE_MARKERS:
          if (pattern in link_content) or (pattern in link):
            unsubscribe_link = link

  return unsubscribe_link, unsubscribe_links

from lxml.html import fromstring as lxml_from_string
from unidecode import unidecode

UNSUBSCRIBE_MARKERS = [

  # English
  "unsub", "blacklist", "opt-out", "opt out", "removealert", "removeme",

  # French
  "desinscription", "desinscrire", "desabonner", "desabonnement",
  "ne souhaitez plus", "ne plus recevoir", "cesser de recevoir"

]

def FindUnsubscribeLink(message):

  unsubscribe_link = None
  unsubscribe_links = []

  for part in message.walk():
    if part.get_content_type() == 'text/html':
      html = part.get_payload(decode=True)

      doc = lxml_from_string(html)

      for element, attribute, link, pos in doc.iterlinks():
        link_content = unidecode(element.text_content()).lower()

        unsubscribe_links.append((repr(link_content)[0:100], link[0:100]))
        for pattern in UNSUBSCRIBE_MARKERS:
          if (pattern in link_content) or (pattern in link.lower()):
            unsubscribe_link = link

  return unsubscribe_link, unsubscribe_links

Fix a bug with uppercase links

Fix a bug with uppercase links


import os
import pickle

class Cache(dict):
    def __init__(self, django_folder, database, cache_path):
        self.filename = os.path.join(
            cache_path,
            "{}_{}.pickle".format(django_folder.replace(os.sep, "_"), database),
        )

        if not os.path.exists(os.path.dirname(self.filename)):
            os.makedirs(os.path.dirname(self.filename))

        super().__init__()

    def load(self):
        try:
            with open(self.filename, "rb") as f:
                tmp_dict = pickle.load(f)
                self.update(tmp_dict)
        except OSError:
            pass

    def save(self):
        with open(self.filename, "wb") as f:
            pickle.dump(self, f, protocol=2)

import os
import pickle

class Cache(dict):
    def __init__(self, django_folder, database, cache_path):
        self.filename = os.path.join(
            cache_path,
            "{}_{}.pickle".format(str(django_folder).replace(os.sep, "_"), database),
        )

        if not os.path.exists(os.path.dirname(self.filename)):
            os.makedirs(os.path.dirname(self.filename))

        super().__init__()

    def load(self):
        try:
            with open(self.filename, "rb") as f:
                tmp_dict = pickle.load(f)
                self.update(tmp_dict)
        except OSError:
            pass

    def save(self):
        with open(self.filename, "wb") as f:
            pickle.dump(self, f, protocol=2)

feat: Support `Path` type for `project_root_path`

Django has switched to using pathlib.Path in startproject. This adds support for these in the project root path config option.


#!/usr/bin/env python

from __future__ import print_function

import os

from flask.ext.script import Manager, Server
from flask.ext.migrate import Migrate, MigrateCommand

from app import create_app, db

application = create_app(os.getenv('DM_ENVIRONMENT') or 'development')
manager = Manager(application)
manager.add_command("runserver", Server(port=5000))
migrate = Migrate(application, db)
manager.add_command('db', MigrateCommand)

@manager.command
def list_routes():
    """List URLs of all application routes."""
    for rule in sorted(application.url_map.iter_rules(), key=lambda r: r.rule):
        print("{:10} {}".format(", ".join(rule.methods - set(['OPTIONS', 'HEAD'])), rule.rule))

if __name__ == '__main__':
    manager.run()

#!/usr/bin/env python

from __future__ import print_function

import os

from dmutils import init_manager
from flask.ext.migrate import Migrate, MigrateCommand

from app import create_app, db

application = create_app(os.getenv('DM_ENVIRONMENT') or 'development')
manager = init_manager(application, 5000, ['./json_schemas'])

migrate = Migrate(application, db)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

Use new dmutils init_manager to set up reload on schema changes


from django.shortcuts import render
from django.http import HttpResponse

def get_work(request):
    return HttpResponse('Not implemented')

def work_completed(request):
    return HttpResponse('Not implemented')

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

def get_work(request):
    return HttpResponse('Not implemented')

@csrf_exempt
def work_completed(request):
    return HttpResponse('Not implemented')

Allow POST request to work_completed view


from ..helpers import get_allowed

import subprocess
import web

class ShutdownMachineR:
    """
    This endpoint is for shutting down a running machine.
    """
    allow_origin, rest_url = get_allowed.get_allowed()
    def GET(self, machine):
        web.header('Access-Control-Allow-Origin', self.allow_origin)
        try:
            out = subprocess.check_output("/usr/local/bin/docker-machine stop "+machine, shell=True)
        except:
            out = "unable to stop machine"
        return str(out)

from ..helpers import get_allowed

import subprocess
import web

class ShutdownMachineR:
    """
    This endpoint is for shutting down a running machine.
    """
    allow_origin, rest_url = get_allowed.get_allowed()

    def GET(self, machine):
        try:
            web.header('Access-Control-Allow-Origin', self.allow_origin)
        except Exception as e: # no pragma
            print(e.message)
        try:
            out = subprocess.check_output("/usr/local/bin/docker-machine stop "+machine, shell=True)
        except:
            out = "unable to stop machine"
        return str(out)

Put the web.header function in a try/except block


from peewee import *

db = SqliteDatabase('emails.db')

class Transaction(Model):
    cost = FloatField()
    address = CharField()
    tempPass = CharField()
    domain = CharField(index=True)
    email = CharField(primary_key=True, unique=True)
    is_complete = BooleanField(default=False, index=True)

    class Meta:
        database = db

from datetime import datetime

from peewee import *

from dinosaurs import settings
from dinosaurs.transaction.coin import generate_address

db = SqliteDatabase(settings.database)

class Transaction(Model):
    cost = FloatField()
    address = CharField()
    started = DateField()
    tempPass = CharField()
    domain = CharField(index=True)
    email = CharField(primary_key=True, unique=True)
    is_complete = BooleanField(default=False, index=True)

    class Meta:
        database = db

    def __init__(self, *args, **kwargs):
        kwargs['started'] = datetime.now()
        kwargs['address'] = generate_address()
        super(Transaction, self).__init__(*args, **kwargs)

    @property
    def expired(self):
        return (datetime.now() - self.started).minutes > 4

    @property
    def seconds_left(self):
        return (datetime.now() - self.started).total_seconds

Update what a transaction is

Update what a transaction is


'''
prime_test(n) returns a True if n is a prime number else it returns False
'''

def prime_test(n):
    if n <= 1:
        return False
    if n==2 or n==3:
        return True
    if n%2==0 or n%3==0:
        return False
    j = 5
    while(j*j <= n):
        if n%(j)==0 or n%(j+2)==0:
            return False
        j += 6
    return True

def prime_test(n):
    # prime numbers are greater than 1
    if num > 1:
    # check for factors
        for i in range(2,num):
           if (num % i) == 0:
               print(num,"is not a prime number")
               print(i,"times",num//i,"is",num)
               break
        else:
           print(num,"is a prime number")

    # if input number is less than
    # or equal to 1, it is not prime
    else:
    print(num,"is not a prime number")

'''
prime_test(n) returns a True if n is a prime number else it returns False
'''

def prime_test(n):
    if n <= 1:
        return False
    if n==2 or n==3:
        return True
    if n%2==0 or n%3==0:
        return False
    j = 5
    while(j*j <= n):
        if n%(j)==0 or n%(j+2)==0:
            return False
        j += 6
    return True

def prime_test(n):
    # prime numbers are greater than 1
    if num > 1:
    # check for factors
        for i in range(2,num):
           if (num % i) == 0:
               #print(num,"is not a prime number")
               #print(i,"times",num//i,"is",num)
                return False
               break
        else:
           #print(num,"is a prime number")
            return True

    # if input number is less than
    # or equal to 1, it is not prime
    else:
    #print(num,"is not a prime number")
    return False

Change the return type to boolean

Change the return type to boolean


# -*- coding: utf-8 -*-
"""
utils.py
~~~~~~~~

Defines utility functions used by UPnPy.
"""

def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.is_lower():
            outstr.append(char)
        elif outstr[-1].is_lower():
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)

# -*- coding: utf-8 -*-
"""
utils.py
~~~~~~~~

Defines utility functions used by UPnPy.
"""

def camelcase_to_underscore(text):
    """
    Convert a camelCasedString to one separated_by_underscores. Treats
    neighbouring capitals as acronyms and doesn't separated them, e.g. URL does
    not become u_r_l. That would be stupid.

    :param text: The string to convert.
    """
    outstr = []

    for char in text:
        if char.islower():
            outstr.append(char)
        elif (len(outstr) > 0) and (outstr[-1].islower()):
            outstr.append('_')
            outstr.append(char.lower())
        else:
            outstr.append(char.lower())

    return ''.join(outstr)

Correct an AttributeError and a potential IndexErr


from bot.api.telegram import TelegramBotApi
from bot.storage import State

class Api:
    def __init__(self, telegram_api: TelegramBotApi, state: State):
        self.telegram_api = telegram_api
        self.state = state

    def get_me(self):
        return self.telegram_api.get_me()

    def send_message(self, message):
        return self.telegram_api.send_message(chat_id=message.chat.id, text=message.text,
                                              reply_to_message_id=message.reply_to_message_id)

    def get_pending_updates(self):
        yield from self.get_updates(timeout=0)

    def get_updates(self, timeout=45):
        updates = self.telegram_api.get_updates(offset=self.__get_updates_offset(), timeout=timeout)
        for update in updates:
            self.__set_updates_offset(update.update_id)
            yield update

    def __get_updates_offset(self):
        return self.state.next_update_id

    def __set_updates_offset(self, last_update_id):
        self.state.next_update_id = str(last_update_id + 1)

from bot.api.telegram import TelegramBotApi
from bot.storage import State

class Api:
    def __init__(self, telegram_api: TelegramBotApi, state: State):
        self.telegram_api = telegram_api
        self.state = state

    def get_me(self):
        return self.telegram_api.get_me()

    def send_message(self, message):
        return self.telegram_api.send_message(chat_id=message.chat.id, text=message.text,
                                              reply_to_message_id=message.reply_to_message_id)

    def get_pending_updates(self):
        return self.get_updates(timeout=0)

    def get_updates(self, timeout=45):
        updates = self.telegram_api.get_updates(offset=self.__get_updates_offset(), timeout=timeout)
        for update in updates:
            self.__set_updates_offset(update.update_id)
            yield update

    def __get_updates_offset(self):
        return self.state.next_update_id

    def __set_updates_offset(self, last_update_id):
        self.state.next_update_id = str(last_update_id + 1)

Change yield from to return to be compatible with python 3.2


from sqlite3 import Connection, Row

class Decoder:
    def __init__(self):
        self.conn = Connection('nes.sqlite')
        self.conn.row_factory = Row

    def __del__(self):
        self.conn.close()

    def decode(self, opcode):
        c = self.conn.cursor()
        c.execute('select * from instruction where opcode=?', [opcode])
        row = c.fetchone()
        return dict(zip(row.keys(), row))

from sqlite3 import Connection, Row

class Decoder:
    def __init__(self):
        self.conn = Connection('nes.sqlite')
        self.conn.row_factory = Row

    def __del__(self):
        self.conn.close()

    def decode(self, opcode):
        c = self.conn.cursor()
        c.execute('select * from instruction where opcode=?', [opcode])
        row = c.fetchone()
        if row:
            return dict(zip(row.keys(), row))
        else:
            raise NotImplementedError('Undocumented Opcode: ' + str(opcode))

Raise an exception when it's an undocumented opcode.