<commit_before>"""
Tests for applications in the 'corpora' domain.

"""

from testcases import CloudCareTestCase

class BasicTestTestCase(CloudCareTestCase):
    app_name = 'Basic Test'
    module_name = 'Basic Test'
    case_list_forms = ['Update Case', 'Close Case']
    teardown_close_case_form = 'Close Case'

    def create_case(self):
        self.open_form('New Case')
        name = self.random_string()
        self.find_element_by_id('textfield').send_keys(name)
        self.submit_form()

        return name

    def test_first_form(self):
        self.open_form('First Form')

        name = self.random_string()
        self.find_element_by_id('textfield').send_keys(name)
        self.submit_form()

        self.open_form('Update Case')
        self.assertNotIn(name, self.page_source)

    def test_new_case(self):
        name = self.create_case()
        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_id('case-details')
        tds = self.find_elements_by_xpath("//section[@id='case-details']//td")
        self.assertTrue(len(tds) > 0)
        for td in tds:
            self.assertTrue(td.text == '?' or td.text == name)

    def test_update_case(self):
        name = self.create_case()

        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_link_text('Enter Update Case').click()

        self.find_element_by_xpath("//span[text()='Blue']").click()
        self.submit_form()

        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_xpath(
            "//section[@id='case-details']//td[text()='blue']"
        )

    def test_close_case(self):
        name = self.create_case()
        self.open_form('Close Case')
        self.click_case(name)
        self.find_element_by_link_text('Enter Close Case').click()
        self.find_element_by_id('textfield').send_keys('reason for closing')
        self.submit_form()
        self.open_form('Update Case')
        self.assertNotIn(name, self.page_source)

#class BasicFormTestsTestCase(CloudCareTestCase):
    #app_name = 'Coverage'
    #module_name = 'Basic Form Tests'

    #def test_datatypes(self):
        #pass

    #def test_groups(self):
        #pass

    #def test_constraints(self):
        #pass

    #def test_other(self):
        #pass

    #def test_repeats(self):
        #pass

<commit_msg>remove stub testcase<commit_after>"""
Tests for applications in the 'corpora' domain.

"""

from testcases import CloudCareTestCase

class BasicTestTestCase(CloudCareTestCase):
    app_name = 'Basic Test'
    module_name = 'Basic Test'
    case_list_forms = ['Update Case', 'Close Case']
    teardown_close_case_form = 'Close Case'

    def create_case(self):
        self.open_form('New Case')
        name = self.random_string()
        self.find_element_by_id('textfield').send_keys(name)
        self.submit_form()

        return name

    def test_first_form(self):
        self.open_form('First Form')

        name = self.random_string()
        self.find_element_by_id('textfield').send_keys(name)
        self.submit_form()

        self.open_form('Update Case')
        self.assertNotIn(name, self.page_source)

    def test_new_case(self):
        name = self.create_case()
        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_id('case-details')
        tds = self.find_elements_by_xpath("//section[@id='case-details']//td")
        self.assertTrue(len(tds) > 0)
        for td in tds:
            self.assertTrue(td.text == '?' or td.text == name)

    def test_update_case(self):
        name = self.create_case()

        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_link_text('Enter Update Case').click()

        self.find_element_by_xpath("//span[text()='Blue']").click()
        self.submit_form()

        self.open_form('Update Case')
        self.click_case(name)
        self.find_element_by_xpath(
            "//section[@id='case-details']//td[text()='blue']"
        )

    def test_close_case(self):
        name = self.create_case()
        self.open_form('Close Case')
        self.click_case(name)
        self.find_element_by_link_text('Enter Close Case').click()
        self.find_element_by_id('textfield').send_keys('reason for closing')
        self.submit_form()
        self.open_form('Update Case')
        self.assertNotIn(name, self.page_source)
<|endoftext|>


<commit_before>from ..models import SQLLocation
from .util import LocationHierarchyTestCase
from corehq.apps.users.models import WebUser
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users

class BaseTestLocationQuerysetMethods(LocationHierarchyTestCase):
    location_type_names = ['state', 'county', 'city']
    location_structure = [
        ('Massachusetts', [
            ('Middlesex', [
                ('Cambridge', []),
                ('Somerville', []),
            ]),
            ('Suffolk', [
                ('Boston', []),
            ])
         ]),
        ('California', [
             ('Los Angeles', []),
        ])
    ]

class TestLocationQuerysetMethods(BaseTestLocationQuerysetMethods):

    def test_filter_by_user_input(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_by_user_input(self.domain, "Middlesex"))
        self.assertItemsEqual(
            ['Middlesex'],
            [loc.name for loc in middlesex_locs]
        )

    def test_filter_path_by_user_input(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_path_by_user_input(self.domain, "Middlesex"))
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

    def test_filter_by_partial_match(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_path_by_user_input(self.domain, "Middle"))
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

