text:
import json
import time
import unittest2

import test.functional as tf
from copy import deepcopy

from swift.common.utils import MD5_OF_EMPTY_STRING
from test.functional.tests import Base, Base2, BaseEnv, Utils
from test.functional import cluster_info, SkipTest
from test.functional.swift_test_client import Account, Connection, \
    ResponseError

def setUpModule():
    tf.setup_package()

def tearDownModule():
    tf.teardown_package()

class TestObjectVersioningEnv(BaseEnv):
    versioning_enabled = None  # tri-state: None initially, then True/False
    location_header_key = 'X-Versions-Location'
    account2 = None

    @classmethod
    def setUp(cls):
        super(TestObjectVersioningEnv, cls).setUp()
        # Second connection for ACL tests
        config2 = deepcopy(tf.config)
        config2['account'] = tf.config['account2']
        config2['username'] = tf.config['username2']
        config2['password'] = tf.config['password2']
        cls.conn2 = Connection(config2)
        cls.conn2.authenticate()

        # avoid getting a prefix that stops halfway through an encoded
        # character
        prefix = Utils.create_name().decode("utf-8")[:10].encode("utf-8")

        cls.versions_container = cls.account.container(prefix + "-versions")
        if not cls.versions_container.create():
            raise ResponseError(cls.conn.response)

        cls.container = cls.account.container(prefix + "-objs")
        container_headers = {
            cls.location_header_key: cls.versions_container.name}
        if not cls.container.create(hdrs=container_headers):
            if cls.conn.response.status == 412:
                cls.versioning_enabled = False
                return
            raise ResponseError(cls.conn.response)

        container_info = cls.container.info()
        # if versioning is off, then cls.location_header_key won't persist
        cls.versioning_enabled = 'versions' in container_info

        # setup another account to test ACLs
        config2 = deepcopy(tf.config)
        config2['account'] = tf.config['account2']
        config2['username'] = tf.config['username2']
        config2['password'] = tf.config['password2']
        cls.conn2 = Connection(config2)
        cls.storage_url2, cls.storage_token2 = cls.conn2.authenticate()
        cls.account2 = cls.conn2.get_account()
        cls.account2.delete_containers()

        # setup another account with no access to anything to test ACLs
        config3 = deepcopy(tf.config)
        config3['account'] = tf.config['account']
        config3['username'] = tf.config['username3']
        config3['password'] = tf.config['password3']
        cls.conn3 = Connection(config3)
        cls.storage_url3, cls.storage_token3 = cls.conn3.authenticate()
        cls.account3 = cls.conn3.get_account()

    @classmethod
    def tearDown(cls):
        if cls.account:
            cls.account.delete_containers()
        if cls.account2:
            cls.account2.delete_containers()

class TestCrossPolicyObjectVersioningEnv(BaseEnv):
    # tri-state: None initially, then True/False
    versioning_enabled = None
    multiple_policies_enabled = None
    policies = None
    location_header_key = 'X-Versions-Location'
    account2 = None

    @classmethod
    def setUp(cls):
        super(TestCrossPolicyObjectVersioningEnv, cls).setUp()
        if cls.multiple_policies_enabled is None:
            try:
                cls.policies = tf.FunctionalStoragePolicyCollection.from_info()
            except AssertionError:
                pass

        if cls.policies and len(cls.policies) > 1:
            cls.multiple_policies_enabled = True
        else:
            cls.multiple_policies_enabled = False
            cls.versioning_enabled = True
            # We don't actually know the state of versioning, but without
            # multiple policies the tests should be skipped anyway. Claiming
            # versioning support lets us report the right reason for skipping.
            return

        policy = cls.policies.select()
        version_policy = cls.policies.exclude(name=policy['name']).select()

        # Second connection for ACL tests
        config2 = deepcopy(tf.config)
        config2['account'] = tf.config['account2']
        config2['username'] = tf.config['username2']
        config2['password'] = tf.config['password2']
        cls.conn2 = Connection(config2)
        cls.conn2.authenticate()

        # avoid getting a prefix that stops halfway through an encoded
        # character
        prefix = Utils.create_name().decode("utf-8")[:10].encode("utf-8")

        cls.versions_container = cls.account.container(prefix + "-versions")
        if not cls.versions_container.create(
                {'X-Storage-Policy': policy['name']}):
            raise ResponseError(cls.conn.response)

        cls.container = cls.account.container(prefix + "-objs")
        if not cls.container.create(
                hdrs={cls.location_header_key: cls.versions_container.name,
                      'X-Storage-Policy': version_policy['name']}):
            if cls.conn.response.status == 412:
                cls.versioning_enabled = False
                return
            raise ResponseError(cls.conn.response)

        container_info = cls.container.info()
        # if versioning is off, then X-Versions-Location won't persist
        cls.versioning_enabled = 'versions' in container_info

        # setup another account to test ACLs
        config2 = deepcopy(tf.config)
        config2['account'] = tf.config['account2']
        config2['username'] = tf.config['username2']
        config2['password'] = tf.config['password2']
        cls.conn2 = Connection(config2)
        cls.storage_url2, cls.storage_token2 = cls.conn2.authenticate()
        cls.account2 = cls.conn2.get_account()
        cls.account2.delete_containers()

        # setup another account with no access to anything to test ACLs
        config3 = deepcopy(tf.config)
        config3['account'] = tf.config['account']
        config3['username'] = tf.config['username3']
        config3['password'] = tf.config['password3']
        cls.conn3 = Connection(config3)
        cls.storage_url3, cls.storage_token3 = cls.conn3.authenticate()
        cls.account3 = cls.conn3.get_account()

    @classmethod
    def tearDown(cls):
        if cls.account:
            cls.account.delete_containers()
        if cls.account2:
            cls.account2.delete_containers()

class TestObjectVersioningHistoryModeEnv(TestObjectVersioningEnv):
    location_header_key = 'X-History-Location'

