from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

# TODO: When Python 2.7 is released this becomes a try/except falling
# back to Django's implementation.
from django.utils.importlib import import_module

def get_backend():
    """
    Return an instance of the registration backend for use on this
    site, as determined by the ``REGISTRATION_BACKEND`` setting. Raise
    ``django.core.exceptions.ImproperlyConfigured`` if the specified
    backend cannot be located.

    """
    i = settings.REGISTRATION_BACKEND.rfind('.')
    module, attr = settings.REGISTRATION_BACKEND[:i], settings.REGISTRATION_BACKEND[i+1:]
    try:
        mod = import_module(module)
    except ImportError, e:
        raise ImproperlyConfigured('Error loading registration backend %s: "%s"' % (module, e))
    try:
        backend_class = getattr(mod, attr)
    except AttributeError:
        raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr))
    return backend_class()

Response:
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

# TODO: When Python 2.7 is released this becomes a try/except falling
# back to Django's implementation.
from django.utils.importlib import import_module

def get_backend():
    """
    Return an instance of the registration backend for use on this
    site, as determined by the ``REGISTRATION_BACKEND`` setting. Raise
    ``django.core.exceptions.ImproperlyConfigured`` if the specified
    backend cannot be located.

    """
    i = settings.REGISTRATION_BACKEND.rfind('.')
    module, attr = settings.REGISTRATION_BACKEND[:i], settings.REGISTRATION_BACKEND[i+1:]
    try:
        mod = import_module(module)
    except ImportError, e:
        raise ImproperlyConfigured('Error loading registration backend %s: "%s"' % (module, e))
    try:
        backend_class = getattr(mod, attr)
    except AttributeError:
        raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr))
    return backend_class()

Prompt:
Add reminder to myself to to importlib fallback.

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.importlib import import_module

def get_backend():
    """
    Return an instance of the registration backend for use on this
    site, as determined by the ``REGISTRATION_BACKEND`` setting. Raise
    ``django.core.exceptions.ImproperlyConfigured`` if the specified
    backend cannot be located.

    """
    i = settings.REGISTRATION_BACKEND.rfind('.')
    module, attr = settings.REGISTRATION_BACKEND[:i], settings.REGISTRATION_BACKEND[i+1:]
    try:
        mod = import_module(module)
    except ImportError, e:
        raise ImproperlyConfigured('Error loading registration backend %s: "%s"' % (module, e))
    try:
        backend_class = getattr(mod, attr)
    except AttributeError:
        raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr))
    return backend_class()


# coding: utf-8
from omega_client import OmegaClient

clt = OmegaClient('http://offlineforward.dataman-inc.net', 'admin@shurenyun.com', 'Dataman1234')

print clt.get_clusters()
print clt.get_cluster(630)
print clt.get_node_identifier(630)
print clt.post_nodes(630, id='83ec44c13e2a482aa4645713d3857ff6', name='test_node')
print clt.get_apps(630)
print clt.get_app(627, 750)

Add one sample usage file.

Add one sample usage file.

Response:
# coding: utf-8
from omega_client import OmegaClient

clt = OmegaClient('http://offlineforward.dataman-inc.net', 'admin@shurenyun.com', 'Dataman1234')

print clt.get_clusters()
print clt.get_cluster(630)
print clt.get_node_identifier(630)
print clt.post_nodes(630, id='83ec44c13e2a482aa4645713d3857ff6', name='test_node')
print clt.get_apps(630)
print clt.get_app(627, 750)

Prompt: Add one sample usage file.


#!/usr/bin/env python
import doctest
import sys
if hasattr(doctest, "testfile"):
    total_failures, total_tests = (0, 0)

    print("=== Test file: README ===")
    failure, tests = doctest.testfile('README', optionflags=doctest.ELLIPSIS)
    total_failures += failure
    total_tests += tests

    print("=== Test file: test.rst ===")
    failure, tests = doctest.testfile('test/test.rst', optionflags=doctest.ELLIPSIS)
    total_failures += failure
    total_tests += tests

    print("=== Test IPy module ===")
    import IPy
    failure, tests = doctest.testmod(IPy)
    total_failures += failure
    total_tests += tests

    print("=== Overall Results ===")
    print("total tests %d, failures %d" % (total_tests, total_failures))
    if total_failures:
        sys.exit(1)
else:
    sys.stderr.write("WARNING: doctest has no function testfile (before Python 2.4), unable to check README\n")

Response:
#!/usr/bin/env python
import doctest
import sys
if hasattr(doctest, "testfile"):
    total_failures, total_tests = (0, 0)

    print("=== Test file: README ===")
    failure, tests = doctest.testfile('README', optionflags=doctest.ELLIPSIS)
    total_failures += failure
    total_tests += tests

    print("=== Test file: test.rst ===")
    failure, tests = doctest.testfile('test/test.rst', optionflags=doctest.ELLIPSIS)
    total_failures += failure
    total_tests += tests

    print("=== Test IPy module ===")
    import IPy
    failure, tests = doctest.testmod(IPy)
    total_failures += failure
    total_tests += tests

    print("=== Overall Results ===")
    print("total tests %d, failures %d" % (total_tests, total_failures))
    if total_failures:
        sys.exit(1)
else:
    sys.stderr.write("WARNING: doctest has no function testfile (before Python 2.4), unable to check README\n")

Prompt:
Allow doctest runner to keep going after failures

It will still return an error code, but there is little need to halt the
running of the three different doctest modules if an early one fails,
which may in fact mask the real reason for failure in an IPy internal
method.

Signed-off-by: Dan McGee <a6e5737275ff1276377ee261739f3ee963671241@gmail.com>

#!/usr/bin/env python
import doctest
import sys
if hasattr(doctest, "testfile"):
    print("=== Test file: README ===")
    failure, tests = doctest.testfile('README', optionflags=doctest.ELLIPSIS)
    if failure:
        sys.exit(1)

    print("=== Test file: test.rst ===")
    failure, tests = doctest.testfile('test/test.rst', optionflags=doctest.ELLIPSIS)
    if failure:
        sys.exit(1)

    print("=== Test IPy module ===")
    import IPy
    failure, tests = doctest.testmod(IPy)
    if failure:
        sys.exit(1)
else:
    sys.stderr.write("WARNING: doctest has no function testfile (before Python 2.4), unable to check README\n")


from setuptools import setup

setup(
    name='hypothesis-pb',
    packages=['hypothesis_protobuf'],
    platforms='any',
    version='1.2.0',
    description='Hypothesis extension to allow generating protobuf messages matching a schema.',
    author='H. Chase Stevens',
    author_email='chase@chasestevens.com',
    url='https://github.com/hchasestevens/hypothesis-protobuf',
    license='MIT',
    install_requires=[
        'hypothesis>=3.4.2',
        'protobuf>=3.3.0,<4.0.0',
    ],
    tests_require=['pytest>=3.1.2', 'future>=0.16.0'],
    extras_require={'dev': ['pytest>=3.1.2', 'future>=0.16.0']},
    classifiers=[
        'Framework :: Hypothesis',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ]
)

Response:
from setuptools import setup

setup(
    name='hypothesis-pb',
    packages=['hypothesis_protobuf'],
    platforms='any',
    version='1.2.0',
    description='Hypothesis extension to allow generating protobuf messages matching a schema.',
    author='H. Chase Stevens',
    author_email='chase@chasestevens.com',
    url='https://github.com/hchasestevens/hypothesis-protobuf',
    license='MIT',
    install_requires=[
        'hypothesis>=3.4.2',
        'protobuf>=3.3.0,<4.0.0',
    ],
    tests_require=['pytest>=3.1.2', 'future>=0.16.0'],
    extras_require={'dev': ['pytest>=3.1.2', 'future>=0.16.0']},
    classifiers=[
        'Framework :: Hypothesis',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ]
)

Prompt:
Add hypothesis as framework for discoverability
from setuptools import setup

setup(
    name='hypothesis-pb',
    packages=['hypothesis_protobuf'],
    platforms='any',
    version='1.2.0',
    description='Hypothesis extension to allow generating protobuf messages matching a schema.',
    author='H. Chase Stevens',
    author_email='chase@chasestevens.com',
    url='https://github.com/hchasestevens/hypothesis-protobuf',
    license='MIT',
    install_requires=[
        'hypothesis>=3.4.2',
        'protobuf>=3.3.0,<4.0.0',
    ],
    tests_require=['pytest>=3.1.2', 'future>=0.16.0'],
    extras_require={'dev': ['pytest>=3.1.2', 'future>=0.16.0']},
    classifiers=[
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.6',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: MIT License',
    ]
)


#------------------------------------------------------------------------------
# Copyright (c) 2010, Enthought Inc
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.

#
# Author: Enthought Inc
# Description: Qt API selector. Can be used to switch between pyQt and PySide
#------------------------------------------------------------------------------

import os

def prepare_pyqt4():
    # Set PySide compatible APIs.
    import sip
    sip.setapi('QString', 2)
    sip.setapi('QVariant', 2)
    sip.setapi('QDate', 2)

qt_api = os.environ.get('QT_API')

if qt_api is None:
    try:
        import PySide
        qt_api = 'pyside'
    except ImportError:
        try:
            prepare_pyqt4()
            import PyQt4
            qt_api = 'pyqt'
        except ImportError:
            raise ImportError('Cannot import PySide or PyQt4')

elif qt_api == 'pyqt':
    prepare_pyqt4()

elif qt_api != 'pyside':
    raise RuntimeError("Invalid Qt API %r, valid values are: 'pyqt' or 'pyside'"
                       % qt_api)

Response:
#------------------------------------------------------------------------------
# Copyright (c) 2010, Enthought Inc
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.

#
# Author: Enthought Inc
# Description: Qt API selector. Can be used to switch between pyQt and PySide
#------------------------------------------------------------------------------

import os

def prepare_pyqt4():
    # Set PySide compatible APIs.
    import sip
    sip.setapi('QString', 2)
    sip.setapi('QVariant', 2)
    sip.setapi('QDate', 2)

qt_api = os.environ.get('QT_API')

if qt_api is None:
    try:
        import PySide
        qt_api = 'pyside'
    except ImportError:
        try:
            prepare_pyqt4()
            import PyQt4
            qt_api = 'pyqt'
        except ImportError:
            raise ImportError('Cannot import PySide or PyQt4')

elif qt_api == 'pyqt':
    prepare_pyqt4()

elif qt_api != 'pyside':
    raise RuntimeError("Invalid Qt API %r, valid values are: 'pyqt' or 'pyside'"
                       % qt_api)

Prompt:
Set the sip QDate API for enaml interop.

#------------------------------------------------------------------------------
# Copyright (c) 2010, Enthought Inc
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.

