# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import google.auth

EXPECT_PROJECT_ID = os.environ.get('EXPECT_PROJECT_ID')

def test_application_default_credentials(verify_refresh):
    credentials, project_id = google.auth.default()

    if EXPECT_PROJECT_ID is not None:
        assert project_id is not None

    verify_refresh(credentials)

Fix system tests when running on GCE

Response:
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import google.auth

EXPECT_PROJECT_ID = os.environ.get('EXPECT_PROJECT_ID')

def test_application_default_credentials(verify_refresh):
    credentials, project_id = google.auth.default()

    if EXPECT_PROJECT_ID is not None:
        assert project_id is not None

    verify_refresh(credentials)

Prompt:
Fix system tests when running on GCE

The new project ID logic for Cloud SDK invokes Cloud SDK directly. Cloud SDK helpfully falls back to the GCE project ID if the project ID is unset in the configuration. This breaks one of our previous expectations.

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import google.auth

EXPECT_PROJECT_ID = os.environ.get('EXPECT_PROJECT_ID')

def test_application_default_credentials(verify_refresh):
    credentials, project_id = google.auth.default()

    if EXPECT_PROJECT_ID is not None:
        assert project_id is not None
    else:
        assert project_id is None

    verify_refresh(credentials)


import logging
import os

from twisted.web import server, guard, resource
from twisted.cred import portal

from lbrynet import conf
from .auth import PasswordChecker, HttpPasswordRealm
from .util import initialize_api_key_file

log = logging.getLogger(__name__)

class AuthJSONRPCResource(resource.Resource):
    def __init__(self, protocol):
        resource.Resource.__init__(self)
        self.putChild(b"", protocol)
        self.putChild(conf.settings['API_ADDRESS'].encode(), protocol)

    def getChild(self, name, request):
        request.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
        request.setHeader('expires', '0')
        return self if name == '' else resource.Resource.getChild(self, name, request)

    def getServerFactory(self):
        if conf.settings['use_auth_http']:
            log.info("Using authenticated API")
            pw_path = os.path.join(conf.settings['data_dir'], ".api_keys")
            initialize_api_key_file(pw_path)
            checker = PasswordChecker.load_file(pw_path)
            realm = HttpPasswordRealm(self)
            portal_to_realm = portal.Portal(realm, [checker, ])
            factory = guard.BasicCredentialFactory('Login to lbrynet api')
            root = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ])
        else:
            log.info("Using non-authenticated API")
            root = self
        return server.Site(root)

Make curl work in py3 again

Make curl work in py3 again

Response:
import logging
import os

from twisted.web import server, guard, resource
from twisted.cred import portal

from lbrynet import conf
from .auth import PasswordChecker, HttpPasswordRealm
from .util import initialize_api_key_file

log = logging.getLogger(__name__)

class AuthJSONRPCResource(resource.Resource):
    def __init__(self, protocol):
        resource.Resource.__init__(self)
        self.putChild(b"", protocol)
        self.putChild(conf.settings['API_ADDRESS'].encode(), protocol)

    def getChild(self, name, request):
        request.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
        request.setHeader('expires', '0')
        return self if name == '' else resource.Resource.getChild(self, name, request)

    def getServerFactory(self):
        if conf.settings['use_auth_http']:
            log.info("Using authenticated API")
            pw_path = os.path.join(conf.settings['data_dir'], ".api_keys")
            initialize_api_key_file(pw_path)
            checker = PasswordChecker.load_file(pw_path)
            realm = HttpPasswordRealm(self)
            portal_to_realm = portal.Portal(realm, [checker, ])
            factory = guard.BasicCredentialFactory('Login to lbrynet api')
            root = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ])
        else:
            log.info("Using non-authenticated API")
            root = self
        return server.Site(root)

Prompt:
Make curl work in py3 again

import logging
import os

from twisted.web import server, guard, resource
from twisted.cred import portal

from lbrynet import conf
from .auth import PasswordChecker, HttpPasswordRealm
from .util import initialize_api_key_file

log = logging.getLogger(__name__)

class AuthJSONRPCResource(resource.Resource):
    def __init__(self, protocol):
        resource.Resource.__init__(self)
        self.putChild("", protocol)
        self.putChild(conf.settings['API_ADDRESS'], protocol)

    def getChild(self, name, request):
        request.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
        request.setHeader('expires', '0')
        return self if name == '' else resource.Resource.getChild(self, name, request)

    def getServerFactory(self):
        if conf.settings['use_auth_http']:
            log.info("Using authenticated API")
            pw_path = os.path.join(conf.settings['data_dir'], ".api_keys")
            initialize_api_key_file(pw_path)
            checker = PasswordChecker.load_file(pw_path)
            realm = HttpPasswordRealm(self)
            portal_to_realm = portal.Portal(realm, [checker, ])
            factory = guard.BasicCredentialFactory('Login to lbrynet api')
            root = guard.HTTPAuthSessionWrapper(portal_to_realm, [factory, ])
        else:
            log.info("Using non-authenticated API")
            root = self
        return server.Site(root)


# -*- coding: utf-8 -*-
from distutils.core import setup

setup(
    name='oceanoptics',
    version='0.2.7',
    author='Andreas Poehlmann, Jose A. Jimenez-Berni, Ben Gamari, Simon Dickreuter, Ian Ross Williams',
    author_email='mail@andreaspoehlmann.de',
    packages=['oceanoptics', 'oceanoptics.spectrometers'],
    description='A Python driver for Ocean Optics spectrometers.',
    long_description=open('README.md').read(),
    requires=['python (>= 2.7)', 'pyusb (>= 1.0)', 'numpy'],
)

Add author and increase version number

Add author and increase version number

Response:
# -*- coding: utf-8 -*-
from distutils.core import setup

setup(
    name='oceanoptics',
    version='0.2.7',
    author='Andreas Poehlmann, Jose A. Jimenez-Berni, Ben Gamari, Simon Dickreuter, Ian Ross Williams',
    author_email='mail@andreaspoehlmann.de',
    packages=['oceanoptics', 'oceanoptics.spectrometers'],
    description='A Python driver for Ocean Optics spectrometers.',
    long_description=open('README.md').read(),
    requires=['python (>= 2.7)', 'pyusb (>= 1.0)', 'numpy'],
)

Prompt:
Add author and increase version number

# -*- coding: utf-8 -*-
from distutils.core import setup

setup(
    name='oceanoptics',
    version='0.2.6',
    author='Andreas Poehlmann, Jose A. Jimenez-Berni, Ben Gamari, Simon Dickreuter',
    author_email='mail@andreaspoehlmann.de',
    packages=['oceanoptics', 'oceanoptics.spectrometers'],
    description='A Python driver for Ocean Optics spectrometers.',
    long_description=open('README.md').read(),
    requires=['python (>= 2.7)', 'pyusb (>= 1.0)', 'numpy'],
)


# tests.test_set_pref

import nose.tools as nose
import yvs.set_pref as yvs

def test_set_language():
    """should set preferred language"""
    new_language = 'es'
    yvs.main('language:{}'.format(new_language))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['language'], new_language)
    bible = yvs.shared.get_bible_data(user_prefs['language'])
    nose.assert_equal(user_prefs['version'], bible['default_version'])

def test_set_version():
    """should set preferred version"""
    new_version = 59
    yvs.main('version:{}'.format(new_version))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['version'], new_version)

def test_set_search_engine():
    """should set preferred search engine"""
    new_search_engine = 'yahoo'
    yvs.main('searchEngine:{}'.format(new_search_engine))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['searchEngine'], new_search_engine)

Response:
# tests.test_set_pref

import nose.tools as nose
import yvs.set_pref as yvs

def test_set_language():
    """should set preferred language"""
    new_language = 'es'
    yvs.main('language:{}'.format(new_language))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['language'], new_language)
    bible = yvs.shared.get_bible_data(user_prefs['language'])
    nose.assert_equal(user_prefs['version'], bible['default_version'])

def test_set_version():
    """should set preferred version"""
    new_version = 59
    yvs.main('version:{}'.format(new_version))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['version'], new_version)

def test_set_search_engine():
    """should set preferred search engine"""
    new_search_engine = 'yahoo'
    yvs.main('searchEngine:{}'.format(new_search_engine))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['searchEngine'], new_search_engine)

Prompt:
Add test for setting preferred search engine

# tests.test_set_pref

import nose.tools as nose
import yvs.set_pref as yvs

def test_set_language():
    """should set preferred language"""
    new_language = 'es'
    yvs.main('language:{}'.format(new_language))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['language'], new_language)
    bible = yvs.shared.get_bible_data(user_prefs['language'])
    nose.assert_equal(user_prefs['version'], bible['default_version'])

def test_set_version():
    """should set preferred version"""
    new_version = 59
    yvs.main('version:{}'.format(new_version))
    user_prefs = yvs.shared.get_user_prefs()
    nose.assert_equal(user_prefs['version'], new_version)


import json

class Database(dict):
    """Holds a dict that contains all the information about the users in a channel"""
    def __init__(self, irc):
        super(Database, self).__init__(json.load(open("userdb.json")))
        self.irc = irc

    def remove_entry(self, event, nick):
        try:
            del self[event.target][nick]
        except KeyError:
            for i in self[event.target].values():
                if i['host'] == event.source.host:
                    del self[event.target][i['hostmask'].split("!")[0]]
                    break

    def add_entry(self, channel, nick, hostmask, account):
        temp = {
            'hostmask': hostmask,
            'host': hostmask.split("@")[1],
            'account': account,
            'seen': [__import__("time").time(), ""]
        }
        failed = False
        try:
            user = self[channel][nick]
        except KeyError:
            failed = True
            self[channel][nick] = temp

        if not failed:
            del temp['seen']
            user.update(temp)

    def get_user_host(self, channel, nick):
        try:
            host = "*!*@" + self[channel][nick]['host']
        except KeyError:
            self.irc.send("WHO {0} nuhs%nhuac".format(channel))
            host = "*!*@" + self[channel][nick]['host']
        return host

    def flush(self):
        with open('userdb.json', 'w') as f:
            json.dump(self, f, indent=2, separators=(',', ': '))
            f.write("\n")

Response:
import json