class TestObjectVersioning(Base):
    env = TestObjectVersioningEnv

    def setUp(self):
        super(TestObjectVersioning, self).setUp()
        if self.env.versioning_enabled is False:
            raise SkipTest("Object versioning not enabled")
        elif self.env.versioning_enabled is not True:
            # just some sanity checking
            raise Exception(
                "Expected versioning_enabled to be True/False, got %r" %
                (self.env.versioning_enabled,))

    def _tear_down_files(self):
        try:
            # only delete files and not containers
            # as they were configured in self.env
            # get rid of any versions so they aren't restored
            self.env.versions_container.delete_files()
            # get rid of originals
            self.env.container.delete_files()
            # in history mode, deleted originals got copied to versions, so
            # clear that again
            self.env.versions_container.delete_files()
        except ResponseError:
            pass

    def tearDown(self):
        super(TestObjectVersioning, self).tearDown()
        self._tear_down_files()

    def test_clear_version_option(self):
        # sanity
        self.assertEqual(self.env.container.info()['versions'],
                         self.env.versions_container.name)
        self.env.container.update_metadata(
            hdrs={self.env.location_header_key: ''})
        self.assertIsNone(self.env.container.info().get('versions'))

        # set location back to the way it was
        self.env.container.update_metadata(
            hdrs={self.env.location_header_key:
                  self.env.versions_container.name})
        self.assertEqual(self.env.container.info()['versions'],
                         self.env.versions_container.name)

    def _test_overwriting_setup(self):
        container = self.env.container
        versions_container = self.env.versions_container
        cont_info = container.info()
        self.assertEqual(cont_info['versions'], versions_container.name)
        expected_content_types = []
        obj_name = Utils.create_name()

        versioned_obj = container.file(obj_name)
        put_headers = {'Content-Type': 'text/jibberish01',
                       'Content-Encoding': 'gzip',
                       'Content-Disposition': 'attachment; filename=myfile'}
        versioned_obj.write("aaaaa", hdrs=put_headers)
        obj_info = versioned_obj.info()
        self.assertEqual('text/jibberish01', obj_info['content_type'])
        expected_content_types.append('text/jibberish01')

        # the allowed headers are configurable in object server, so we cannot
        # assert that content-encoding or content-disposition get *copied* to
        # the object version unless they were set on the original PUT, so
        # populate expected_headers by making a HEAD on the original object
        resp_headers = dict(versioned_obj.conn.response.getheaders())
        expected_headers = {}
        for k, v in put_headers.items():
            if k.lower() in resp_headers:
                expected_headers[k] = v

        self.assertEqual(0, versions_container.info()['object_count'])
        versioned_obj.write("bbbbb", hdrs={'Content-Type': 'text/jibberish02',
                            'X-Object-Meta-Foo': 'Bar'})
        versioned_obj.initialize()
        self.assertEqual(versioned_obj.content_type, 'text/jibberish02')
        expected_content_types.append('text/jibberish02')
        self.assertEqual(versioned_obj.metadata['foo'], 'Bar')

        # the old version got saved off
        self.assertEqual(1, versions_container.info()['object_count'])
        versioned_obj_name = versions_container.files()[0]
        prev_version = versions_container.file(versioned_obj_name)
        prev_version.initialize()
        self.assertEqual("aaaaa", prev_version.read())
        self.assertEqual(prev_version.content_type, 'text/jibberish01')

        resp_headers = dict(prev_version.conn.response.getheaders())
        for k, v in expected_headers.items():
            self.assertIn(k.lower(), resp_headers)
            self.assertEqual(v, resp_headers[k.lower()])

        # make sure the new obj metadata did not leak to the prev. version
        self.assertNotIn('foo', prev_version.metadata)

        # check that POST does not create a new version
        versioned_obj.sync_metadata(metadata={'fu': 'baz'})
        self.assertEqual(1, versions_container.info()['object_count'])
        expected_content_types.append('text/jibberish02')

        # if we overwrite it again, there are two versions
        versioned_obj.write("ccccc")
        self.assertEqual(2, versions_container.info()['object_count'])
        versioned_obj_name = versions_container.files()[1]
        prev_version = versions_container.file(versioned_obj_name)
        prev_version.initialize()
        self.assertEqual("bbbbb", prev_version.read())
        self.assertEqual(prev_version.content_type, 'text/jibberish02')
        self.assertNotIn('foo', prev_version.metadata)
        self.assertIn('fu', prev_version.metadata)

        # versioned_obj keeps the newest content
        self.assertEqual("ccccc", versioned_obj.read())

        # test copy from a different container
        src_container = self.env.account.container(Utils.create_name())
        self.assertTrue(src_container.create())
        src_name = Utils.create_name()
        src_obj = src_container.file(src_name)
        src_obj.write("ddddd", hdrs={'Content-Type': 'text/jibberish04'})
        src_obj.copy(container.name, obj_name)

        self.assertEqual("ddddd", versioned_obj.read())
        versioned_obj.initialize()
        self.assertEqual(versioned_obj.content_type, 'text/jibberish04')
        expected_content_types.append('text/jibberish04')

        # make sure versions container has the previous version
        self.assertEqual(3, versions_container.info()['object_count'])
        versioned_obj_name = versions_container.files()[2]
        prev_version = versions_container.file(versioned_obj_name)
        prev_version.initialize()
        self.assertEqual("ccccc", prev_version.read())

        # for further use in the mode-specific tests
        return (versioned_obj, expected_headers, expected_content_types)

    def test_overwriting(self):
        versions_container = self.env.versions_container
        versioned_obj, expected_headers, expected_content_types = \
            self._test_overwriting_setup()

        # pop one for the current version
        expected_content_types.pop()
        self.assertEqual(expected_content_types, [
            o['content_type'] for o in versions_container.files(
                parms={'format': 'json'})])

        # test delete
        versioned_obj.delete()
        self.assertEqual("ccccc", versioned_obj.read())
        expected_content_types.pop()
        self.assertEqual(expected_content_types, [
            o['content_type'] for o in versions_container.files(
                parms={'format': 'json'})])

        versioned_obj.delete()
        self.assertEqual("bbbbb", versioned_obj.read())
        expected_content_types.pop()
        self.assertEqual(expected_content_types, [
            o['content_type'] for o in versions_container.files(
                parms={'format': 'json'})])

        versioned_obj.delete()
        self.assertEqual("aaaaa", versioned_obj.read())
        self.assertEqual(0, versions_container.info()['object_count'])

        # verify that all the original object headers have been copied back
        obj_info = versioned_obj.info()
        self.assertEqual('text/jibberish01', obj_info['content_type'])
        resp_headers = dict(versioned_obj.conn.response.getheaders())
        for k, v in expected_headers.items():
            self.assertIn(k.lower(), resp_headers)
            self.assertEqual(v, resp_headers[k.lower()])

        versioned_obj.delete()
        self.assertRaises(ResponseError, versioned_obj.read)

    def assert_most_recent_version(self, obj_name, content,
                                   should_be_dlo=False):
        archive_versions = self.env.versions_container.files(parms={
            'prefix': '%03x%s/' % (len(obj_name), obj_name),
            'reverse': 'yes'})
        archive_file = self.env.versions_container.file(archive_versions[0])
        self.assertEqual(content, archive_file.read())
        resp_headers = dict(archive_file.conn.response.getheaders())
        if should_be_dlo:
            self.assertIn('x-object-manifest', resp_headers)
        else:
            self.assertNotIn('x-object-manifest', resp_headers)

    def _test_versioning_dlo_setup(self):
        if tf.in_process:
            tf.skip_if_no_xattrs()

        container = self.env.container
        versions_container = self.env.versions_container
        obj_name = Utils.create_name()

        for i in ('1', '2', '3'):
            time.sleep(.01)  # guarantee that the timestamp changes
            obj_name_seg = obj_name + '/' + i
            versioned_obj = container.file(obj_name_seg)
            versioned_obj.write(i)
            # immediately overwrite
            versioned_obj.write(i + i)

        self.assertEqual(3, versions_container.info()['object_count'])

        man_file = container.file(obj_name)

        # write a normal file first
        man_file.write('old content')

        # guarantee that the timestamp changes
        time.sleep(.01)

        # overwrite with a dlo manifest
        man_file.write('', hdrs={"X-Object-Manifest": "%s/%s/" %
                       (self.env.container.name, obj_name)})

        self.assertEqual(4, versions_container.info()['object_count'])
        self.assertEqual("112233", man_file.read())
        self.assert_most_recent_version(obj_name, 'old content')

        # overwrite the manifest with a normal file
        man_file.write('new content')
        self.assertEqual(5, versions_container.info()['object_count'])

        # new most-recent archive is the dlo
        self.assert_most_recent_version(obj_name, '112233', should_be_dlo=True)

        return obj_name, man_file

    def test_versioning_dlo(self):
        obj_name, man_file = self._test_versioning_dlo_setup()

        # verify that restore works properly
        man_file.delete()
        self.assertEqual(4, self.env.versions_container.info()['object_count'])
        self.assertEqual("112233", man_file.read())
        resp_headers = dict(man_file.conn.response.getheaders())
        self.assertIn('x-object-manifest', resp_headers)

        self.assert_most_recent_version(obj_name, 'old content')

        man_file.delete()
        self.assertEqual(3, self.env.versions_container.info()['object_count'])
        self.assertEqual("old content", man_file.read())

    def test_versioning_container_acl(self):
        # create versions container and DO NOT give write access to account2
        versions_container = self.env.account.container(Utils.create_name())
        self.assertTrue(versions_container.create(hdrs={
            'X-Container-Write': ''
        }))

        # check account2 cannot write to versions container
        fail_obj_name = Utils.create_name()
        fail_obj = versions_container.file(fail_obj_name)
        self.assertRaises(ResponseError, fail_obj.write, "should fail",
                          cfg={'use_token': self.env.storage_token2})

        # create container and give write access to account2
        # don't set X-Versions-Location just yet
        container = self.env.account.container(Utils.create_name())
        self.assertTrue(container.create(hdrs={
            'X-Container-Write': self.env.conn2.user_acl}))

        # check account2 cannot set X-Versions-Location on container
        self.assertRaises(ResponseError, container.update_metadata, hdrs={
            self.env.location_header_key: versions_container},
            cfg={'use_token': self.env.storage_token2})

        # good! now let admin set the X-Versions-Location
        # p.s.: sticking a 'x-remove' header here to test precedence
        # of both headers. Setting the location should succeed.
        self.assertTrue(container.update_metadata(hdrs={
            'X-Remove-' + self.env.location_header_key[len('X-'):]:
            versions_container,
            self.env.location_header_key: versions_container}))

        # write object twice to container and check version
        obj_name = Utils.create_name()
        versioned_obj = container.file(obj_name)
        self.assertTrue(versioned_obj.write("never argue with the data",
                        cfg={'use_token': self.env.storage_token2}))
        self.assertEqual(versioned_obj.read(), "never argue with the data")

        self.assertTrue(
            versioned_obj.write("we don't have no beer, just tequila",
                                cfg={'use_token': self.env.storage_token2}))
        self.assertEqual(versioned_obj.read(),
                         "we don't have no beer, just tequila")
        self.assertEqual(1, versions_container.info()['object_count'])

        # read the original uploaded object
        for filename in versions_container.files():
            backup_file = versions_container.file(filename)
            break
        self.assertEqual(backup_file.read(), "never argue with the data")

        # user3 (some random user with no access to any of account1)
        # tries to read from versioned container
        self.assertRaises(ResponseError, backup_file.read,
                          cfg={'use_token': self.env.storage_token3})

        # create an object user3 can try to copy
        a2_container = self.env.account2.container(Utils.create_name())
        a2_container.create(
            hdrs={'X-Container-Read': self.env.conn3.user_acl},
            cfg={'use_token': self.env.storage_token2})
        a2_obj = a2_container.file(Utils.create_name())
        self.assertTrue(a2_obj.write("unused",
                        cfg={'use_token': self.env.storage_token2}))

        # user3 cannot write, delete, or copy to/from source container either
        number_of_versions = versions_container.info()['object_count']
        self.assertRaises(ResponseError, versioned_obj.write,
                          "some random user trying to write data",
                          cfg={'use_token': self.env.storage_token3})
        self.assertEqual(number_of_versions,
                         versions_container.info()['object_count'])
        self.assertRaises(ResponseError, versioned_obj.delete,
                          cfg={'use_token': self.env.storage_token3})
        self.assertEqual(number_of_versions,
                         versions_container.info()['object_count'])
        self.assertRaises(
            ResponseError, versioned_obj.write,
            hdrs={'X-Copy-From': '%s/%s' % (a2_container.name, a2_obj.name),
                  'X-Copy-From-Account': self.env.conn2.account_name},
            cfg={'use_token': self.env.storage_token3})
        self.assertEqual(number_of_versions,
                         versions_container.info()['object_count'])
        self.assertRaises(
            ResponseError, a2_obj.copy_account,
            self.env.conn.account_name, container.name, obj_name,
            cfg={'use_token': self.env.storage_token3})
        self.assertEqual(number_of_versions,
                         versions_container.info()['object_count'])

        # user2 can't read or delete from versions-location
        self.assertRaises(ResponseError, backup_file.read,
                          cfg={'use_token': self.env.storage_token2})
        self.assertRaises(ResponseError, backup_file.delete,
                          cfg={'use_token': self.env.storage_token2})

        # but is able to delete from the source container
        # this could be a helpful scenario for dev ops that want to setup
        # just one container to hold object versions of multiple containers
        # and each one of those containers are owned by different users
        self.assertTrue(versioned_obj.delete(
                        cfg={'use_token': self.env.storage_token2}))

        # tear-down since we create these containers here
        # and not in self.env
        a2_container.delete_recursive()
        versions_container.delete_recursive()
        container.delete_recursive()

    def _test_versioning_check_acl_setup(self):
        container = self.env.container
        versions_container = self.env.versions_container
        versions_container.create(hdrs={'X-Container-Read': '.r:*,.rlistings'})

        obj_name = Utils.create_name()
        versioned_obj = container.file(obj_name)
        versioned_obj.write("aaaaa")
        self.assertEqual("aaaaa", versioned_obj.read())

        versioned_obj.write("bbbbb")
        self.assertEqual("bbbbb", versioned_obj.read())

        # Use token from second account and try to delete the object
        org_token = self.env.account.conn.storage_token
        self.env.account.conn.storage_token = self.env.conn2.storage_token
        try:
            with self.assertRaises(ResponseError) as cm:
                versioned_obj.delete()
            self.assertEqual(403, cm.exception.status)
        finally:
            self.env.account.conn.storage_token = org_token

        # Verify with token from first account
        self.assertEqual("bbbbb", versioned_obj.read())
        return versioned_obj

    def test_versioning_check_acl(self):
        versioned_obj = self._test_versioning_check_acl_setup()
        versioned_obj.delete()
        self.assertEqual("aaaaa", versioned_obj.read())

    def _check_overwriting_symlink(self):
        # assertions common to x-versions-location and x-history-location modes
        container = self.env.container
        versions_container = self.env.versions_container

        tgt_a_name = Utils.create_name()
        tgt_b_name = Utils.create_name()

        tgt_a = container.file(tgt_a_name)
        tgt_a.write("aaaaa")

        tgt_b = container.file(tgt_b_name)
        tgt_b.write("bbbbb")

        symlink_name = Utils.create_name()
        sym_tgt_header = '%s/%s' % (container.name, tgt_a_name)
        sym_headers_a = {'X-Symlink-Target': sym_tgt_header}
        symlink = container.file(symlink_name)
        symlink.write("", hdrs=sym_headers_a)
        self.assertEqual("aaaaa", symlink.read())

        sym_headers_b = {'X-Symlink-Target': '%s/%s' % (container.name,
                                                        tgt_b_name)}
        symlink.write("", hdrs=sym_headers_b)
        self.assertEqual("bbbbb", symlink.read())

        # the old version got saved off
        self.assertEqual(1, versions_container.info()['object_count'])
        versioned_obj_name = versions_container.files()[0]
        prev_version = versions_container.file(versioned_obj_name)
        prev_version_info = prev_version.info(parms={'symlink': 'get'})
        self.assertEqual("aaaaa", prev_version.read())
        self.assertEqual(MD5_OF_EMPTY_STRING, prev_version_info['etag'])
        self.assertEqual(sym_tgt_header,
                         prev_version_info['x_symlink_target'])
        return symlink, tgt_a

    def test_overwriting_symlink(self):
        if 'symlink' not in cluster_info:
            raise SkipTest("Symlinks not enabled")

        symlink, target = self._check_overwriting_symlink()
        # test delete
        symlink.delete()
        sym_info = symlink.info(parms={'symlink': 'get'})
        self.assertEqual("aaaaa", symlink.read())
        self.assertEqual(MD5_OF_EMPTY_STRING, sym_info['etag'])
        self.assertEqual('%s/%s' % (self.env.container.name, target.name),
                         sym_info['x_symlink_target'])

    def _setup_symlink(self):
        target = self.env.container.file('target-object')
        target.write('target object data')
        symlink = self.env.container.file('symlink')
        symlink.write('', hdrs={
            'Content-Type': 'application/symlink',
            'X-Symlink-Target': '%s/%s' % (
                self.env.container.name, target.name)})
        return symlink, target

    def _assert_symlink(self, symlink, target):
        self.assertEqual('target object data', symlink.read())
        self.assertEqual(target.info(), symlink.info())
        self.assertEqual('application/symlink',
                         symlink.info(parms={
                             'symlink': 'get'})['content_type'])

    def _check_copy_destination_restore_symlink(self):
        # assertions common to x-versions-location and x-history-location modes
        symlink, target = self._setup_symlink()
        symlink.write('this is not a symlink')
        # the symlink is versioned
        version_container_files = self.env.versions_container.files(
            parms={'format': 'json'})
        self.assertEqual(1, len(version_container_files))
        versioned_obj_info = version_container_files[0]
        self.assertEqual('application/symlink',
                         versioned_obj_info['content_type'])
        versioned_obj = self.env.versions_container.file(
            versioned_obj_info['name'])
        # the symlink is still a symlink
        self._assert_symlink(versioned_obj, target)
        # test manual restore (this creates a new backup of the overwrite)
        versioned_obj.copy(self.env.container.name, symlink.name,
                           parms={'symlink': 'get'})
        self._assert_symlink(symlink, target)
        # symlink overwritten by write then copy -> 2 versions
        self.assertEqual(2, self.env.versions_container.info()['object_count'])
        return symlink, target

    def test_copy_destination_restore_symlink(self):
        if 'symlink' not in cluster_info:
            raise SkipTest("Symlinks not enabled")

        symlink, target = self._check_copy_destination_restore_symlink()
        # and versioned writes restore
        symlink.delete()
        self.assertEqual(1, self.env.versions_container.info()['object_count'])
        self.assertEqual('this is not a symlink', symlink.read())
        symlink.delete()
        self.assertEqual(0, self.env.versions_container.info()['object_count'])
        self._assert_symlink(symlink, target)

    def test_put_x_copy_from_restore_symlink(self):
        if 'symlink' not in cluster_info:
            raise SkipTest("Symlinks not enabled")

        symlink, target = self._setup_symlink()
        symlink.write('this is not a symlink')
        version_container_files = self.env.versions_container.files()
        self.assertEqual(1, len(version_container_files))
        versioned_obj = self.env.versions_container.file(
            version_container_files[0])
        symlink.write(parms={'symlink': 'get'}, cfg={
            'no_content_type': True}, hdrs={
                'X-Copy-From': '%s/%s' % (
                    self.env.versions_container, versioned_obj.name)})
        self._assert_symlink(symlink, target)