#
# Author: Enthought Inc
# Description: Qt API selector. Can be used to switch between pyQt and PySide
#------------------------------------------------------------------------------

import os

def prepare_pyqt4():
    # Set PySide compatible APIs.
    import sip
    sip.setapi('QString', 2)
    sip.setapi('QVariant', 2)

qt_api = os.environ.get('QT_API')

if qt_api is None:
    try:
        import PySide
        qt_api = 'pyside'
    except ImportError:
        try:
            prepare_pyqt4()
            import PyQt4
            qt_api = 'pyqt'
        except ImportError:
            raise ImportError('Cannot import PySide or PyQt4')

elif qt_api == 'pyqt':
    prepare_pyqt4()

elif qt_api != 'pyside':
    raise RuntimeError("Invalid Qt API %r, valid values are: 'pyqt' or 'pyside'"
                       % qt_api)


try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO

try:
    from contextlib import redirect_stdout
except ImportError:
    import sys
    import contextlib

    @contextlib.contextmanager
    def redirect_stdout(target):
        original = sys.stdout
        sys.stdout = target
        yield
        sys.stdout = original

def string_to_file(string):
    return StringIO(string)

def capture_print(func, args=None):
    f = StringIO()
    with redirect_stdout(f):
        if args:
            func(args)
        else:
            func()

    return f.getvalue()

try:
    from re import fullmatch
except ImportError:
    from re import match

    def fullmatch(regex, *args):
        if not regex.pattern.endswith("$"):
            return match(regex.pattern + "$", *args)

        return match(regex.pattern, *args)

try:
    unicode('')
except NameError:
    unicode = str

def text(value):
    return unicode(value)

Make fullmatch work on python 2.7.

Bug: Make fullmatch work on python 2.7.

Response:
try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO

try:
    from contextlib import redirect_stdout
except ImportError:
    import sys
    import contextlib

    @contextlib.contextmanager
    def redirect_stdout(target):
        original = sys.stdout
        sys.stdout = target
        yield
        sys.stdout = original

def string_to_file(string):
    return StringIO(string)

def capture_print(func, args=None):
    f = StringIO()
    with redirect_stdout(f):
        if args:
            func(args)
        else:
            func()

    return f.getvalue()

try:
    from re import fullmatch
except ImportError:
    from re import match

    def fullmatch(regex, *args):
        if not regex.pattern.endswith("$"):
            return match(regex.pattern + "$", *args)

        return match(regex.pattern, *args)

try:
    unicode('')
except NameError:
    unicode = str

def text(value):
    return unicode(value)

Prompt:
Bug: Make fullmatch work on python 2.7.

try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO

try:
    from contextlib import redirect_stdout
except ImportError:
    import sys
    import contextlib

    @contextlib.contextmanager
    def redirect_stdout(target):
        original = sys.stdout
        sys.stdout = target
        yield
        sys.stdout = original

def string_to_file(string):
    return StringIO(string)

def capture_print(func, args=None):
    f = StringIO()
    with redirect_stdout(f):
        if args:
            func(args)
        else:
            func()

    return f.getvalue()

try:
    from re import fullmatch
except ImportError:
    from re import match
    def fullmatch(pattern, *args):
        if not pattern.endswith("$"):
            pattern += "$"
        return match(pattern, *args)

try:
    unicode('')
except NameError:
    unicode = str

def text(value):
    return unicode(value)


from spack import *

class PyScikitLearn(Package):
    """"""
    homepage = "https://pypi.python.org/pypi/scikit-learn"
    url      = "https://pypi.python.org/packages/source/s/scikit-learn/scikit-learn-0.15.2.tar.gz"

    version('0.15.2', 'd9822ad0238e17b382a3c756ea94fe0d')
    version('0.16.1', '363ddda501e3b6b61726aa40b8dbdb7e')
    version('0.17.1', 'a2f8b877e6d99b1ed737144f5a478dfc')

    extends('python')

    def install(self, spec, prefix):
        python('setup.py', 'install', '--prefix=%s' % prefix)

Add version 0.17.1 of scikit-learn.

Add version 0.17.1 of scikit-learn.

Response:
from spack import *

class PyScikitLearn(Package):
    """"""
    homepage = "https://pypi.python.org/pypi/scikit-learn"
    url      = "https://pypi.python.org/packages/source/s/scikit-learn/scikit-learn-0.15.2.tar.gz"

    version('0.15.2', 'd9822ad0238e17b382a3c756ea94fe0d')
    version('0.16.1', '363ddda501e3b6b61726aa40b8dbdb7e')
    version('0.17.1', 'a2f8b877e6d99b1ed737144f5a478dfc')

    extends('python')

    def install(self, spec, prefix):
        python('setup.py', 'install', '--prefix=%s' % prefix)

Prompt:
Add version 0.17.1 of scikit-learn.

from spack import *

class PyScikitLearn(Package):
    """"""
    homepage = "https://pypi.python.org/pypi/scikit-learn"
    url      = "https://pypi.python.org/packages/source/s/scikit-learn/scikit-learn-0.15.2.tar.gz"

    version('0.15.2', 'd9822ad0238e17b382a3c756ea94fe0d')
    version('0.16.1', '363ddda501e3b6b61726aa40b8dbdb7e')

    extends('python')

    def install(self, spec, prefix):
        python('setup.py', 'install', '--prefix=%s' % prefix)


import subprocess
import unittest

from aws_status.status_page import StatusPage

class TestStatusPage(unittest.TestCase):
    def test_number_of_detected_feeds(self):
        command = "curl -s http://status.aws.amazon.com/|grep rss|sort -u|wc -l"
        p = subprocess.Popen(command, stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE,
                                      shell=True)
        output, errors = p.communicate()
        expected_feeds = int(output)
        self.assertEqual(expected_feeds, len(StatusPage().rss_urls))

Add integration test for number of detected RSS feeds

Response:
import subprocess
import unittest

from aws_status.status_page import StatusPage

class TestStatusPage(unittest.TestCase):
    def test_number_of_detected_feeds(self):
        command = "curl -s http://status.aws.amazon.com/|grep rss|sort -u|wc -l"
        p = subprocess.Popen(command, stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE,
                                      shell=True)
        output, errors = p.communicate()
        expected_feeds = int(output)
        self.assertEqual(expected_feeds, len(StatusPage().rss_urls))

Prompt: Add integration test for number of detected RSS feeds


"""Module with all classes related to links.

Links are low level abstractions representing connections between two
interfaces.
"""

import json

from uuid import uuid4

from kytos.core.common import GenericEntity

class Link(GenericEntity):
    """Define a link between two Endpoints."""

    def __init__(self, endpoint_a, endpoint_b):
        """Create a Link instance and set its attributes."""
        self.endpoint_a = endpoint_a
        self.endpoint_b = endpoint_b
        self._uuid = uuid4()
        super().__init__()

    def __eq__(self, other):
        """Check if two instances of Link are equal."""
        return ((self.endpoint_a == other.endpoint_a and
                 self.endpoint_b == other.endpoint_b) or
                (self.endpoint_a == other.endpoint_b and
                 self.endpoint_b == other.endpoint_a))

    @property
    def id(self):  # pylint: disable=invalid-name
        """Return id from Link intance.

        Returns:
            string: link id.

        """
        return "{}".format(self._uuid)

    def as_dict(self):
        """Return the Link as a dictionary."""
        return {'id': self.id,
                'endpoint_a': self.endpoint_a.as_dict(),
                'endpoint_b': self.endpoint_b.as_dict(),
                'metadata': self.metadata,
                'active': self.active,
                'enabled': self.enabled}

    def as_json(self):
        """Return the Link as a JSON string."""
        return json.dumps(self.as_dict())

Define Link ID as UUID

Define Link ID as UUID

Response:
"""Module with all classes related to links.

Links are low level abstractions representing connections between two
interfaces.
"""

import json

from uuid import uuid4

from kytos.core.common import GenericEntity

class Link(GenericEntity):
    """Define a link between two Endpoints."""

    def __init__(self, endpoint_a, endpoint_b):
        """Create a Link instance and set its attributes."""
        self.endpoint_a = endpoint_a
        self.endpoint_b = endpoint_b
        self._uuid = uuid4()
        super().__init__()

    def __eq__(self, other):
        """Check if two instances of Link are equal."""
        return ((self.endpoint_a == other.endpoint_a and
                 self.endpoint_b == other.endpoint_b) or
                (self.endpoint_a == other.endpoint_b and
                 self.endpoint_b == other.endpoint_a))

    @property
    def id(self):  # pylint: disable=invalid-name
        """Return id from Link intance.

        Returns:
            string: link id.

        """
        return "{}".format(self._uuid)

    def as_dict(self):
        """Return the Link as a dictionary."""
        return {'id': self.id,
                'endpoint_a': self.endpoint_a.as_dict(),
                'endpoint_b': self.endpoint_b.as_dict(),
                'metadata': self.metadata,
                'active': self.active,
                'enabled': self.enabled}

    def as_json(self):
        """Return the Link as a JSON string."""
        return json.dumps(self.as_dict())

Prompt:
Define Link ID as UUID

"""Module with all classes related to links.

Links are low level abstractions representing connections between two
interfaces.
"""

import json

from kytos.core.common import GenericEntity

class Link(GenericEntity):
    """Define a link between two Endpoints."""

    def __init__(self, endpoint_a, endpoint_b):
        """Create a Link instance and set its attributes."""
        self.endpoint_a = endpoint_a
        self.endpoint_b = endpoint_b
        super().__init__()

    def __eq__(self, other):
        """Check if two instances of Link are equal."""
        return ((self.endpoint_a == other.endpoint_a and
                 self.endpoint_b == other.endpoint_b) or
                (self.endpoint_a == other.endpoint_b and
                 self.endpoint_b == other.endpoint_a))

    @property
    def id(self):  # pylint: disable=invalid-name
        """Return id from Link intance.

        Returns:
            string: link id.

        """
        return "{}:{}".format(self.endpoint_a.id, self.endpoint_b.id)

    def as_dict(self):
        """Return the Link as a dictionary."""
        return {'id': self.id,
                'endpoint_a': self.endpoint_a.as_dict(),
                'endpoint_b': self.endpoint_b.as_dict(),
                'metadata': self.metadata,
                'active': self.active,
                'enabled': self.enabled}

    def as_json(self):
        """Return the Link as a JSON string."""
        return json.dumps(self.as_dict())


from django.template.loader import BaseLoader
from django.db import connection
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils._os import safe_join
from django.template.base import TemplateDoesNotExist