class Database(dict):
    """Holds a dict that contains all the information about the users in a channel"""
    def __init__(self, irc):
        super(Database, self).__init__(json.load(open("userdb.json")))
        self.irc = irc

    def remove_entry(self, event, nick):
        try:
            del self[event.target][nick]
        except KeyError:
            for i in self[event.target].values():
                if i['host'] == event.source.host:
                    del self[event.target][i['hostmask'].split("!")[0]]
                    break

    def add_entry(self, channel, nick, hostmask, account):
        temp = {
            'hostmask': hostmask,
            'host': hostmask.split("@")[1],
            'account': account,
            'seen': [__import__("time").time(), ""]
        }
        failed = False
        try:
            user = self[channel][nick]
        except KeyError:
            failed = True
            self[channel][nick] = temp

        if not failed:
            del temp['seen']
            user.update(temp)

    def get_user_host(self, channel, nick):
        try:
            host = "*!*@" + self[channel][nick]['host']
        except KeyError:
            self.irc.send("WHO {0} nuhs%nhuac".format(channel))
            host = "*!*@" + self[channel][nick]['host']
        return host

    def flush(self):
        with open('userdb.json', 'w') as f:
            json.dump(self, f, indent=2, separators=(',', ': '))
            f.write("\n")

Prompt:
Add new-line at EOF, when dumping userdb

import json

class Database(dict):
    """Holds a dict that contains all the information about the users in a channel"""
    def __init__(self, irc):
        super(Database, self).__init__(json.load(open("userdb.json")))
        self.irc = irc

    def remove_entry(self, event, nick):
        try:
            del self[event.target][nick]
        except KeyError:
            for i in self[event.target].values():
                if i['host'] == event.source.host:
                    del self[event.target][i['hostmask'].split("!")[0]]
                    break

    def add_entry(self, channel, nick, hostmask, account):
        temp = {
            'hostmask': hostmask,
            'host': hostmask.split("@")[1],
            'account': account,
            'seen': [__import__("time").time(), ""]
        }
        failed = False
        try:
            user = self[channel][nick]
        except KeyError:
            failed = True
            self[channel][nick] = temp

        if not failed:
            del temp['seen']
            user.update(temp)

    def get_user_host(self, channel, nick):
        try:
            host = "*!*@" + self[channel][nick]['host']
        except KeyError:
            self.irc.send("WHO {0} nuhs%nhuac".format(channel))
            host = "*!*@" + self[channel][nick]['host']
        return host

    def flush(self):
        with open('userdb.json', 'w') as f:
            json.dump(self, f, indent=2, separators=(',', ': '))


#!/usr/bin/python
# -*- coding: utf-8 -*-
'''Tests for static methos conflicts with class methods'''

import unittest

from PySide.QtCore import *

class Foo(QFile):
  pass

class qmetaobject_test(unittest.TestCase):
    def test_QMetaObject(self):
        qobj = QObject()
        qobj_metaobj = qobj.metaObject()
        self.assertEqual(qobj_metaobj.className(), "QObject")

        obj = QFile()
        m = obj.metaObject()
        self.assertEqual(m.className(), "QFile")
        self.assertNotEqual(m.methodCount(), qobj_metaobj.methodCount())

        obj = Foo()
        m = obj.metaObject()
        self.assertEqual(m.className(), "Foo")
        f = QFile()
        fm = f.metaObject()
        self.assertEqual(m.methodCount(), fm.methodCount())

if __name__ == '__main__':
    unittest.main()

Response:
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''Tests for static methos conflicts with class methods'''

import unittest

from PySide.QtCore import *

class Foo(QFile):
  pass

class qmetaobject_test(unittest.TestCase):
    def test_QMetaObject(self):
        qobj = QObject()
        qobj_metaobj = qobj.metaObject()
        self.assertEqual(qobj_metaobj.className(), "QObject")

        obj = QFile()
        m = obj.metaObject()
        self.assertEqual(m.className(), "QFile")
        self.assertNotEqual(m.methodCount(), qobj_metaobj.methodCount())

        obj = Foo()
        m = obj.metaObject()
        self.assertEqual(m.className(), "Foo")
        f = QFile()
        fm = f.metaObject()
        self.assertEqual(m.methodCount(), fm.methodCount())

if __name__ == '__main__':
    unittest.main()

Prompt:
Fix qmentaobject test to work with dynamic metaobject.

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''Tests for static methos conflicts with class methods'''

import unittest

from PySide.QtCore import *

class Foo(QFile):
  pass

class qmetaobject_test(unittest.TestCase):
    def test_QMetaObject(self):
        qobj = QObject()
        qobj_metaobj = qobj.metaObject()
        self.assertEqual(qobj_metaobj.className(), "QObject")

        obj = QFile()
        m = obj.metaObject()
        self.assertEqual(m.className(), "QFile")
        self.assertNotEqual(m.methodCount(), qobj_metaobj.methodCount())

        obj = Foo()
        m = obj.metaObject()
        self.assertEqual(m.className(), "Foo")
        self.assertEqual(m.methodCount(), QFile().metaObject().methodCount())

if __name__ == '__main__':
    unittest.main()


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

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class MemberofParliament(scrapy.Item):
	"""
	Data structure to define Member of Parliament information
	"""
	mp_id = scrapy.Field()
	mp_name = scrapy.Field()
	mp_constituency = scrapy.Field()
	mp_party = scrapy.Field()
	mp_photo = scrapy.Field()

class RajyaSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Rajya Sabha question
	"""
	q_no = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()

class LokSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Lok Sabha question
	"""
	q_no = scrapy.Field()
	q_session = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()
	q_url = scrapy.Field()
	q_annex = scrapy.Field()

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

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class MemberofParliament(scrapy.Item):
	"""
	Data structure to define Member of Parliament information
	"""
	mp_id = scrapy.Field()
	mp_name = scrapy.Field()
	mp_constituency = scrapy.Field()
	mp_party = scrapy.Field()
	mp_photo = scrapy.Field()

class RajyaSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Rajya Sabha question
	"""
	q_no = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()

class LokSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Lok Sabha question
	"""
	q_no = scrapy.Field()
	q_session = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()
	q_url = scrapy.Field()
	q_annex = scrapy.Field()

Prompt:
Add fields to save question url and annexure links

Details of each question is in another link and some questions have annexures
(in English/Hindi), add fields to save all these items

Signed-off-by: Arun Siluvery <66692e34e783869a1e5829b4c5eee5e1a471c4f7@gmail.com>

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

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

class MemberofParliament(scrapy.Item):
	"""
	Data structure to define Member of Parliament information
	"""
	mp_id = scrapy.Field()
	mp_name = scrapy.Field()
	mp_constituency = scrapy.Field()
	mp_party = scrapy.Field()
	mp_photo = scrapy.Field()

class RajyaSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Rajya Sabha question
	"""
	q_no = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()

class LokSabhaQuestion(scrapy.Item):
	"""
	Data structure to define a Lok Sabha question
	"""
	q_no = scrapy.Field()
	q_session = scrapy.Field()
	q_type = scrapy.Field()
	q_date = scrapy.Field()
	q_ministry = scrapy.Field()
	q_member = scrapy.Field()
	q_subject = scrapy.Field()


from rules import ascii_code_rule, no_short_title_rule, enforce_year_rule, no_super_long_title_rule
import utils
import pprint
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding

DEFAULT = 'default'
SCHEMAS = {
    'ASCII_CODE_RULE': ascii_code_rule.test,
    'NO_SHORT_TITLE_RULE': no_short_title_rule.test,
    'ENFORCE_YEAR_RULE': enforce_year_rule.test,
    'NO_SUPER_LONG_TITLE_RULE': no_super_long_title_rule.test
}

def get_yes_no():
    while True:
        answer = raw_input()
        if answer.lower() in ['y', 'yes', 'n', 'no']:
            return answer.lower() in ['y', 'yes']
        else:
            print "Please enter either y or n"

def retain_after_matching_schemas(entry):
    results = {}

    for rule, tester in SCHEMAS.iteritems():
        results[rule] = tester(entry)

    if not all(results[rule]['passed'] for rule in results):
        print 'The following item seems suspicious according to the following rule(s):'
        for rule, result in results.iteritems():
            if not result['passed']:
                print rule + ': ' + result['error']

        print 'Do you want to keep it? (y/n)'
        print '-' * 32
        utils.print_entry(entry)
        print '\n'

        return get_yes_no()
    else:
        return True

def main(file_name, output='default'):
    with open(file_name) as bibtex_file:
        parser = BibTexParser()
        parser.customization = homogeneize_latex_encoding
        bib_database = bibtexparser.load(bibtex_file, parser=parser)

    toRetain = []
    toDump = []

    # Filter out entries violating schemas
    for entry in bib_database.entries:
        if retain_after_matching_schemas(entry):
            toRetain.append(entry)
        else:
            toDump.append(entry)

    print toDump

Implement filter based on user input

Implement filter based on user input

Response:
from rules import ascii_code_rule, no_short_title_rule, enforce_year_rule, no_super_long_title_rule
import utils
import pprint
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding

DEFAULT = 'default'
SCHEMAS = {
    'ASCII_CODE_RULE': ascii_code_rule.test,
    'NO_SHORT_TITLE_RULE': no_short_title_rule.test,
    'ENFORCE_YEAR_RULE': enforce_year_rule.test,
    'NO_SUPER_LONG_TITLE_RULE': no_super_long_title_rule.test
}

def get_yes_no():
    while True:
        answer = raw_input()
        if answer.lower() in ['y', 'yes', 'n', 'no']:
            return answer.lower() in ['y', 'yes']
        else:
            print "Please enter either y or n"

def retain_after_matching_schemas(entry):
    results = {}

    for rule, tester in SCHEMAS.iteritems():
        results[rule] = tester(entry)

    if not all(results[rule]['passed'] for rule in results):
        print 'The following item seems suspicious according to the following rule(s):'
        for rule, result in results.iteritems():
            if not result['passed']:
                print rule + ': ' + result['error']

        print 'Do you want to keep it? (y/n)'
        print '-' * 32
        utils.print_entry(entry)
        print '\n'

        return get_yes_no()
    else:
        return True

def main(file_name, output='default'):
    with open(file_name) as bibtex_file:
        parser = BibTexParser()
        parser.customization = homogeneize_latex_encoding
        bib_database = bibtexparser.load(bibtex_file, parser=parser)

    toRetain = []
    toDump = []

    # Filter out entries violating schemas
    for entry in bib_database.entries:
        if retain_after_matching_schemas(entry):
            toRetain.append(entry)
        else:
            toDump.append(entry)

    print toDump

Prompt:
Implement filter based on user input

from rules import ascii_code_rule
import utils
import pprint
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding

DEFAULT = 'default'
pp = pprint.PrettyPrinter()

def main(file_name, output='default'):
    with open(file_name) as bibtex_file:
        parser = BibTexParser()
        parser.customization = homogeneize_latex_encoding
        bib_database = bibtexparser.load(bibtex_file, parser=parser)

    for entry in bib_database.entries:
        result = ascii_code_rule.test(entry)

        if not result["passed"]:
            print 'The following item seems suspicious according to the rule:'
            print result['error']
            print 'Do you want to keep it? (y/n)'
            print '-' * 32
            utils.print_entry(entry)
            print entry
            print '\n'


import Queue
import json

import EBQP

from . import world
from . import types
from . import consts

class GameRequestHandler:
    def __init__(self):
        self.world = None
        self.responses = {
            EBQP.new: self.respond_new,
        }

    def process(self, request):
        request_pieces = request.split(EBQP.packet_delimiter, 1)
        command = request_pieces[0]
        params = request_pieces[1].strip() if len(request_pieces) > 1 else ''
        try:
            json_args = json.loads(params)
        except Exception as e:
            return "process:failure:bad json"

        if command in self.responses:
            return self.responses[command](json_args)
        else:
            return "process:failure:unsupported command"

    def respond_new(self, args):
        uids = args['uids']
        self.world = world.World(uids)
        self.world.add_unit(uids[0], types.new_unit('Tank', consts.RED))

        self.responses = {
            EBQP.view: self.respond_view,
            EBQP.move: self.respond_move,
        }
        return 'new:success'

    def respond_view(self, args):
        return 'view:success:%s' % self.world.to_json()

    #TODO
    def respond_move(self, args):
        return 'move:failure:unimplemented'

Fix crash on game creation

Fix crash on game creation

Response:
import Queue
import json

import EBQP

from . import world
from . import types
from . import consts

class GameRequestHandler:
    def __init__(self):
        self.world = None
        self.responses = {
            EBQP.new: self.respond_new,
        }

    def process(self, request):
        request_pieces = request.split(EBQP.packet_delimiter, 1)
        command = request_pieces[0]
        params = request_pieces[1].strip() if len(request_pieces) > 1 else ''
        try:
            json_args = json.loads(params)
        except Exception as e:
            return "process:failure:bad json"

        if command in self.responses:
            return self.responses[command](json_args)
        else:
            return "process:failure:unsupported command"

    def respond_new(self, args):
        uids = args['uids']
        self.world = world.World(uids)
        self.world.add_unit(uids[0], types.new_unit('Tank', consts.RED))

        self.responses = {
            EBQP.view: self.respond_view,
            EBQP.move: self.respond_move,
        }
        return 'new:success'

    def respond_view(self, args):
        return 'view:success:%s' % self.world.to_json()

    #TODO
    def respond_move(self, args):
        return 'move:failure:unimplemented'

Prompt:
Fix crash on game creation

import Queue
import json

import EBQP

from . import world
from . import types
from . import consts

class GameRequestHandler:
    def __init__(self):
        self.world = None
        self.responses = {
            EBQP.new: self.respond_new,
        }

    def process(self, request):
        request_pieces = request.split(EBQP.packet_delimiter, 1)
        command = request_pieces[0]
        params = request_pieces[1].strip() if len(request_pieces) > 1 else ''
        try:
            json_args = json.loads(params)
        except Exception as e:
            return "process:failure:bad json"

        if command in self.responses:
            return self.responses[command](json_args)
        else:
            return "process:failure:unsupported command"

    def respond_new(self, args):
        uids = args['uids']
        self.world = world.World(uids)
        self.world.add_unit(uid1, types.new_unit('Tank', consts.RED))

        self.responses = {
            EBQP.view: self.respond_view,
            EBQP.move: self.respond_move,
        }
        return 'new:success'

    def respond_view(self, args):
        return 'view:success:%s' % self.world.to_json()

    #TODO
    def respond_move(self, args):
        return 'move:failure:unimplemented'


from django.utils import encoding
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler as drf_exception_handler

from rest_framework_json_api.utils import format_value

def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    errors = []
    # handle generic errors. ValidationError('test') in a view for example
    if isinstance(response.data, list):
        for message in response.data:
            errors.append({
                'detail': message,
                'source': {
                    'pointer': '/data',
                },
                'status': encoding.force_text(response.status_code),
            })
    # handle all errors thrown from serializers
    else:
        for field, error in response.data.items():
            field = format_value(field)
            pointer = '/data/attributes/{}'.format(field)
            # see if they passed a dictionary to ValidationError manually
            if isinstance(error, dict):
                errors.append(error)
            else:
                if isinstance(error, list):
                    for message in error:
                        errors.append({
                            'detail': message,
                            'source': {
                                'pointer': pointer,
                            },
                            'status': encoding.force_text(response.status_code),
                        })
                else:
                    errors.append({
                        'detail': message,
                        'source': {
                            'pointer': pointer,
                        },
                        'status': encoding.force_text(response.status_code),
                    })

    context['view'].resource_name = 'errors'
    response.data = errors
    return response

class Conflict(APIException):
    status_code = status.HTTP_409_CONFLICT
    default_detail = _('Conflict.')

Response:
from django.utils import encoding
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler as drf_exception_handler

from rest_framework_json_api.utils import format_value

def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    errors = []
    # handle generic errors. ValidationError('test') in a view for example
    if isinstance(response.data, list):
        for message in response.data:
            errors.append({
                'detail': message,
                'source': {
                    'pointer': '/data',
                },
                'status': encoding.force_text(response.status_code),
            })
    # handle all errors thrown from serializers
    else:
        for field, error in response.data.items():
            field = format_value(field)
            pointer = '/data/attributes/{}'.format(field)
            # see if they passed a dictionary to ValidationError manually
            if isinstance(error, dict):
                errors.append(error)
            else:
                if isinstance(error, list):
                    for message in error:
                        errors.append({
                            'detail': message,
                            'source': {
                                'pointer': pointer,
                            },
                            'status': encoding.force_text(response.status_code),
                        })
                else:
                    errors.append({
                        'detail': message,
                        'source': {
                            'pointer': pointer,
                        },
                        'status': encoding.force_text(response.status_code),
                    })

    context['view'].resource_name = 'errors'
    response.data = errors
    return response

class Conflict(APIException):
    status_code = status.HTTP_409_CONFLICT
    default_detail = _('Conflict.')

Prompt:
Fix for some error messages that were split into several messages

The exception handler expects the error to be a list on line 33. In my
case they were a string, which lead to the split of the string into
multiple errors containing one character

from django.utils import encoding
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler as drf_exception_handler

from rest_framework_json_api.utils import format_value

def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    errors = []
    # handle generic errors. ValidationError('test') in a view for example
    if isinstance(response.data, list):
        for message in response.data:
            errors.append({
                'detail': message,
                'source': {
                    'pointer': '/data',
                },
                'status': encoding.force_text(response.status_code),
            })
    # handle all errors thrown from serializers
    else:
        for field, error in response.data.items():
            field = format_value(field)
            pointer = '/data/attributes/{}'.format(field)
            # see if they passed a dictionary to ValidationError manually
            if isinstance(error, dict):
                errors.append(error)
            else:
                for message in error:
                    errors.append({
                        'detail': message,
                        'source': {
                            'pointer': pointer,
                        },
                        'status': encoding.force_text(response.status_code),
                    })

    context['view'].resource_name = 'errors'
    response.data = errors
    return response

class Conflict(APIException):
    status_code = status.HTTP_409_CONFLICT
    default_detail = _('Conflict.')


# Ridiculously simple test of the os.startfile function for Windows.
#
# empty.vbs is an empty file (except for a comment), which does
# nothing when run with cscript or wscript.
#
# A possible improvement would be to have empty.vbs do something that
# we can detect here, to make sure that not only the os.startfile()
# call succeeded, but also the the script actually has run.

import unittest
from test import test_support

# use this form so that the test is skipped when startfile is not available:
from os import startfile

class TestCase(unittest.TestCase):
    def test_nonexisting(self):
        self.assertRaises(OSError, startfile, "nonexisting.vbs")

    def test_nonexisting_u(self):
        self.assertRaises(OSError, startfile, u"nonexisting.vbs")

    def test_empty(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        startfile(empty)
        startfile(empty, "open")

    def test_empty_u(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        startfile(unicode(empty, "mbcs"))
        startfile(unicode(empty, "mbcs"), "open")

def test_main():
    test_support.run_unittest(TestCase)

if __name__=="__main__":
    test_main()

Response:
# Ridiculously simple test of the os.startfile function for Windows.
#
# empty.vbs is an empty file (except for a comment), which does
# nothing when run with cscript or wscript.
#
# A possible improvement would be to have empty.vbs do something that
# we can detect here, to make sure that not only the os.startfile()
# call succeeded, but also the the script actually has run.

import unittest
from test import test_support

# use this form so that the test is skipped when startfile is not available:
from os import startfile

class TestCase(unittest.TestCase):
    def test_nonexisting(self):
        self.assertRaises(OSError, startfile, "nonexisting.vbs")

    def test_nonexisting_u(self):
        self.assertRaises(OSError, startfile, u"nonexisting.vbs")

    def test_empty(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        startfile(empty)
        startfile(empty, "open")

    def test_empty_u(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        startfile(unicode(empty, "mbcs"))
        startfile(unicode(empty, "mbcs"), "open")

def test_main():
    test_support.run_unittest(TestCase)

if __name__=="__main__":
    test_main()

Prompt:
Change the import statement so that the test is skipped when
os.startfile is not present.

# Ridiculously simple test of the os.startfile function for Windows.
#
# empty.vbs is an empty file (except for a comment), which does
# nothing when run with cscript or wscript.
#
# A possible improvement would be to have empty.vbs do something that
# we can detect here, to make sure that not only the os.startfile()
# call succeeded, but also the the script actually has run.

import unittest
from test import test_support
import os

class TestCase(unittest.TestCase):
    def test_nonexisting(self):
        self.assertRaises(OSError, os.startfile, "nonexisting.vbs")

    def test_nonexisting_u(self):
        self.assertRaises(OSError, os.startfile, u"nonexisting.vbs")

    def test_empty(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        os.startfile(empty)
        os.startfile(empty, "open")

    def test_empty_u(self):
        empty = os.path.join(os.path.dirname(__file__), "empty.vbs")
        os.startfile(unicode(empty, "mbcs"))
        os.startfile(unicode(empty, "mbcs"), "open")

def test_main():
    test_support.run_unittest(TestCase)

if __name__=="__main__":
    test_main()


from twisted.plugin import IPlugin
from txircd.module_interface import IMode, IModuleData, Mode, ModuleData
from txircd.utils import ModeType
from zope.interface import implements

class ChannelOpAccess(ModuleData, Mode):
	implements(IPlugin, IModuleData, IMode)

	name = "ChannelOpAccess"
	affectedActions = {
		"checkchannellevel": 10,
		"checkexemptchanops": 10
	}

	def actions(self):
		return [ ("modeactioncheck-channel-W-checkchannellevel", 1, self.checkMode),
		         ("modeactioncheck-channel-W-checkexemptchanops", 1, self.checkMode) ]

	def channelModes(self):
		return [ ("W", ModeType.List, self) ]

	def checkMode(self, channel, checkType, paramChannel, user):
		if "W" not in channel.mode:
			continue
		for paramData in channel.mode["W"]:
			if paramData[0] == checkType:
				return paramData[0]
		return None

	def checkSet(self, channel, param):
		checkedParams = []
		for parameter in param.split(","):
			status, permissionType = parameter.split(":", 1)
			if status not in self.ircd.channelStatuses:
				continue
			checkedParams.append(parameter)
		return checkedParams

	def apply(self, actionType, channel, param, checkType, paramChannel, user):
		status, permissionType = parameter.split(":", 1)
		if permissionType != checkType:
			return None
		if status not in self.ircd.channelStatuses:
			return False # For security, we'll favor those that were restricting permissions while a certain status was loaded.
		level = self.ircd.channelStatuses[status][1]
		return (channel.userRank(user) >= level)

chanAccess = ChannelOpAccess()

Allow channel ops to change the required level for specific permissions

Response:
from twisted.plugin import IPlugin
from txircd.module_interface import IMode, IModuleData, Mode, ModuleData
from txircd.utils import ModeType
from zope.interface import implements

class ChannelOpAccess(ModuleData, Mode):
	implements(IPlugin, IModuleData, IMode)

	name = "ChannelOpAccess"
	affectedActions = {
		"checkchannellevel": 10,
		"checkexemptchanops": 10
	}

	def actions(self):
		return [ ("modeactioncheck-channel-W-checkchannellevel", 1, self.checkMode),
		         ("modeactioncheck-channel-W-checkexemptchanops", 1, self.checkMode) ]

	def channelModes(self):
		return [ ("W", ModeType.List, self) ]

	def checkMode(self, channel, checkType, paramChannel, user):
		if "W" not in channel.mode:
			continue
		for paramData in channel.mode["W"]:
			if paramData[0] == checkType:
				return paramData[0]
		return None

	def checkSet(self, channel, param):
		checkedParams = []
		for parameter in param.split(","):
			status, permissionType = parameter.split(":", 1)
			if status not in self.ircd.channelStatuses:
				continue
			checkedParams.append(parameter)
		return checkedParams

	def apply(self, actionType, channel, param, checkType, paramChannel, user):
		status, permissionType = parameter.split(":", 1)
		if permissionType != checkType:
			return None
		if status not in self.ircd.channelStatuses:
			return False # For security, we'll favor those that were restricting permissions while a certain status was loaded.
		level = self.ircd.channelStatuses[status][1]
		return (channel.userRank(user) >= level)

chanAccess = ChannelOpAccess()

Prompt: Allow channel ops to change the required level for specific permissions


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

# Standard Libs
import datetime
import os
import sys

# Add flask_hal to the Path
root = os.path.abspath(
    os.path.join(
        os.path.dirname(__file__),
        '..',
    )
)

sys.path.append(os.path.join(root, 'flask_hal'))

# First Party Libs
import flask_hal  # noqa

# Project details
project = u'Flask-HAL'
copyright = u'{0}, SOON_ London Ltd'.format(datetime.datetime.utcnow().year)
version = open('./../VERSION.txt').read().strip()
release = version

# Sphinx Config
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.todo',
    'sphinx.ext.napoleon']

exclude_patterns = []

# Theme
sys.path.append(os.path.abspath('_themes'))
html_theme_path = ['_themes', ]
html_static_path = ['_static', ]
html_theme = 'kr'
html_sidebars = {
    'index':    ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html'],
    '**':       ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html']
}

Load version from version file

Load version from version file

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

# Standard Libs
import datetime
import os
import sys

# Add flask_hal to the Path
root = os.path.abspath(
    os.path.join(
        os.path.dirname(__file__),
        '..',
    )
)

sys.path.append(os.path.join(root, 'flask_hal'))

# First Party Libs
import flask_hal  # noqa

# Project details
project = u'Flask-HAL'
copyright = u'{0}, SOON_ London Ltd'.format(datetime.datetime.utcnow().year)
version = open('./../VERSION.txt').read().strip()
release = version

# Sphinx Config
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.todo',
    'sphinx.ext.napoleon']

exclude_patterns = []

# Theme
sys.path.append(os.path.abspath('_themes'))
html_theme_path = ['_themes', ]
html_static_path = ['_static', ]
html_theme = 'kr'
html_sidebars = {
    'index':    ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html'],
    '**':       ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html']
}

Prompt:
Load version from version file

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

# Standard Libs
import datetime
import os
import sys

# Add flask_hal to the Path
root = os.path.abspath(
    os.path.join(
        os.path.dirname(__file__),
        '..',
    )
)

sys.path.append(os.path.join(root, 'flask_hal'))

# First Party Libs
import flask_hal  # noqa

# Project details
project = u'Flask-HAL'
copyright = u'{0}, SOON_ London Ltd'.format(datetime.datetime.utcnow().year)
version = '2015.10.8'
release = version

# Sphinx Config
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.viewcode',
    'sphinx.ext.todo',
    'sphinx.ext.napoleon']

exclude_patterns = []

# Theme
sys.path.append(os.path.abspath('_themes'))
html_theme_path = ['_themes', ]
html_static_path = ['_static', ]
html_theme = 'kr'
html_sidebars = {
    'index':    ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html'],
    '**':       ['sidebar_intro.html', 'localtoc.html', 'relations.html',
                 'sourcelink.html', 'searchbox.html']
}


import logging

from .base import *

# SECURITY WARNING: keep the secret key used in production secret!
if 'SECRET_KEY' not in os.environ:
    logging.warning(
        "No SECRET_KEY given in environ, please have a check."
        "If you have a local_settings file, please ignore this warning."
    )
SECRET_KEY = os.environ.get('SECRET_KEY', "PLEASE_REPLACE_ME")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# SECURITY WARNING: use your actual domain name in production!
ALLOWED_HOSTS = ['*']

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

USE_X_FORWARDED_HOST = True

REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] = [
    'rest_framework.renderers.JSONRenderer',
]

try:
    from .local_settings import *
except ImportError:
    pass

Add ignore info for secret-key env-test

Response:
import logging

from .base import *

# SECURITY WARNING: keep the secret key used in production secret!
if 'SECRET_KEY' not in os.environ:
    logging.warning(
        "No SECRET_KEY given in environ, please have a check."
        "If you have a local_settings file, please ignore this warning."
    )
SECRET_KEY = os.environ.get('SECRET_KEY', "PLEASE_REPLACE_ME")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# SECURITY WARNING: use your actual domain name in production!
ALLOWED_HOSTS = ['*']

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

USE_X_FORWARDED_HOST = True

REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] = [
    'rest_framework.renderers.JSONRenderer',
]

try:
    from .local_settings import *
except ImportError:
    pass

Prompt:
Doc: Add ignore info for secret-key env-test

import logging

from .base import *

# SECURITY WARNING: keep the secret key used in production secret!
if 'SECRET_KEY' not in os.environ:
    logging.warning(
        "No SECRET_KEY given in environ, please have a check"
    )
SECRET_KEY = os.environ.get('SECRET_KEY', "PLEASE_REPLACE_ME")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# SECURITY WARNING: use your actual domain name in production!
ALLOWED_HOSTS = ['*']

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

USE_X_FORWARDED_HOST = True

REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] = [
    'rest_framework.renderers.JSONRenderer',
]

try:
    from .local_settings import *
except ImportError:
    pass


# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Single source of truth for dsub's version.

This must remain small and dependency-free so that any dsub module may
import it without creating circular dependencies. Note that this module
is parsed as a text file by setup.py and changes to the format of this
file could break setup.py.

The version should follow formatting requirements specified in PEP-440.
 - https://www.python.org/dev/peps/pep-0440

A typical release sequence will be versioned as:
  0.1.3.dev0 -> 0.1.3 -> 0.1.4.dev0 -> ...
"""