class TestObjectVersioningUTF8(Base2, TestObjectVersioning):

    def tearDown(self):
        self._tear_down_files()
        super(TestObjectVersioningUTF8, self).tearDown()

class TestCrossPolicyObjectVersioning(TestObjectVersioning):
    env = TestCrossPolicyObjectVersioningEnv

    def setUp(self):
        super(TestCrossPolicyObjectVersioning, self).setUp()
        if self.env.multiple_policies_enabled is False:
            raise SkipTest('Cross policy test requires multiple policies')
        elif self.env.multiple_policies_enabled is not True:
            # just some sanity checking
            raise Exception("Expected multiple_policies_enabled "
                            "to be True/False, got %r" % (
                                self.env.versioning_enabled,))

class TestObjectVersioningHistoryMode(TestObjectVersioning):
    env = TestObjectVersioningHistoryModeEnv

    # those override tests includes assertions for delete versioned objects
    # behaviors different from default object versioning using
    # x-versions-location.

    def test_overwriting(self):
        versions_container = self.env.versions_container
        versioned_obj, expected_headers, expected_content_types = \
            self._test_overwriting_setup()

        # test delete
        # at first, delete will succeed with 204
        versioned_obj.delete()
        expected_content_types.append(
            'application/x-deleted;swift_versions_deleted=1')
        # after that, any time the delete doesn't restore the old version
        # and we will get 404 NotFound
        for x in range(3):
            with self.assertRaises(ResponseError) as cm:
                versioned_obj.delete()
            self.assertEqual(404, cm.exception.status)
            expected_content_types.append(
                'application/x-deleted;swift_versions_deleted=1')
        # finally, we have 4 versioned items and 4 delete markers total in
        # the versions container
        self.assertEqual(8, versions_container.info()['object_count'])
        self.assertEqual(expected_content_types, [
            o['content_type'] for o in versions_container.files(
                parms={'format': 'json'})])

        # update versioned_obj
        versioned_obj.write("eeee", hdrs={'Content-Type': 'text/thanksgiving',
                            'X-Object-Meta-Bar': 'foo'})
        # verify the PUT object is kept successfully
        obj_info = versioned_obj.info()
        self.assertEqual('text/thanksgiving', obj_info['content_type'])

        # we still have delete-marker there
        self.assertEqual(8, versions_container.info()['object_count'])

        # update versioned_obj
        versioned_obj.write("ffff", hdrs={'Content-Type': 'text/teriyaki',
                            'X-Object-Meta-Food': 'chickin'})
        # verify the PUT object is kept successfully
        obj_info = versioned_obj.info()
        self.assertEqual('text/teriyaki', obj_info['content_type'])

        # new obj will be inserted after delete-marker there
        self.assertEqual(9, versions_container.info()['object_count'])

        versioned_obj.delete()
        with self.assertRaises(ResponseError) as cm:
            versioned_obj.read()
        self.assertEqual(404, cm.exception.status)
        self.assertEqual(11, versions_container.info()['object_count'])

    def test_versioning_dlo(self):
        obj_name, man_file = \
            self._test_versioning_dlo_setup()

        man_file.delete()
        with self.assertRaises(ResponseError) as cm:
            man_file.read()
        self.assertEqual(404, cm.exception.status)
        self.assertEqual(7, self.env.versions_container.info()['object_count'])

        expected = ['old content', '112233', 'new content', '']

        bodies = [
            self.env.versions_container.file(f).read()
            for f in self.env.versions_container.files(parms={
                'prefix': '%03x%s/' % (len(obj_name), obj_name)})]
        self.assertEqual(expected, bodies)

    def test_versioning_check_acl(self):
        versioned_obj = self._test_versioning_check_acl_setup()
        versioned_obj.delete()
        with self.assertRaises(ResponseError) as cm:
            versioned_obj.read()
        self.assertEqual(404, cm.exception.status)

        # we have 3 objects in the versions_container, 'aaaaa', 'bbbbb'
        # and delete-marker with empty content
        self.assertEqual(3, self.env.versions_container.info()['object_count'])
        files = self.env.versions_container.files()
        for actual, expected in zip(files, ['aaaaa', 'bbbbb', '']):
            prev_version = self.env.versions_container.file(actual)
            self.assertEqual(expected, prev_version.read())

    def test_overwriting_symlink(self):
        if 'symlink' not in cluster_info:
            raise SkipTest("Symlinks not enabled")

        symlink, target = self._check_overwriting_symlink()
        # test delete
        symlink.delete()
        with self.assertRaises(ResponseError) as cm:
            symlink.read()
        self.assertEqual(404, cm.exception.status)

    def test_copy_destination_restore_symlink(self):
        if 'symlink' not in cluster_info:
            raise SkipTest("Symlinks not enabled")

        symlink, target = self._check_copy_destination_restore_symlink()
        symlink.delete()
        with self.assertRaises(ResponseError) as cm:
            symlink.read()
        self.assertEqual(404, cm.exception.status)
        # 2 versions plus delete marker and deleted version
        self.assertEqual(4, self.env.versions_container.info()['object_count'])

class TestSloWithVersioning(unittest2.TestCase):

    def setUp(self):
        if 'slo' not in cluster_info:
            raise SkipTest("SLO not enabled")
        if tf.in_process:
            tf.skip_if_no_xattrs()

        self.conn = Connection(tf.config)
        self.conn.authenticate()
        self.account = Account(
            self.conn, tf.config.get('account', tf.config['username']))
        self.account.delete_containers()

        # create a container with versioning
        self.versions_container = self.account.container(Utils.create_name())
        self.container = self.account.container(Utils.create_name())
        self.segments_container = self.account.container(Utils.create_name())
        if not self.container.create(
                hdrs={'X-Versions-Location': self.versions_container.name}):
            raise ResponseError(self.conn.response)
        if 'versions' not in self.container.info():
            raise SkipTest("Object versioning not enabled")

        for cont in (self.versions_container, self.segments_container):
            if not cont.create():
                raise ResponseError(self.conn.response)

        # create some segments
        self.seg_info = {}
        for letter, size in (('a', 1024 * 1024),
                             ('b', 1024 * 1024)):
            seg_name = letter
            file_item = self.segments_container.file(seg_name)
            file_item.write(letter * size)
            self.seg_info[seg_name] = {
                'size_bytes': size,
                'etag': file_item.md5,
                'path': '/%s/%s' % (self.segments_container.name, seg_name)}

    def _create_manifest(self, seg_name):
        # create a manifest in the versioning container
        file_item = self.container.file("my-slo-manifest")
        file_item.write(
            json.dumps([self.seg_info[seg_name]]),
            parms={'multipart-manifest': 'put'})
        return file_item

    def _assert_is_manifest(self, file_item, seg_name):
        manifest_body = file_item.read(parms={'multipart-manifest': 'get'})
        resp_headers = dict(file_item.conn.response.getheaders())
        self.assertIn('x-static-large-object', resp_headers)
        self.assertEqual('application/json; charset=utf-8',
                         file_item.content_type)
        try:
            manifest = json.loads(manifest_body)
        except ValueError:
            self.fail("GET with multipart-manifest=get got invalid json")

        self.assertEqual(1, len(manifest))
        key_map = {'etag': 'hash', 'size_bytes': 'bytes', 'path': 'name'}
        for k_client, k_slo in key_map.items():
            self.assertEqual(self.seg_info[seg_name][k_client],
                             manifest[0][k_slo])

    def _assert_is_object(self, file_item, seg_name):
        file_contents = file_item.read()
        self.assertEqual(1024 * 1024, len(file_contents))
        self.assertEqual(seg_name, file_contents[0])
        self.assertEqual(seg_name, file_contents[-1])

    def tearDown(self):
        # remove versioning to allow simple container delete
        self.container.update_metadata(hdrs={'X-Versions-Location': ''})
        self.account.delete_containers()

    def test_slo_manifest_version(self):
        file_item = self._create_manifest('a')
        # sanity check: read the manifest, then the large object
        self._assert_is_manifest(file_item, 'a')
        self._assert_is_object(file_item, 'a')

        # upload new manifest
        file_item = self._create_manifest('b')
        # sanity check: read the manifest, then the large object
        self._assert_is_manifest(file_item, 'b')
        self._assert_is_object(file_item, 'b')

        versions_list = self.versions_container.files()
        self.assertEqual(1, len(versions_list))
        version_file = self.versions_container.file(versions_list[0])
        # check the version is still a manifest
        self._assert_is_manifest(version_file, 'a')
        self._assert_is_object(version_file, 'a')

        # delete the newest manifest
        file_item.delete()

        # expect the original manifest file to be restored
        self._assert_is_manifest(file_item, 'a')
        self._assert_is_object(file_item, 'a')