class TenantTemplateLoader(BaseLoader):
    is_usable = True

    def get_template_sources(self, template_name, template_dirs=None):
        """
        Returns the absolute paths to "template_name", when appended to each
        directory in "template_dirs". Any paths that don't lie inside one of the
        template dirs are excluded from the result set, for security reasons.
        """
        if not connection.tenant:
            return
        if not template_dirs:
            try:
                template_dirs = settings.MULTITENANT_TEMPLATE_DIRS
            except AttributeError:
                raise ImproperlyConfigured('To use %s.%s you must define the MULTITENANT_TEMPLATE_DIRS' %
                                           (__name__, TenantTemplateLoader.__name__))
        for template_dir in template_dirs:
            try:
                if '%s' in template_dir:
                    yield safe_join(template_dir % connection.tenant.client_name, 'templates', template_name)
                else:
                    yield safe_join(template_dir, connection.tenant.client_name, 'templates', template_name)
            except UnicodeDecodeError:
                # The template dir name was a bytestring that wasn't valid UTF-8.
                raise
            except ValueError:
                # The joined path was located outside of this particular
                # template_dir (it might be inside another one, so this isn't
                # fatal).
                pass

    def load_template_source(self, template_name, template_dirs=None):
        tried = []
        for filepath in self.get_template_sources(template_name, template_dirs):
            try:
                with open(filepath, 'rb') as fp:
                    return (fp.read().decode(settings.FILE_CHARSET), filepath)
            except IOError:
                tried.append(filepath)
        if tried:
            error_msg = "Tried %s" % tried
        else:
            error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
        raise TemplateDoesNotExist(error_msg)
    load_template_source.is_usable = True

Add template loader for clients

Add template loader for clients

Response:
from django.template.loader import BaseLoader
from django.db import connection
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils._os import safe_join
from django.template.base import TemplateDoesNotExist

class TenantTemplateLoader(BaseLoader):
    is_usable = True

    def get_template_sources(self, template_name, template_dirs=None):
        """
        Returns the absolute paths to "template_name", when appended to each
        directory in "template_dirs". Any paths that don't lie inside one of the
        template dirs are excluded from the result set, for security reasons.
        """
        if not connection.tenant:
            return
        if not template_dirs:
            try:
                template_dirs = settings.MULTITENANT_TEMPLATE_DIRS
            except AttributeError:
                raise ImproperlyConfigured('To use %s.%s you must define the MULTITENANT_TEMPLATE_DIRS' %
                                           (__name__, TenantTemplateLoader.__name__))
        for template_dir in template_dirs:
            try:
                if '%s' in template_dir:
                    yield safe_join(template_dir % connection.tenant.client_name, 'templates', template_name)
                else:
                    yield safe_join(template_dir, connection.tenant.client_name, 'templates', template_name)
            except UnicodeDecodeError:
                # The template dir name was a bytestring that wasn't valid UTF-8.
                raise
            except ValueError:
                # The joined path was located outside of this particular
                # template_dir (it might be inside another one, so this isn't
                # fatal).
                pass

    def load_template_source(self, template_name, template_dirs=None):
        tried = []
        for filepath in self.get_template_sources(template_name, template_dirs):
            try:
                with open(filepath, 'rb') as fp:
                    return (fp.read().decode(settings.FILE_CHARSET), filepath)
            except IOError:
                tried.append(filepath)
        if tried:
            error_msg = "Tried %s" % tried
        else:
            error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
        raise TemplateDoesNotExist(error_msg)
    load_template_source.is_usable = True

Prompt: Add template loader for clients


from membrane import Membrane, Proxy
from rt import Wait

def test_membrane():
    m1 = Membrane.create()
    m1.transport = {'protocol': 'null',
                    'membrane': m1}
    m2 = Membrane.create()
    m2.transport = {'protocol': 'null',
                    'membrane': m1}
    m1 << 'start'
    m2 << 'start'

    w = Wait.create()
    actor1 = Wait.create()
    actor2 = Wait.create()

    m2 << {'get_uid': actor2,
           'reply_to': w}
    u2 = w.act()['uid']

    m1 << {'create_proxy': u2,
           'transport': {'protocol': 'null',
                         'membrane': m2},
           'reply_to': w}
    proxy = w.act()['proxy']
    proxy << {'foo': 5,
              'reply_to': actor1}

    msg = actor2.act()
    assert msg['foo'] == 5

    proxy2 = msg['reply_to']
    assert isinstance(proxy2, Proxy)

    proxy2 << {'bar': 3,
               'reply_to': actor2}
    msg = actor1.act()
    assert msg['bar'] == 3
    assert msg['reply_to'] is proxy

Add test for membrane behavior

Add test for membrane behavior

Response:
from membrane import Membrane, Proxy
from rt import Wait

def test_membrane():
    m1 = Membrane.create()
    m1.transport = {'protocol': 'null',
                    'membrane': m1}
    m2 = Membrane.create()
    m2.transport = {'protocol': 'null',
                    'membrane': m1}
    m1 << 'start'
    m2 << 'start'

    w = Wait.create()
    actor1 = Wait.create()
    actor2 = Wait.create()

    m2 << {'get_uid': actor2,
           'reply_to': w}
    u2 = w.act()['uid']

    m1 << {'create_proxy': u2,
           'transport': {'protocol': 'null',
                         'membrane': m2},
           'reply_to': w}
    proxy = w.act()['proxy']
    proxy << {'foo': 5,
              'reply_to': actor1}

    msg = actor2.act()
    assert msg['foo'] == 5

    proxy2 = msg['reply_to']
    assert isinstance(proxy2, Proxy)

    proxy2 << {'bar': 3,
               'reply_to': actor2}
    msg = actor1.act()
    assert msg['bar'] == 3
    assert msg['reply_to'] is proxy

Prompt: Add test for membrane behavior


import multiprocessing
import pprint
from ppm import Trie

trie = None

def test(a):
    x, trie = a
    trie.add('b')
    trie.add('b')
    print(x, trie.bit_encoding)
    return trie.bit_encoding

if __name__ == '__main__':
    trie = Trie(5)
    trie.add('a')
    alist = [ (x, trie) for x in range(0, multiprocessing.cpu_count()) ]
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    result = pool.map(test, alist)
    pprint.pprint(result)

Add a race condition tester

Add a race condition tester

Response:
import multiprocessing
import pprint
from ppm import Trie

trie = None

def test(a):
    x, trie = a
    trie.add('b')
    trie.add('b')
    print(x, trie.bit_encoding)
    return trie.bit_encoding

if __name__ == '__main__':
    trie = Trie(5)
    trie.add('a')
    alist = [ (x, trie) for x in range(0, multiprocessing.cpu_count()) ]
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    result = pool.map(test, alist)
    pprint.pprint(result)

Prompt: Add a race condition tester


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):
    def create_simple_fields(apps, schema_editor):
        ExtraField = apps.get_model('candidates', 'ExtraField')
        db_alias = schema_editor.connection.alias

        ExtraField.objects.using(db_alias).update_or_create(
            key='favourite_biscuits',
            defaults={
                'label': 'Favourite Biscuit 🍪',
                'type': 'line',
                'order': 1,
            }
        )

    dependencies = [
        ('uk', '0004_add_biography'),
    ]

    operations = [
        migrations.RunPython(create_simple_fields),
    ]

Add Favourite Biscuit as a field for candidates

Response:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):
    def create_simple_fields(apps, schema_editor):
        ExtraField = apps.get_model('candidates', 'ExtraField')
        db_alias = schema_editor.connection.alias

        ExtraField.objects.using(db_alias).update_or_create(
            key='favourite_biscuits',
            defaults={
                'label': 'Favourite Biscuit 🍪',
                'type': 'line',
                'order': 1,
            }
        )

    dependencies = [
        ('uk', '0004_add_biography'),
    ]

    operations = [
        migrations.RunPython(create_simple_fields),
    ]

Prompt: Add Favourite Biscuit as a field for candidates


from setuptools import setup, find_packages
import os

version = '0.1.2'

LONG_DESCRIPTION = """
===============
django-helpdesk
===============

This is a Django-powered helpdesk ticket tracker, designed to
plug into an existing Django website and provide you with
internal (or, perhaps, external) helpdesk management.
"""

setup(
    name='django-helpdesk',
    version=version,
    description="Django-powered ticket tracker for your helpdesk",
    long_description=LONG_DESCRIPTION,
    classifiers=[
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Framework :: Django",
        "Environment :: Web Environment",
        "Operating System :: OS Independent",
        "Intended Audience :: Customer Service",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Topic :: Office/Business",
        "Topic :: Software Development :: Bug Tracking",
    ],
    keywords=['django', 'helpdesk', 'tickets', 'incidents', 'cases'],
    author='Ross Poulton',
    author_email='ross@rossp.org',
    url='http://github.com/rossp/django-helpdesk',
    license='BSD',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=['setuptools'],
)

Response:
from setuptools import setup, find_packages
import os

version = '0.1.2'

LONG_DESCRIPTION = """
===============
django-helpdesk
===============

This is a Django-powered helpdesk ticket tracker, designed to
plug into an existing Django website and provide you with
internal (or, perhaps, external) helpdesk management.
"""

setup(
    name='django-helpdesk',
    version=version,
    description="Django-powered ticket tracker for your helpdesk",
    long_description=LONG_DESCRIPTION,
    classifiers=[
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Framework :: Django",
        "Environment :: Web Environment",
        "Operating System :: OS Independent",
        "Intended Audience :: Customer Service",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Topic :: Office/Business",
        "Topic :: Software Development :: Bug Tracking",
    ],
    keywords=['django', 'helpdesk', 'tickets', 'incidents', 'cases'],
    author='Ross Poulton',
    author_email='ross@rossp.org',
    url='http://github.com/rossp/django-helpdesk',
    license='BSD',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=['setuptools'],
)

Prompt:
Increment version number for updated pypi package

from setuptools import setup, find_packages
import os

version = '0.1.1'

LONG_DESCRIPTION = """
===============
django-helpdesk
===============

This is a Django-powered helpdesk ticket tracker, designed to
plug into an existing Django website and provide you with
internal (or, perhaps, external) helpdesk management.
"""

setup(
    name='django-helpdesk',
    version=version,
    description="Django-powered ticket tracker for your helpdesk",
    long_description=LONG_DESCRIPTION,
    classifiers=[
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Framework :: Django",
        "Environment :: Web Environment",
        "Operating System :: OS Independent",
        "Intended Audience :: Customer Service",
        "License :: OSI Approved :: BSD License",
        "Natural Language :: English",
        "Topic :: Office/Business",
        "Topic :: Software Development :: Bug Tracking",
    ],
    keywords=['django', 'helpdesk', 'tickets', 'incidents', 'cases'],
    author='Ross Poulton',
    author_email='ross@rossp.org',
    url='http://github.com/rossp/django-helpdesk',
    license='BSD',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    install_requires=['setuptools'],
)