class TestLocationScopedQueryset(BaseTestLocationQuerysetMethods):

    @classmethod
    def setUpClass(cls):
        super(TestLocationScopedQueryset, cls).setUpClass()
        delete_all_users()

    def setUp(self):
        super(TestLocationScopedQueryset, self).setUp()
        self.web_user = WebUser.create(self.domain, 'blah', 'password')
        self.web_user.set_location(self.domain, self.locations['Middlesex'])

    def tearDown(self):
        delete_all_users()
        super(TestLocationScopedQueryset, self).tearDown()

    def test_access_all_locations_enabled(self):
        all_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual(self.locations.values(), all_locs)

    def test_primary_location_assigned_and_descendants(self):
        self.restrict_user_to_location(self.web_user)
        accessible_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )

        self.assertItemsEqual(
            [self.locations[location] for location in ["Middlesex", "Cambridge", "Somerville"]],
            accessible_locs
        )

    def test_location_assigned_and_their_descendants(self):
        self.web_user.add_to_assigned_locations(self.domain, self.locations['California'])
        self.restrict_user_to_location(self.web_user)
        accessible_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )

        accessible_loc_names = ["Middlesex", "Cambridge", "Somerville", "California", "Los Angeles"]
        self.assertItemsEqual(
            [self.locations[location] for location in accessible_loc_names],
            accessible_locs
        )

    def test_location_restricted_but_unassigned(self):
        # unassigned users shouldn't be able to access any locations
        unassigned_user = WebUser.create(self.domain, 'unassigned', 'password')
        self.restrict_user_to_location(unassigned_user)
        self.assertItemsEqual(
            [],
            SQLLocation.objects.accessible_to_user(self.domain, unassigned_user)
        )

    def test_filter_path_by_user_input(self):
        self.restrict_user_to_location(self.web_user)

        # User searching for higher in the hierarchy is only given the items
        # they are allowed to see
        middlesex_locs = (
            SQLLocation.objects
            .filter_path_by_user_input(self.domain, "Massachusetts")
            .accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

        # User searching for a branch they don't have access to get nothing
        no_locs = (
            SQLLocation.objects
            .filter_path_by_user_input(self.domain, "Suffolk")
            .accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual([], no_locs)
<commit_msg>fix dimagimon complaint [skip ci]<commit_after>from ..models import SQLLocation
from .util import LocationHierarchyTestCase
from corehq.apps.users.models import WebUser
from corehq.apps.users.dbaccessors.all_commcare_users import delete_all_users

class BaseTestLocationQuerysetMethods(LocationHierarchyTestCase):
    location_type_names = ['state', 'county', 'city']
    location_structure = [
        ('Massachusetts', [
            ('Middlesex', [
                ('Cambridge', []),
                ('Somerville', []),
            ]),
            ('Suffolk', [
                ('Boston', []),
            ])
        ]),
        ('California', [
            ('Los Angeles', []),
        ])
    ]

class TestLocationQuerysetMethods(BaseTestLocationQuerysetMethods):

    def test_filter_by_user_input(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_by_user_input(self.domain, "Middlesex"))
        self.assertItemsEqual(
            ['Middlesex'],
            [loc.name for loc in middlesex_locs]
        )

    def test_filter_path_by_user_input(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_path_by_user_input(self.domain, "Middlesex"))
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

    def test_filter_by_partial_match(self):
        middlesex_locs = (SQLLocation.objects
                          .filter_path_by_user_input(self.domain, "Middle"))
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

class TestLocationScopedQueryset(BaseTestLocationQuerysetMethods):

    @classmethod
    def setUpClass(cls):
        super(TestLocationScopedQueryset, cls).setUpClass()
        delete_all_users()

    def setUp(self):
        super(TestLocationScopedQueryset, self).setUp()
        self.web_user = WebUser.create(self.domain, 'blah', 'password')
        self.web_user.set_location(self.domain, self.locations['Middlesex'])

    def tearDown(self):
        delete_all_users()
        super(TestLocationScopedQueryset, self).tearDown()

    def test_access_all_locations_enabled(self):
        all_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual(self.locations.values(), all_locs)

    def test_primary_location_assigned_and_descendants(self):
        self.restrict_user_to_location(self.web_user)
        accessible_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )

        self.assertItemsEqual(
            [self.locations[location] for location in ["Middlesex", "Cambridge", "Somerville"]],
            accessible_locs
        )

    def test_location_assigned_and_their_descendants(self):
        self.web_user.add_to_assigned_locations(self.domain, self.locations['California'])
        self.restrict_user_to_location(self.web_user)
        accessible_locs = (
            SQLLocation.objects.accessible_to_user(self.domain, self.web_user)
        )

        accessible_loc_names = ["Middlesex", "Cambridge", "Somerville", "California", "Los Angeles"]
        self.assertItemsEqual(
            [self.locations[location] for location in accessible_loc_names],
            accessible_locs
        )

    def test_location_restricted_but_unassigned(self):
        # unassigned users shouldn't be able to access any locations
        unassigned_user = WebUser.create(self.domain, 'unassigned', 'password')
        self.restrict_user_to_location(unassigned_user)
        self.assertItemsEqual(
            [],
            SQLLocation.objects.accessible_to_user(self.domain, unassigned_user)
        )

    def test_filter_path_by_user_input(self):
        self.restrict_user_to_location(self.web_user)

        # User searching for higher in the hierarchy is only given the items
        # they are allowed to see
        middlesex_locs = (
            SQLLocation.objects
            .filter_path_by_user_input(self.domain, "Massachusetts")
            .accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual(
            ['Middlesex', 'Cambridge', 'Somerville'],
            [loc.name for loc in middlesex_locs]
        )

        # User searching for a branch they don't have access to get nothing
        no_locs = (
            SQLLocation.objects
            .filter_path_by_user_input(self.domain, "Suffolk")
            .accessible_to_user(self.domain, self.web_user)
        )
        self.assertItemsEqual([], no_locs)
<|endoftext|>


<commit_before># 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.

INCLUDES = """
#ifdef _WIN32
#include <Wincrypt.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
"""

TYPES = """
static const char *const Cryptography_urandom_engine_name;
static const char *const Cryptography_urandom_engine_id;
"""

FUNCTIONS = """
int Cryptography_add_urandom_engine(void);
"""

MACROS = """
"""

CUSTOMIZATIONS = """
static const char *Cryptography_urandom_engine_id= "urandom";
static const char *Cryptography_urandom_engine_name = "urandom_engine";

#ifndef _WIN32
static int urandom_fd = -1;

static int urandom_rand_bytes(unsigned char *buffer, int size) {
    ssize_t n;
    while (0 < size) {
        do {
            n = read(urandom_fd, buffer, (size_t)size);
        } while (n < 0 && errno == EINTR);
        if (n <= 0) {
            return 0;
            break;
        }
        buffer += n;
        size -= n;
    }
    return 1;
}

static int urandom_rand_status(void) {
    return 1;
}

static int urandom_init(ENGINE *e) {
    urandom_fd = open("/dev/urandom", O_RDONLY);
    if (urandom_fd > -1) {
        return 1;
    } else {
        return 0;
    }
}

static int urandom_finish(ENGINE *e) {
    int n;
    do {
        n = close(urandom_fd);
    } while (n < 0 && errno == EINTR);
    if (n < 0) {
        return 0;
    } else {
        return 1;
    }
}
#endif

#ifdef _WIN32
/* This handle is never explicitly released. Instead, the operating
   system will release it when the process terminates. */
static HCRYPTPROV hCryptProv = 0;

static int urandom_init(ENGINE *e) {
    if (CryptAcquireContext(&hCryptProv, NULL, NULL,
                            PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
        return 1;
    } else {
        return 0;
    }
}

static int urandom_rand_bytes(unsigned char *buffer, int size) {
    size_t chunk;

    if (hCryptProv == 0) {
        return 0;
    }

    while (size > 0) {
        chunk = size;
        if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) {
            return 0;
        }
        buffer += chunk;
        size -= chunk;
    }
    return 1;
}

static int urandom_finish(ENGINE *e) {
    return 1;
}

static int urandom_rand_status(void) {
    return 1;
}
#endif /* MS_WINDOWS */

static RAND_METHOD urandom_rand = {
    NULL,
    urandom_rand_bytes,
    NULL,
    NULL,
    urandom_rand_bytes,
    urandom_rand_status,
};

int Cryptography_add_urandom_engine(void) {
    ENGINE *e = ENGINE_new();
    if(!ENGINE_set_id(e, Cryptography_urandom_engine_id) ||
            !ENGINE_set_name(e, Cryptography_urandom_engine_name) ||
            !ENGINE_set_RAND(e, &urandom_rand) ||
            !ENGINE_set_init_function(e, urandom_init) ||
            !ENGINE_set_finish_function(e, urandom_finish)) {
        return 0;
    }
    if (!ENGINE_add(e)) {
        ENGINE_free(e);
        return 0;
    }
    if (!ENGINE_free(e)) {
        return 0;
    }

    return 1;
}
"""

CONDITIONAL_NAMES = {}
<commit_msg>additional check to verify the ENGINE_new doesn't return null<commit_after># 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.

INCLUDES = """
#ifdef _WIN32
#include <Wincrypt.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
"""

TYPES = """
static const char *const Cryptography_urandom_engine_name;
static const char *const Cryptography_urandom_engine_id;
"""

FUNCTIONS = """
int Cryptography_add_urandom_engine(void);
"""

MACROS = """
"""

CUSTOMIZATIONS = """
static const char *Cryptography_urandom_engine_id= "urandom";
static const char *Cryptography_urandom_engine_name = "urandom_engine";

#ifndef _WIN32
static int urandom_fd = -1;

static int urandom_rand_bytes(unsigned char *buffer, int size) {
    ssize_t n;
    while (0 < size) {
        do {
            n = read(urandom_fd, buffer, (size_t)size);
        } while (n < 0 && errno == EINTR);
        if (n <= 0) {
            return 0;
            break;
        }
        buffer += n;
        size -= n;
    }
    return 1;
}

static int urandom_rand_status(void) {
    return 1;
}

static int urandom_init(ENGINE *e) {
    urandom_fd = open("/dev/urandom", O_RDONLY);
    if (urandom_fd > -1) {
        return 1;
    } else {
        return 0;
    }
}

static int urandom_finish(ENGINE *e) {
    int n;
    do {
        n = close(urandom_fd);
    } while (n < 0 && errno == EINTR);
    if (n < 0) {
        return 0;
    } else {
        return 1;
    }
}
#endif

#ifdef _WIN32
/* This handle is never explicitly released. Instead, the operating
   system will release it when the process terminates. */
static HCRYPTPROV hCryptProv = 0;

static int urandom_init(ENGINE *e) {
    if (CryptAcquireContext(&hCryptProv, NULL, NULL,
                            PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
        return 1;
    } else {
        return 0;
    }
}

static int urandom_rand_bytes(unsigned char *buffer, int size) {
    size_t chunk;

    if (hCryptProv == 0) {
        return 0;
    }

    while (size > 0) {
        chunk = size;
        if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) {
            return 0;
        }
        buffer += chunk;
        size -= chunk;
    }
    return 1;
}

static int urandom_finish(ENGINE *e) {
    return 1;
}

static int urandom_rand_status(void) {
    return 1;
}
#endif /* MS_WINDOWS */

static RAND_METHOD urandom_rand = {
    NULL,
    urandom_rand_bytes,
    NULL,
    NULL,
    urandom_rand_bytes,
    urandom_rand_status,
};

int Cryptography_add_urandom_engine(void) {
    ENGINE *e = ENGINE_new();
    if (e == NULL) {
        return 0;
    }
    if(!ENGINE_set_id(e, Cryptography_urandom_engine_id) ||
            !ENGINE_set_name(e, Cryptography_urandom_engine_name) ||
            !ENGINE_set_RAND(e, &urandom_rand) ||
            !ENGINE_set_init_function(e, urandom_init) ||
            !ENGINE_set_finish_function(e, urandom_finish)) {
        return 0;
    }
    if (!ENGINE_add(e)) {
        ENGINE_free(e);
        return 0;
    }
    if (!ENGINE_free(e)) {
        return 0;
    }

    return 1;
}
"""

CONDITIONAL_NAMES = {}
<|endoftext|>


<commit_before>class DictOutput():
  """DictOutput.
  """

  def __init__(self, mode='CamelCase'):
    """DictOutput constructor

    :param mode: output mode, default ``CamelCase``
    :type mode: str
    """
    self.map = dict()
    self.coord_map = ["left", "top", "right", "bottom"]
    # add your input type if you create one.
    self.input_types = ["image", "face"]

    if mode == 'CamelCase':
        self.fillDictCamelCase()
    else:
        self.fillDictOld()

  def fillInputFieldsCamelCase(self, input, input_str):
    """Fill ``self.map`` fields in CamelCase for input type ``input``

    :param input: input type string
    :type input: str
    :param input_str: input type string in CamelCase
    :type input_str: str
    """
    self.map['query_' + input] = "Query" + input_str
    self.map['similar_' + input + 's'] = "Similar" + input_str + "s"
    self.map[input + 's'] = input_str + "s"
    self.map['number_'+input+'s'] = "Number" + input_str + "s"
    self.map['number_similar_'+input+'s'] = "NumberSimilar" + input_str + "s"
    self.map['all_similar_'+input+'s'] = "AllSimilar" + input_str + "s"

  def fillInputFieldsOld(self, input, input_str):
    """Fill ``self.map`` fields in snake_case for input type ``input``

    :param input: input type string
    :type input: str
    :param input_str: input type string in snake_case
    :type input_str: str
    """
    self.map['query_' + input] = "query_" + input_str
    self.map['similar_' + input + 's'] = "similar_" + input_str + "s"
    self.map[input + 's'] = input_str + "s"
    self.map['number_'+input+'s'] = "number_" + input_str + "s"
    self.map['number_similar_'+input+'s'] = "number_similar_" + input_str + "s"
    self.map['all_similar_'+input+'s'] = "all_similar_" + input_str + "s"

  def fillDictCamelCase(self):
    """Fill ``self.map`` dictionary in CamelCase
    """
    self.map['query_sha1'] = "QuerySha1"
    self.map['query_url'] = "QueryURL"
    self.map['image_sha1s'] = "ImageSha1s"
    self.map['img_info'] = "ImgInfo"
    self.map['distances'] = "Distances"
    self.map['cached_image_urls'] = "CachedImageURLs"
    for input in self.input_types:
      input_str = input.title()
      self.fillInputFieldsCamelCase(input, input_str)

  def fillDictOld(self):
    """Fill ``self.map`` dictionary in snake_case
    """
    self.map['query_sha1'] = "query_sha1"
    self.map['query_url'] = "query_url"
    self.map['image_sha1s'] = "image_sha1s"
    self.map['img_info'] = "img_info"
    self.map['distances'] = "distances"
    self.map['cached_image_urls'] = "cached_image_urls"
    for input in self.input_types:
      self.fillInputFieldsOld(input, input)

  def format_output(self, dets, sim_images, sim_dets, sim_score, options_dict=dict(),
                    input_type="image"):
    """Format search output

    :param dets: list of query samples, images or list of detections in each image
    :type dets: list
    :param sim_images: list of similar images
    :type sim_images: list
    :param sim_dets: list of similar detections (only used if ``input_type`` is not "image")
    :type sim_dets: list
    :param sim_score: list of similarity scores (actually distances...)
    :type sim_score: list
    :param options_dict: options dictionary
    :type options_dict: dict
    :param input_type: input type ("image" or something else that requires detection e.g. "face")
    :type input_type: str
    :return: formatted output
    :rtype: collections.OrderedDict
    """
    import time
    from collections import OrderedDict
    print "[format_output: log] options are: {}".format(options_dict)
    start_build_output = time.time()
    output = []
    images_query = set()
    nb_faces_query = 0
    nb_faces_similar = 0
    nb_images_similar = 0

    #print 'dets',len(dets),dets
    #print 'sim_dets', len(sim_dets), sim_dets

    if input_type != "image":

      for i in range(len(dets)):

        # No detection in query
        if not dets[i][1]:
          output.append(dict())
          out_i = len(output) - 1
          output[out_i][self.map['query_sha1']] = dets[i][0]
          if dets[i][2]:
            output[out_i][self.map['query_url']] = dets[i][2]
          output[out_i][self.map['img_info']] = dets[i][3:5]
          images_query.add(dets[i][0])
          output[out_i][self.map['similar_'+input_type+'s']] = OrderedDict([[self.map['number_'+input_type+'s'], 0],
                                                                 [self.map['image_sha1s'], []],
                                                                 [self.map[input_type+'s'], []],
                                                                 [self.map['cached_image_urls'], []],
                                                                 [self.map['distances'], []]])
          continue

        # We found some faces...
        #print len(dets[i][1])
        for j, face_bbox in enumerate(dets[i][1]):
          nb_faces_query += 1

          # Add one output for each face query
          output.append(dict())
          out_i = len(output) - 1
          output[out_i][self.map['query_sha1']] = dets[i][0]
          output[out_i][self.map['query_'+input_type]] = dets[i][1][j]
          if dets[i][2]:
            output[out_i][self.map['query_url']] = dets[i][2]
          output[out_i][self.map['img_info']] = dets[i][3:]
          images_query.add(dets[i][0])

          nb_dets = 0
          #print "sim_dets[i]",sim_dets[i]

          if sim_dets[i]:
            if len(sim_dets[i])>j and sim_dets[i][j]:
              #print "sim_dets[i][j]",sim_dets[i][j]
              nb_dets = len(sim_dets[i][j])

          output[out_i][self.map['similar_'+input_type+'s']] = OrderedDict([[self.map['number_'+input_type+'s'], nb_dets],
                                                                [self.map['image_sha1s'], []],
                                                                [self.map[input_type+'s'], []],
                                                                [self.map['img_info'], []],
                                                                [self.map['cached_image_urls'], []],
                                                                [self.map['distances'], []]])

          #print 'nb_faces: %d' % nb_faces

          # Explore list of similar faces
          for jj in range(nb_dets):
            sim_det = sim_dets[i][j][jj]
            nb_faces_similar += 1
            #print sim_images[i][j][jj]
            output[out_i][self.map['similar_'+input_type+'s']][self.map['image_sha1s']].append(sim_images[i][j][jj][0].strip())
            if sim_images[i][j][jj] > 1:
              try:
                output[out_i][self.map['similar_'+input_type+'s']][self.map['cached_image_urls']].append(sim_images[i][j][jj][1][self.url_field].strip())
              except Exception:
                output[out_i][self.map['similar_'+input_type+'s']][self.map['cached_image_urls']].append("")

            tmp_face_dict = dict()
            for tfi, tfcoord in enumerate(sim_det.split('_')[1:]):
              tmp_face_dict[self.coord_map[tfi]] = int(tfcoord)
            output[out_i][self.map['similar_'+input_type+'s']][self.map[input_type+'s']].append(tmp_face_dict)
            # this is not in HBase for all/most images...
            #osf_imginfo = sim_images[i][j][jj][1][self.img_info_field].strip()
            output[out_i][self.map['similar_'+input_type+'s']][self.map['img_info']].append('')
            output[out_i][self.map['similar_'+input_type+'s']][self.map['distances']].append(sim_score[i][j][jj])

      outp = OrderedDict([[self.map['number_images'], len(images_query)],
                          [self.map['number_'+input_type+'s'], nb_faces_query],
                          [self.map['number_similar_'+input_type+'s'], nb_faces_similar],
                          [self.map['all_similar_'+input_type+'s'], output]])

    else:
      # no detections, dets contains list of query images sha1

      for i in range(len(dets)):
        sha1 = dets[i][0]

        # Add one output for each image query
        output.append(dict())
        out_i = len(output) - 1
        output[out_i][self.map['query_sha1']] = sha1
        if dets[i][1]:
          output[out_i][self.map['query_url']] = dets[i][1]

        nb_images = 0
        if sim_images[i][0]:
          nb_images = len(sim_images[i][0])

        output[out_i][self.map['similar_images']] = OrderedDict([[self.map['number_images'], nb_images],
                                                              [self.map['image_sha1s'], []],
                                                              [self.map['cached_image_urls'], []],
                                                              [self.map['distances'], []]])

        # Explore list of similar images
        for j in range(nb_images):
          #print "sim_images[i][j]",sim_images[i][0][j]
          nb_images_similar += 1
          output[out_i][self.map['similar_images']][self.map['image_sha1s']].append(sim_images[i][0][j][0].strip())
          try:
            output[out_i][self.map['similar_images']][self.map['cached_image_urls']].append(sim_images[i][0][j][1][self.url_field].strip())
          except Exception:
            output[out_i][self.map['similar_images']][self.map['cached_image_urls']].append("")
          output[out_i][self.map['similar_images']][self.map['distances']].append(float(sim_score[i][0][j]))

      outp = OrderedDict([[self.map['number_images'], len(dets)],
                          [self.map['number_similar_images'], nb_images_similar],
                          [self.map['all_similar_images'], output]])

    print("[format_output: log] build_output took: {}".format(time.time() - start_build_output))
    #print outp
    return outp
<commit_msg>check sim_images[i] is not empty<commit_after>class DictOutput():
  """DictOutput.
  """

  def __init__(self, mode='CamelCase'):
    """DictOutput constructor

    :param mode: output mode, default ``CamelCase``
    :type mode: str
    """
    self.map = dict()
    self.coord_map = ["left", "top", "right", "bottom"]
    # add your input type if you create one.
    self.input_types = ["image", "face"]

    if mode == 'CamelCase':
        self.fillDictCamelCase()
    else:
        self.fillDictOld()

  def fillInputFieldsCamelCase(self, input, input_str):
    """Fill ``self.map`` fields in CamelCase for input type ``input``

    :param input: input type string
    :type input: str
    :param input_str: input type string in CamelCase
    :type input_str: str
    """
    self.map['query_' + input] = "Query" + input_str
    self.map['similar_' + input + 's'] = "Similar" + input_str + "s"
    self.map[input + 's'] = input_str + "s"
    self.map['number_'+input+'s'] = "Number" + input_str + "s"
    self.map['number_similar_'+input+'s'] = "NumberSimilar" + input_str + "s"
    self.map['all_similar_'+input+'s'] = "AllSimilar" + input_str + "s"

  def fillInputFieldsOld(self, input, input_str):
    """Fill ``self.map`` fields in snake_case for input type ``input``

    :param input: input type string
    :type input: str
    :param input_str: input type string in snake_case
    :type input_str: str
    """
    self.map['query_' + input] = "query_" + input_str
    self.map['similar_' + input + 's'] = "similar_" + input_str + "s"
    self.map[input + 's'] = input_str + "s"
    self.map['number_'+input+'s'] = "number_" + input_str + "s"
    self.map['number_similar_'+input+'s'] = "number_similar_" + input_str + "s"
    self.map['all_similar_'+input+'s'] = "all_similar_" + input_str + "s"

  def fillDictCamelCase(self):
    """Fill ``self.map`` dictionary in CamelCase
    """
    self.map['query_sha1'] = "QuerySha1"
    self.map['query_url'] = "QueryURL"
    self.map['image_sha1s'] = "ImageSha1s"
    self.map['img_info'] = "ImgInfo"
    self.map['distances'] = "Distances"
    self.map['cached_image_urls'] = "CachedImageURLs"
    for input in self.input_types:
      input_str = input.title()
      self.fillInputFieldsCamelCase(input, input_str)

  def fillDictOld(self):
    """Fill ``self.map`` dictionary in snake_case
    """
    self.map['query_sha1'] = "query_sha1"
    self.map['query_url'] = "query_url"
    self.map['image_sha1s'] = "image_sha1s"
    self.map['img_info'] = "img_info"
    self.map['distances'] = "distances"
    self.map['cached_image_urls'] = "cached_image_urls"
    for input in self.input_types:
      self.fillInputFieldsOld(input, input)

  def format_output(self, dets, sim_images, sim_dets, sim_score, options_dict=dict(),
                    input_type="image"):
    """Format search output

    :param dets: list of query samples, images or list of detections in each image
    :type dets: list
    :param sim_images: list of similar images
    :type sim_images: list
    :param sim_dets: list of similar detections (only used if ``input_type`` is not "image")
    :type sim_dets: list
    :param sim_score: list of similarity scores (actually distances...)
    :type sim_score: list
    :param options_dict: options dictionary
    :type options_dict: dict
    :param input_type: input type ("image" or something else that requires detection e.g. "face")
    :type input_type: str
    :return: formatted output
    :rtype: collections.OrderedDict
    """
    import time
    from collections import OrderedDict
    print "[format_output: log] options are: {}".format(options_dict)
    start_build_output = time.time()
    output = []
    images_query = set()
    nb_faces_query = 0
    nb_faces_similar = 0
    nb_images_similar = 0

    #print 'dets',len(dets),dets
    #print 'sim_dets', len(sim_dets), sim_dets

    if input_type != "image":

      for i in range(len(dets)):

        # No detection in query
        if not dets[i][1]:
          output.append(dict())
          out_i = len(output) - 1
          output[out_i][self.map['query_sha1']] = dets[i][0]
          if dets[i][2]:
            output[out_i][self.map['query_url']] = dets[i][2]
          output[out_i][self.map['img_info']] = dets[i][3:5]
          images_query.add(dets[i][0])
          output[out_i][self.map['similar_'+input_type+'s']] = OrderedDict([[self.map['number_'+input_type+'s'], 0],
                                                                 [self.map['image_sha1s'], []],
                                                                 [self.map[input_type+'s'], []],
                                                                 [self.map['cached_image_urls'], []],
                                                                 [self.map['distances'], []]])
          continue

        # We found some faces...
        #print len(dets[i][1])
        for j, face_bbox in enumerate(dets[i][1]):
          nb_faces_query += 1

          # Add one output for each face query
          output.append(dict())
          out_i = len(output) - 1
          output[out_i][self.map['query_sha1']] = dets[i][0]
          output[out_i][self.map['query_'+input_type]] = dets[i][1][j]
          if dets[i][2]:
            output[out_i][self.map['query_url']] = dets[i][2]
          output[out_i][self.map['img_info']] = dets[i][3:]
          images_query.add(dets[i][0])

          nb_dets = 0
          #print "sim_dets[i]",sim_dets[i]

          if sim_dets[i]:
            if len(sim_dets[i])>j and sim_dets[i][j]:
              #print "sim_dets[i][j]",sim_dets[i][j]
              nb_dets = len(sim_dets[i][j])

          output[out_i][self.map['similar_'+input_type+'s']] = OrderedDict([[self.map['number_'+input_type+'s'], nb_dets],
                                                                [self.map['image_sha1s'], []],
                                                                [self.map[input_type+'s'], []],
                                                                [self.map['img_info'], []],
                                                                [self.map['cached_image_urls'], []],
                                                                [self.map['distances'], []]])

          #print 'nb_faces: %d' % nb_faces

          # Explore list of similar faces
          for jj in range(nb_dets):
            sim_det = sim_dets[i][j][jj]
            nb_faces_similar += 1
            #print sim_images[i][j][jj]
            output[out_i][self.map['similar_'+input_type+'s']][self.map['image_sha1s']].append(sim_images[i][j][jj][0].strip())
            if sim_images[i][j][jj] > 1:
              try:
                output[out_i][self.map['similar_'+input_type+'s']][self.map['cached_image_urls']].append(sim_images[i][j][jj][1][self.url_field].strip())
              except Exception:
                output[out_i][self.map['similar_'+input_type+'s']][self.map['cached_image_urls']].append("")

            tmp_face_dict = dict()
            for tfi, tfcoord in enumerate(sim_det.split('_')[1:]):
              tmp_face_dict[self.coord_map[tfi]] = int(tfcoord)
            output[out_i][self.map['similar_'+input_type+'s']][self.map[input_type+'s']].append(tmp_face_dict)
            # this is not in HBase for all/most images...
            #osf_imginfo = sim_images[i][j][jj][1][self.img_info_field].strip()
            output[out_i][self.map['similar_'+input_type+'s']][self.map['img_info']].append('')
            output[out_i][self.map['similar_'+input_type+'s']][self.map['distances']].append(sim_score[i][j][jj])

      outp = OrderedDict([[self.map['number_images'], len(images_query)],
                          [self.map['number_'+input_type+'s'], nb_faces_query],
                          [self.map['number_similar_'+input_type+'s'], nb_faces_similar],
                          [self.map['all_similar_'+input_type+'s'], output]])

    else:
      # no detections, dets contains list of query images sha1

      for i in range(len(dets)):
        sha1 = dets[i][0]

        # Add one output for each image query
        output.append(dict())
        out_i = len(output) - 1
        output[out_i][self.map['query_sha1']] = sha1
        if dets[i][1]:
          output[out_i][self.map['query_url']] = dets[i][1]

        nb_images = 0
        if sim_images[i] and sim_images[i][0]:
          nb_images = len(sim_images[i][0])

        output[out_i][self.map['similar_images']] = OrderedDict([[self.map['number_images'], nb_images],
                                                              [self.map['image_sha1s'], []],
                                                              [self.map['cached_image_urls'], []],
                                                              [self.map['distances'], []]])

        # Explore list of similar images
        for j in range(nb_images):
          #print "sim_images[i][j]",sim_images[i][0][j]
          nb_images_similar += 1
          output[out_i][self.map['similar_images']][self.map['image_sha1s']].append(sim_images[i][0][j][0].strip())
          try:
            output[out_i][self.map['similar_images']][self.map['cached_image_urls']].append(sim_images[i][0][j][1][self.url_field].strip())
          except Exception:
            output[out_i][self.map['similar_images']][self.map['cached_image_urls']].append("")
          output[out_i][self.map['similar_images']][self.map['distances']].append(float(sim_score[i][0][j]))

      outp = OrderedDict([[self.map['number_images'], len(dets)],
                          [self.map['number_similar_images'], nb_images_similar],
                          [self.map['all_similar_images'], output]])

    print("[format_output: log] build_output took: {}".format(time.time() - start_build_output))
    #print outp
    return outp
<|endoftext|>


<commit_before>from cumulusci.tasks.salesforce.BaseSalesforceApiTask import BaseSalesforceApiTask
from cumulusci.core.utils import process_list_arg

class CreatePermissionSet(BaseSalesforceApiTask):
    task_options = {
        "api_name": {
            "description": "API name of generated Permission Set",
            "required": True,
        },
        "label": {"description": "Label of generated Permission Set"},
        "user_permissions": {
            "description": "List of User Permissions to include in the Permission Set.",
            "required": True,
        },
    }

    def _init_options(self, kwargs):
        super()._init_options(kwargs)

        self.options["user_permissions"] = process_list_arg(
            self.options["user_permissions"]
        )

    def _run_task(self):
        result = self.sf.PermissionSet.create(
            {
                "Name": self.options["api_name"],
                "Label": self.options.get("label") or self.options["api_name"],
                **{perm: True for perm in self.options["user_permissions"]},
            }
        )
        result = self.sf.PermissionSetAssignment.create(
            {"AssigneeId": self.org_config.user_id, "PermissionSetId": result["id"]}
        )
<commit_msg>Remove  unneeded assignment<commit_after>from cumulusci.tasks.salesforce.BaseSalesforceApiTask import BaseSalesforceApiTask
from cumulusci.core.utils import process_list_arg

class CreatePermissionSet(BaseSalesforceApiTask):
    task_options = {
        "api_name": {
            "description": "API name of generated Permission Set",
            "required": True,
        },
        "label": {"description": "Label of generated Permission Set"},
        "user_permissions": {
            "description": "List of User Permissions to include in the Permission Set.",
            "required": True,
        },
    }

    def _init_options(self, kwargs):
        super()._init_options(kwargs)

        self.options["user_permissions"] = process_list_arg(
            self.options["user_permissions"]
        )

    def _run_task(self):
        result = self.sf.PermissionSet.create(
            {
                "Name": self.options["api_name"],
                "Label": self.options.get("label") or self.options["api_name"],
                **{perm: True for perm in self.options["user_permissions"]},
            }
        )
        self.sf.PermissionSetAssignment.create(
            {"AssigneeId": self.org_config.user_id, "PermissionSetId": result["id"]}
        )
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

from binascii import a2b_hex, b2a_hex
import copy
import json
import logging
from struct import pack, unpack

import gevent
from gevent import queue, ssl, socket, select

ERROR_RESPONSE_LENGTH = 6
ERROR_RESPONSE_FORMAT = (
    '!'  # network big-endian
    'B'  # command
    'B'  # status
    'I'  # identifier
)

ENHANCED_NOTIFICATION_FORMAT = (
    '!'  # network big-endian
    'B'  # command
    'I'  # identifier
    'I'  # expiry
    'H'  # token length
    '32s'  # token
    'H'  # payload length
    '%ds'  # payload
)

ENHANCED_NOTIFICATION_COMMAND = 1
MAX_PAYLOAD_LENGTH = 2048
TOKEN_LENGTH = 32
ERROR_RESPONSE_LENGTH = 6

class APNsConnection(object):
    """
    A generic connection class for communicating with the APNs
    """
    def __init__(self, cert_file=None, key_file=None, timeout=None):
        super(APNsConnection, self).__init__()
        self.cert_file = cert_file
        self.key_file = key_file
        self.timeout = timeout
        self._socket = None
        self._ssl = None
        self.connection_alive = False

    def __del__(self):
        self.disconnect()

    def connect(self):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.connect((self.server, self.port))
        self._ssl = ssl.wrap_socket(
            self._socket,
            self.key_file,
            self.cert_file)
        self.connection_alive = True

    def disconnect(self):
        if self.connection_alive:
            if self._socket:
                self._socket.close()
            if self._ssl:
                self._ssl.close()
            self.connection_alive = False

    def reconnect(self):
        self.disconnect()
        self.connect()

    def connection(self):
        if not self._ssl or not self.connection_alive:
            self.connect()
        return self._ssl

    def read(self, n=None):
        return self.connection().read(n)

    def write(self, string):
        return self.connection().write(string)

class PayloadAlert(object):
    def __init__(
            self, body=None, action_loc_key=None, loc_key=None,
            loc_args=None, launch_image=None):
        super(PayloadAlert, self).__init__()
        self.body = body
        self.action_loc_key = action_loc_key
        self.loc_key = loc_key
        self.loc_args = loc_args
        self.launch_image = launch_image

    def dict(self):
        d = {}
        if self.body:
            d['body'] = self.body
        if self.action_loc_key:
            d['action-loc-key'] = self.action_loc_key
        if self.loc_key:
            d['loc-key'] = self.loc_key
        if self.loc_args:
            d['loc-args'] = self.loc_args
        if self.launch_image:
            d['launch-image'] = self.launch_image
        return d

class Payload(object):
    """A class representing an APNs message payload"""
    def __init__(
            self, alert=None, badge=None, sound=None, category=None,
            custom={}, content_available=False):
        super(Payload, self).__init__()
        self.alert = alert
        self.badge = badge
        self.sound = sound
        self.category = category
        self.custom = custom
        self.content_available = content_available
        self._check_size()

    def dict(self):
        """Returns the payload as a regular Python dictionary"""
        d = {}
        if self.alert:
            if isinstance(self.alert, PayloadAlert):
                d['alert'] = self.alert.dict()
            else:
                d['alert'] = self.alert
        if self.sound:
            d['sound'] = self.sound
        if self.badge is not None:
            d['badge'] = int(self.badge)
        if self.category:
            d['category'] = self.category

        if self.content_available:
            d.update({'content-available': 1})

        d = {'aps': d}
        d.update(self.custom)
        return d

    def json(self):
        return json.dumps(
            self.dict(),
            separators=(',', ':'),
            ensure_ascii=False).encode('utf-8')

    def _check_size(self):
        payload_length = len(self.json())
        if payload_length > MAX_PAYLOAD_LENGTH:
            raise PayloadTooLargeError(payload_length)

    def __repr__(self):
        attrs = ("alert", "badge", "sound", "category", "custom")
        args = ", ".join(["%s=%r" % (n, getattr(self, n)) for n in attrs])
        return "%s(%s)" % (self.__class__.__name__, args)

class PayloadTooLargeError(StandardError):
    def __init__(self, payload_size):
        super(PayloadTooLargeError, self).__init__()
        self.payload_size = payload_size

class GatewayConnection(APNsConnection):
    def __init__(self, use_sandbox=False, **kwargs):
        super(GatewayConnection, self).__init__(**kwargs)
        self.server = (
            'gateway.push.apple.com',
            'gateway.sandbox.push.apple.com')[use_sandbox]
        self.port = 2195

    def get_notification(self, token_hex, payload, identifier, expiry):
        token = a2b_hex(token_hex)
        payload = payload.json()
        fmt = ENHANCED_NOTIFICATION_FORMAT % len(payload)
        notification = pack(
            fmt, ENHANCED_NOTIFICATION_COMMAND, identifier,
            expiry, TOKEN_LENGTH, token, len(payload), payload)
        return notification

    def send_notification(self, token_hex, payload, identifier=0, expiry=0):
        self.write(
            self.get_notification(token_hex, payload, identifier, expiry))

class Pipe(object):
    def __init__(
            self, cert_file=None, key_file=None,
            use_sandbox=False, connection_count=1):
        super(Pipe, self).__init__()
        self.use_sandbox = use_sandbox
        self.cert_file = cert_file
        self.key_file = key_file
        self.connection_count = connection_count
        # init queue
        self.push_queue = queue.Queue(maxsize=self.connection_count*100)
        self.threads = []
        self.invalid = False

    def push_worker(self):
        if self.invalid:
            return
        gateway_connection = GatewayConnection(
            use_sandbox=self.use_sandbox,
            cert_file=self.cert_file,
            key_file=self.key_file,
        )
        pushed_buffer = queue.Queue(maxsize=100)

        push_id = 0
        while True:
            if self.invalid:
                return
            try:
                gateway_connection.reconnect()
                while True:
                    rlist, wlist, _ = select.select(
                        [gateway_connection._ssl],
                        [gateway_connection._ssl],
                        [],
                        10)
                    if rlist:
                        buff = gateway_connection.read(ERROR_RESPONSE_LENGTH)
                        if len(buff) == ERROR_RESPONSE_LENGTH:
                            command, status, error_identifier = \
                                unpack(ERROR_RESPONSE_FORMAT, buff)
                            if 8 == command:
                                found = False
                                while not pushed_buffer.empty():
                                    identifier, job = pushed_buffer.get()
                                    if found:
                                        while self.push_queue.full():
                                            gevent.sleep(1)
                                        self.push_queue.put(job)
                                    elif identifier == error_identifier:
                                        found = True
                        else:
                            logging.error((
                                self.key_file,
                                self.invalid,
                                self.push_queue.qsize(),
                                'unexcepted read buf length'))
                            break
                    elif wlist:
                        job = self.push_queue.get()
                        push_id += 1
                        gateway_connection.send_notification(
                            job['device_token'],
                            Payload(**job['payload']),
                            push_id)
                        if pushed_buffer.full():
                            pushed_buffer.get()
                        pushed_buffer.put((push_id, job))
                        gevent.sleep(0.1)
            except ssl.SSLError, e:
                if e.errno == ssl.SSL_ERROR_SSL:
                    self.invalid = True
            except (socket.error, IOError), e:
                pass
            except Exception, e:
                logging.error(
                    (self.key_file, self.invalid, self.push_queue.qsize(), e))

    def start(self):
        for i in range(self.connection_count):
            self.threads.append(gevent.spawn(self.push_worker))

    def stop(self):
        for i in range(300):
            if self.push_queue.qsize():
                gevent.sleep(1)
            else:
                break

    def send(self, job):
        logging.error((self.key_file, self.invalid, self.push_queue.qsize()))
        if self.invalid:
            return
        while self.push_queue.qsize() > self.connection_count * 50:
            gevent.sleep(1)
        self.push_queue.put(job)
<commit_msg>add waiting for new job sleep<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

from binascii import a2b_hex, b2a_hex
import copy
import json
import logging
from struct import pack, unpack

import gevent
from gevent import queue, ssl, socket, select

ERROR_RESPONSE_LENGTH = 6
ERROR_RESPONSE_FORMAT = (
    '!'  # network big-endian
    'B'  # command
    'B'  # status
    'I'  # identifier
)

ENHANCED_NOTIFICATION_FORMAT = (
    '!'  # network big-endian
    'B'  # command
    'I'  # identifier
    'I'  # expiry
    'H'  # token length
    '32s'  # token
    'H'  # payload length
    '%ds'  # payload
)

ENHANCED_NOTIFICATION_COMMAND = 1
MAX_PAYLOAD_LENGTH = 2048
TOKEN_LENGTH = 32
ERROR_RESPONSE_LENGTH = 6

class APNsConnection(object):
    """
    A generic connection class for communicating with the APNs
    """
    def __init__(self, cert_file=None, key_file=None, timeout=None):
        super(APNsConnection, self).__init__()
        self.cert_file = cert_file
        self.key_file = key_file
        self.timeout = timeout
        self._socket = None
        self._ssl = None
        self.connection_alive = False

    def __del__(self):
        self.disconnect()

    def connect(self):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.connect((self.server, self.port))
        self._ssl = ssl.wrap_socket(
            self._socket,
            self.key_file,
            self.cert_file)
        self.connection_alive = True

    def disconnect(self):
        if self.connection_alive:
            if self._socket:
                self._socket.close()
            if self._ssl:
                self._ssl.close()
            self.connection_alive = False

    def reconnect(self):
        self.disconnect()
        self.connect()

    def connection(self):
        if not self._ssl or not self.connection_alive:
            self.connect()
        return self._ssl

    def read(self, n=None):
        return self.connection().read(n)

    def write(self, string):
        return self.connection().write(string)

class PayloadAlert(object):
    def __init__(
            self, body=None, action_loc_key=None, loc_key=None,
            loc_args=None, launch_image=None):
        super(PayloadAlert, self).__init__()
        self.body = body
        self.action_loc_key = action_loc_key
        self.loc_key = loc_key
        self.loc_args = loc_args
        self.launch_image = launch_image

    def dict(self):
        d = {}
        if self.body:
            d['body'] = self.body
        if self.action_loc_key:
            d['action-loc-key'] = self.action_loc_key
        if self.loc_key:
            d['loc-key'] = self.loc_key
        if self.loc_args:
            d['loc-args'] = self.loc_args
        if self.launch_image:
            d['launch-image'] = self.launch_image
        return d

class Payload(object):
    """A class representing an APNs message payload"""
    def __init__(
            self, alert=None, badge=None, sound=None, category=None,
            custom={}, content_available=False):
        super(Payload, self).__init__()
        self.alert = alert
        self.badge = badge
        self.sound = sound
        self.category = category
        self.custom = custom
        self.content_available = content_available
        self._check_size()

    def dict(self):
        """Returns the payload as a regular Python dictionary"""
        d = {}
        if self.alert:
            if isinstance(self.alert, PayloadAlert):
                d['alert'] = self.alert.dict()
            else:
                d['alert'] = self.alert
        if self.sound:
            d['sound'] = self.sound
        if self.badge is not None:
            d['badge'] = int(self.badge)
        if self.category:
            d['category'] = self.category

        if self.content_available:
            d.update({'content-available': 1})

        d = {'aps': d}
        d.update(self.custom)
        return d

    def json(self):
        return json.dumps(
            self.dict(),
            separators=(',', ':'),
            ensure_ascii=False).encode('utf-8')

    def _check_size(self):
        payload_length = len(self.json())
        if payload_length > MAX_PAYLOAD_LENGTH:
            raise PayloadTooLargeError(payload_length)

    def __repr__(self):
        attrs = ("alert", "badge", "sound", "category", "custom")
        args = ", ".join(["%s=%r" % (n, getattr(self, n)) for n in attrs])
        return "%s(%s)" % (self.__class__.__name__, args)

class PayloadTooLargeError(StandardError):
    def __init__(self, payload_size):
        super(PayloadTooLargeError, self).__init__()
        self.payload_size = payload_size

class GatewayConnection(APNsConnection):
    def __init__(self, use_sandbox=False, **kwargs):
        super(GatewayConnection, self).__init__(**kwargs)
        self.server = (
            'gateway.push.apple.com',
            'gateway.sandbox.push.apple.com')[use_sandbox]
        self.port = 2195

    def get_notification(self, token_hex, payload, identifier, expiry):
        token = a2b_hex(token_hex)
        payload = payload.json()
        fmt = ENHANCED_NOTIFICATION_FORMAT % len(payload)
        notification = pack(
            fmt, ENHANCED_NOTIFICATION_COMMAND, identifier,
            expiry, TOKEN_LENGTH, token, len(payload), payload)
        return notification

    def send_notification(self, token_hex, payload, identifier=0, expiry=0):
        self.write(
            self.get_notification(token_hex, payload, identifier, expiry))

class Pipe(object):
    def __init__(
            self, cert_file=None, key_file=None,
            use_sandbox=False, connection_count=1):
        super(Pipe, self).__init__()
        self.use_sandbox = use_sandbox
        self.cert_file = cert_file
        self.key_file = key_file
        self.connection_count = connection_count
        # init queue
        self.push_queue = queue.Queue(maxsize=self.connection_count*100)
        self.threads = []
        self.invalid = False

    def push_worker(self):
        if self.invalid:
            return
        gateway_connection = GatewayConnection(
            use_sandbox=self.use_sandbox,
            cert_file=self.cert_file,
            key_file=self.key_file,
        )
        pushed_buffer = queue.Queue(maxsize=100)

        push_id = 0
        while True:
            if self.invalid:
                return
            try:
                gateway_connection.reconnect()
                while True:
                    rlist, wlist, _ = select.select(
                        [gateway_connection._ssl],
                        [gateway_connection._ssl],
                        [],
                        10)
                    if rlist:
                        buff = gateway_connection.read(ERROR_RESPONSE_LENGTH)
                        if len(buff) == ERROR_RESPONSE_LENGTH:
                            command, status, error_identifier = \
                                unpack(ERROR_RESPONSE_FORMAT, buff)
                            if 8 == command:
                                found = False
                                while not pushed_buffer.empty():
                                    identifier, job = pushed_buffer.get()
                                    if found:
                                        while self.push_queue.full():
                                            gevent.sleep(1)
                                        self.push_queue.put(job)
                                    elif identifier == error_identifier:
                                        found = True
                        else:
                            logging.error((
                                self.key_file,
                                self.invalid,
                                self.push_queue.qsize(),
                                'unexcepted read buf length'))
                            break
                    elif wlist:
                        job = self.push_queue.get()
                        push_id += 1
                        gateway_connection.send_notification(
                            job['device_token'],
                            Payload(**job['payload']),
                            push_id)
                        if pushed_buffer.full():
                            pushed_buffer.get()
                        pushed_buffer.put((push_id, job))
                        gevent.sleep(0.1)
                    else:
                        gateway_connection.disconnect()
                        while not self.push_queue.qsize():
                            gevent.sleep(5)
                            logging.error((
                                self.key_file,
                                self.invalid,
                                self.push_queue.qsize(),
                                'waiting for new job'))

            except ssl.SSLError, e:
                if e.errno == ssl.SSL_ERROR_SSL:
                    self.invalid = True
            except (socket.error, IOError), e:
                pass
            except Exception, e:
                logging.error(
                    (self.key_file, self.invalid, self.push_queue.qsize(), e))

    def start(self):
        for i in range(self.connection_count):
            self.threads.append(gevent.spawn(self.push_worker))

    def stop(self):
        for i in range(300):
            if self.push_queue.qsize():
                gevent.sleep(1)
            else:
                break

    def send(self, job):
        logging.error((self.key_file, self.invalid, self.push_queue.qsize()))
        if self.invalid:
            return
        while self.push_queue.qsize() > self.connection_count * 50:
            gevent.sleep(1)
        self.push_queue.put(job)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
    Copyright 2011 cloudControl GmbH

    This is the s3-image-upload.py script for cloudControl's new architecture. With this
    file a user is enabled to upload a given file (a squashed image file) to S3.

    In order for this to work, the AWS credentials need to be set via environment variables!
    Make sure to set following environment variables:

        $ export AWS_ACCESS_KEY_ID=<Your AWS Access Key ID>
        $ export AWS_SECRET_ACCESS_KEY=<Your AWS Secret Access Key>

    Bug request to: Hans-Gunther Schmidt (hgs@cloudcontrol.com)
"""
import sys
import boto
from liblog import log

def connect():
    try:
        s3 = boto.connect_s3()
    except boto.exception.NoAuthHandlerFound:
        sys.exit("Could not find valid AWS credentials! Make sure to set "
                 "\'AWS_ACCESS_KEY_ID\' and \'AWS_SECRET_ACCESS_KEY\'!")

    return s3

def upload(image_file, image_key, bucket_name):
    """
        Upload a given image file to our S3 bucket
    """
    try:
        s3 = connect()

        #        bucket = s3.create_bucket(bucket_name, location=Location.EU)
        bucket = s3.create_bucket(bucket_name)

        key = bucket.new_key(image_key)
        key.set_contents_from_filename(image_file)

    except boto.exception.S3CreateError as error:
        log("Could not create {}! Error: {}".format(image_key, error))
    except (boto.exception.S3DataError, boto.exception.S3PermissionsError, boto.exception.S3ResponseError) as error:
        log("Ran into boto.exception.S3DataError! Error: {}".format(error))
    except Exception as error:
        log("Unexpected error! Error: {}".format(error))
        sys.exit(1)

def download(destination_file, image_key, bucket_name):
    """
        Upload a given image file to our S3 bucket
    """
    try:
        s3 = connect()
        key = s3.get_bucket(bucket_name).get_key(image_key)
        key.get_contents_to_filename(destination_file)
    except boto.exception.S3CreateError as error:
        log("Could not create {}! Error: {}".format(image_key, error))
    except (boto.exception.S3DataError, boto.exception.S3PermissionsError, boto.exception.S3ResponseError) as error:
        log("Ran into boto.exception.S3DataError! Error: {}".format(error))
    except Exception as error:
        log("Unexpected error! Error: {}".format(error))
        sys.exit(1)
<commit_msg>enabling libs3 to use new logging + cleanups<commit_after># -*- coding: utf-8 -*-
"""
    Copyright 2011 cloudControl GmbH

    This is the s3-image-upload.py script for cloudControl's new architecture. With this
    file a user is enabled to upload a given file (a squashed image file) to S3.

    In order for this to work, the AWS credentials need to be set via environment variables!
    Make sure to set following environment variables:

        $ export AWS_ACCESS_KEY_ID=<Your AWS Access Key ID>
        $ export AWS_SECRET_ACCESS_KEY=<Your AWS Secret Access Key>

    Bug request to: Hans-Gunther Schmidt (hgs@cloudcontrol.com)
"""
import sys
import boto
from LogHandler import LogHandler

####################################################################
#
# FUNCTIONS
#
####################################################################

def connect():
    """
        Connect to S3 with credentials
    """
    log = LogHandler().get_logger()

    try:
        s3 = boto.connect_s3()
        log.debug(">>> Connected to S3")
    except boto.exception.NoAuthHandlerFound:
        sys.exit("Could not find valid AWS credentials! Make sure to set \'AWS_ACCESS_KEY_ID\' and "
                 "\'AWS_SECRET_ACCESS_KEY\'!")
    return s3

def upload(image_file, image_key, bucket_name):
    """
        Upload a given image file to our S3 bucket
    """
    log = LogHandler().get_logger()

    try:
        s3 = connect()
        bucket = s3.create_bucket(bucket_name)
        key = bucket.new_key(image_key)
        key.set_contents_from_filename(image_file)
    except boto.exception.S3CreateError as error:
        log.error("Could not create {}! Error: {}".format(image_key, error))
    except (boto.exception.S3DataError, boto.exception.S3PermissionsError, boto.exception.S3ResponseError) as error:
        log.error("Ran into boto.exception.S3DataError! Error: {}".format(error))
    except Exception as error:
        log.error("Unexpected error! Error: {}".format(error))
        sys.exit(1)

    return 0

def download(destination_file, image_key, bucket_name):
    """
        Upload a given image file to our S3 bucket
    """
    log = LogHandler().get_logger()

    try:
        s3 = connect()
        key = s3.get_bucket(bucket_name).get_key(image_key)
        key.get_contents_to_filename(destination_file)
    except boto.exception.S3CreateError as error:
        log.error("Could not create {}! Error: {}".format(image_key, error))
    except (boto.exception.S3DataError, boto.exception.S3PermissionsError, boto.exception.S3ResponseError) as error:
        log.error("Ran into boto.exception.S3DataError! Error: {}".format(error))
    except Exception as error:
        log.error("Unexpected error! Error: {}".format(error))
        sys.exit(1)

    return 0
<|endoftext|>


<commit_before>"""Registering translated models for the ``multilingual_initiatives`` app."""
from simple_translation.translation_pool import translation_pool

from .models import Initiative, InitiativeTranslation

translation_pool.register_translation(Initiative, InitiativeTranslation)
<commit_msg>Removed simple_translate.py<commit_after><|endoftext|>


<commit_before>"""
Signal handlers for invalidating cached data.
"""

import six
from django.conf import settings
from django.dispatch.dispatcher import receiver
from opaque_keys.edx.locator import LibraryLocator

from xmodule.modulestore.django import SignalHandler

from . import config
from .api import clear_course_from_cache
from .tasks import update_course_in_cache_v2

@receiver(SignalHandler.course_published)
def update_block_structure_on_course_publish(sender, course_key, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that a course has been published in the module
    store and creates/updates the corresponding cache entry.
    Ignores publish signals from content libraries.
    """
    if isinstance(course_key, LibraryLocator):
        return

    if config.waffle().is_enabled(config.INVALIDATE_CACHE_ON_PUBLISH):
        clear_course_from_cache(course_key)

    update_course_in_cache_v2.apply_async(
        kwargs=dict(course_id=six.text_type(course_key)),
        countdown=settings.BLOCK_STRUCTURES_SETTINGS['COURSE_PUBLISH_TASK_DELAY'],
    )

@receiver(SignalHandler.course_deleted)
def _delete_block_structure_on_course_delete(sender, course_key, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that a course has been deleted from the
    module store and invalidates the corresponding cache entry if one
    exists.
    """
    clear_course_from_cache(course_key)
<commit_msg>Catches BlockStructureNotFound raised when clearing course from cache<commit_after>"""
Signal handlers for invalidating cached data.
"""

import logging

import six
from django.conf import settings
from django.dispatch.dispatcher import receiver
from opaque_keys.edx.locator import LibraryLocator

from xmodule.modulestore.django import SignalHandler

from . import config
from .api import clear_course_from_cache
from .models import BlockStructureNotFound
from .tasks import update_course_in_cache_v2

log = logging.getLogger(__name__)

@receiver(SignalHandler.course_published)
def update_block_structure_on_course_publish(sender, course_key, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that a course has been published in the module
    store and creates/updates the corresponding cache entry.
    Ignores publish signals from content libraries.
    """
    if isinstance(course_key, LibraryLocator):
        return

    if config.waffle().is_enabled(config.INVALIDATE_CACHE_ON_PUBLISH):
        try:
            clear_course_from_cache(course_key)
        except BlockStructureNotFound:
            log.warning(
                u"BlockStructure: %s not found when trying to clear course from cache",
                course_key,
            )

    update_course_in_cache_v2.apply_async(
        kwargs=dict(course_id=six.text_type(course_key)),
        countdown=settings.BLOCK_STRUCTURES_SETTINGS['COURSE_PUBLISH_TASK_DELAY'],
    )

@receiver(SignalHandler.course_deleted)
def _delete_block_structure_on_course_delete(sender, course_key, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that a course has been deleted from the
    module store and invalidates the corresponding cache entry if one
    exists.
    """
    clear_course_from_cache(course_key)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from openprocurement.api.views.tender import TenderResource
from openprocurement.tender.openeu.views.tender import TenderEUResource
from openprocurement.tender.openua.validation import validate_patch_tender_ua_data
from openprocurement.tender.competitivedialogue.utils import patch_eu, set_ownership
from openprocurement.api.utils import opresource, json_view, save_tender, context_unpack
from openprocurement.tender.competitivedialogue.models import CD_EU_TYPE, CD_UA_TYPE, STAGE_2_EU_TYPE, STAGE_2_UA_TYPE

@opresource(name='Competitive Dialogue for EU procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=CD_EU_TYPE,
            description="Open Contracting compatible data exchange format. See  for more info")
class CompetitiveDialogueEUResource(TenderEUResource):
    """ Resource handler for Competitive Dialogue EU"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Competitive Dialogue for UA procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=CD_UA_TYPE,
            description="Open Contracting compatible data exchange format. See # for more info")
class CompetitiveDialogueUAResource(TenderResource):
    """ Resource handler for Competitive Dialogue UA"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender Stage 2 for UA procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=STAGE_2_UA_TYPE,
            description="")
class TenderStage2UAResource(TenderResource):
    """ Resource handler for tender stage 2 UA"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender Stage 2 for EU procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=STAGE_2_EU_TYPE,
            description="")
class TenderStage2UEResource(TenderResource):
    """ Resource handler for tender stage 2 EU"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender stage2 credentials',
            path='/tenders/{tender_id}/credentials',
            procurementMethodType=STAGE_2_EU_TYPE,
            description="Tender stage2 credentials")
class TenderStage2CredentialsResource(TenderResource):
    def __init__(self, request, context):
        super(TenderStage2CredentialsResource, self).__init__(request, context)
        self.server = request.registry.couchdb_server

    @json_view(permission='generate_credentials')
    def patch(self):
        tender = self.request.validated['tender']
        if tender.status != "draft":
            self.request.errors.add('body', 'data',
                                    'Can\'t generate credentials in current ({}) contract status'.format(
                                        tender.status))
            self.request.errors.status = 403
            return

        set_ownership(tender)
        if save_tender(self.request):
            self.LOGGER.info('Generate Tender stage2 credentials {}'.format(tender.id),
                             extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_patch'}))
            return {
                'data': tender.serialize("view"),
                'access': {
                    'token': tender.owner_token
                }
            }<commit_msg>add view  for get credentials<commit_after># -*- coding: utf-8 -*-
from openprocurement.api.views.tender import TenderResource
from openprocurement.tender.openeu.views.tender import TenderEUResource
from openprocurement.tender.openua.validation import validate_patch_tender_ua_data
from openprocurement.tender.competitivedialogue.utils import patch_eu, set_ownership
from openprocurement.api.utils import opresource, json_view, save_tender, context_unpack
from openprocurement.tender.competitivedialogue.models import CD_EU_TYPE, CD_UA_TYPE, STAGE_2_EU_TYPE, STAGE_2_UA_TYPE

@opresource(name='Competitive Dialogue for EU procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=CD_EU_TYPE,
            description="Open Contracting compatible data exchange format. See  for more info")
class CompetitiveDialogueEUResource(TenderEUResource):
    """ Resource handler for Competitive Dialogue EU"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Competitive Dialogue for UA procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=CD_UA_TYPE,
            description="Open Contracting compatible data exchange format. See # for more info")
class CompetitiveDialogueUAResource(TenderResource):
    """ Resource handler for Competitive Dialogue UA"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender Stage 2 for UA procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=STAGE_2_UA_TYPE,
            description="")
class TenderStage2UAResource(TenderResource):
    """ Resource handler for tender stage 2 UA"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender Stage 2 for EU procedure',
            path='/tenders/{tender_id}',
            procurementMethodType=STAGE_2_EU_TYPE,
            description="")