text:
/* @flow */
import { SIZE, LABEL_POSITION } from './constants';

import type {
  ComponentTheme,
  ComponentThemeFn,
  ThemeValue
} from '../themes/types';

type LabelPosition = $Keys<typeof LABEL_POSITION>;
type Size = $Keys<typeof SIZE>;

export type CheckboxProps = {
  checked?: boolean,
  className?: string,
  defaultChecked?: boolean,
  defaultIndeterminate?: boolean,
  disabled?: boolean,
  hideLabel?: boolean,
  indeterminate?: boolean,
  inputRef?: (node: ?HTMLInputElement) => void,
  invalid?: boolean,
  justify?: boolean,
  label: string | React$Element<*>,
  labelPosition?: LabelPosition,
  name?: string,
  onChange?: (event: SyntheticInputEvent<>) => void,
  onClick?: (event: SyntheticInputEvent<>) => void,
  required?: boolean,
  rootProps?: Object,
  size?: Size,
  value?: string
};

export type CheckboxDefaultProps = {
  labelPosition: LabelPosition,
  size: Size
};

export type CheckboxGroupProps = {
  checked?: Array<string>,
  children?: React$Node,
  data?: Array<{ label: string | React$Element<*>, value: string }>,
  defaultChecked?: Array<string>,
  inline?: boolean,
  name: string,
  onChange?: (event: SyntheticEvent<>) => void,
  rootProps?: Object
};

export type CheckboxThemeFn = ComponentThemeFn<CheckboxTheme>;
export type CheckboxTheme = ComponentTheme<CheckboxThemeKeys>;
type CheckboxThemeKeys = {|
  CheckboxControl_backgroundColor: ThemeValue,
  CheckboxControl_backgroundColor_checked: ThemeValue,
  CheckboxControl_backgroundColor_checkedHover: ThemeValue,
  CheckboxControl_borderColor: ThemeValue,
  CheckboxControl_borderColor_hover: ThemeValue,
  CheckboxControl_borderColor_checked: ThemeValue,
  CheckboxControl_borderColor_checkedHover: ThemeValue,
  CheckboxControl_borderRadius: ThemeValue,
  CheckboxControl_boxShadow_focus: ThemeValue,
  CheckboxControl_size: ThemeValue,
  CheckboxControl_size_jumbo: ThemeValue,
  CheckboxText_color: ThemeValue,
  CheckboxText_fontSize: ThemeValue,
  CheckboxText_fontSize_small: ThemeValue,
  CheckboxText_marginHorizontal: ThemeValue
|};

export type CheckboxGroupThemeFn = ComponentThemeFn<CheckboxGroupTheme>;
export type CheckboxGroupTheme = ComponentTheme<CheckboxGroupThemeKeys>;
type CheckboxGroupThemeKeys = {|
  CheckboxGroupControl_marginHorizontal_inline: ThemeValue,
  CheckboxGroupControl_marginVertical_stacked: ThemeValue,
  CheckboxGroupControl_marginVertical_stackedJumbo: ThemeValue
|};

redpajama_set_name: RedPajamaGithub


text:
//
// Copyright 2013 Mike Friesen
//
// 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.
//

package ca.gobits.bnf.parser;

import java.util.List;

import ca.gobits.bnf.parser.BNFSymbol.BNFRepetition;
import ca.gobits.bnf.tokenizer.BNFToken;

/**
 * BNFParserState holds the states of the parser.
 */
public class BNFParserState {

    /** BNFParserRepetition. */
    public enum BNFParserRepetition {
        /** NONE. */
        NONE,
        /** ZERO OR MORE. */
        ZERO_OR_MORE,
        /** ZERO OR MORE LOOKING FOR FIRST MATCH. */
        ZERO_OR_MORE_LOOKING_FOR_FIRST_MATCH
    }

    /** BNFParser State. */
    public enum ParserState {
        /** NONE. */
        NONE,
        /** MATCH. */
        MATCH,
        /** NO_MATCH_WITH_ZERO_REPETITION_LOOKING_FOR_FIRST_MATCH. */
        NO_MATCH_WITH_ZERO_REPETITION_LOOKING_FOR_FIRST_MATCH,
        /** NO_MATCH. */
        NO_MATCH,
        /** MATCH_WITH_ZERO_REPETITION. */
        MATCH_WITH_ZERO_REPETITION,
        /** NO_MATCH_WITH_ZERO_REPETITION. */
        NO_MATCH_WITH_ZERO_REPETITION,
        /** EMPTY. */
        EMPTY
    }

    /** position in sequence or symbols. */
    private int currentPosition = -1;

    /** current state. */
    private ParserState state;

    /** original token. */
    private BNFToken originalToken;

    /** current token. */
    private BNFToken currentToken;

    /** List<BNFSequence>. */
    private List<BNFSequence> sequences;

    /** BNFSequence. */
    private BNFSequence sequence;

    /** BNFRepetition. */
    private BNFRepetition repetition;

    /** BNFParserRepetition. */
    private BNFParserRepetition parserRepetition;

    /**
     * default constructor.
     */
    private BNFParserState() {
        setState(ParserState.NONE);
        this.parserRepetition = BNFParserRepetition.NONE;
    }

    /**
     * constructor.
     * @param parserState -
     */
    public BNFParserState(final ParserState parserState) {
        this();
        setState(parserState);
    }

    /**
     * constructor.
     * @param seqs -
     * @param token
     *            -
     */
    public BNFParserState(final List<BNFSequence> seqs, final BNFToken token) {
        this(token);
        this.sequences = seqs;
    }

    /**
     * constructor.
     * @param token -
     */
    private BNFParserState(final BNFToken token) {
        this();
        this.originalToken = token;
        this.currentToken = this.originalToken;
    }

    /**
     * constructor.
     * @param seq -
     * @param token -
     */
    public BNFParserState(final BNFSequence seq, final BNFToken token) {
        this(token);
        this.sequence = seq;
    }

    /**
     * constructor.
     * @param sd -
     * @param token -
     * @param parserRep -
     * @param rep -
     */
    public BNFParserState(final List<BNFSequence> sd, final BNFToken token, final BNFParserRepetition parserRep, final BNFRepetition rep) {
        this(sd, token);
        this.parserRepetition = parserRep;
        this.repetition = rep;
    }

    /**
     * constructor.
     *
     * @param seq
     *            -
     * @param token
     *            -
     * @param parserRep
     *            -
     * @param rep
     *            -
     */
    public BNFParserState(final BNFSequence seq, final BNFToken token,
            final BNFParserRepetition parserRep, final BNFRepetition rep) {
        this(seq, token);
        this.parserRepetition = parserRep;
        this.repetition = rep;
    }

    /**
     * @param token
     *            -
     */
    public void advanceToken(final BNFToken token) {
        this.currentToken = token;
    }

    /**
     * reset token value to original.
     */
    public void resetToken() {
        this.currentToken = this.originalToken;
    }

    /**
     * @return boolean
     */
    public boolean isSequences() {
        return this.sequences != null;
    }

    /**
     * @return boolean
     */
    public boolean isSequence() {
        return this.sequence != null;
    }

    /**
     * @return BNFToken
     */
    public BNFToken getCurrentToken() {
        return this.currentToken;
    }

    /**
     * @return BNFSequences
     */
    public List<BNFSequence> getSequences() {
        return this.sequences;
    }

    /**
     * @return boolean
     */
    public boolean isComplete() {
        return this.isCompleteSequence() || isCompleteSymbol();
    }

    /**
     * @return BNFSequence
     */
    public BNFSequence getSequence() {
        return this.sequence;
    }

    /**
     * @return HolderState
     */
    public ParserState getState() {
        return this.state;
    }

    /**
     * @param parserState -
     */
    public void setState(final ParserState parserState) {
        this.state = parserState;
    }

    @Override
    public String toString() {
        if (this.sequences != null) {
            return this.sequences.toString();
        }

        if (this.sequence != null) {
            return this.sequence.toString();
        }

        return "status " + this.state;
    }

    /**
     * @return BNFParserRepetition
     */
    public BNFParserRepetition getParserRepetition() {
        return this.parserRepetition;
    }

    /**
     * @param rep -
     */
    public void setParserRepetition(final BNFParserRepetition rep) {
        this.parserRepetition = rep;
    }

    /**
     * @return BNFSequence
     */
    public BNFSequence getNextSequence() {

        BNFSequence seq = null;
        int i = this.currentPosition + 1;

        if (i < this.getSequences().size()) {
            seq = this.getSequences().get(i);
            this.currentPosition = i;
        }

        return seq;
    }

    /**
     * @return boolean
     */
    public boolean isCompleteSequence() {
        return this.sequences != null
                && this.currentPosition >= this.getSequences().size() - 1;
    }

    /**
     * @return boolean
     */
    public BNFSymbol getNextSymbol() {

        BNFSymbol symbol = null;
        int i = this.currentPosition + 1;

        if (i < this.sequence.getSymbols().size()) {
            symbol = this.sequence.getSymbols().get(i);
            this.currentPosition = i;
        }

        return symbol;
    }

    /**
     * @return boolean
     */
    public boolean isCompleteSymbol() {
        return this.sequence != null
                && this.currentPosition >= this.sequence.getSymbols().size() - 1;
    }

    /**
     * @return BNFRepetition
     */
    public BNFRepetition getRepetition() {
        return this.repetition;
    }

    /**
     * reset parser position.
     */
    public void reset() {
        this.currentPosition = -1;
    }
}


text:
package ontologies;

import com.hp.hpl.jena.rdf.model.*;

/**
 * Vocabulary definitions from http://vocabularies.wikipathways.org/wp.rdf
 * @author Auto-generated by schemagen on 29 Jan 2013 14:26
 */
public class Wp {
    /**
The RDF model that holds the vocabulary terms
 */
    private static Model m_model = ModelFactory.createDefaultModel();

    /**
The namespace of the vocabulary as a string
 */
    public static final String NS = "http://vocabularies.wikipathways.org/wp#";

    /**
The namespace of the vocabulary as a string

     *  @see #NS */
    public static String getURI() {return NS;}

    /**
The namespace of the vocabulary as a resource
 */
    public static final Resource NAMESPACE = m_model.createResource( NS );

    public static final Property bdbChemspider = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbChemspider" );

    public static final Property bdbHgncSymbol = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbHgncSymbol" );

    public static final Property bdbEnsembl = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbEnsembl" );

    public static final Property bdbEntrezGene = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbEntrezGene" );

    public static final Property bdbHmdb = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbHmdb" );

    public static final Property bdbUniprot = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#bdbUniprot" );

    /**
Indicates the pathway category to which a selected pathway adheres
 */
    public static final Property category = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#category" );

    public static final Property email = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#email" );

    /**
DC.identifier
 */
    public static final Property hasIdentifier = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#hasIdentifier" );

    public static final Property hasParticipant = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#hasParticipant" );

    public static final Property hasPathway = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#hasPathway" );

    public static final Property interactionTarget = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#interactionTarget" );

    public static final Property organism = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#organism" );

    public static final Property pathwayOntology = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#pathwayOntology" );

    /**
Links to FOAF
 */
    public static final Property realName = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#realName" );

    /**
DCidentifier
 */
    public static final Property revision = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#revision" );

    public static final Property unifiedIdentifier = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#unifiedIdentifier" );