DSUB_VERSION = '0.1.10.dev0'

Update version number to 0.1.10.dev0.

Response:
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Single source of truth for dsub's version.

This must remain small and dependency-free so that any dsub module may
import it without creating circular dependencies. Note that this module
is parsed as a text file by setup.py and changes to the format of this
file could break setup.py.

The version should follow formatting requirements specified in PEP-440.
 - https://www.python.org/dev/peps/pep-0440

A typical release sequence will be versioned as:
  0.1.3.dev0 -> 0.1.3 -> 0.1.4.dev0 -> ...
"""

DSUB_VERSION = '0.1.10.dev0'

Prompt:
Update version number to 0.1.10.dev0.

PiperOrigin-RevId: 202663603

# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Single source of truth for dsub's version.

This must remain small and dependency-free so that any dsub module may
import it without creating circular dependencies. Note that this module
is parsed as a text file by setup.py and changes to the format of this
file could break setup.py.

The version should follow formatting requirements specified in PEP-440.
 - https://www.python.org/dev/peps/pep-0440

A typical release sequence will be versioned as:
  0.1.3.dev0 -> 0.1.3 -> 0.1.4.dev0 -> ...
"""

DSUB_VERSION = '0.1.9'


import sys
import os
import logging
import time

if not os.path.exists(os.path.expanduser("~/.chatserver")):
    os.makedirs(os.path.expanduser("~/.chatserver"))