class TenderStage2UEResource(TenderResource):
    """ Resource handler for tender stage 2 EU"""

    @json_view(content_type="application/json", validators=(validate_patch_tender_ua_data,), permission='edit_tender')
    def patch(self):
        return patch_eu(self)

@opresource(name='Tender stage2 EU credentials',
            path='/tenders/{tender_id}/credentials',
            procurementMethodType=STAGE_2_EU_TYPE,
            description="Tender stage2 UE credentials")
class TenderStage2EUCredentialsResource(TenderResource):
    def __init__(self, request, context):
        super(TenderStage2EUCredentialsResource, self).__init__(request, context)
        self.server = request.registry.couchdb_server

    @json_view(permission='generate_credentials')
    def patch(self):
        tender = self.request.validated['tender']
        if tender.status != "draft":
            self.request.errors.add('body', 'data',
                                    'Can\'t generate credentials in current ({}) contract status'.format(
                                        tender.status))
            self.request.errors.status = 403
            return

        set_ownership(tender)
        if save_tender(self.request):
            self.LOGGER.info('Generate Tender stage2 credentials {}'.format(tender.id),
                             extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_patch'}))
            return {
                'data': tender.serialize("view"),
                'access': {
                    'token': tender.owner_token
                }
            }

@opresource(name='Tender stage2 UA credentials',
            path='/tenders/{tender_id}/credentials',
            procurementMethodType=STAGE_2_UA_TYPE,
            description="Tender stage2 UA credentials")
class TenderStage2UACredentialsResource(TenderResource):
    def __init__(self, request, context):
        super(TenderStage2UACredentialsResource, self).__init__(request, context)
        self.server = request.registry.couchdb_server

    @json_view(permission='generate_credentials')
    def patch(self):
        tender = self.request.validated['tender']
        if tender.status != "draft":
            self.request.errors.add('body', 'data',
                                    'Can\'t generate credentials in current ({}) contract status'.format(
                                        tender.status))
            self.request.errors.status = 403
            return

        set_ownership(tender)
        if save_tender(self.request):
            self.LOGGER.info('Generate Tender stage2 credentials {}'.format(tender.id),
                             extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_patch'}))
            return {
                'data': tender.serialize("view"),
                'access': {
                    'token': tender.owner_token
                }
            }<|endoftext|>


<commit_before>
from peewee import Model, PrimaryKeyField, CharField, BooleanField, ForeignKeyField, IntegerField, DateTimeField, BlobField
from .PWDatabase import PWDatabase
from neocore.Cryptography.Crypto import Crypto
from neocore.UInt256 import UInt256
from neocore.UInt160 import UInt160
import binascii
from neo.Wallets.Coin import CoinReference
from neo.Blockchain import GetBlockchain

class ModelBase(Model):
    class Meta:
        database = PWDatabase.DBProxy()

class Account(ModelBase):
    Id = PrimaryKeyField()
    PrivateKeyEncrypted = BlobField(unique=True)
    PublicKeyHash = CharField()

class Address(ModelBase):
    Id = PrimaryKeyField()
    ScriptHash = BlobField(unique=True)
    IsWatchOnly = BooleanField(default=False)

    def ToString(self):
        return Crypto.ToAddress(UInt160(data=self.ScriptHash))

class NamedAddress(ModelBase):
    Id = PrimaryKeyField()
    ScriptHash = BlobField(unique=True)
    Title = CharField()

    def UInt160ScriptHash(self):
        return UInt160(data=self.ScriptHash)

    def ToString(self):
        return Crypto.ToAddress(UInt160(data=self.ScriptHash))

class Coin(ModelBase):
    Id = PrimaryKeyField()
    TxId = BlobField()
    Index = IntegerField()
    AssetId = BlobField()
    Value = IntegerField()
    ScriptHash = BlobField()
    State = IntegerField()
    Address = ForeignKeyField(Address)

class Contract(ModelBase):
    Id = PrimaryKeyField()
    RawData = CharField()
    ScriptHash = BlobField()
    PublicKeyHash = CharField()
    Account = ForeignKeyField(Account, null=True)
    Address = ForeignKeyField(Address)

class Key(ModelBase):
    Id = PrimaryKeyField()
    Name = CharField(unique=True)
    Value = BlobField()

class NEP5Token(ModelBase):
    ContractHash = CharField(unique=True)
    Name = CharField()
    Symbol = CharField()
    Decimals = IntegerField()

class Transaction(ModelBase):
    Id = PrimaryKeyField()
    Hash = CharField(unique=True)
    TransactionType = IntegerField()
    RawData = CharField()
    Height = IntegerField()
    DateTime = DateTimeField()

class TransactionInfo(ModelBase):
    Id = PrimaryKeyField()
    CoreTransaction = ForeignKeyField(Transaction)
    Height = IntegerField()
    DateTime = DateTimeField()

class VINHold(ModelBase):
    Id = PrimaryKeyField()
    Index = IntegerField()
    Hash = CharField()
    FromAddress = CharField()
    ToAddress = CharField()
    Amount = IntegerField()
    IsComplete = BooleanField(default=False)

    @property
    def Reference(self):
        reference = CoinReference(prev_hash=self.TXHash, prev_index=self.Index)
        return reference

    @property
    def TXHash(self):
        data = bytearray(binascii.unhexlify(self.Hash.encode('utf-8')))
        data.reverse()
        return UInt256(data=data)

    @property
    def Vin(self):
        index = bytearray(self.Index.to_bytes(1, 'little'))
        return self.TXHash.Data + index

    @property
    def OutputHash(self):
        data = bytearray(binascii.unhexlify(self.ToAddress.encode('utf-8')))
        data.reverse()
        return UInt160(data=data)

    @property
    def OutputAddr(self):
        return Crypto.ToAddress(self.OutputHash)

    @property
    def AssetId(self):
        return self.Output.AssetId

    @property
    def AssetName(self):
        if self.AssetId == GetBlockchain().SystemShare().Hash:
            return 'NEO'
        elif self.AssetId == GetBlockchain().SystemCoin().Hash:
            return 'Gas'
        return 'Unknown'

    @property
    def Output(self):
        tx, height = GetBlockchain().GetTransaction(self.TXHash)
        output = tx.outputs[self.Index]
        return output

    @property
    def InputHash(self):
        data = bytearray(binascii.unhexlify(self.FromAddress.encode('utf-8')))
        data.reverse()
        return UInt160(data=data)

    @property
    def InputAddr(self):
        return Crypto.ToAddress(self.InputHash)

    def ToJson(self):
        jsn = {
            'To': self.OutputAddr,
            'From': self.InputHash.ToString(),
            'Amount': self.Amount,
            'Index': self.Index,
            'TxId': self.Hash,
            'Complete': self.IsComplete
        }

        return jsn
<commit_msg>Update Models<commit_after>
from peewee import Model, PrimaryKeyField, CharField, BooleanField, ForeignKeyField, IntegerField, DateTimeField, BlobField
from .PWDatabase import PWDatabase
from neocore.Cryptography.Crypto import Crypto
from neocore.UInt256 import UInt256
from neocore.UInt160 import UInt160
import binascii
from neo.Wallets.Coin import CoinReference
from neo.Blockchain import GetBlockchain

class ModelBase(Model):
    class Meta:
        database = PWDatabase.DBProxy()

class Account(ModelBase):
    Id = PrimaryKeyField()
    PrivateKeyEncrypted = BlobField(unique=True)
    PublicKeyHash = CharField()

class Address(ModelBase):
    Id = PrimaryKeyField()
    ScriptHash = BlobField(unique=True)
    IsWatchOnly = BooleanField(default=False)

    def ToString(self):
        return Crypto.ToAddress(UInt160(data=self.ScriptHash))

class NamedAddress(ModelBase):
    Id = PrimaryKeyField()
    ScriptHash = BlobField(unique=True)
    Title = CharField()

    def UInt160ScriptHash(self):
        return UInt160(data=self.ScriptHash)

    def ToString(self):
        return Crypto.ToAddress(UInt160(data=self.ScriptHash))

class Coin(ModelBase):
    Id = PrimaryKeyField()
    TxId = BlobField()
    Index = IntegerField()
    AssetId = BlobField()
    Value = IntegerField()
    ScriptHash = BlobField()
    State = IntegerField()
    Address = ForeignKeyField(Address)

class Contract(ModelBase):
    Id = PrimaryKeyField()
    RawData = CharField()
    ScriptHash = BlobField()
    PublicKeyHash = CharField()
    Account = ForeignKeyField(Account, null=True)
    Address = ForeignKeyField(Address)

class Key(ModelBase):
    Id = PrimaryKeyField()
    Name = CharField(unique=True)
    Value = BlobField()

class NEP5Token(ModelBase):
    ContractHash = CharField(unique=True)
    Name = CharField()
    Symbol = CharField()
    Decimals = IntegerField()

class Transaction(ModelBase):
    Id = PrimaryKeyField()
    Hash = BlobField(unique=True)
    TransactionType = IntegerField()
    RawData = CharField()
    Height = IntegerField()
    DateTime = DateTimeField()

class TransactionInfo(ModelBase):
    Id = PrimaryKeyField()
    CoreTransaction = ForeignKeyField(Transaction)
    Height = IntegerField()
    DateTime = DateTimeField()

class VINHold(ModelBase):
    Id = PrimaryKeyField()
    Index = IntegerField()
    Hash = BlobField()
    FromAddress = CharField()
    ToAddress = CharField()
    Amount = IntegerField()
    IsComplete = BooleanField(default=False)

    @property
    def Reference(self):
        reference = CoinReference(prev_hash=self.TXHash, prev_index=self.Index)
        return reference

    @property
    def TXHash(self):
        data = bytearray(binascii.unhexlify(self.Hash.encode('utf-8')))
        data.reverse()
        return UInt256(data=data)

    @property
    def Vin(self):
        index = bytearray(self.Index.to_bytes(1, 'little'))
        return self.TXHash.Data + index

    @property
    def OutputHash(self):
        data = bytearray(binascii.unhexlify(self.ToAddress.encode('utf-8')))
        data.reverse()
        return UInt160(data=data)

    @property
    def OutputAddr(self):
        return Crypto.ToAddress(self.OutputHash)

    @property
    def AssetId(self):
        return self.Output.AssetId

    @property
    def AssetName(self):
        if self.AssetId == GetBlockchain().SystemShare().Hash:
            return 'NEO'
        elif self.AssetId == GetBlockchain().SystemCoin().Hash:
            return 'Gas'
        return 'Unknown'

    @property
    def Output(self):
        tx, height = GetBlockchain().GetTransaction(self.TXHash)
        output = tx.outputs[self.Index]
        return output

    @property
    def InputHash(self):
        data = bytearray(binascii.unhexlify(self.FromAddress.encode('utf-8')))
        data.reverse()
        return UInt160(data=data)

    @property
    def InputAddr(self):
        return Crypto.ToAddress(self.InputHash)

    def ToJson(self):
        jsn = {
            'To': self.OutputAddr,
            'From': self.InputHash.ToString(),
            'Amount': self.Amount,
            'Index': self.Index,
            'TxId': self.Hash,
            'Complete': self.IsComplete
        }

        return jsn
<|endoftext|>


<commit_before># Copyright 2013 New Dream Network, LLC (DreamHost)
#
#    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.
#
# @author: Mark McClain, DreamHost

import sys

import eventlet
eventlet.monkey_patch()

from oslo.config import cfg

from neutron.agent.common import config
from neutron.agent.linux import interface
from neutron.common import config as common_config
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.openstack.common import service
from neutron.services.loadbalancer.agent import agent_manager as manager

OPTS = [
    cfg.IntOpt(
        'periodic_interval',
        default=10,
        help=_('Seconds between periodic task runs')
    )
]

class LbaasAgentService(n_rpc.Service):
    def start(self):
        super(LbaasAgentService, self).start()
        self.tg.add_timer(
            cfg.CONF.periodic_interval,
            self.manager.run_periodic_tasks,
            None,
            None
        )

def main():
    cfg.CONF.register_opts(OPTS)
    cfg.CONF.register_opts(manager.OPTS)
    # import interface options just in case the driver uses namespaces
    cfg.CONF.register_opts(interface.OPTS)
    config.register_interface_driver_opts_helper(cfg.CONF)
    config.register_agent_state_opts_helper(cfg.CONF)
    config.register_root_helper(cfg.CONF)

    common_config.init(sys.argv[1:])
    config.setup_logging(cfg.CONF)

    mgr = manager.LbaasAgentManager(cfg.CONF)
    svc = LbaasAgentService(
        host=cfg.CONF.host,
        topic=topics.LOADBALANCER_AGENT,
        manager=mgr
    )
    service.launch(svc).wait()
<commit_msg>Remove unused arg to config.setup_logging()<commit_after># Copyright 2013 New Dream Network, LLC (DreamHost)
#
#    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.
#
# @author: Mark McClain, DreamHost

import sys

import eventlet
eventlet.monkey_patch()

from oslo.config import cfg

from neutron.agent.common import config
from neutron.agent.linux import interface
from neutron.common import config as common_config
from neutron.common import rpc as n_rpc
from neutron.common import topics
from neutron.openstack.common import service
from neutron.services.loadbalancer.agent import agent_manager as manager

OPTS = [
    cfg.IntOpt(
        'periodic_interval',
        default=10,
        help=_('Seconds between periodic task runs')
    )
]

class LbaasAgentService(n_rpc.Service):
    def start(self):
        super(LbaasAgentService, self).start()
        self.tg.add_timer(
            cfg.CONF.periodic_interval,
            self.manager.run_periodic_tasks,
            None,
            None
        )

def main():
    cfg.CONF.register_opts(OPTS)
    cfg.CONF.register_opts(manager.OPTS)
    # import interface options just in case the driver uses namespaces
    cfg.CONF.register_opts(interface.OPTS)
    config.register_interface_driver_opts_helper(cfg.CONF)
    config.register_agent_state_opts_helper(cfg.CONF)
    config.register_root_helper(cfg.CONF)

    common_config.init(sys.argv[1:])
    config.setup_logging()

    mgr = manager.LbaasAgentManager(cfg.CONF)
    svc = LbaasAgentService(
        host=cfg.CONF.host,
        topic=topics.LOADBALANCER_AGENT,
        manager=mgr
    )
    service.launch(svc).wait()
<|endoftext|>