    /**
Links to FOAF, Sciencepeople
 */
    public static final Property username = m_model.createProperty( "http://vocabularies.wikipathways.org/wp#username" );

    /**
Definition: An author is creator, editor, or curator of a pathway. The author
     *  is known by a unique WikiPathways username.

     */
    public static final Resource Author = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Author" );

    /**
<br><b>Description:</b> A binding interaction between two
     *  physical entities resulting in the formation of an explicit complex that is
     *  reversible without an external factor (e.g. protein complexes). Binding subclass
     *  of Mimbinding <br> <b>Properties:</b> <br> - hasParticipant<br>
     *  <b>GPML:</b> Line with both Points having a "mim-binding" arrowhead.

     */
    public static final Resource Binding = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Binding" );

    /**
Entity in BioPAX
 */
    public static final Resource BiologicalEntity = m_model.createResource( "http://vocabularies.wikipathways.org/wp#BiologicalEntity" );

    /**
<br /> <b>Description</b>:A connection point for two interaction
     *  lines, stylized to indicate left directionality or flow, but not biologically
     *  meaningful.<br /> <b>Properties</b>:<br /> - participant
     *  <br /> <b>GPML</b>: A line with a point with branching glyph
     *  terminating on an anchor; the other point can have another arrowhead type
     *  and terminate on a datanode or group.

     */
    public static final Resource BranchingLeft = m_model.createResource( "http://vocabularies.wikipathways.org/wp#BranchingLeft" );

    /**
<br /> <b>Description</b>:A connection point for two interaction
     *  lines, stylized to indicate right directionality or flow, but not biologically
     *  meaningful.<br /> <b>Properties</b>:<br /> - participant
     *  <br /> <b>GPML</b>: A line with a point with branching glyph
     *  terminating on an anchor; the other point can have another arrowhead type
     *  and terminate on a datanode or group.

     */
    public static final Resource BranchingRight = m_model.createResource( "http://vocabularies.wikipathways.org/wp#BranchingRight" );

    /**
<br /> <b>Description</b>: An interaction where the controller
     *  entity increases the rate of the controlled reaction.<br /> <b>Properties</b>:<br
     *  /> - participant<br /> - controller<br /> - controlled<br
     *  /> <b>GPML</b>: A line with one point with arrowhead terminating
     *  on an anchor; the other point does not have an arrowhead and terminates on
     *  a datanode or group.

     */
    public static final Resource Catalysis = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Catalysis" );

    /**
Definition: A Cell is a shape in a pathway that represents the basic structural
     *  and functional unit of the organism on which this pathways is topical.

     */
    public static final Resource Cell = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Cell" );

    public static final Resource CellularProcess = m_model.createResource( "http://vocabularies.wikipathways.org/wp#CellularProcess" );

    /**
<br /><b>Description: </b>The scission of a covalent bond
     *  or the separation of connections between entity features. <br /> <b>Properties:</b>
     *  <br /> - participant - one or more DataNodes<br /> - source -
     *  one or more DataNodes<br /> - target - one or more DataNodes<br />
     *  <b>GPML:</b> Line with one Point having a "cleavage" arrowhead
     *  on a datanode; the other Point has no arrowhead and is also on a datanode.

     */
    public static final Resource Cleavage = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Cleavage" );

    /**
no corresponding elements in BioPAX and GPML
 */
    public static final Resource Compartment = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Compartment" );

    public static final Resource Complex = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Complex" );

    public static final Resource ControlledVocabulary = m_model.createResource( "http://vocabularies.wikipathways.org/wp#ControlledVocabulary" );

    /**
<br><b>Description: </b>An interaction in which one entity
     *  is transformed into one or more other entities (e.g. the conversion of ATP
     *  to cyclical-AMP). <br> <b>Properties:</b> <br> - participant
     *  - one or more DataNodes<br> - source - one or more DataNodes<br>
     *  - target - one or more DataNodes<br> <b>GPML:</b> Line with
     *  one Point having a "conversion" arrowhead (if both Points have a conversion
     *  arrowhead then we are actually talking about two interactions and they should
     *  be split up in the pathway).

     */
    public static final Resource Conversion = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Conversion" );

    /**
<br /><b>Description: </b>An asymmetric covalent non-reversible
     *  binding reaction (e.g. phosphorylation and acetylation) where one of the substrates
     *  must be a modifier physical entity. <br /> <b>Properties:</b>
     *  <br /> - participant - one or more DataNodes<br /> - source -
     *  one or more DataNodes<br /> - target - one or more DataNodes<br />
     *  <b>GPML:</b> Line with a Point having a "mim-covalent-bond" arrowhead
     *  on a datanode; and the Point with no arrowhead on a datanode.

     */
    public static final Resource CovalentBond = m_model.createResource( "http://vocabularies.wikipathways.org/wp#CovalentBond" );

    public static final Resource CurationTag = m_model.createResource( "http://vocabularies.wikipathways.org/wp#CurationTag" );

    /**
Definition: A datanode is used to denote a biological entity that participates
     *  in a pathway. In WikiPathways the following DataNode types can be present
     *  in a pathway: GeneProduct, Metabolite, Protein, Complex, Rna, Pathway and
     *  Unknown.

     */
    public static final Resource DataNode = m_model.createResource( "http://vocabularies.wikipathways.org/wp#DataNode" );

    /**
UnificationXref in BioPAX
 */
    public static final Resource DatasourceReference = m_model.createResource( "http://vocabularies.wikipathways.org/wp#DatasourceReference" );

    /**
<br><b>A BasicInteraction is a generic biological interaction
     *  between two or more pathway elements that is not identified with a MIM type.</b><br>
     *  * GPML: Line (ArrowHead = none, "Line", "Arrow" or "TBar")<br> * BioPAX:
     *  Interaction

     */
    public static final Resource DirectedInteraction = m_model.createResource( "http://vocabularies.wikipathways.org/wp#DirectedInteraction" );

    /**
Shape in GPML, no corresponding element in BioPAX
 */
    public static final Resource EndoplasmicReticulum = m_model.createResource( "http://vocabularies.wikipathways.org/wp#EndoplasmicReticulum" );

    public static final Resource Entity = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Entity" );

    /**
no corresponding elements in BioPAX and GPML (might be nice to add functionality
     *  to define extracellular space in a pathway)

     */
    public static final Resource Extracellular = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Extracellular" );

    public static final Resource Gap = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Gap" );

    /**
DNA+Gene+RNA+Protein in BioPAX
 */
    public static final Resource GeneProduct = m_model.createResource( "http://vocabularies.wikipathways.org/wp#GeneProduct" );

    /**
Shape in GPML, no corresponding element in BioPAX
 */
    public static final Resource GolgiApparatus = m_model.createResource( "http://vocabularies.wikipathways.org/wp#GolgiApparatus" );

    public static final Resource Group = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Group" );

    /**
<br><b>Description: </b> An interaction where a controller
     *  entity causes a decrease in the velocity or extent of controlled reaction
     *  or contingency; the probability of the controlled interaction occurring remains
     *  greater than 0. <br> <b>Properties:</b><br> - participant
     *  (DataNodes or Interactions)<br> - controller (DataNodes)<br> -
     *  controlled (DataNodes or Interactions)<br> <b>GPML: </b>
     *  Line with one Point having an arrowhead "conversion". The Point without the
     *  arrowhead cannot be an interaction.

     */
    public static final Resource Inhibition = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Inhibition" );

    /**
<br><b>An interaction is a generic biological interaction between
     *  two or more pathway elements.</b><br> * GPML: Line (ArrowHead
     *  = none or all others)<br> * BioPAX: Interaction

     */
    public static final Resource Interaction = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Interaction" );

    /**
no corresponding element in BioPAX GPML:Label
 */
    public static final Resource Label = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Label" );

    public static final Resource MetabolicProcess = m_model.createResource( "http://vocabularies.wikipathways.org/wp#MetabolicProcess" );

    /**
SmallMolecule in BioPAX
 */
    public static final Resource Metabolite = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Metabolite" );

    /**
Shape in GPML, no element in BioPAX
 */
    public static final Resource Mitochondria = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Mitochondria" );

    /**
<br> <b>Description: </b>An asymmetric covalent non-reversible
     *  binding reaction (e.g. phosphorylation and acetylation) where one of the substrates
     *  must be a modifier physical entity. <br> <b>Properties:</b><br>
     *  - participant (one or more DataNodes)<br> - source (one or more DataNodes)<br>
     *  - target (one or more DataNodes)<br> <b>GPML: </b>A Line
     *  with one Point having an arrowhead "mim-modification".

     */
    public static final Resource Modification = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Modification" );

    public static final Resource MolecularFunction = m_model.createResource( "http://vocabularies.wikipathways.org/wp#MolecularFunction" );

    /**
<br><b>Description: </b>An interaction where the controller
     *  entity is necessary for the controlled interaction to proceed. <br>
     *  <b>Properties: </b><br> - participant (DataNodes or Interactions)<br>
     *  - controller (DataNodes)<br> - controlled (DataNodes or Interactions)<br>
     *  <b>GPML: </b>Line with one Point having an arrowhead "mim-necessary-stimulation".
     *  The Point without the arrowhead cannot be an interaction.

     */
    public static final Resource NecessaryStimulation = m_model.createResource( "http://vocabularies.wikipathways.org/wp#NecessaryStimulation" );

    /**
Shape in GPML, no corresponding element in BioPAX
 */
    public static final Resource Nucleus = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Nucleus" );

    /**
Shape in GPML, no corresponding element in BioPAx
 */
    public static final Resource Organelle = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Organelle" );

    /**
Pathway in BioPAX
 */
    public static final Resource Pathway = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Pathway" );

    /**
no corresponding element in BioPAX
 */
    public static final Resource PathwayAnnotation = m_model.createResource( "http://vocabularies.wikipathways.org/wp#PathwayAnnotation" );

    public static final Resource PathwayCategory = m_model.createResource( "http://vocabularies.wikipathways.org/wp#PathwayCategory" );

    public static final Resource PathwayVersion = m_model.createResource( "http://vocabularies.wikipathways.org/wp#PathwayVersion" );

    public static final Resource Person = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Person" );

    public static final Resource PhysiologicalProcess = m_model.createResource( "http://vocabularies.wikipathways.org/wp#PhysiologicalProcess" );

    /**
Protein in BioPAX
 */
    public static final Resource Protein = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Protein" );

    /**
PublicationXref in Biopax
 */
    public static final Resource PublicationReference = m_model.createResource( "http://vocabularies.wikipathways.org/wp#PublicationReference" );

    /**
RNA in Biopax, DataNode in GPML
 */
    public static final Resource RNA = m_model.createResource( "http://vocabularies.wikipathways.org/wp#RNA" );

    /**
Xref in BioPAX
 */
    public static final Resource Reference = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Reference" );

    /**
Shape in GPML, no corresponding element in BioPAX
 */
    public static final Resource SarcoplasmicReticulum = m_model.createResource( "http://vocabularies.wikipathways.org/wp#SarcoplasmicReticulum" );

    /**
<br><b>Description: </b>Enhancement of the velocity or extent
     *  of a reaction or contingency by the controller entity. <br> <b>Properties:
     *  </b><br> - participant (DataNodes or Interactions)<br> -
     *  controller (DataNodes)<br> - controlled (DataNodes or Interactions)<br>
     *  <b>GPML: </b>Line with one Point having an arrowhead "mim-stimulation".
     *  The Point without the arrowhead cannot be an interaction.

     */
    public static final Resource Stimulation = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Stimulation" );