logging.basicConfig(filename=os.path.expanduser("~/.chatserver/chat-"+time.strftime("%d-%m-%Y.log"), level=logging.DEBUG)

sys.stderr.close()
sys.stdout.close()
sys.stdin.close()

from app import app, db, main, socketio
db.create_all()
app.register_blueprint(main)

port = app.config['PORT']
if len(sys.argv) == 2:
    port = int(sys.argv[1])

logging.info("Chat server is now running on 0.0.0.0:%r" % port)
socketio.run(app, host="0.0.0.0", port=port)

Make logger sort by date

Make logger sort by date

Response:
import sys
import os
import logging
import time

if not os.path.exists(os.path.expanduser("~/.chatserver")):
    os.makedirs(os.path.expanduser("~/.chatserver"))
logging.basicConfig(filename=os.path.expanduser("~/.chatserver/chat-"+time.strftime("%d-%m-%Y.log"), level=logging.DEBUG)

sys.stderr.close()
sys.stdout.close()
sys.stdin.close()

from app import app, db, main, socketio
db.create_all()
app.register_blueprint(main)

port = app.config['PORT']
if len(sys.argv) == 2:
    port = int(sys.argv[1])

logging.info("Chat server is now running on 0.0.0.0:%r" % port)
socketio.run(app, host="0.0.0.0", port=port)

Prompt:
Make logger sort by date

import sys
import os
import logging

if not os.path.exists(os.path.expanduser("~/.chatserver")):
    os.makedirs(os.path.expanduser("~/.chatserver"))
logging.basicConfig(filename=os.path.expanduser("~/.chatserver/chat.log"), level=logging.DEBUG)

sys.stderr.close()
sys.stdout.close()
sys.stdin.close()

from app import app, db, main, socketio
db.create_all()
app.register_blueprint(main)

port = app.config['PORT']
if len(sys.argv) == 2:
    port = int(sys.argv[1])

logging.info("Chat server is now running on 0.0.0.0:%r" % port)
socketio.run(app, host="0.0.0.0", port=port)


#!/usr/local/bin/python
from website import carts
import urllib2
import json

def load():
    carts.remove_all()
    request = 'http://data.cityofnewyork.us/resource/akqf-qv4n.json'

    for i in range(0, 24000, 1000):
        query = '?$offset=%d' % i
        data = urllib2.urlopen(request + query)

        results = json.loads(data.read())
        data.close()
        required_keys = ['license_permit_holder', 'license_permit_holder_name',
                'license_permit_number', 'permit_issuance_date',
                'permit_expiration_date', 'longitude_wgs84', 'latitude_wgs84',
                'zip_code', 'borough']

        for r in results:
            for k in required_keys:
                if not r.has_key(k):
                    r[k] = ''

            carts.insert(name=r['license_permit_holder'],
                    owner=r['license_permit_holder_name'],
                    permit_number=r['license_permit_number'],
                    issuance=r['permit_issuance_date'],
                    expiration=r['permit_expiration_date'],
                    loc=[ float(r['longitude_wgs84']),
                        float(r['latitude_wgs84']) ],
                    zip_code=r['zip_code'], borough=r['borough'])

    out = [c for c in carts.find()]
    print len(out)

Response:
#!/usr/local/bin/python
from website import carts
import urllib2
import json

def load():
    carts.remove_all()
    request = 'http://data.cityofnewyork.us/resource/akqf-qv4n.json'

    for i in range(0, 24000, 1000):
        query = '?$offset=%d' % i
        data = urllib2.urlopen(request + query)

        results = json.loads(data.read())
        data.close()
        required_keys = ['license_permit_holder', 'license_permit_holder_name',
                'license_permit_number', 'permit_issuance_date',
                'permit_expiration_date', 'longitude_wgs84', 'latitude_wgs84',
                'zip_code', 'borough']

        for r in results:
            for k in required_keys:
                if not r.has_key(k):
                    r[k] = ''

            carts.insert(name=r['license_permit_holder'],
                    owner=r['license_permit_holder_name'],
                    permit_number=r['license_permit_number'],
                    issuance=r['permit_issuance_date'],
                    expiration=r['permit_expiration_date'],
                    loc=[ float(r['longitude_wgs84']),
                        float(r['latitude_wgs84']) ],
                    zip_code=r['zip_code'], borough=r['borough'])

    out = [c for c in carts.find()]
    print len(out)

Prompt:
Change cart structure and url endpoint for getting cart data

#!/usr/local/bin/python
from website import carts
import urllib2
import json

def load():
    carts.remove_all()
    host = 'http://data.cityofnewyork.us/resource/xfyi-uyt5.json'

    for i in range(0, 7000, 1000):
        query = 'permit_type_description=MOBILE+FOOD+UNIT&$offset=%d' % i
        request = host + '?' + query
        data = urllib2.urlopen(request)

        results = json.loads(data.read())
        data.close()
        required_keys = ['longitude_wgs84', 'latitude_wgs84', 'street', 'address', 'zip_code', 'borough', 'license_permit_holder']

        for r in results:
            for k in required_keys:
                if not r.has_key(k):
                    r[k] = ''

            carts.insert(lat=r['latitude_wgs84'], lng=r['longitude_wgs84'],
                    address=r['address'] + ' ' + r['street'],
                    zip_code=r['zip_code'], borough=r['borough'],
                    name=r['license_permit_holder'])

    out = [c for c in carts.find()]
    print len(out)


import construct

from tlsenum import hello_constructs

class ClientHello(object):

    @property
    def protocol_version(self):
        return self._protocol_version

    @protocol_version.setter
    def protocol_version(self, protocol_version):
        assert protocol_version in ["3.0", "1.0", "1.1", "1.2"]

        self._protocol_version = protocol_version

        if protocol_version == "3.0":
            self._protocol_minor = 0
        elif protocol_version == "1.0":
            self._protocol_minor = 1
        elif protocol_version == "1.1":
            self._protocol_minor = 2
        elif protocol_version == "1.2":
            self._protocol_minor = 3

    def build(self):
        return hello_constructs.ProtocolVersion.build(
            construct.Container(major=3, minor=self._protocol_minor)
        )

Add a work-in-progress parser for the ClientHello message.

Response:
import construct

from tlsenum import hello_constructs

class ClientHello(object):

    @property
    def protocol_version(self):
        return self._protocol_version

    @protocol_version.setter
    def protocol_version(self, protocol_version):
        assert protocol_version in ["3.0", "1.0", "1.1", "1.2"]

        self._protocol_version = protocol_version

        if protocol_version == "3.0":
            self._protocol_minor = 0
        elif protocol_version == "1.0":
            self._protocol_minor = 1
        elif protocol_version == "1.1":
            self._protocol_minor = 2
        elif protocol_version == "1.2":
            self._protocol_minor = 3

    def build(self):
        return hello_constructs.ProtocolVersion.build(
            construct.Container(major=3, minor=self._protocol_minor)
        )

Prompt: Add a work-in-progress parser for the ClientHello message.


Add newline at end of file.

Add newline at end of file.

Response:
from unittest import TestCase

import eduid_IdP_html

__author__ = 'ft'

class TestLoad_settings(TestCase):

    def test_load_settings(self):
        settings = eduid_IdP_html.load_settings()
        self.assertTrue(settings['gettext_domain'] is not None)

Prompt:
Add newline at end of file.

from unittest import TestCase

import eduid_IdP_html

__author__ = 'ft'

class TestLoad_settings(TestCase):

    def test_load_settings(self):
        settings = eduid_IdP_html.load_settings()
        self.assertTrue(settings['gettext_domain'] is not None)


import requests
from plotly.offline import plot
import plotly.graph_objs as go

def build_data_for_monitored_url(id):
    '''Fetches and prepares data for plotting for the given URL id'''
    r = requests.get('http://localhost:8081/monitor_results/' + str(id))
    # build traces for plotting from monitoring data
    request_times = list()
    timestamps = list()
    timestamp  = 0
    url = r.json()[0]['urlToMonitor']['url']
    for monitoring_data in r.json():
        request_time = monitoring_data['timeNeededForRequest']
        request_times.append(request_time)
        timestamps.append(timestamp)
        timestamp = timestamp + 1
    return go.Scatter(x = timestamps, y = request_times, name = url)

# get all monitored sites and fetch data for it
r = requests.get('http://localhost:8081/monitored-sites')
plotting_data = list()
for monitored_site in r.json():
    print('Fetching data for ' + monitored_site['url'])
    data_for_site = build_data_for_monitored_url(monitored_site['id'])
    plotting_data.append(data_for_site)

plot(plotting_data, filename='request_times.html')

Implement fetching all monitored data

Implement fetching all monitored data

Response:
import requests
from plotly.offline import plot
import plotly.graph_objs as go

def build_data_for_monitored_url(id):
    '''Fetches and prepares data for plotting for the given URL id'''
    r = requests.get('http://localhost:8081/monitor_results/' + str(id))
    # build traces for plotting from monitoring data
    request_times = list()
    timestamps = list()
    timestamp  = 0
    url = r.json()[0]['urlToMonitor']['url']
    for monitoring_data in r.json():
        request_time = monitoring_data['timeNeededForRequest']
        request_times.append(request_time)
        timestamps.append(timestamp)
        timestamp = timestamp + 1
    return go.Scatter(x = timestamps, y = request_times, name = url)

# get all monitored sites and fetch data for it
r = requests.get('http://localhost:8081/monitored-sites')
plotting_data = list()
for monitored_site in r.json():
    print('Fetching data for ' + monitored_site['url'])
    data_for_site = build_data_for_monitored_url(monitored_site['id'])
    plotting_data.append(data_for_site)

plot(plotting_data, filename='request_times.html')

Prompt:
Implement fetching all monitored data

import requests
from plotly.offline import plot
import plotly.graph_objs as go

r = requests.get('http://localhost:8081/monitor_results/1')
print(r.json())
# build traces for plotting from monitoring data
request_times = list()
timestamps = list()
timestamp  = 0
url = r.json()[0]['urlToMonitor']['url']
for monitoring_data in r.json():
    request_time = monitoring_data['timeNeededForRequest']
    request_times.append(request_time)
    timestamps.append(timestamp)
    timestamp = timestamp + 1

plot([go.Scatter(x = timestamps, y = request_times, name = 'THE NAME'), go.Scatter(x = timestamps, y = request_times, name =
    'THE OTHER NAME')], filename='request_times.html')


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

from flask.ext.testing import TestCase as BaseTestCase
from ggrc import db
from ggrc.app import app
from ggrc.models import create_db, drop_db
from google.appengine.api import memcache
from google.appengine.ext import testbed

use_migrations = False

class TestCase(BaseTestCase):
  def setUp(self):
    create_db(use_migrations, quiet=True)
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_memcache_stub()

  def tearDown(self):
    db.session.remove()
    drop_db(use_migrations, quiet=True)
    self.testbed.deactivate()

  def create_app(self):
    app.testing = True
    app.debug = False
    return app

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

from flask.ext.testing import TestCase as BaseTestCase
from ggrc import db
from ggrc.app import app
from ggrc.models import create_db, drop_db
from google.appengine.api import memcache
from google.appengine.ext import testbed

use_migrations = False

class TestCase(BaseTestCase):
  def setUp(self):
    create_db(use_migrations, quiet=True)
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_memcache_stub()

  def tearDown(self):
    db.session.remove()
    drop_db(use_migrations, quiet=True)
    self.testbed.deactivate()

  def create_app(self):
    app.testing = True
    app.debug = False
    return app

Prompt:
Allow Google AppEngine Memcache stub to be used for running unit tests including calls to caching layer

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

from flask.ext.testing import TestCase as BaseTestCase
from ggrc import db
from ggrc.app import app
from ggrc.models import create_db, drop_db

use_migrations = False

class TestCase(BaseTestCase):
  def setUp(self):
    create_db(use_migrations, quiet=True)

  def tearDown(self):
    db.session.remove()
    drop_db(use_migrations, quiet=True)

  def create_app(self):
    app.testing = True
    app.debug = False
    return app


from datetime import timedelta, datetime

import telecommand
from obc.experiments import ExperimentType
from system import auto_power_on
from tests.base import BaseTest
from utils import TestEvent

class TestExperimentDetumbling(BaseTest):
    @auto_power_on(auto_power_on=False)
    def __init__(self, *args, **kwargs):
        super(TestExperimentDetumbling, self).__init__(*args, **kwargs)

    def _start(self):
        e = TestEvent()

        def on_reset(_):
            e.set()
        self.system.comm.on_hardware_reset = on_reset

        self.system.obc.power_on(clean_state=True)
        self.system.obc.wait_to_start()

        e.wait_for_change(1)

    def test_should_perform_experiment(self):
        self._start()
        start_time = datetime.now()
        self.system.rtc.set_response_time(start_time)

        self.system.comm.put_frame(telecommand.PerformDetumblingExperiment(duration=timedelta(hours=4)))

        self.system.obc.wait_for_experiment(ExperimentType.Detumbling, 40)

        self.system.obc.advance_time(timedelta(hours=4, minutes=1).total_seconds() * 1000)
        self.system.rtc.set_response_time(start_time + timedelta(hours=4, minutes=1))

        self.system.obc.wait_for_experiment(None, 20)

Response:
from datetime import timedelta, datetime

import telecommand
from obc.experiments import ExperimentType
from system import auto_power_on
from tests.base import BaseTest
from utils import TestEvent

class TestExperimentDetumbling(BaseTest):
    @auto_power_on(auto_power_on=False)
    def __init__(self, *args, **kwargs):
        super(TestExperimentDetumbling, self).__init__(*args, **kwargs)

    def _start(self):
        e = TestEvent()

        def on_reset(_):
            e.set()
        self.system.comm.on_hardware_reset = on_reset

        self.system.obc.power_on(clean_state=True)
        self.system.obc.wait_to_start()

        e.wait_for_change(1)

    def test_should_perform_experiment(self):
        self._start()
        start_time = datetime.now()
        self.system.rtc.set_response_time(start_time)

        self.system.comm.put_frame(telecommand.PerformDetumblingExperiment(duration=timedelta(hours=4)))

        self.system.obc.wait_for_experiment(ExperimentType.Detumbling, 40)

        self.system.obc.advance_time(timedelta(hours=4, minutes=1).total_seconds() * 1000)
        self.system.rtc.set_response_time(start_time + timedelta(hours=4, minutes=1))

        self.system.obc.wait_for_experiment(None, 20)

Prompt:
Fix race condition in detumbling experiment test

In detumbling experiment test, experiment was commanded to run for 4
hours. After that OBC time was advanced also by 4 hours, however it was
not enough as during next mission loop OBC time was few milliseconds
before scheduled experiment end.

from datetime import timedelta, datetime

import telecommand
from obc.experiments import ExperimentType
from system import auto_power_on
from tests.base import BaseTest
from utils import TestEvent

class TestExperimentDetumbling(BaseTest):
    @auto_power_on(auto_power_on=False)
    def __init__(self, *args, **kwargs):
        super(TestExperimentDetumbling, self).__init__(*args, **kwargs)

    def _start(self):
        e = TestEvent()

        def on_reset(_):
            e.set()
        self.system.comm.on_hardware_reset = on_reset

        self.system.obc.power_on(clean_state=True)
        self.system.obc.wait_to_start()

        e.wait_for_change(1)

    def test_should_perform_experiment(self):
        self._start()
        start_time = datetime.now()
        self.system.rtc.set_response_time(start_time)

        self.system.comm.put_frame(telecommand.PerformDetumblingExperiment(duration=timedelta(hours=4)))

        self.system.obc.wait_for_experiment(ExperimentType.Detumbling, 40)

        self.system.obc.advance_time(timedelta(hours=4).total_seconds() * 1000)
        self.system.rtc.set_response_time(start_time + timedelta(hours=4))

        self.system.obc.wait_for_experiment(None, 20)


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('icekit_events', '0011_event_show_in_calendar'),
    ]

    operations = [
        migrations.AlterModelTable(
            name='event',
            table=None,
        ),
        migrations.AlterModelTable(
            name='recurrencerule',
            table=None,
        ),
        migrations.RunSQL(
            "UPDATE django_content_type SET app_label='icekit_events' WHERE app_label='eventkit'",
            # No-op: I haven't yet found a way to make this reversible in the
            # way you would expect without unique constraint DB errors, whereas
            # it works (according to unit tests at least) with a no-op.
            "UPDATE django_content_type SET app_label=app_label WHERE app_label='NONE!'",
        ),
    ]

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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('icekit_events', '0011_event_show_in_calendar'),
    ]

    operations = [
        migrations.AlterModelTable(
            name='event',
            table=None,
        ),
        migrations.AlterModelTable(
            name='recurrencerule',
            table=None,
        ),
        migrations.RunSQL(
            "UPDATE django_content_type SET app_label='icekit_events' WHERE app_label='eventkit'",
            # No-op: I haven't yet found a way to make this reversible in the
            # way you would expect without unique constraint DB errors, whereas
            # it works (according to unit tests at least) with a no-op.
            "UPDATE django_content_type SET app_label=app_label WHERE app_label='NONE!'",
        ),
    ]