#!/usr/bin/env python

"""
Take evt3 file and use region files to subtract off sources that are already known - image will have lots of holes
Goals by Friday 6/31 - Get script working for one image at a time

below = code used by Giacomo to create filtered image
ftcopy 'acisf00635_000N001_evt3.fits[EVENTS][regfilter("my_source.reg")]' test.fits

code that works with CIAO
dmcopy "acisf00635_000N001_evt3.fits[exclude sky=region(acisf00635_000N001_r0101_reg3.fits)]" filter_test.fits opt=all
"""

import argparse
import subprocess
import os

if __name__=="__main__":

    parser = argparse.ArgumentParser(description='Filter known sources out of level 3 event file')
    parser.add_argument("--obsid",help="Observation ID Numbers",nargs='+',type=int,required=True)

    # assumption = all region files and event files are already downloaded into same directory

    args = parser.parse_args()

    #changes me from chandra_suli folder up three levels to VM_shared folder, where evt3 and reg3 files are held
    os.chdir("../../../")

    for i in args.obsid:

        subprocess.call("find %d -name \"*reg3.fits.gz\" > %d_reg.txt" %(i,i), shell=True)

Copy reg file names into text file

Copy reg file names into text file

Response:
#!/usr/bin/env python

"""
Take evt3 file and use region files to subtract off sources that are already known - image will have lots of holes
Goals by Friday 6/31 - Get script working for one image at a time

below = code used by Giacomo to create filtered image
ftcopy 'acisf00635_000N001_evt3.fits[EVENTS][regfilter("my_source.reg")]' test.fits

code that works with CIAO
dmcopy "acisf00635_000N001_evt3.fits[exclude sky=region(acisf00635_000N001_r0101_reg3.fits)]" filter_test.fits opt=all
"""

import argparse
import subprocess
import os

if __name__=="__main__":

    parser = argparse.ArgumentParser(description='Filter known sources out of level 3 event file')
    parser.add_argument("--obsid",help="Observation ID Numbers",nargs='+',type=int,required=True)

    # assumption = all region files and event files are already downloaded into same directory

    args = parser.parse_args()

    #changes me from chandra_suli folder up three levels to VM_shared folder, where evt3 and reg3 files are held
    os.chdir("../../../")

    for i in args.obsid:

        subprocess.call("find %d -name \"*reg3.fits.gz\" > %d_reg.txt" %(i,i), shell=True)

Prompt:
Copy reg file names into text file

"""
Take evt3 file and use region files to subtract off sources that are already known - image will have lots of holes
Goals by Friday 6/31 - Get script working for one image at a time

below = code used by Giacomo to create filtered image
ftcopy 'acisf00635_000N001_evt3.fits[EVENTS][regfilter("my_source.reg")]' test.fits
"""


import time
import threading
import socket

class NetworkFeeder:
    """
    A class that feeds data to a socket port
    """

    def __init__(self, proto, host, port, data, is_client=True, delay=5, timeout=2):

        if not is_client:
            raise NotImplementedError("Server mode is not implemented.")

        if proto != "tcp":
            raise NotImplementedError("Only TCP mode is supported for now.")

        self._proto = proto
        self._is_client = is_client
        self._delay = delay
        self._data = data
        self._host = host
        self._port = port
        self._timeout = timeout

        t = threading.Thread(target=self.worker)
        t.start()

    def worker(self):
        if self._delay:
            time.sleep(self._delay)

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM if self._proto == "tcp" else socket.SOCK_DGRAM)
        sock.settimeout(self._timeout)
        sock.connect((self._host, self._port))

        sock.send(self._data)

        sock.close()

Response:
import time
import threading
import socket

class NetworkFeeder:
    """
    A class that feeds data to a socket port
    """

    def __init__(self, proto, host, port, data, is_client=True, delay=5, timeout=2):

        if not is_client:
            raise NotImplementedError("Server mode is not implemented.")

        if proto != "tcp":
            raise NotImplementedError("Only TCP mode is supported for now.")

        self._proto = proto
        self._is_client = is_client
        self._delay = delay
        self._data = data
        self._host = host
        self._port = port
        self._timeout = timeout

        t = threading.Thread(target=self.worker)
        t.start()

    def worker(self):
        if self._delay:
            time.sleep(self._delay)

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM if self._proto == "tcp" else socket.SOCK_DGRAM)
        sock.settimeout(self._timeout)
        sock.connect((self._host, self._port))

        sock.send(self._data)

        sock.close()

Prompt:
NetworkFeeder: Set default delay to 5 seconds (because of my slow laptop).

import time
import threading
import socket

class NetworkFeeder:
    """
    A class that feeds data to a socket port
    """

    def __init__(self, proto, host, port, data, is_client=True, delay=3, timeout=2):

        if not is_client:
            raise NotImplementedError("Server mode is not implemented.")

        if proto != "tcp":
            raise NotImplementedError("Only TCP mode is supported for now.")

        self._proto = proto
        self._is_client = is_client
        self._delay = delay
        self._data = data
        self._host = host
        self._port = port
        self._timeout = timeout

        t = threading.Thread(target=self.worker)
        t.start()

    def worker(self):
        if self._delay:
            time.sleep(self._delay)

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM if self._proto == "tcp" else socket.SOCK_DGRAM)
        sock.settimeout(self._timeout)
        sock.connect((self._host, self._port))

        sock.send(self._data)

        sock.close()


# This file is part of rinohtype, the Python document preparation system.
#
# Copyright (c) Brecht Machiels.
#
# Use of this source code is subject to the terms of the GNU Affero General
# Public License v3. See the LICENSE file or http://www.gnu.org/licenses/.

import inspect
import os
import sys

from .. import DATA_PATH
from ..style import StyleSheetFile

from .matcher import matcher

__all__ = ['matcher', 'sphinx', 'sphinx_base14']

STYLESHEETS_PATH = os.path.join(DATA_PATH, 'stylesheets')

def path(filename):
    return os.path.join(STYLESHEETS_PATH, filename)

sphinx = StyleSheetFile(path('sphinx.rts'))

sphinx_article = StyleSheetFile(path('sphinx_article.rts'))

sphinx_base14 = StyleSheetFile(path('base14.rts'))

# generate docstrings for the StyleSheet instances

for name, stylesheet in inspect.getmembers(sys.modules[__name__]):
    if not isinstance(stylesheet, StyleSheetFile):
        continue
    stylesheet.__doc__ = ('{}\n\nEntry point name: ``{}``'
                          .format(stylesheet.description, stylesheet))

Response:
# This file is part of rinohtype, the Python document preparation system.
#
# Copyright (c) Brecht Machiels.
#
# Use of this source code is subject to the terms of the GNU Affero General
# Public License v3. See the LICENSE file or http://www.gnu.org/licenses/.

import inspect
import os
import sys

from .. import DATA_PATH
from ..style import StyleSheetFile

from .matcher import matcher

__all__ = ['matcher', 'sphinx', 'sphinx_base14']

STYLESHEETS_PATH = os.path.join(DATA_PATH, 'stylesheets')

def path(filename):
    return os.path.join(STYLESHEETS_PATH, filename)

sphinx = StyleSheetFile(path('sphinx.rts'))

sphinx_article = StyleSheetFile(path('sphinx_article.rts'))

sphinx_base14 = StyleSheetFile(path('base14.rts'))

# generate docstrings for the StyleSheet instances

for name, stylesheet in inspect.getmembers(sys.modules[__name__]):
    if not isinstance(stylesheet, StyleSheetFile):
        continue
    stylesheet.__doc__ = ('{}\n\nEntry point name: ``{}``'
                          .format(stylesheet.description, stylesheet))

Prompt:
Fix the auto-generated docstrings of style sheets

# This file is part of rinohtype, the Python document preparation system.
#
# Copyright (c) Brecht Machiels.
#
# Use of this source code is subject to the terms of the GNU Affero General
# Public License v3. See the LICENSE file or http://www.gnu.org/licenses/.

import inspect
import os
import sys

from .. import DATA_PATH
from ..style import StyleSheetFile

from .matcher import matcher

__all__ = ['matcher', 'sphinx', 'sphinx_base14']

STYLESHEETS_PATH = os.path.join(DATA_PATH, 'stylesheets')

def path(filename):
    return os.path.join(STYLESHEETS_PATH, filename)

sphinx = StyleSheetFile(path('sphinx.rts'))

sphinx_article = StyleSheetFile(path('sphinx_article.rts'))

sphinx_base14 = StyleSheetFile(path('base14.rts'))

# generate docstrings for the StyleSheet instances

for name, stylesheet in inspect.getmembers(sys.modules[__name__]):
    if not isinstance(stylesheet, StyleSheetFile):
        continue
    stylesheet.__doc__ = (':entry point name: ``{}``\n\n{}'
                          .format(stylesheet, stylesheet.description))


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

from __future__ import absolute_import

import ast
import sys
import struct
import functools

try:
    import ujson as json
    json_dumps = json.dumps
except ImportError:
    import json
    json_dumps = functools.partial(json.dumps, separators=',:')

PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
else:
    string_types = basestring,

def flatten_dtype(dtype):
    dtype = str(dtype)
    if dtype.startswith(('{', '[')):
        return ast.literal_eval(dtype)
    return dtype

def write_i64(stream, *values):
    for value in values:
        stream.write(struct.pack('<Q', value))

def read_i64(stream, count=None):
    if count is None:
        return struct.unpack('<Q', stream.read(8))[0]
    return tuple(struct.unpack('<Q', stream.read(8))[0] for _ in range(count))

def write_json(stream, data):
    payload = json_dumps(data).encode('utf-8')
    write_i64(stream, len(payload))
    stream.write(payload)
    return len(payload) + 8

def read_json(stream):
    length = read_i64(stream)
    return json.loads(stream.read(length).decode('utf-8'))

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

from __future__ import absolute_import

import ast
import sys
import struct
import functools

try:
    import ujson as json
    json_dumps = json.dumps
except ImportError:
    import json
    json_dumps = functools.partial(json.dumps, separators=',:')

PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
else:
    string_types = basestring,

def flatten_dtype(dtype):
    dtype = str(dtype)
    if dtype.startswith(('{', '[')):
        return ast.literal_eval(dtype)
    return dtype

def write_i64(stream, *values):
    for value in values:
        stream.write(struct.pack('<Q', value))

def read_i64(stream, count=None):
    if count is None:
        return struct.unpack('<Q', stream.read(8))[0]
    return tuple(struct.unpack('<Q', stream.read(8))[0] for _ in range(count))