    /**
<br> <b>Description: </b>A type of production without loss
     *  that involves a template (e.g. the polymerization of a nucleic acid macromolecule
     *  from a nucleic acid macromolecule template). <br> <b>Properties:</b><br>
     *  - particpant (two or more DataNodes)<br> - template (one or more DataNodes)
     *  <br> - product (one or more DataNodes)<br> <b>GPML: </b>Line
     *  with one Point having an arrowhead "mim-transcription-translation". Cannot
     *  be reversible!

     */
    public static final Resource TranscriptionTranslation = m_model.createResource( "http://vocabularies.wikipathways.org/wp#TranscriptionTranslation" );

    public static final Resource Unknown = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Unknown" );

    public static final Resource UtilityClass = m_model.createResource( "http://vocabularies.wikipathways.org/wp#UtilityClass" );

    /**
Shape in GPML, no corresponding element in BioPAX
 */
    public static final Resource Vesicle = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Vesicle" );

    public static final Resource University = m_model.createResource( "http://vocabularies.wikipathways.org/wp#Maastricht%20University" );

    public static final Resource andra = m_model.createResource( "http://vocabularies.wikipathways.org/wp#andra" );

}


text:
<?php
/**
 * @author David Spreekmeester | Grrr.nl
 */
class Garp_Spawn_Config_Format_Json implements Garp_Spawn_Config_Format_Interface {

	/**
	 * Parses the raw configuration data.
	 * @param String $modelId The ID of this model
	 * @param Mixed $rawConfig Raw configuration data
	 * @param Boolean $allowEmpty Whether this configuration is allowed to be left empty
	 * @return Array Key / values configuration pairs
	 */
	public function parse($modelId, $rawConfig, $allowEmpty = false) {
		$config = json_decode($rawConfig, true);

		if (!is_null($config)) {
			$this->_validate($config, $allowEmpty);
			return $config;
		} else throw new Exception("The {$modelId} configuration file contains invalid JSON. Don't forget the double quotes around array keys and string-like values. Or use jsonlint.com to validate your file.");
	}

	protected function _validate(array $config, $allowEmpty) {
		if (!$config && !$allowEmpty) {
			throw new Exception("The configuration is empty. Don't forget the double quotes around array keys and string-like values.");
		} elseif (array_key_exists('id', $config)) {
			throw new Exception("The 'id' property cannot be defined in the model configuration file, but is provided separately.");
		}
	}
}


text:
if [[ -z "${COLOR_RESET+x}" ]]; then
  if type tput >/dev/null 2>&1; then
    readonly COLOR_RED="$(tput setaf 1)"
    readonly COLOR_GREEN="$(tput setaf 2)"
    readonly COLOR_YELLOW="$(tput setaf 3)"
    readonly COLOR_RESET="$(tput sgr0)"
  else
    readonly COLOR_RED=""
    readonly COLOR_GREEN=""
    readonly COLOR_YELLOW=""
    readonly COLOR_RESET=""
  fi
fi


text: iOS-Charts,a collection of awesome iOS Charts


text:
My first appearance on github.
What I am going to do is just testing.


text:
describe('Hello', () => {
  it('says hello', () => {
    const hello = new Hello('Babel');

    expect(hello.say()).toEqual('Hello, Babel!');
  });
});


text:
package org.apache.hadoop.yarn.server.resourcemanager;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.Container;
import org.apache.hadoop.yarn.api.records.ContainerExitStatus;
import org.apache.hadoop.yarn.api.records.ContainerId;
import org.apache.hadoop.yarn.api.records.ContainerState;
import org.apache.hadoop.yarn.api.records.ContainerStatus;
import org.apache.hadoop.yarn.api.records.NodeId;
import org.apache.hadoop.yarn.api.records.Priority;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.api.records.ResourceRequest;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.event.Dispatcher;
import org.apache.hadoop.yarn.event.DrainDispatcher;
import org.apache.hadoop.yarn.server.resourcemanager.applicationsmanager.TestAMRestart;
import org.apache.hadoop.yarn.server.resourcemanager.recovery.MemoryRMStateStore;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptState;
import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer;
import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainerState;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler;
import org.apache.hadoop.yarn.server.utils.BuilderUtils;
import org.apache.hadoop.yarn.util.resource.Resources;
import org.junit.Assert;
import org.junit.Test;

/**
 * Validate system behavior when the am-scheduling logic 'blacklists' a node for
 * an application because of AM failures.
 */
public class TestNodeBlacklistingOnAMFailures {

  @Test(timeout = 100000)
  public void testNodeBlacklistingOnAMFailure() throws Exception {

    YarnConfiguration conf = new YarnConfiguration();
    conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class,
        ResourceScheduler.class);
    conf.setBoolean(YarnConfiguration.AM_SCHEDULING_NODE_BLACKLISTING_ENABLED,
        true);

    DrainDispatcher dispatcher = new DrainDispatcher();
    MockRM rm = startRM(conf, dispatcher);
    CapacityScheduler scheduler = (CapacityScheduler) rm.getResourceScheduler();

    // Register 5 nodes, so that we can blacklist atleast one if AM container
    // is failed. As per calculation it will be like, 5nodes * 0.2 (default)=1.
    // First register 2 nodes, and after AM lauched register 3 more nodes.
    MockNM nm1 =
        new MockNM("127.0.0.1:1234", 8000, rm.getResourceTrackerService());
    nm1.registerNode();

    MockNM nm2 =
        new MockNM("127.0.0.2:2345", 8000, rm.getResourceTrackerService());
    nm2.registerNode();

    RMApp app = rm.submitApp(200);

    MockAM am1 = MockRM.launchAndRegisterAM(app, rm, nm1);
    ContainerId amContainerId =
        ContainerId.newContainerId(am1.getApplicationAttemptId(), 1);
    RMContainer rmContainer = scheduler.getRMContainer(amContainerId);
    NodeId nodeWhereAMRan = rmContainer.getAllocatedNode();

    MockNM currentNode, otherNode;
    if (nodeWhereAMRan.equals(nm1.getNodeId())) {
      currentNode = nm1;
      otherNode = nm2;
    } else {
      currentNode = nm2;
      otherNode = nm1;
    }

    // register 3 nodes now
    MockNM nm3 =
        new MockNM("127.0.0.3:2345", 8000, rm.getResourceTrackerService());
    nm3.registerNode();

    MockNM nm4 =
        new MockNM("127.0.0.4:2345", 8000, rm.getResourceTrackerService());
    nm4.registerNode();

    MockNM nm5 =
        new MockNM("127.0.0.5:2345", 8000, rm.getResourceTrackerService());
    nm5.registerNode();

    // Set the exist status to INVALID so that we can verify that the system
    // automatically blacklisting the node
    makeAMContainerExit(rm, amContainerId, currentNode,
        ContainerExitStatus.INVALID);

    // restart the am
    RMAppAttempt attempt = MockRM.waitForAttemptScheduled(app, rm);
    System.out.println("New AppAttempt launched " + attempt.getAppAttemptId());

    // Try the current node a few times
    for (int i = 0; i <= 2; i++) {
      currentNode.nodeHeartbeat(true);
      dispatcher.await();

      Assert.assertEquals(
          "AppAttemptState should still be SCHEDULED if currentNode is "
              + "blacklisted correctly", RMAppAttemptState.SCHEDULED,
          attempt.getAppAttemptState());
    }

    // Now try the other node
    otherNode.nodeHeartbeat(true);
    dispatcher.await();

    // Now the AM container should be allocated
    MockRM.waitForState(attempt, RMAppAttemptState.ALLOCATED, 20000);

    MockAM am2 = rm.sendAMLaunched(attempt.getAppAttemptId());
    rm.waitForState(attempt.getAppAttemptId(), RMAppAttemptState.LAUNCHED);
    amContainerId =
        ContainerId.newContainerId(am2.getApplicationAttemptId(), 1);
    rmContainer = scheduler.getRMContainer(amContainerId);
    nodeWhereAMRan = rmContainer.getAllocatedNode();

    // The other node should now receive the assignment
    Assert.assertEquals(
        "After blacklisting, AM should have run on the other node",
        otherNode.getNodeId(), nodeWhereAMRan);

    am2.registerAppAttempt();
    rm.waitForState(app.getApplicationId(), RMAppState.RUNNING);