Prompt:
Make migration reverse no-op a valid SQL query

When using a PostgreSQL database with Django 1.7 empty reverse query
statements in DB migrations cause an error, so we replace the empty
no-op statement with a valid query that still does nothing so the
reverse migration will work in this case.

This problem doesn't seem to affect Django 1.8, which must be smarter
about accepted but not actually running empty statements in DB
migrations.

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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('icekit_events', '0011_event_show_in_calendar'),
    ]

    operations = [
        migrations.AlterModelTable(
            name='event',
            table=None,
        ),
        migrations.AlterModelTable(
            name='recurrencerule',
            table=None,
        ),
        migrations.RunSQL(
            "UPDATE django_content_type SET app_label='icekit_events' WHERE app_label='eventkit'",
            # No-op: I haven't yet found a way to make this reversible in the
            # way you would expect without unique constraint DB errors, whereas
            # it works (according to unit tests at least) with a no-op.
            "",
        ),
    ]


import os
from flask_app.database import init_db

# Generate new secret key
key_file_path = 'flask_app/secret_key.py'
if not os.path.isfile(key_file_path):
    secret_key = os.urandom(24).encode('hex').strip()
    with open(key_file_path, 'w') as key_file:
        key_file.write('secret_key = """' + secret_key + '""".decode("hex")')

# Initialize database
init_db()

Response:
import os
from flask_app.database import init_db

# Generate new secret key
key_file_path = 'flask_app/secret_key.py'
if not os.path.isfile(key_file_path):
    secret_key = os.urandom(24).encode('hex').strip()
    with open(key_file_path, 'w') as key_file:
        key_file.write('secret_key = """' + secret_key + '""".decode("hex")')

# Initialize database
init_db()

Prompt:
Check if keyfile exists before generating new key

import os
from flask_app.database import init_db

# Generate new secret key
secret_key = os.urandom(24).encode('hex').strip()
with open('flask_app/secret_key.py', 'w') as key_file:
    key_file.write('secret_key = """' + secret_key + '""".decode("hex")')

# Initialize database
init_db()


from slackclient._user import User
from slackclient._server import Server, SlackLoginError
from slackclient._channel import Channel
import json
import pytest

@pytest.fixture
def login_data():
    login_data = open('_pytest/data/rtm.start.json', 'r').read()
    login_data = json.loads(login_data)
    return login_data

def test_Server(server):
    assert type(server) == Server

def test_Server_parse_channel_data(server, login_data):
    server.parse_channel_data(login_data["channels"])
    assert type(server.channels.find('general')) == Channel

def test_Server_parse_user_data(server, login_data):
    server.parse_user_data(login_data["users"])
    assert type(server.users.find('fakeuser')) == User