def write_json(stream, data):
    payload = json_dumps(data).encode('utf-8')
    write_i64(stream, len(payload))
    stream.write(payload)
    return len(payload) + 8

def read_json(stream):
    length = read_i64(stream)
    return json.loads(stream.read(length).decode('utf-8'))

Prompt:
Write compact json when using built-in json.dumps

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

from __future__ import absolute_import

import ast
import sys
import struct

try:
    import ujson as json
except ImportError:
    import json

PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
else:
    string_types = basestring,

def flatten_dtype(dtype):
    dtype = str(dtype)
    if dtype.startswith(('{', '[')):
        return ast.literal_eval(dtype)
    return dtype

def write_i64(stream, *values):
    for value in values:
        stream.write(struct.pack('<Q', value))

def read_i64(stream, count=None):
    if count is None:
        return struct.unpack('<Q', stream.read(8))[0]
    return tuple(struct.unpack('<Q', stream.read(8))[0] for _ in range(count))

def write_json(stream, data):
    payload = json.dumps(data).encode('utf-8')
    write_i64(stream, len(payload))
    stream.write(payload)
    return len(payload) + 8

def read_json(stream):
    length = read_i64(stream)
    return json.loads(stream.read(length).decode('utf-8'))


__all__ = ['long']

if 'long' in __builtins__:
    long = __builtins__['long']
else:
    class long(int):
        pass

Response:
__all__ = ['long']

if 'long' in __builtins__:
    long = __builtins__['long']
else:
    class long(int):
        pass

Prompt:
Fix bug encoding long type in python 2.x

__all__ = ['long']

if hasattr(__builtins__, 'long'):
    long = long
else:
    class long(int):
        pass


#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import specs.folly as folly
import specs.fizz as fizz
import specs.rsocket as rsocket
import specs.sodium as sodium
import specs.wangle as wangle
import specs.zstd as zstd

from shell_quoting import ShellQuoted

def fbcode_builder_spec(builder):
    # This API should change rarely, so build the latest tag instead of master.
    builder.add_option(
        'no1msd/mstch:git_hash',
        ShellQuoted('$(git describe --abbrev=0 --tags)')
    )
    return {
        'depends_on': [folly, fizz, sodium, rsocket, wangle, zstd],
        'steps': [
            # This isn't a separete spec, since only fbthrift uses mstch.
            builder.github_project_workdir('no1msd/mstch', 'build'),
            builder.cmake_install('no1msd/mstch'),
            builder.fb_github_cmake_install('fbthrift/thrift'),
        ],
    }

Response:
#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import specs.folly as folly
import specs.fizz as fizz
import specs.rsocket as rsocket
import specs.sodium as sodium
import specs.wangle as wangle
import specs.zstd as zstd

from shell_quoting import ShellQuoted

def fbcode_builder_spec(builder):
    # This API should change rarely, so build the latest tag instead of master.
    builder.add_option(
        'no1msd/mstch:git_hash',
        ShellQuoted('$(git describe --abbrev=0 --tags)')
    )
    return {
        'depends_on': [folly, fizz, sodium, rsocket, wangle, zstd],
        'steps': [
            # This isn't a separete spec, since only fbthrift uses mstch.
            builder.github_project_workdir('no1msd/mstch', 'build'),
            builder.cmake_install('no1msd/mstch'),
            builder.fb_github_cmake_install('fbthrift/thrift'),
        ],
    }

Prompt:
Cut fbcode_builder dep for thrift on krb5

Summary: [Thrift] Cut `fbcode_builder` dep for `thrift` on `krb5`. In the past, Thrift depended on Kerberos and the `krb5` implementation for its transport-layer security. However, Thrift has since migrated fully to Transport Layer Security for its transport-layer security and no longer has any build-time dependency on `krb5`. Clean this up.

Reviewed By: stevegury, vitaut

Differential Revision: D14814205

fbshipit-source-id: dca469d22098e34573674194facaaac6c4c6aa32

#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import specs.folly as folly
import specs.fizz as fizz
import specs.rsocket as rsocket
import specs.sodium as sodium
import specs.wangle as wangle
import specs.zstd as zstd

from shell_quoting import ShellQuoted

def fbcode_builder_spec(builder):
    # This API should change rarely, so build the latest tag instead of master.
    builder.add_option(
        'no1msd/mstch:git_hash',
        ShellQuoted('$(git describe --abbrev=0 --tags)')
    )
    builder.add_option('krb5/krb5:git_hash', 'krb5-1.16.1-final')
    return {
        'depends_on': [folly, fizz, sodium, rsocket, wangle, zstd],
        'steps': [
            # This isn't a separete spec, since only fbthrift uses mstch.
            builder.github_project_workdir('no1msd/mstch', 'build'),
            builder.cmake_install('no1msd/mstch'),
            builder.github_project_workdir('krb5/krb5', 'src'),
            builder.autoconf_install('krb5/krb5'),
            builder.fb_github_cmake_install('fbthrift/thrift'),
        ],
    }


import configobj
import validate
import os

def _get_filename():
    """Return the configuration file path."""
    appname = 'dlstats'
    if os.name == 'posix':
        if "HOME" in os.environ:
            if os.path.isfile(os.environ["HOME"]+'/.'+appname):
                return os.environ["HOME"]+'/.'+appname
        if os.path.isfile('/etc/'+appname):
            return '/etc/'+appname
        else:
            raise FileNotFoundError('No configuration file found.')
    elif os.name == 'mac':
        return ("%s/Library/Application Support/%s" % (os.environ["HOME"], appname))
    elif os.name == 'nt':
        return ("%s\Application Data\%s" % (os.environ["HOMEPATH"], appname))
    else:
        raise UnsupportedOSError(os.name)

configuration_filename = _get_filename()

_configspec = """
[General]
logging_directory = string()
socket_directory = string()
[MongoDB]
host = ip_addr()
port = integer()
max_pool_size = integer()
socketTimeoutMS = integer()
connectTimeoutMS = integer()
waitQueueTimeout = integer()
waitQueueMultiple = integer()
auto_start_request = boolean()
use_greenlets = boolean()
[ElasticSearch]
host = integer()
port = integer()
[Fetchers]
[[Eurostat]]
url_table_of_contents = string()"""
configuration = configobj.ConfigObj(configuration_filename,
                                    configspec=_configspec.split('\n'))
validator = validate.Validator()
configuration.validate(validator)
configuration = configuration.dict()

Test for environment variable existence

Test for environment variable existence

Response:
import configobj
import validate
import os

def _get_filename():
    """Return the configuration file path."""
    appname = 'dlstats'
    if os.name == 'posix':
        if "HOME" in os.environ:
            if os.path.isfile(os.environ["HOME"]+'/.'+appname):
                return os.environ["HOME"]+'/.'+appname
        if os.path.isfile('/etc/'+appname):
            return '/etc/'+appname
        else:
            raise FileNotFoundError('No configuration file found.')
    elif os.name == 'mac':
        return ("%s/Library/Application Support/%s" % (os.environ["HOME"], appname))
    elif os.name == 'nt':
        return ("%s\Application Data\%s" % (os.environ["HOMEPATH"], appname))
    else:
        raise UnsupportedOSError(os.name)

configuration_filename = _get_filename()

_configspec = """
[General]
logging_directory = string()
socket_directory = string()
[MongoDB]
host = ip_addr()
port = integer()
max_pool_size = integer()
socketTimeoutMS = integer()
connectTimeoutMS = integer()
waitQueueTimeout = integer()
waitQueueMultiple = integer()
auto_start_request = boolean()
use_greenlets = boolean()
[ElasticSearch]
host = integer()
port = integer()
[Fetchers]
[[Eurostat]]
url_table_of_contents = string()"""
configuration = configobj.ConfigObj(configuration_filename,
                                    configspec=_configspec.split('\n'))
validator = validate.Validator()
configuration.validate(validator)
configuration = configuration.dict()

Prompt:
Test for environment variable existence

import configobj
import validate
import os

def _get_filename():
    """Return the configuration file path."""
    appname = 'dlstats'
    if os.name == 'posix':
        if os.path.isfile(os.environ["HOME"]+'/.'+appname):
            return os.environ["HOME"]+'/.'+appname
        elif os.path.isfile('/etc/'+appname):
            return '/etc/'+appname
        else:
            raise FileNotFoundError('No configuration file found.')
    elif os.name == 'mac':
        return ("%s/Library/Application Support/%s" % (os.environ["HOME"], appname))
    elif os.name == 'nt':
        return ("%s\Application Data\%s" % (os.environ["HOMEPATH"], appname))
    else:
        raise UnsupportedOSError(os.name)

configuration_filename = _get_filename()

_configspec = """
[General]
logging_directory = string()
socket_directory = string()
[MongoDB]
host = ip_addr()
port = integer()
max_pool_size = integer()
socketTimeoutMS = integer()
connectTimeoutMS = integer()
waitQueueTimeout = integer()
waitQueueMultiple = integer()
auto_start_request = boolean()
use_greenlets = boolean()
[ElasticSearch]
host = integer()
port = integer()
[Fetchers]
[[Eurostat]]
url_table_of_contents = string()"""
configuration = configobj.ConfigObj(configuration_filename,
                                    configspec=_configspec.split('\n'))
validator = validate.Validator()
configuration.validate(validator)
configuration = configuration.dict()


from django.contrib.auth.forms import AuthenticationForm
from django.contrib.sites.models import get_current_site
from django.conf import settings

from haystack.forms import SearchForm
from entities.models import Entity

from oshot.forms import EntityChoiceForm

def forms(request):
    context = {"search_form": SearchForm()}
    try:
        kwargs = request.resolver_match.kwargs
        if 'entity_slug' in kwargs:
            entity = Entity.objects.get(slug=kwargs['entity_slug'])
            initial = {'entity': entity.id}
        elif 'entity_id' in kwargs:
            entity = Entity.objects.get(id=kwargs['entity_id'])
            initial = {'entity': entity.id}
        else:
            initial = {}
        context['entity_form'] = EntityChoiceForm(initial=initial, auto_id=False)
    except AttributeError:
        pass

    if not request.user.is_authenticated():
        context["login_form"] = AuthenticationForm()
    # TODO: remove
    context["site"] = get_current_site(request)
    context["ANALYTICS_ID"] = getattr(settings, 'ANALYTICS_ID', False)
    return context

Clean bug with static file serving

Clean bug with static file serving

Response:
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.sites.models import get_current_site
from django.conf import settings

from haystack.forms import SearchForm
from entities.models import Entity

from oshot.forms import EntityChoiceForm