    List<Container> allocatedContainers =
        TestAMRestart.allocateContainers(currentNode, am2, 1);
    Assert.assertEquals(
        "Even though AM is blacklisted from the node, application can "
            + "still allocate non-AM containers there",
        currentNode.getNodeId(), allocatedContainers.get(0).getNodeId());
  }

  @Test(timeout = 100000)
  public void testNodeBlacklistingOnAMFailureStrictNodeLocality()
      throws Exception {
    YarnConfiguration conf = new YarnConfiguration();
    conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class,
        ResourceScheduler.class);
    conf.setBoolean(YarnConfiguration.AM_SCHEDULING_NODE_BLACKLISTING_ENABLED,
        true);

    DrainDispatcher dispatcher = new DrainDispatcher();
    MockRM rm = startRM(conf, dispatcher);
    CapacityScheduler scheduler = (CapacityScheduler) rm.getResourceScheduler();

    // Register 5 nodes, so that we can blacklist atleast one if AM container
    // is failed. As per calculation it will be like, 5nodes * 0.2 (default)=1.
    MockNM nm1 =
        new MockNM("127.0.0.1:1234", 8000, rm.getResourceTrackerService());
    nm1.registerNode();

    MockNM nm2 =
        new MockNM("127.0.0.2:2345", 8000, rm.getResourceTrackerService());
    nm2.registerNode();

    MockNM nm3 =
        new MockNM("127.0.0.3:2345", 8000, rm.getResourceTrackerService());
    nm3.registerNode();

    MockNM nm4 =
        new MockNM("127.0.0.4:2345", 8000, rm.getResourceTrackerService());
    nm4.registerNode();

    MockNM nm5 =
        new MockNM("127.0.0.5:2345", 8000, rm.getResourceTrackerService());
    nm5.registerNode();

    // Specify a strict locality on nm2
    List<ResourceRequest> reqs = new ArrayList<>();
    ResourceRequest nodeReq = ResourceRequest.newInstance(
        Priority.newInstance(0), nm2.getNodeId().getHost(),
        Resource.newInstance(200, 1), 1, true);
    ResourceRequest rackReq = ResourceRequest.newInstance(
        Priority.newInstance(0), "/default-rack",
        Resource.newInstance(200, 1), 1, false);
    ResourceRequest anyReq = ResourceRequest.newInstance(
        Priority.newInstance(0), ResourceRequest.ANY,
        Resource.newInstance(200, 1), 1, false);
    reqs.add(anyReq);
    reqs.add(rackReq);
    reqs.add(nodeReq);
    RMApp app = rm.submitApp(reqs);

    MockAM am1 = MockRM.launchAndRegisterAM(app, rm, nm2);
    ContainerId amContainerId =
        ContainerId.newContainerId(am1.getApplicationAttemptId(), 1);
    RMContainer rmContainer = scheduler.getRMContainer(amContainerId);
    NodeId nodeWhereAMRan = rmContainer.getAllocatedNode();
    Assert.assertEquals(nm2.getNodeId(), nodeWhereAMRan);

    // Set the exist status to INVALID so that we can verify that the system
    // automatically blacklisting the node
    makeAMContainerExit(rm, amContainerId, nm2, ContainerExitStatus.INVALID);

    // restart the am
    RMAppAttempt attempt = MockRM.waitForAttemptScheduled(app, rm);
    System.out.println("New AppAttempt launched " + attempt.getAppAttemptId());

    nm2.nodeHeartbeat(true);
    dispatcher.await();

    // Now the AM container should be allocated
    MockRM.waitForState(attempt, RMAppAttemptState.ALLOCATED, 20000);

    MockAM am2 = rm.sendAMLaunched(attempt.getAppAttemptId());
    rm.waitForState(attempt.getAppAttemptId(), RMAppAttemptState.LAUNCHED);
    amContainerId =
        ContainerId.newContainerId(am2.getApplicationAttemptId(), 1);
    rmContainer = scheduler.getRMContainer(amContainerId);
    nodeWhereAMRan = rmContainer.getAllocatedNode();

    // The second AM should be on the same node because the strict locality
    // made the eligible nodes only 1, so the blacklisting threshold kicked in
    System.out.println("AM ran on " + nodeWhereAMRan);
    Assert.assertEquals(nm2.getNodeId(), nodeWhereAMRan);

    am2.registerAppAttempt();
    rm.waitForState(app.getApplicationId(), RMAppState.RUNNING);
  }

  @Test(timeout = 100000)
  public void testNodeBlacklistingOnAMFailureRelaxedNodeLocality()
      throws Exception {
    YarnConfiguration conf = new YarnConfiguration();
    conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class,
        ResourceScheduler.class);
    conf.setBoolean(YarnConfiguration.AM_SCHEDULING_NODE_BLACKLISTING_ENABLED,
        true);

    DrainDispatcher dispatcher = new DrainDispatcher();
    MockRM rm = startRM(conf, dispatcher);
    CapacityScheduler scheduler = (CapacityScheduler) rm.getResourceScheduler();

    // Register 5 nodes, so that we can blacklist atleast one if AM container
    // is failed. As per calculation it will be like, 5nodes * 0.2 (default)=1.
    MockNM nm1 =
        new MockNM("127.0.0.1:1234", 8000, rm.getResourceTrackerService());
    nm1.registerNode();

    MockNM nm2 =
        new MockNM("127.0.0.2:2345", 8000, rm.getResourceTrackerService());
    nm2.registerNode();

    MockNM nm3 =
        new MockNM("127.0.0.3:2345", 8000, rm.getResourceTrackerService());
    nm3.registerNode();

    MockNM nm4 =
        new MockNM("127.0.0.4:2345", 8000, rm.getResourceTrackerService());
    nm4.registerNode();

    MockNM nm5 =
        new MockNM("127.0.0.5:2345", 8000, rm.getResourceTrackerService());
    nm5.registerNode();

    // Specify a relaxed locality on nm2
    List<ResourceRequest> reqs = new ArrayList<>();
    ResourceRequest nodeReq = ResourceRequest.newInstance(
        Priority.newInstance(0), nm2.getNodeId().getHost(),
        Resource.newInstance(200, 1), 1, true);
    ResourceRequest rackReq = ResourceRequest.newInstance(
        Priority.newInstance(0), "/default-rack",
        Resource.newInstance(200, 1), 1, true);
    ResourceRequest anyReq = ResourceRequest.newInstance(
        Priority.newInstance(0), ResourceRequest.ANY,
        Resource.newInstance(200, 1), 1, true);
    reqs.add(anyReq);
    reqs.add(rackReq);
    reqs.add(nodeReq);
    RMApp app = rm.submitApp(reqs);

    MockAM am1 = MockRM.launchAndRegisterAM(app, rm, nm2);
    ContainerId amContainerId =
        ContainerId.newContainerId(am1.getApplicationAttemptId(), 1);
    RMContainer rmContainer = scheduler.getRMContainer(amContainerId);
    NodeId nodeWhereAMRan = rmContainer.getAllocatedNode();
    Assert.assertEquals(nm2.getNodeId(), nodeWhereAMRan);

    // Set the exist status to INVALID so that we can verify that the system
    // automatically blacklisting the node
    makeAMContainerExit(rm, amContainerId, nm2, ContainerExitStatus.INVALID);

    // restart the am
    RMAppAttempt attempt = MockRM.waitForAttemptScheduled(app, rm);
    System.out.println("New AppAttempt launched " + attempt.getAppAttemptId());

    nm2.nodeHeartbeat(true);
    nm1.nodeHeartbeat(true);
    nm3.nodeHeartbeat(true);
    nm4.nodeHeartbeat(true);
    nm5.nodeHeartbeat(true);
    dispatcher.await();

    // Now the AM container should be allocated
    MockRM.waitForState(attempt, RMAppAttemptState.ALLOCATED, 20000);

    MockAM am2 = rm.sendAMLaunched(attempt.getAppAttemptId());
    rm.waitForState(attempt.getAppAttemptId(), RMAppAttemptState.LAUNCHED);
    amContainerId =
        ContainerId.newContainerId(am2.getApplicationAttemptId(), 1);
    rmContainer = scheduler.getRMContainer(amContainerId);
    nodeWhereAMRan = rmContainer.getAllocatedNode();

    // The second AM should be on a different node because the relaxed locality
    // made the app schedulable on other nodes and nm2 is blacklisted
    System.out.println("AM ran on " + nodeWhereAMRan);
    Assert.assertNotEquals(nm2.getNodeId(), nodeWhereAMRan);

    am2.registerAppAttempt();
    rm.waitForState(app.getApplicationId(), RMAppState.RUNNING);
  }

  @Test(timeout = 100000)
  public void testNoBlacklistingForNonSystemErrors() throws Exception {

    YarnConfiguration conf = new YarnConfiguration();
    conf.setBoolean(YarnConfiguration.AM_SCHEDULING_NODE_BLACKLISTING_ENABLED,
        true);
    // disable the float so it is possible to blacklist the entire cluster
    conf.setFloat(
        YarnConfiguration.AM_SCHEDULING_NODE_BLACKLISTING_DISABLE_THRESHOLD,
        1.5f);
    conf.setInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS, 100);

    DrainDispatcher dispatcher = new DrainDispatcher();
    MockRM rm = startRM(conf, dispatcher);

    MockNM node =
        new MockNM("127.0.0.1:1234", 8000, rm.getResourceTrackerService());
    node.registerNode();

    RMApp app = rm.submitApp(200);
    ApplicationId appId = app.getApplicationId();

    int numAppAttempts = 1;

    // Now the AM container should be allocated
    RMAppAttempt attempt = MockRM.waitForAttemptScheduled(app, rm);
    node.nodeHeartbeat(true);
    dispatcher.await();
    MockRM.waitForState(attempt, RMAppAttemptState.ALLOCATED, 20000);
    rm.sendAMLaunched(attempt.getAppAttemptId());
    rm.waitForState(attempt.getAppAttemptId(), RMAppAttemptState.LAUNCHED);
    ApplicationAttemptId appAttemptId =
        ApplicationAttemptId.newInstance(appId, numAppAttempts);
    ContainerId amContainerId = ContainerId.newContainerId(appAttemptId, 1);

    for (int containerExitStatus : new int[] {
        ContainerExitStatus.PREEMPTED,
        ContainerExitStatus.KILLED_BY_RESOURCEMANAGER,
        // ContainerExitStatus.KILLED_BY_APPMASTER,
        ContainerExitStatus.KILLED_AFTER_APP_COMPLETION,
        ContainerExitStatus.ABORTED, ContainerExitStatus.DISKS_FAILED,
        ContainerExitStatus.KILLED_EXCEEDED_VMEM,
        ContainerExitStatus.KILLED_EXCEEDED_PMEM }) {

      // Set the exist status to be containerExitStatus so that we can verify
      // that the system automatically blacklisting the node
      makeAMContainerExit(rm, amContainerId, node, containerExitStatus);

      // restart the am
      attempt = MockRM.waitForAttemptScheduled(app, rm);
      System.out
          .println("New AppAttempt launched " + attempt.getAppAttemptId());

      node.nodeHeartbeat(true);
      dispatcher.await();

      MockRM.waitForState(attempt, RMAppAttemptState.ALLOCATED, 20000);
      rm.sendAMLaunched(attempt.getAppAttemptId());
      rm.waitForState(attempt.getAppAttemptId(), RMAppAttemptState.LAUNCHED);

      numAppAttempts++;
      appAttemptId = ApplicationAttemptId.newInstance(appId, numAppAttempts);
      amContainerId = ContainerId.newContainerId(appAttemptId, 1);
      rm.waitForState(node, amContainerId, RMContainerState.ACQUIRED);
    }
  }

  private void makeAMContainerExit(MockRM rm, ContainerId amContainer,
      MockNM node, int exitStatus) throws Exception, InterruptedException {
    ContainerStatus containerStatus =
        BuilderUtils.newContainerStatus(amContainer, ContainerState.COMPLETE,
            "", exitStatus, Resources.createResource(200));
    node.containerStatus(containerStatus);
    ApplicationAttemptId amAttemptID = amContainer.getApplicationAttemptId();
    rm.waitForState(amAttemptID, RMAppAttemptState.FAILED);
    rm.waitForState(amAttemptID.getApplicationId(), RMAppState.ACCEPTED);
  }

  private MockRM startRM(YarnConfiguration conf,
      final DrainDispatcher dispatcher) {

    MemoryRMStateStore memStore = new MemoryRMStateStore();
    memStore.init(conf);

    MockRM rm1 = new MockRM(conf, memStore) {
      @Override
      protected Dispatcher createDispatcher() {
        return dispatcher;
      }
    };

    rm1.start();
    return rm1;
  }
}


text:
package org.emmalanguage
package compiler.tools

import compiler.Common
import compiler.lang.cf.ControlFlow
import compiler.lang.core.Core

import resource._

import java.io.FileWriter
import java.nio.file.Path

trait GraphTools extends Common with ControlFlow {
  self: Core =>

  import UniverseImplicits._

  object CytoscapeGraphJsonProtocol extends spray.json.DefaultJsonProtocol {
    import spray.json._

    case class NodeData(id: String, label: String, parent: Option[String])
    case class EdgeData(id: String, source: String, target: String, label: String)

    sealed trait Element

    case class NodeElement
    (
      data: NodeData,
      group: String = "nodes",
      style: Option[Map[String, String]] = None
    ) extends Element

    case class EdgeElement
    (
      data: EdgeData,
      group: String = "edges",
      style: Option[Map[String, String]] = None
    ) extends Element

    implicit val nodeDataFormat = jsonFormat3(NodeData)
    implicit val edgeDataFormat = jsonFormat4(EdgeData)
    implicit val nodeElementFormat = jsonFormat3(NodeElement)
    implicit val edgeElementFormat = jsonFormat3(EdgeElement)

    implicit object ElementJsonFormat extends RootJsonFormat[Element] {
      def write(c: Element) = c match {
        case n: NodeElement => n.toJson
        case e: EdgeElement => e.toJson
      }

      def read(value: JsValue) = {
        value.asJsObject.getFields("group") match {
          case Seq(JsString(group)) if group == "nodes" => value.convertTo[NodeElement]
          case Seq(JsString(group)) if group == "edges" => value.convertTo[EdgeElement]
          case x => throw new IllegalArgumentException(s"Unable to convert to `${classOf[Element].getSimpleName}`: $x")
        }
      }
    }
  }

  object GraphTools {