def test_Server_cantconnect(server):
    with pytest.raises(SlackLoginError):
        reply = server.ping()

@pytest.mark.xfail
def test_Server_ping(server, monkeypatch):
    #monkeypatch.setattr("", lambda: True)
    monkeypatch.setattr("websocket.create_connection", lambda: True)
    reply = server.ping()

Response:
from slackclient._user import User
from slackclient._server import Server, SlackLoginError
from slackclient._channel import Channel
import json
import pytest

@pytest.fixture
def login_data():
    login_data = open('_pytest/data/rtm.start.json', 'r').read()
    login_data = json.loads(login_data)
    return login_data

def test_Server(server):
    assert type(server) == Server

def test_Server_parse_channel_data(server, login_data):
    server.parse_channel_data(login_data["channels"])
    assert type(server.channels.find('general')) == Channel

def test_Server_parse_user_data(server, login_data):
    server.parse_user_data(login_data["users"])
    assert type(server.users.find('fakeuser')) == User

def test_Server_cantconnect(server):
    with pytest.raises(SlackLoginError):
        reply = server.ping()

@pytest.mark.xfail
def test_Server_ping(server, monkeypatch):
    #monkeypatch.setattr("", lambda: True)
    monkeypatch.setattr("websocket.create_connection", lambda: True)
    reply = server.ping()

Prompt:
Fix PEP8 white spacing (space after comma)

from slackclient._user import User
from slackclient._server import Server, SlackLoginError
from slackclient._channel import Channel
import json
import pytest

@pytest.fixture
def login_data():
    login_data = open('_pytest/data/rtm.start.json','r').read()
    login_data = json.loads(login_data)
    return login_data

def test_Server(server):
    assert type(server) == Server

def test_Server_parse_channel_data(server, login_data):
    server.parse_channel_data(login_data["channels"])
    assert type(server.channels.find('general')) == Channel

def test_Server_parse_user_data(server, login_data):
    server.parse_user_data(login_data["users"])
    assert type(server.users.find('fakeuser')) == User

def test_Server_cantconnect(server):
    with pytest.raises(SlackLoginError):
        reply = server.ping()

@pytest.mark.xfail
def test_Server_ping(server, monkeypatch):
    #monkeypatch.setattr("", lambda: True)
    monkeypatch.setattr("websocket.create_connection", lambda: True)
    reply = server.ping()


# Copyright (c) 2012-2013, Mark Peek <mark@peek.org>
# All rights reserved.
#
# See LICENSE file for full license.

from . import AWSObject, AWSProperty, Tags
from .compat import policytypes
from .validators import boolean

class Subscription(AWSProperty):
    props = {
        'Endpoint': (basestring, True),
        'Protocol': (basestring, True),
    }

class SubscriptionResource(AWSObject):
    resource_type = "AWS::SNS::Subscription"

    props = {
        'DeliveryPolicy': (dict, False),
        'Endpoint': (basestring, False),
        'FilterPolicy': (dict, False),
        'Protocol': (basestring, True),
        'RawMessageDelivery': (boolean, False),
        'Region': (basestring, False),
        'TopicArn': (basestring, True),
    }

class TopicPolicy(AWSObject):
    resource_type = "AWS::SNS::TopicPolicy"

    props = {
        'PolicyDocument': (policytypes, True),
        'Topics': (list, True),
    }

class Topic(AWSObject):
    resource_type = "AWS::SNS::Topic"

    props = {
        'DisplayName': (basestring, False),
        'KmsMasterKeyId': (basestring, False),
        'Subscription': ([Subscription], False),
        'Tags': (Tags, False),
        'TopicName': (basestring, False),
    }

Response:
# Copyright (c) 2012-2013, Mark Peek <mark@peek.org>
# All rights reserved.
#
# See LICENSE file for full license.

from . import AWSObject, AWSProperty, Tags
from .compat import policytypes
from .validators import boolean

class Subscription(AWSProperty):
    props = {
        'Endpoint': (basestring, True),
        'Protocol': (basestring, True),
    }

class SubscriptionResource(AWSObject):
    resource_type = "AWS::SNS::Subscription"

    props = {
        'DeliveryPolicy': (dict, False),
        'Endpoint': (basestring, False),
        'FilterPolicy': (dict, False),
        'Protocol': (basestring, True),
        'RawMessageDelivery': (boolean, False),
        'Region': (basestring, False),
        'TopicArn': (basestring, True),
    }

class TopicPolicy(AWSObject):
    resource_type = "AWS::SNS::TopicPolicy"

    props = {
        'PolicyDocument': (policytypes, True),
        'Topics': (list, True),
    }

class Topic(AWSObject):
    resource_type = "AWS::SNS::Topic"

    props = {
        'DisplayName': (basestring, False),
        'KmsMasterKeyId': (basestring, False),
        'Subscription': ([Subscription], False),
        'Tags': (Tags, False),
        'TopicName': (basestring, False),
    }

Prompt:
Add Tags to SNS::Topic per 2019-11-31 changes

# Copyright (c) 2012-2013, Mark Peek <mark@peek.org>
# All rights reserved.
#
# See LICENSE file for full license.

from . import AWSObject, AWSProperty
from .compat import policytypes
from .validators import boolean

class Subscription(AWSProperty):
    props = {
        'Endpoint': (basestring, True),
        'Protocol': (basestring, True),
    }

class SubscriptionResource(AWSObject):
    resource_type = "AWS::SNS::Subscription"

    props = {
        'DeliveryPolicy': (dict, False),
        'Endpoint': (basestring, False),
        'FilterPolicy': (dict, False),
        'Protocol': (basestring, True),
        'RawMessageDelivery': (boolean, False),
        'Region': (basestring, False),
        'TopicArn': (basestring, True),
    }

class TopicPolicy(AWSObject):
    resource_type = "AWS::SNS::TopicPolicy"

    props = {
        'PolicyDocument': (policytypes, True),
        'Topics': (list, True),
    }

class Topic(AWSObject):
    resource_type = "AWS::SNS::Topic"

    props = {
        'DisplayName': (basestring, False),
        'KmsMasterKeyId': (basestring, False),
        'Subscription': ([Subscription], False),
        'TopicName': (basestring, False),
    }


#!/usr/bin/env python
"""Script to collect coverage information on ofStateManager"""

import os
import sys
import inspect
import coverage
import subprocess

def main():
	"""Main function"""
	arguments = ''
	if len(sys.argv) > 1:
		arguments = ' '.join(sys.argv[1:])
	testdir = os.path.abspath(os.path.dirname(
			inspect.getfile(inspect.currentframe())))
	script_path = os.path.join(testdir, '..', 'ofStateManager.py')

	os.environ['COVERAGE_PROCESS_START'] = os.path.join(testdir, '.coveragerc')
	os.environ['COVERAGE_FILE'] = os.path.join(testdir, '.coverage')

	subprocess.call('coverage erase', shell=True, cwd=testdir)
	subprocess.call('coverage run -m py.test ' + arguments, shell=True, cwd=testdir)
	subprocess.call('coverage combine', shell=True, cwd=testdir)
	subprocess.call('coverage html -d ' + os.path.join(testdir, 'htmlcov') +
					' --include=' + script_path, shell=True, cwd=testdir)
	subprocess.call('coverage report --include=' + script_path,
					shell=True, cwd=testdir)

if __name__ == '__main__':
	main()

Response:
#!/usr/bin/env python
"""Script to collect coverage information on ofStateManager"""

import os
import sys
import inspect
import coverage
import subprocess

def main():
	"""Main function"""
	arguments = ''
	if len(sys.argv) > 1:
		arguments = ' '.join(sys.argv[1:])
	testdir = os.path.abspath(os.path.dirname(
			inspect.getfile(inspect.currentframe())))
	script_path = os.path.join(testdir, '..', 'ofStateManager.py')

	os.environ['COVERAGE_PROCESS_START'] = os.path.join(testdir, '.coveragerc')
	os.environ['COVERAGE_FILE'] = os.path.join(testdir, '.coverage')

	subprocess.call('coverage erase', shell=True, cwd=testdir)
	subprocess.call('coverage run -m py.test ' + arguments, shell=True, cwd=testdir)
	subprocess.call('coverage combine', shell=True, cwd=testdir)
	subprocess.call('coverage html -d ' + os.path.join(testdir, 'htmlcov') +
					' --include=' + script_path, shell=True, cwd=testdir)
	subprocess.call('coverage report --include=' + script_path,
					shell=True, cwd=testdir)

if __name__ == '__main__':
	main()

Prompt:
Replace coverage API calls by subprocess calls.
#!/usr/bin/env python
"""Script to collect coverage information on ofStateManager"""

import os
import sys
import inspect
import coverage
import subprocess

def main():
	"""Main function"""
	arguments = ''
	if len(sys.argv) > 1:
		arguments = ' '.join(sys.argv[1:])
	testdir = os.path.abspath(os.path.dirname(
			inspect.getfile(inspect.currentframe())))
	os.environ['COVERAGE_PROCESS_START'] = os.path.join(testdir, '.coveragerc')
	os.environ['COVERAGE_FILE'] = os.path.join(testdir, '.coverage')
	cov = coverage.coverage(source=os.path.join(testdir, '..'),
		include=os.path.join(testdir, '..', 'ofStateManager.py'))
	cov.erase()
	subprocess.call('coverage run -m py.test ' + arguments, shell=True,
					cwd=testdir)
	cov.combine()
	cov.html_report(directory=os.path.join(testdir, 'htmlcov'))
	cov.report(show_missing=False)

if __name__ == '__main__':
	main()


#!/usr/bin/env python

from setuptools import setup, find_packages

__author__ = "Nitrax <nitrax@lokisec.fr>"
__copyright__ = "Copyright 2017, Legobot"

description = 'Lego providing networking tools'
name = 'legos.nettools'
setup(
    name=name,
    version='0.1.0',
    namespace_packages=name.split('.')[:-1],
    license='MIT',
    description=description,
    author='Nitrax',
    url='https://github.com/Legobot/' + name,
    install_requires=['legobot>=1.1.4,<=2.0.0',
                      'python-whois',
                      'urllib3',
                      'bandit==1.3.0',
                      'flake8==3.2.1',
                      'pytest==3.0.5'
                      ],
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3'
    ],
    packages=find_packages()
)