def forms(request):
    context = {"search_form": SearchForm()}
    try:
        kwargs = request.resolver_match.kwargs
        if 'entity_slug' in kwargs:
            entity = Entity.objects.get(slug=kwargs['entity_slug'])
            initial = {'entity': entity.id}
        elif 'entity_id' in kwargs:
            entity = Entity.objects.get(id=kwargs['entity_id'])
            initial = {'entity': entity.id}
        else:
            initial = {}
        context['entity_form'] = EntityChoiceForm(initial=initial, auto_id=False)
    except AttributeError:
        pass

    if not request.user.is_authenticated():
        context["login_form"] = AuthenticationForm()
    # TODO: remove
    context["site"] = get_current_site(request)
    context["ANALYTICS_ID"] = getattr(settings, 'ANALYTICS_ID', False)
    return context

Prompt:
Clean bug with static file serving

from django.contrib.auth.forms import AuthenticationForm
from django.contrib.sites.models import get_current_site
from django.conf import settings

from haystack.forms import SearchForm
from entities.models import Entity

from oshot.forms import EntityChoiceForm

def forms(request):
    context = {"search_form": SearchForm()}
    kwargs = request.resolver_match.kwargs
    if 'entity_slug' in kwargs:
        entity = Entity.objects.get(slug=kwargs['entity_slug'])
        initial = {'entity': entity.id}
    elif 'entity_id' in kwargs:
        entity = Entity.objects.get(id=kwargs['entity_id'])
        initial = {'entity': entity.id}
    else:
        initial = {}
    context['entity_form'] = EntityChoiceForm(initial=initial, auto_id=False)

    if not request.user.is_authenticated():
        context["login_form"] = AuthenticationForm()
    # TODO: remove
    context["site"] = get_current_site(request)
    context["ANALYTICS_ID"] = getattr(settings, 'ANALYTICS_ID', False)
    return context


#    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 neutron.db.migration.cli import *  # noqa

def main():
    config = alembic_config.Config(
        os.path.join(os.path.dirname(__file__), 'alembic.ini'))
    config.set_main_option(
        'script_location',
        'gbpservice.neutron.db.migration:alembic_migrations')
    config.neutron_config = CONF
    CONF(project='neutron')
    CONF.command.func(config, CONF.command.name)

Response:
#    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 neutron.db.migration.cli import *  # noqa

def main():
    config = alembic_config.Config(
        os.path.join(os.path.dirname(__file__), 'alembic.ini'))
    config.set_main_option(
        'script_location',
        'gbpservice.neutron.db.migration:alembic_migrations')
    config.neutron_config = CONF
    CONF(project='neutron')
    CONF.command.func(config, CONF.command.name)

Prompt:
Set project when doing neutron DB migrations

That way, the default configuration files/dirs from the neutron
projects are read when doing the DB migrations.
This is useful if eg. some configuration files are in
/etc/neutron/neutron.conf.d/ . Theses files will then be automatically
evaluated.

Change-Id: I4997a86c4df5fa45f7682d653a5e66b1ae184a62

#    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 neutron.db.migration.cli import *  # noqa

def main():
    config = alembic_config.Config(
        os.path.join(os.path.dirname(__file__), 'alembic.ini'))
    config.set_main_option(
        'script_location',
        'gbpservice.neutron.db.migration:alembic_migrations')
    config.neutron_config = CONF
    CONF()
    CONF.command.func(config, CONF.command.name)


# vim: tabstop=4 shiftwidth=4 softtabstop=4

"""Main entry point into the S3 Credentials service.

TODO-DOCS
"""

import base64
import hmac

from hashlib import sha1

from keystone import config
from keystone.common import wsgi
from keystone.contrib import ec2

CONF = config.CONF

class S3Extension(wsgi.ExtensionRouter):
    def add_routes(self, mapper):
        controller = S3Controller()
        # validation
        mapper.connect('/s3tokens',
                       controller=controller,
                       action='authenticate',
                       conditions=dict(method=['POST']))

class S3Controller(ec2.Ec2Controller):
    def check_signature(self, creds_ref, credentials):
        msg = base64.urlsafe_b64decode(str(credentials['token']))
        key = str(creds_ref['secret'])
        signed = base64.encodestring(hmac.new(key, msg, sha1).digest()).strip()

        if credentials['signature'] != signed:
            raise Exception("Not Authorized")

Make it as a subclass.

Response:
# vim: tabstop=4 shiftwidth=4 softtabstop=4

"""Main entry point into the S3 Credentials service.

TODO-DOCS
"""

import base64
import hmac

from hashlib import sha1

from keystone import config
from keystone.common import wsgi
from keystone.contrib import ec2

CONF = config.CONF

class S3Extension(wsgi.ExtensionRouter):
    def add_routes(self, mapper):
        controller = S3Controller()
        # validation
        mapper.connect('/s3tokens',
                       controller=controller,
                       action='authenticate',
                       conditions=dict(method=['POST']))

class S3Controller(ec2.Ec2Controller):
    def check_signature(self, creds_ref, credentials):
        msg = base64.urlsafe_b64decode(str(credentials['token']))
        key = str(creds_ref['secret'])
        signed = base64.encodestring(hmac.new(key, msg, sha1).digest()).strip()

        if credentials['signature'] != signed:
            raise Exception("Not Authorized")

Prompt:
Make it as a subclass.

as advised by termie make it as a subclass instead of patching the
method.

# vim: tabstop=4 shiftwidth=4 softtabstop=4

"""Main entry point into the S3 Credentials service.

TODO-DOCS
"""

import base64
import hmac

from hashlib import sha1

from keystone import config
from keystone.common import wsgi
from keystone.contrib import ec2

CONF = config.CONF

def check_signature(creds_ref, credentials):
    signature = credentials['signature']
    msg = base64.urlsafe_b64decode(str(credentials['token']))
    key = str(creds_ref['secret'])
    signed = base64.encodestring(hmac.new(key, msg, sha1).digest()).strip()

    if signature == signed:
        pass
    else:
        raise Exception("Not Authorized")

class S3Extension(wsgi.ExtensionRouter):
    def add_routes(self, mapper):
        controller = ec2.Ec2Controller()
        controller.check_signature = check_signature
        # validation
        mapper.connect('/s3tokens',
                       controller=controller,
                       action='authenticate',
                       conditions=dict(method=['POST']))

        # No need CRUD stuff since we are sharing keystone.contrib.ec2
        # infos.


import json

from oic.oauth2.message import ErrorResponse

from .util import should_fragment_encode

class BearerTokenError(ValueError):
    pass

class InvalidAuthorizationCode(ValueError):
    pass

class InvalidAccessToken(ValueError):
    pass

class InvalidRefreshToken(ValueError):
    pass

class InvalidSubjectIdentifier(ValueError):
    pass

class InvalidScope(ValueError):
    pass

class InvalidClientAuthentication(ValueError):
    pass

class InvalidAuthenticationRequest(ValueError):
    def __init__(self, message, parsed_request, oauth_error=None):
        super().__init__(message)
        self.request = parsed_request
        self.oauth_error = oauth_error

    def to_error_url(self):
        redirect_uri = self.request.get('redirect_uri')
        if redirect_uri and self.oauth_error:
            error_resp = ErrorResponse(error=self.oauth_error, error_message=str(self))
            return error_resp.request(redirect_uri, should_fragment_encode(self.request))

        return None

class AuthorizationError(Exception):
    pass

class InvalidTokenRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error

class InvalidUserinfoRequest(ValueError):
    pass

class InvalidClientRegistrationRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error

    def to_json(self):
        error = {'error': self.oauth_error, 'error_description': str(self)}
        return json.dumps(error)

Response:
import json

from oic.oauth2.message import ErrorResponse

from .util import should_fragment_encode

class BearerTokenError(ValueError):
    pass

class InvalidAuthorizationCode(ValueError):
    pass

class InvalidAccessToken(ValueError):
    pass

class InvalidRefreshToken(ValueError):
    pass

class InvalidSubjectIdentifier(ValueError):
    pass

class InvalidScope(ValueError):
    pass

class InvalidClientAuthentication(ValueError):
    pass

class InvalidAuthenticationRequest(ValueError):
    def __init__(self, message, parsed_request, oauth_error=None):
        super().__init__(message)
        self.request = parsed_request
        self.oauth_error = oauth_error

    def to_error_url(self):
        redirect_uri = self.request.get('redirect_uri')
        if redirect_uri and self.oauth_error:
            error_resp = ErrorResponse(error=self.oauth_error, error_message=str(self))
            return error_resp.request(redirect_uri, should_fragment_encode(self.request))

        return None

class AuthorizationError(Exception):
    pass

class InvalidTokenRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error

class InvalidUserinfoRequest(ValueError):
    pass

class InvalidClientRegistrationRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error

    def to_json(self):
        error = {'error': self.oauth_error, 'error_description': str(self)}
        return json.dumps(error)

Prompt:
Add JSON convenience to InvalidRegistrationRequest.

from oic.oauth2.message import ErrorResponse

from .util import should_fragment_encode

class BearerTokenError(ValueError):
    pass

class InvalidAuthorizationCode(ValueError):
    pass

class InvalidAccessToken(ValueError):
    pass

class InvalidRefreshToken(ValueError):
    pass

class InvalidSubjectIdentifier(ValueError):
    pass

class InvalidScope(ValueError):
    pass

class InvalidClientAuthentication(ValueError):
    pass

class InvalidAuthenticationRequest(ValueError):
    def __init__(self, message, parsed_request, oauth_error=None):
        super().__init__(message)
        self.request = parsed_request
        self.oauth_error = oauth_error

    def to_error_url(self):
        redirect_uri = self.request.get('redirect_uri')
        if redirect_uri and self.oauth_error:
            error_resp = ErrorResponse(error=self.oauth_error, error_message=str(self))
            return error_resp.request(redirect_uri, should_fragment_encode(self.request))

        return None

class AuthorizationError(Exception):
    pass

class InvalidTokenRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error

class InvalidUserinfoRequest(ValueError):
    pass

class InvalidClientRegistrationRequest(ValueError):
    def __init__(self, message, oauth_error='invalid_request'):
        super().__init__(message)
        self.oauth_error = oauth_error


import sublime, sublime_plugin

class SumCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sum_view = self.view.window().new_file()
        sum_view.set_name('Sum')

        file_text = self.view.substr(sublime.Region(0, self.view.size()))

        numbers = []
        for s in file_text.split():
            if s.isdigit():
                numbers.append(int(s))
            else:
                try:
                    numbers.append(float(s))
                except ValueError:
                    pass

        result = sum(numbers)
        sum_view.insert(edit, 0, str(result))

        sum_view.set_read_only(True)
        sum_view.set_scratch(True)