    import Core.{Lang => core}
    import CytoscapeGraphJsonProtocol._
    import spray.json._

    val cs = Comprehension.Syntax(API.DataBag.sym)

    def mkGraph(tree: u.Tree): Iterable[Element] = {
      val graph = ControlFlow.cfg(tree)
      val mkLabel = label(graph) _

      val nestFlipped = (for {
        parent <- graph.nest.nodes
        child <- graph.nest.successors(parent)
      } yield child -> parent).toMap

      val nodes = for {
        n <- graph.uses.keys
      } yield NodeElement(
        data = NodeData(
          id = n.name.toString,
          label = mkLabel(n),
          parent = nestFlipped.get(n).map(_.name.toString)
        )
      )

      val edges = for {
        from <- graph.data.nodes
        to <- graph.data.predecessors(from)
      } yield {
        val source = from.name.toString
        val target = to.name.toString
        val label = to.name.toString
        EdgeElement(
          data = EdgeData(
            id = s"${source}_to_$target",
            source = source,
            target = target,
            label = label
          )
        )
      }
      nodes ++ edges
    }

    private[emmalanguage] def mkJsonGraph(id: String, tree: u.Tree): JsValue =
      JsObject(id -> mkGraph(tree).toJson)

    private[emmalanguage] def writeJsonGraph(basePath: Path, id: String, tree: u.Tree): u.Tree = {
      basePath.toFile.mkdirs() // make parent
      val targetPath: Path = basePath.resolve(s"$id.json")
      for(pw <- managed(new FileWriter(targetPath.toAbsolutePath.toString))) {
        pw.write(mkJsonGraph(id, tree).toString())
      }
      tree
    }

    /**
     * Renders a tree in Cytoscape graph json format.
     *
     * The method returns the identical tree while writing the graph as an effect.
     *
     * @param basePath the base path of the file
     * @param name the filename
     * @return a function returning the identity of the tree
     */
    def renderGraph(basePath: Path)(name: String): u.Tree => u.Tree =
      writeJsonGraph(basePath, name, _)

    private[emmalanguage] def label(graph: FlowGraph[u.TermSymbol])(sym: u.TermSymbol): String =
      graph.data.label(sym).map(_.rhs) match {
        case Some(core.Atomic(x)) => u.showCode(x)
        case Some(core.DefCall(_, method, _, _)) => method.name.decodedName.toString
        case Some(core.Lambda(_, _, _)) => "Lambda"
        case Some(cs.Comprehension(_, _)) => "For-Comprehension"
        case Some(_) => s"[Unknown Symbol: ${sym.name.decodedName}]"
        case None => s"Closure (${sym.name.decodedName})"
      }
  }
}


text:
from Templates.make_excel import make2exel
import Main.Tenderplan.participants2excel as p2ex

import openpyxl

excel_name = r'E:\Лиды_экспорт.xlsx'

make2exel(
    ['Название компании', 'ИНН', 'Список выигранных тендеров', 'Список остальных тендеров c участием'], excel_name)

wb = openpyxl.load_workbook(excel_name)
sheet = wb.active

participants_dict_list = p2ex.get_participants()

curr_row = 1
for x in range(len(participants_dict_list)):
    curr_row += 1
    print(participants_dict_list[x]['company'])
    sheet.cell(row=curr_row, column=1).value = participants_dict_list[x]['company']

    part_row = win_row = curr_row
    for t in range(len(participants_dict_list[x]['tender_name'])):
        if list(participants_dict_list[x]['tender_name'][t].values())[0] == 'par':
            sheet.cell(row=part_row, column=4).value = (list(participants_dict_list[x]['tender_name'][t].keys())[0])
            part_row += 1
        else:
            sheet.cell(row=win_row, column=3).value = (list(participants_dict_list[x]['tender_name'][t].keys())[0])
            win_row += 1
        curr_row += 1
        # if t > len(participants_dict_list[x]['tender_name']):
        #     curr_row += 1
wb.save(excel_name)


text:
package io.novaordis.gld.driver.todeplete.command;

import io.novaordis.gld.api.todiscard.Configuration;
import io.novaordis.utilities.testing.Tests;
import org.slf4j.Logger;
import org.junit.After;
import org.slf4j.LoggerFactory;

public class GenerateKeysTest extends CommandTest
{
    // Constants -------------------------------------------------------------------------------------------------------

    private static final Logger log = LoggerFactory.getLogger(GenerateKeysTest.class);

    // Static ----------------------------------------------------------------------------------------------------------

    // Attributes ------------------------------------------------------------------------------------------------------

    // Constructors ----------------------------------------------------------------------------------------------------

    // Public ----------------------------------------------------------------------------------------------------------

    @After
    public void scratchCleanup() throws Exception
    {
        Tests.cleanup();
    }

//    @Test
//    public void missingKeyCount() throws Exception
//    {
//        MockConfiguration mc = new MockConfiguration();
//        GenerateKeys gc = new GenerateKeys(mc);
//
//        try
//        {
//            gc.getKeyCount();
//            fail("should fail with UserErrorException on account of missing key count");
//        }
//        catch(UserErrorException e)
//        {
//            log.info(e.getMessage());
//        }
//    }
//
//    @Test
//    public void keyCountNotAnInteger() throws Exception
//    {
//        MockConfiguration mc = new MockConfiguration();
//        GenerateKeys gc = new GenerateKeys(mc);
//        gc.addArgument("blah");
//
//        try
//        {
//            gc.getKeyCount();
//            fail("should fail with UserErrorException on account of invalid key count");
//        }
//        catch(UserErrorException e)
//        {
//            log.info(e.getMessage());
//        }
//    }
//
//    @Test
//    public void keyCount() throws Exception
//    {
//        MockConfiguration mc = new MockConfiguration();
//        GenerateKeys gc = new GenerateKeys(mc);
//        gc.addArgument("15");
//
//        int i = gc.getKeyCount();
//
//        assertEquals(15, i);
//    }
//
//    @Test
//    public void missingFileName() throws Exception
//    {
//        MockConfiguration mc = new MockConfiguration();
//        GenerateKeys gc = new GenerateKeys(mc);
//        gc.addArgument("10");
//
//        try
//        {
//            gc.initialize();
//            fail("should fail with UserErrorException on account of missing key store file name");
//        }
//        catch(UserErrorException e)
//        {
//            log.info(e.getMessage());
//        }
//    }
//
//    @Test
//    public void generateKeys() throws Exception
//    {
//        File f = new File(Tests.getScratchDir(), "keys.txt");
//
//        MockConfiguration mc = new MockConfiguration();
//        GenerateKeys gc = new GenerateKeys(mc);
//        gc.addArgument("3");
//        mc.setKeyStoreFile(f.getPath());
//
//        gc.initialize();
//
//        gc.execute();
//
//        assertTrue(f.isFile());
//
//        String s = Files.read(f);
//        StringTokenizer st = new StringTokenizer(s, "\n");
//
//        assertTrue(st.hasMoreTokens());
//        log.info(st.nextToken());
//
//        assertTrue(st.hasMoreTokens());
//        log.info(st.nextToken());
//
//        assertTrue(st.hasMoreTokens());
//        log.info(st.nextToken());
//
//        assertFalse(st.hasMoreTokens());
//    }

    // Package protected -----------------------------------------------------------------------------------------------

    // Protected -------------------------------------------------------------------------------------------------------

    @Override
    protected GenerateKeys getCommandToTest(Configuration c)
    {
        return new GenerateKeys(c);
    }

    // Private ---------------------------------------------------------------------------------------------------------

    // Inner classes ---------------------------------------------------------------------------------------------------

}


text: """All feets tests"""


text:
layout: docs
permalink: /docs/adapters/development/index.html
---

# Adapter Basics

All adapters inherit from the Adapter class in the `src/adapter.coffee` file.  There are certain methods that you will want to override.  Here is a basic stub of what an extended Adapter class would look like:

```coffee
class Sample extends Adapter

  constructor: ->
    @robot.logger.info "Constructor"
    super

  send: (envelope, strings...) ->
    @robot.logger.info "Send"

  reply: (envelope, strings...) ->
    @robot.logger.info "Reply"

  run: ->
    @robot.logger.info "Run"
    @emit "connected"
    user = new User 1001, name: 'Sample User'
    message = new TextMessage user, 'Some Sample Message', 'MSG-001'
    @robot.receive message

exports.use = (robot) ->
  new Sample robot
```

# Setting Up Your Development Environment

1. Create a new folder for your adapter `hubot-sample`
  - `mkdir hubot-sample`
2. Change your working directory to `hubot-sample`
  - `cd hubot-sample`
3. Run `npm init` to create your package.json
  - make sure the entry point is `src/sample.coffee`
4. Add your `.gitignore` to include `node_modules`
5. Edit the `src/sample.coffee` file to include the above stub for your adapter
6. Edit the `package.json` to add a peer dependency on `hubot`

  ```json
  "dependencies": {
  },
  "peerDependencies": {
    "hubot": ">=2.0"
  },
  "devDependencies": {
    "coffee-script": ">=1.2.0"
  }
  ```

7. Generate your Hubot using the `yo hubot` [command](https://hubot.github.com/docs/)
8. Change working directories to the `hubot` you created in step 7.
9. Now perform an `npm link` to add your adapter to `hubot`
  - `npm link ../hubot-sample`
10. Run `hubot -a sample`

# Gotchas

There is a an open issue in the node community around [npm linked peer dependencies not working](https://github.com/npm/npm/issues/5875).  To get this working for our project you will need to do some minor changes to your code.

1. For the import in your `hubot-sample` adapter, add the following code
```coffee
try
  {Robot,Adapter,TextMessage,User} = require 'hubot'
catch
  prequire = require('parent-require')
  {Robot,Adapter,TextMessage,User} = prequire 'hubot'
  ```
2. In your `hubot-sample` folder, modify the `package.json` to include the following dependency so this custom import mechanism will work
```json
  "dependencies": {
    "parent-require": "^1.0.0"
  }
  ```
3. Now try running `hubot -a sample` again and see that the imports are properly loaded.
4. Once this is working properly, you can build out the functionality of your adapter as you see fit.  Take a look at some of the other adapters to get some ideas for your implementation.
  - Once packaged and deployed via `npm`, you won't need the dependency in `hubot` anymore since the peer dependency should work as an official module.


text:
package com.securionpay.response;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.securionpay.enums.FileUploadPurpose;
import com.securionpay.enums.FileUploadType;

import java.util.HashMap;
import java.util.Map;

import static com.securionpay.util.SecurionPayUtils.toStringNullSafe;

public class FileUpload {
	private String id;
	private Long created;
	private String purpose;
	private Integer size;
	private String type;
	private String url;

	@JsonIgnore
	private final Map<String, Object> other = new HashMap<>();

	public String getId() {
		return id;
	}

	public Long getCreated() {
		return created;
	}

	public FileUploadPurpose getPurpose() {
		return FileUploadPurpose.fromValue(purpose);
	}

	public String getPurposeAsString() {
		return purpose;
	}

	public Integer getSize() {
		return size;
	}

	public FileUploadType getType() {
		return FileUploadType.fromValue(type);
	}

	public String getTypeAsString() {
		return type;
	}

	public String getUrl() {
		return url;
	}

	public String get(String name) {
		return toStringNullSafe(other.get(name));
	}

	@JsonAnySetter
	private void set(String name, Object value) {
		other.put(name, value);
	}
}