Fix trove classifier for pypi

Fix trove classifier for pypi

Response:
#!/usr/bin/env python

from setuptools import setup, find_packages

__author__ = "Nitrax <nitrax@lokisec.fr>"
__copyright__ = "Copyright 2017, Legobot"

description = 'Lego providing networking tools'
name = 'legos.nettools'
setup(
    name=name,
    version='0.1.0',
    namespace_packages=name.split('.')[:-1],
    license='MIT',
    description=description,
    author='Nitrax',
    url='https://github.com/Legobot/' + name,
    install_requires=['legobot>=1.1.4,<=2.0.0',
                      'python-whois',
                      'urllib3',
                      'bandit==1.3.0',
                      'flake8==3.2.1',
                      'pytest==3.0.5'
                      ],
    classifiers=[
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3'
    ],
    packages=find_packages()
)

Prompt:
Fix trove classifier for pypi

#!/usr/bin/env python

from setuptools import setup, find_packages

__author__ = "Nitrax <nitrax@lokisec.fr>"
__copyright__ = "Copyright 2017, Legobot"

description = 'Lego providing networking tools'
name = 'legos.nettools'
setup(
    name=name,
    version='0.1.0',
    namespace_packages=name.split('.')[:-1],
    license='MIT',
    description=description,
    author='Nitrax',
    url='https://github.com/Legobot/' + name,
    install_requires=['legobot>=1.1.4,<=2.0.0',
                      'python-whois',
                      'urllib3',
                      'bandit==1.3.0',
                      'flake8==3.2.1',
                      'pytest==3.0.5'
                      ],
    classifiers=[
        'License :: MIT',

        'Programming Language :: Python :: 3'
    ],
    packages=find_packages()
)


import tornado
import time
from bson.json_util import dumps
from tornado.options import options

class FilesServiceHandler(tornado.web.RequestHandler):
    def initialize(self, logger, mongodb):
        self.logger = logger
        self.mongodb = mongodb[options.db_name]['Files']

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):
        self.logger.info('Request to file upload')

        result = []

        for item in self.request.files.values():
            for file_info in item:
                timestamp = time.time()

                data = {
                    'name': '%s-%s' % (timestamp, file_info['filename']),
                    'location': 'TBD',
                    'context': 'context',
                    'realName': file_info['filename'],
                    'mimeType': file_info['content_type'],
                    'deleted': False,
                    'timestamp': timestamp,
                    'restrictions': {
                        'quota': False,
                        'session': False
                    }
                }

                self.logger.info('File uploaded: %s with mime type %s' % (data['realName'], data['mimeType']))

                with open('%s/%s' % (options.files_dir, data['name']), 'w') as f:
                    f.write(file_info['body'])

                self.logger.info('File saved at %s' % data['name'])

                yield self.mongodb.save(data)
                result.append(data)

        self.write(dumps(result))

Save file info in DB

Save file info in DB

Response:
import tornado
import time
from bson.json_util import dumps
from tornado.options import options

class FilesServiceHandler(tornado.web.RequestHandler):
    def initialize(self, logger, mongodb):
        self.logger = logger
        self.mongodb = mongodb[options.db_name]['Files']

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):
        self.logger.info('Request to file upload')

        result = []

        for item in self.request.files.values():
            for file_info in item:
                timestamp = time.time()

                data = {
                    'name': '%s-%s' % (timestamp, file_info['filename']),
                    'location': 'TBD',
                    'context': 'context',
                    'realName': file_info['filename'],
                    'mimeType': file_info['content_type'],
                    'deleted': False,
                    'timestamp': timestamp,
                    'restrictions': {
                        'quota': False,
                        'session': False
                    }
                }

                self.logger.info('File uploaded: %s with mime type %s' % (data['realName'], data['mimeType']))

                with open('%s/%s' % (options.files_dir, data['name']), 'w') as f:
                    f.write(file_info['body'])

                self.logger.info('File saved at %s' % data['name'])

                yield self.mongodb.save(data)
                result.append(data)

        self.write(dumps(result))

Prompt:
Save file info in DB

import tornado
import time
from bson.json_util import dumps
from tornado.options import options

class FilesServiceHandler(tornado.web.RequestHandler):
    def initialize(self, logger, mongodb):
        self.logger = logger
        self.mongodb = mongodb

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):
        self.logger.info('Request to file upload')

        for item in self.request.files.values():
            file_info = item[0]

            self.logger.info('File uploaded: %s with mime type %s' % (file_info['filename'], file_info['content_type']))
            name = '%s-%s' % (time.time(), file_info['filename'])

            with open('%s/%s' % (options.files_dir, name), 'w') as f:
                f.write(file_info['body'])

            self.logger.info('File saved at %s' % name)

        self.write('done')


#!/usr/bin/env python
TRAJECTORY = 'linear'

if TRAJECTORY == 'linear':
    SIMULATION_TIME_IN_SECONDS = 80.0
elif TRAJECTORY == 'circular':
    SIMULATION_TIME_IN_SECONDS = 120.0
elif TRAJECTORY == 'squared':
    SIMULATION_TIME_IN_SECONDS = 160.0

DELTA_T = 0.1 # this is the sampling time
STEPS = int(SIMULATION_TIME_IN_SECONDS / DELTA_T)

# control constants
K_X = 0.90
K_Y = 0.90
K_THETA = 0.90

# PID control constants
K_P_V = 0.2
K_I_V = 1.905
K_D_V = 0.00

K_P_W = 0.45
K_I_W = 1.25
K_D_W = 0.000

Response:
#!/usr/bin/env python
TRAJECTORY = 'linear'

if TRAJECTORY == 'linear':
    SIMULATION_TIME_IN_SECONDS = 80.0
elif TRAJECTORY == 'circular':
    SIMULATION_TIME_IN_SECONDS = 120.0
elif TRAJECTORY == 'squared':
    SIMULATION_TIME_IN_SECONDS = 160.0

DELTA_T = 0.1 # this is the sampling time
STEPS = int(SIMULATION_TIME_IN_SECONDS / DELTA_T)

# control constants
K_X = 0.90
K_Y = 0.90
K_THETA = 0.90

# PID control constants
K_P_V = 0.2
K_I_V = 1.905
K_D_V = 0.00

K_P_W = 0.45
K_I_W = 1.25
K_D_W = 0.000

Prompt:
Increase simulation time for linear trajectory

#!/usr/bin/env python
TRAJECTORY = 'linear'

if TRAJECTORY == 'linear':
    SIMULATION_TIME_IN_SECONDS = 40.0
elif TRAJECTORY == 'circular':
    SIMULATION_TIME_IN_SECONDS = 120.0
elif TRAJECTORY == 'squared':
    SIMULATION_TIME_IN_SECONDS = 160.0

DELTA_T = 0.1 # this is the sampling time
STEPS = int(SIMULATION_TIME_IN_SECONDS / DELTA_T)

# control constants
K_X = 0.90
K_Y = 0.90
K_THETA = 0.90

# PID control constants
K_P_V = 0.2
K_I_V = 1.905
K_D_V = 0.00

K_P_W = 0.45
K_I_W = 1.25
K_D_W = 0.000


# 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 tempest.lib import decorators

from senlin.tests.tempest.api import base

class TestAPIVersionShow(base.BaseSenlinTest):

    @decorators.idempotent_id('4a270caa-9917-4acd-98ef-6636f9618037')
    def test_API_version_show(self):
        resp, body = self.client.request('GET', '/v1/')
        res = self.client._parsed_resp(resp, body)

        # Verify resp of API version show
        self.assertEqual(200, res['status'])
        self.assertIsNotNone(res['body'])
        version = res['body']
        for key in ['id', 'links', 'media-types', 'status', 'updated']:
            self.assertIn(key, version)
        self.assertEqual('1.0', version['id'])

Add tempest API test for API version show

This patch add tempest API test for API version show.

Change-Id: I1cf7cba550bb04629acab9899be310fd3b767576

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 tempest.lib import decorators

from senlin.tests.tempest.api import base

class TestAPIVersionShow(base.BaseSenlinTest):

    @decorators.idempotent_id('4a270caa-9917-4acd-98ef-6636f9618037')
    def test_API_version_show(self):
        resp, body = self.client.request('GET', '/v1/')
        res = self.client._parsed_resp(resp, body)

        # Verify resp of API version show
        self.assertEqual(200, res['status'])
        self.assertIsNotNone(res['body'])
        version = res['body']
        for key in ['id', 'links', 'media-types', 'status', 'updated']:
            self.assertIn(key, version)
        self.assertEqual('1.0', version['id'])

Prompt:
Add tempest API test for API version show

This patch add tempest API test for API version show.

Change-Id: I1cf7cba550bb04629acab9899be310fd3b767576


import time
import urllib2
from BeautifulSoup import *
# from bs4 import *

print 'Welcome to the Get Twitter tool. This tool will allow you to download a page from Twitter to be used to extract the data.'

print "Current date & time {}".format(time.strftime("%c"))
userResponse = raw_input("Please enter the full URL from the Tweet page: ")
response = urllib2.urlopen(userResponse)
html = response.read()

soup = BeautifulSoup(html)
with open(time.strftime("%c"), 'a') as f:
	f.write(html)

Response:
import time
import urllib2
from BeautifulSoup import *
# from bs4 import *

print 'Welcome to the Get Twitter tool. This tool will allow you to download a page from Twitter to be used to extract the data.'

print "Current date & time {}".format(time.strftime("%c"))
userResponse = raw_input("Please enter the full URL from the Tweet page: ")
response = urllib2.urlopen(userResponse)
html = response.read()

soup = BeautifulSoup(html)
with open(time.strftime("%c"), 'a') as f:
	f.write(html)

Prompt:
Call html page the date and time of get request

The html file that is outputted how has the current date and time as its name
import urllib2
from BeautifulSoup import *

print 'Welcome to the Get Twitter tool. This tool will allow you to download a page from Twitter to be used to extract the data'

userResponse = raw_input("Please enter the full URL from the Tweet page")
response = urllib2.urlopen(userResponse)
html = response.read()

soup = BeautifulSoup(html)
with open('TwitterPage', 'a') as f:
	f.write(html)