Response:
import sublime, sublime_plugin

class SumCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sum_view = self.view.window().new_file()
        sum_view.set_name('Sum')

        file_text = self.view.substr(sublime.Region(0, self.view.size()))

        numbers = []
        for s in file_text.split():
            if s.isdigit():
                numbers.append(int(s))
            else:
                try:
                    numbers.append(float(s))
                except ValueError:
                    pass

        result = sum(numbers)
        sum_view.insert(edit, 0, str(result))

        sum_view.set_read_only(True)
        sum_view.set_scratch(True)

Prompt:
Add up all ints (base 10) and floats in the file

import sublime, sublime_plugin

class SumCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sum_view = self.view.window().new_file()
        sum_view.set_name('Sum')

        file_text = self.view.substr(sublime.Region(0, self.view.size()))
        sum_view.insert(edit, 0, file_text)

        sum_view.set_read_only(True)
        sum_view.set_scratch(True)


"""Tests for the WebSocketSubscriber handlers."""

import json

from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.websocket import websocket_connect
from tornado.testing import AsyncHTTPTestCase, gen_test

from tornadose.handlers import WebSocketSubscriber
import utilities

class WebSocketSubscriberTestCase(AsyncHTTPTestCase):
    def setUp(self):
        self.store = utilities.TestStore()
        super(WebSocketSubscriberTestCase, self).setUp()

    def get_app(self):
        return Application([
            (r'/', WebSocketSubscriber, dict(store=self.store))
        ])

    @gen_test
    def test_get_message(self):
        url = self.get_url('/').replace("http://", "ws://")
        conn = yield websocket_connect(url)
        self.store.submit('test')
        IOLoop.current().call_later(0.01, self.store.publish)
        msg = yield conn.read_message()
        msg = json.loads(msg)
        self.assertEqual(msg['data'], 'test')
        conn.close()

Fix test case for WebSocketSubscriber

Response:
"""Tests for the WebSocketSubscriber handlers."""

import json

from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.websocket import websocket_connect
from tornado.testing import AsyncHTTPTestCase, gen_test

from tornadose.handlers import WebSocketSubscriber
import utilities

class WebSocketSubscriberTestCase(AsyncHTTPTestCase):
    def setUp(self):
        self.store = utilities.TestStore()
        super(WebSocketSubscriberTestCase, self).setUp()

    def get_app(self):
        return Application([
            (r'/', WebSocketSubscriber, dict(store=self.store))
        ])

    @gen_test
    def test_get_message(self):
        url = self.get_url('/').replace("http://", "ws://")
        conn = yield websocket_connect(url)
        self.store.submit('test')
        IOLoop.current().call_later(0.01, self.store.publish)
        msg = yield conn.read_message()
        msg = json.loads(msg)
        self.assertEqual(msg['data'], 'test')
        conn.close()

Prompt:
Fix test case for WebSocketSubscriber

Switched to unittest-style testing (pytest is a bit too magical
especially with the pytest-tornado extension). I may change all
tests later to use unittest.

"""Tests for the WebSocketSubscriber handlers."""

import json
import pytest

from tornado.web import Application
from tornado.websocket import websocket_connect

from tornadose.handlers import WebSocketSubscriber
import utilities

@pytest.fixture
def store():
    return utilities.TestStore()

@pytest.fixture
def app():
    return Application([
        (r'/', WebSocketSubscriber, dict(store=store))
    ])

@pytest.mark.gen_test
def test_get_message(http_server, io_loop, base_url, store):
    conn = yield websocket_connect('ws' + base_url.split('http')[1])
    store.submit('test')
    io_loop.call_later(0.01, store.publish)
    msg = yield conn.read_message()
    msg = json.loads(msg)
    assert msg['data'] == 'test'
    conn.close()


import os

import tornado.web
import tornado.ioloop

class SingleStatic(tornado.web.StaticFileHandler):
    def initialize(self, path):
        self.dirname, self.filename = os.path.split(path)
        super(SingleStatic, self).initialize(self.dirname)

    def get(self, path=None, include_body=True):
        super(SingleStatic, self).get(self.filename, include_body)

Add a view that serves a single static file

Response:
import os

import tornado.web
import tornado.ioloop

class SingleStatic(tornado.web.StaticFileHandler):
    def initialize(self, path):
        self.dirname, self.filename = os.path.split(path)
        super(SingleStatic, self).initialize(self.dirname)

    def get(self, path=None, include_body=True):
        super(SingleStatic, self).get(self.filename, include_body)

Prompt: Add a view that serves a single static file


#!/bin/env python
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
from time import sleep
from getpass import getpass

if __name__ == '__main__':
    driver = webdriver.phantomjs.webdriver.WebDriver()
    driver.get('https://facebook.com')
    driver.find_element_by_id('email').send_keys(input('Email: '))
    driver.find_element_by_id('pass').send_keys(getpass())
    driver.find_element_by_id('loginbutton').click()
    driver.get('https://facebook.com/pokes/')
    assert "Forgot password?" not in driver.page_source

    c = 0
    while True:
        try:
            for i in driver.find_elements_by_link_text("Poke Back"):
                i.click()
                c += 1
                print("Clicked so far: " + str(c))
        except StaleElementReferenceException:
            print("Found exception, reloading page")
            driver.get('https://facebook.com/pokes/')

        sleep(0.5)

Add notice on page reload

Add notice on page reload

Response:
#!/bin/env python
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
from time import sleep
from getpass import getpass

if __name__ == '__main__':
    driver = webdriver.phantomjs.webdriver.WebDriver()
    driver.get('https://facebook.com')
    driver.find_element_by_id('email').send_keys(input('Email: '))
    driver.find_element_by_id('pass').send_keys(getpass())
    driver.find_element_by_id('loginbutton').click()
    driver.get('https://facebook.com/pokes/')
    assert "Forgot password?" not in driver.page_source

    c = 0
    while True:
        try:
            for i in driver.find_elements_by_link_text("Poke Back"):
                i.click()
                c += 1
                print("Clicked so far: " + str(c))
        except StaleElementReferenceException:
            print("Found exception, reloading page")
            driver.get('https://facebook.com/pokes/')

        sleep(0.5)

Prompt:
Add notice on page reload

#!/bin/env python
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
from time import sleep
from getpass import getpass

if __name__ == '__main__':
    driver = webdriver.phantomjs.webdriver.WebDriver()
    driver.get('https://facebook.com')
    driver.find_element_by_id('email').send_keys(input('Email: '))
    driver.find_element_by_id('pass').send_keys(getpass())
    driver.find_element_by_id('loginbutton').click()
    driver.get('https://facebook.com/pokes/')
    assert "Forgot password?" not in driver.page_source

    c = 0
    c2 = 0
    while True:
        try:
            for i in driver.find_elements_by_link_text("Poke Back"):
                i.click()
                c += 1
                print("Clicked so far: " + str(c))
        except StaleElementReferenceException:
            driver.get('https://facebook.com/pokes/')

        sleep(0.5)


#!/usr/bin/env python
"""Script to transform letsencrypt certificate files into string that can be inserted into
environment variable for Tutum to pick up.

Put your private key in certs/privkey.pem and your certificate in certs/fullchain.pem,
then run this script in order to obtain a certificate string compatible with HAProxy and ready
for inserting into an environment variable.
"""
import re

with open('certs/privkey.pem', 'rb') as f:
    private_key = f.read().replace('\n', '\\n')

with open('certs/fullchain.pem', 'rb') as f:
    certificate = f.read().replace('\n', '\\n')

print("""{}{}""".format(private_key, certificate))

Add script for transforming letsencrypt certificate

Response:
#!/usr/bin/env python
"""Script to transform letsencrypt certificate files into string that can be inserted into
environment variable for Tutum to pick up.

Put your private key in certs/privkey.pem and your certificate in certs/fullchain.pem,
then run this script in order to obtain a certificate string compatible with HAProxy and ready
for inserting into an environment variable.
"""
import re

with open('certs/privkey.pem', 'rb') as f:
    private_key = f.read().replace('\n', '\\n')

with open('certs/fullchain.pem', 'rb') as f:
    certificate = f.read().replace('\n', '\\n')

print("""{}{}""".format(private_key, certificate))

Prompt: Add script for transforming letsencrypt certificate


from setuptools import setup, find_packages

setup(
    name='weaveserver',
    version='0.8',
    author='Srivatsan Iyer',
    author_email='supersaiyanmode.rox@gmail.com',
    packages=find_packages(),
    license='MIT',
    description='Library to interact with Weave Server',
    long_description=open('README.md').read(),
    install_requires=[
        'weavelib',
        'eventlet!=0.22',
        'bottle',
        'GitPython',
        'redis',
        'appdirs',
        'peewee',
    ],
    entry_points={
        'console_scripts': [
            'weave-launch = app:handle_launch',
            'weave-main = app:handle_main'
        ]
    }
)

Response:
from setuptools import setup, find_packages

setup(
    name='weaveserver',
    version='0.8',
    author='Srivatsan Iyer',
    author_email='supersaiyanmode.rox@gmail.com',
    packages=find_packages(),
    license='MIT',
    description='Library to interact with Weave Server',
    long_description=open('README.md').read(),
    install_requires=[
        'weavelib',
        'eventlet!=0.22',
        'bottle',
        'GitPython',
        'redis',
        'appdirs',
        'peewee',
    ],
    entry_points={
        'console_scripts': [
            'weave-launch = app:handle_launch',
            'weave-main = app:handle_main'
        ]
    }
)

Prompt:
Remove sqlite3 package. Its available by default in most python distributions.

from setuptools import setup, find_packages

setup(
    name='weaveserver',
    version='0.8',
    author='Srivatsan Iyer',
    author_email='supersaiyanmode.rox@gmail.com',
    packages=find_packages(),
    license='MIT',
    description='Library to interact with Weave Server',
    long_description=open('README.md').read(),
    install_requires=[
        'weavelib',
        'eventlet!=0.22',
        'bottle',
        'GitPython',
        'redis',
        'sqlite3',
        'appdirs',
        'peewee',
    ],
    entry_points={
        'console_scripts': [
            'weave-launch = app:handle_launch',
            'weave-main = app:handle_main'
        ]
    }
)


# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__

"""
This app may define some new parameters that can be modified in the
Django settings module.  Let's say one such parameter is XGDS_DATA_FOO.
The default value for XGDS_DATA_FOO is defined in this file, like this:

  XGDS_DATA_FOO = 'my default value'

If the admin for the site doesn't like the default value, they can
override it in the site-level settings module, like this:

  XGDS_DATA_FOO = 'a better value'

Other modules can access the value of FOO like this:

  from xgds_data import settings
  print settings.XGDS_DATA_FOO

Don't try to get the value of XGDS_DATA_FOO from django.conf.settings.
That settings object will not know about the default value!
"""

# choose models to support in siteSettings.py. mostly obsolete.
XGDS_DATA_SEARCH_MODELS = ()

# choose django apps not to list for search purposes
XGDS_DATA_SEARCH_SKIP_APP_PATTERNS = (
    r'^django\..*',
    r'^geocam.*',
    r'^pipeline$',
)

# XGDS_DATA_LOG_ENABLED = False

Move log enabling out of submodule

Move log enabling out of submodule

Response:
# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__

"""
This app may define some new parameters that can be modified in the
Django settings module.  Let's say one such parameter is XGDS_DATA_FOO.
The default value for XGDS_DATA_FOO is defined in this file, like this:

  XGDS_DATA_FOO = 'my default value'

If the admin for the site doesn't like the default value, they can
override it in the site-level settings module, like this:

  XGDS_DATA_FOO = 'a better value'

Other modules can access the value of FOO like this:

  from xgds_data import settings
  print settings.XGDS_DATA_FOO

Don't try to get the value of XGDS_DATA_FOO from django.conf.settings.
That settings object will not know about the default value!
"""

# choose models to support in siteSettings.py. mostly obsolete.
XGDS_DATA_SEARCH_MODELS = ()

# choose django apps not to list for search purposes
XGDS_DATA_SEARCH_SKIP_APP_PATTERNS = (
    r'^django\..*',
    r'^geocam.*',
    r'^pipeline$',
)

# XGDS_DATA_LOG_ENABLED = False

Prompt:
Move log enabling out of submodule

# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__

"""
This app may define some new parameters that can be modified in the
Django settings module.  Let's say one such parameter is XGDS_DATA_FOO.
The default value for XGDS_DATA_FOO is defined in this file, like this:

  XGDS_DATA_FOO = 'my default value'

If the admin for the site doesn't like the default value, they can
override it in the site-level settings module, like this:

  XGDS_DATA_FOO = 'a better value'

Other modules can access the value of FOO like this:

  from xgds_data import settings
  print settings.XGDS_DATA_FOO

Don't try to get the value of XGDS_DATA_FOO from django.conf.settings.
That settings object will not know about the default value!
"""

# choose models to support in siteSettings.py. mostly obsolete.
XGDS_DATA_SEARCH_MODELS = ()

# choose django apps not to list for search purposes
XGDS_DATA_SEARCH_SKIP_APP_PATTERNS = (
    r'^django\..*',
    r'^geocam.*',
    r'^pipeline$',
)

XGDS_DATA_LOG_ENABLED = False


from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import RedirectView
from django.contrib.auth.views import login, logout_then_login
from keyform import views

urlpatterns = [
    url(r'^$', views.HomeView.as_view(), name='home'),
    url(r'^table.php$', RedirectView.as_view(pattern_name='home', permanent=True)),
    url(r'^contact$', views.ContactView.as_view(), name='contact'),
    url(r'^edit-contact/(?P<pk>\d+)$', views.EditContactView.as_view(), name='edit-contact'),
    url(r'^create-contact$', views.NewContactView.as_view(), name='create-contact'),
    url(r'^edit-request/(?P<pk>\d+)$', views.RequestView.as_view(), name='edit-request'),
    url(r'^create$', views.KeyRequest.as_view(), name='create'),
    url(r'^add-comment$', views.RequestCommentView.as_view(), name='add-comment'),
    url(r'^login$', login, name='login', kwargs={'template_name': 'keyform/login.html'}),
    url(r'^logout$', logout_then_login, name='logout'),
]

Add redirect for old hotlinks

Add redirect for old hotlinks

Response:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import RedirectView
from django.contrib.auth.views import login, logout_then_login
from keyform import views

urlpatterns = [
    url(r'^$', views.HomeView.as_view(), name='home'),
    url(r'^table.php$', RedirectView.as_view(pattern_name='home', permanent=True)),
    url(r'^contact$', views.ContactView.as_view(), name='contact'),
    url(r'^edit-contact/(?P<pk>\d+)$', views.EditContactView.as_view(), name='edit-contact'),
    url(r'^create-contact$', views.NewContactView.as_view(), name='create-contact'),
    url(r'^edit-request/(?P<pk>\d+)$', views.RequestView.as_view(), name='edit-request'),
    url(r'^create$', views.KeyRequest.as_view(), name='create'),
    url(r'^add-comment$', views.RequestCommentView.as_view(), name='add-comment'),
    url(r'^login$', login, name='login', kwargs={'template_name': 'keyform/login.html'}),
    url(r'^logout$', logout_then_login, name='logout'),
]

Prompt:
Add redirect for old hotlinks

from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth.views import login, logout_then_login
from keyform import views

urlpatterns = [
    url(r'^$', views.HomeView.as_view(), name='home'),
    url(r'^contact$', views.ContactView.as_view(), name='contact'),
    url(r'^edit-contact/(?P<pk>\d+)$', views.EditContactView.as_view(), name='edit-contact'),
    url(r'^create-contact$', views.NewContactView.as_view(), name='create-contact'),
    url(r'^edit-request/(?P<pk>\d+)$', views.RequestView.as_view(), name='edit-request'),
    url(r'^create$', views.KeyRequest.as_view(), name='create'),
    url(r'^add-comment$', views.RequestCommentView.as_view(), name='add-comment'),
    url(r'^login$', login, name='login', kwargs={'template_name': 'keyform/login.html'}),
    url(r'^logout$', logout_then_login, name='logout'),
]


import numpy as np
import copperhead.compiler.backendtypes as ET
import copperhead.compiler.coretypes as T
from copperhead.compiler.conversions import back_to_front_type

def to_numpy(ary):
    front_type = back_to_front_type(ary.type)
    if not isinstance(front_type, T.Seq):
        raise ValueError("Not convertible to numpy")
    sub = front_type.unbox()
    if str(sub) == str(T.Int):
        return np.fromiter(ary, dtype=np.int32, count=-1)
    elif str(sub) == str(T.Long):
        return np.fromiter(ary, dtype=np.int64, count=-1)
    elif str(sub) == str(T.Float):
        return np.fromiter(ary, dtype=np.float32, count=-1)
    elif str(sub) == str(T.Double):
        return np.fromiter(ary, dtype=np.float64, count=-1)
    elif str(sub) == str(T.Bool):
        return np.fromiter(ary, dtype=np.bool, count=-1)
    else:
        raise ValueError("Not convertible to numpy")

Add conversion function between cuarray and np.ndarray.

Response:
import numpy as np
import copperhead.compiler.backendtypes as ET
import copperhead.compiler.coretypes as T
from copperhead.compiler.conversions import back_to_front_type

def to_numpy(ary):
    front_type = back_to_front_type(ary.type)
    if not isinstance(front_type, T.Seq):
        raise ValueError("Not convertible to numpy")
    sub = front_type.unbox()
    if str(sub) == str(T.Int):
        return np.fromiter(ary, dtype=np.int32, count=-1)
    elif str(sub) == str(T.Long):
        return np.fromiter(ary, dtype=np.int64, count=-1)
    elif str(sub) == str(T.Float):
        return np.fromiter(ary, dtype=np.float32, count=-1)
    elif str(sub) == str(T.Double):
        return np.fromiter(ary, dtype=np.float64, count=-1)
    elif str(sub) == str(T.Bool):
        return np.fromiter(ary, dtype=np.bool, count=-1)
    else:
        raise ValueError("Not convertible to numpy")

Prompt: Add conversion function between cuarray and np.ndarray.


# This file is part of the File Deduper project. It is subject to
# the the revised 3-clause BSD license terms as set out in the LICENSE
# file found in the top-level directory of this distribution. No part of this
# project, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE fileself.
import os

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

from .models import ImageFile
from .util import Util

def ScanFiles(session, FOLDER):
    for root, dirs, files in os.walk(FOLDER):
        if root.split('/')[-1].startswith('.'):
            continue

        for count, filename in enumerate(files, start=1):
            if filename.startswith('.'):
                continue

            fullpath = os.path.join(root, filename)

            if not os.path.isfile(fullpath):
                continue

            if Util.file_record_exists(session, fullpath):
                print('{count} of {length}: Skipping {filename}'.format(
                    count=count, length=len(files), filename=filename))
            else:
                print('{count} of {length}: Processing {filename}'.format(
                    count=count, length=len(files), filename=filename))
                new_file = ImageFile(name=filename, fullpath=fullpath,
                    filehash=Util.hash_file(fullpath))
                session.add(new_file)
                session.commit()

    session.close()

Response:
# This file is part of the File Deduper project. It is subject to
# the the revised 3-clause BSD license terms as set out in the LICENSE
# file found in the top-level directory of this distribution. No part of this
# project, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE fileself.
import os

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

from .models import ImageFile
from .util import Util

def ScanFiles(session, FOLDER):
    for root, dirs, files in os.walk(FOLDER):
        if root.split('/')[-1].startswith('.'):
            continue

        for count, filename in enumerate(files, start=1):
            if filename.startswith('.'):
                continue

            fullpath = os.path.join(root, filename)

            if not os.path.isfile(fullpath):
                continue

            if Util.file_record_exists(session, fullpath):
                print('{count} of {length}: Skipping {filename}'.format(
                    count=count, length=len(files), filename=filename))
            else:
                print('{count} of {length}: Processing {filename}'.format(
                    count=count, length=len(files), filename=filename))
                new_file = ImageFile(name=filename, fullpath=fullpath,
                    filehash=Util.hash_file(fullpath))
                session.add(new_file)
                session.commit()

    session.close()

Prompt:
Check that fullpath is a regular file before continuing

# This file is part of the File Deduper project. It is subject to
# the the revised 3-clause BSD license terms as set out in the LICENSE
# file found in the top-level directory of this distribution. No part of this
# project, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE fileself.
import os

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

from .models import ImageFile
from .util import Util

def ScanFiles(session, FOLDER):
    for root, dirs, files in os.walk(FOLDER):
        if root.split('/')[-1].startswith('.'):
            continue

        for count, filename in enumerate(files, start=1):
            if filename.startswith('.'):
                continue

            fullpath = os.path.join(root, filename)

            if Util.file_record_exists(session, fullpath):
                print('{count} of {length}: Skipping {filename}'.format(
                    count=count, length=len(files), filename=filename))
            else:
                print('{count} of {length}: Processing {filename}'.format(
                    count=count, length=len(files), filename=filename))
                new_file = ImageFile(name=filename, fullpath=fullpath,
                    filehash=Util.hash_file(fullpath))
                session.add(new_file)
                session.commit()

    session.close()