<commit_before># -*- coding: utf-8 -*-

import time

import mendeley
from modularodm import fields

from website import settings
from website.addons.base import AddonNodeSettingsBase
from website.addons.base import AddonUserSettingsBase
from website.oauth.models import ExternalProvider

from . import utils
from .api import APISession

def serialize_folder(name, account_id, parent_id=None, list_id=None):
    retval = {
        'name': name,
        'provider_account_id': account_id,
        'provider_list_id': list_id,
    }
    if parent_id:
        retval['parent_list_id'] = parent_id

    return retval

class AddonMendeleyUserSettings(AddonUserSettingsBase):

    def _get_connected_accounts(self):
        """Get user's connected Mendeley accounts"""
        return [
            x for x in self.owner.external_accounts if x.provider == 'mendeley'
        ]

    def to_json(self, user):
        ret = super(AddonMendeleyUserSettings, self).to_json(user)
        ret['accounts'] = [
            utils.serialize_account(each)
            for each in self._get_connected_accounts()
        ]
        return ret

class AddonMendeleyNodeSettings(AddonNodeSettingsBase):
    external_account = fields.ForeignField('externalaccount',
                                           backref='connected')

    mendeley_list_id = fields.StringField()

    # Keep track of all user settings that have been associated with this
    #   instance. This is so OAuth grants can be checked, even if the grant is
    #   not currently being used.
    associated_user_settings = fields.AbstractForeignField(list=True)

    _api = None

    @property
    def api(self):
        """authenticated ExternalProvider instance"""
        if self._api is None:
            self._api = Mendeley()
            self._api.account = self.external_account
        return self._api

    def grant_oauth_access(self, user, external_account, metadata=None):
        """Grant OAuth access, updates metadata on user settings
        :param User user:
        :param ExternalAccount external_account:
        :param dict metadata:
        """
        user_settings = user.get_addon('mendeley')

        # associate the user settings with this node's settings
        if user_settings not in self.associated_user_settings:
            self.associated_user_settings.append(user_settings)
            self.save()

        user_settings.grant_oauth_access(
            node=self.owner,
            external_account=external_account,
            metadata=metadata,
        )

        user_settings.save()

    def verify_oauth_access(self, external_account, list_id):
        """Determine if access to the ExternalAccount has been granted
        :param ExternalAccount external_account:
        :param str list_id: ID of the Mendeley list requested
        :rtype bool:
        """
        for user_settings in self.associated_user_settings:
            try:
                granted = user_settings.oauth_grants[self.owner._id][external_account._id]
            except KeyError:
                # no grant for this node, move along
                continue

            if list_id in granted.get('lists', []):
                return True
        return False

    def get_accounts(self, user):
        accounts = [
            account for account
            in user.external_accounts
            if account.provider == 'mendeley'
        ]
        if self.external_account and self.external_account not in accounts:
            accounts.append(self.external_account)
        return accounts

    def to_json(self, user):
        ret = super(AddonMendeleyNodeSettings, self).to_json(user)
        ret.update({
            'listId': self.mendeley_list_id,
            'accounts': [utils.serialize_account(each) for each in self.get_accounts(user)],
            'currentAccount': utils.serialize_account(self.external_account),
        })
        return ret

class Mendeley(ExternalProvider):
    name = "Mendeley"
    short_name = "mendeley"

    client_id = settings.MENDELEY_CLIENT_ID
    client_secret = settings.MENDELEY_CLIENT_SECRET

    auth_url_base = 'https://api.mendeley.com/oauth/authorize'
    callback_url = 'https://api.mendeley.com/oauth/token'
    default_scopes = ['all']

    _client = None

    def handle_callback(self, response):
        client = self._get_client(response)

        # make a second request for the Mendeley user's ID and name
        profile = client.profiles.me

        return {
            'provider_id': profile.id,
            'display_name': profile.display_name
        }

    def _get_client(self, credentials):
        if not self._client:
            partial = mendeley.Mendeley(
                client_id=self.client_id,
                client_secret=self.client_secret,
                redirect_uri='http://cos.ngrok.com/oauth/callback/mendeley/',
            )
            self._client = APISession(partial, credentials)

        return self._client

    def _get_folders(self):
        """Get a list of a user's folders"""

        client = self.client

        return client.folders.list().items

    @property
    def client(self):
        """An API session with Mendeley"""
        if not self._client:
            self._client = self._get_client({
                'access_token': self.account.oauth_key,
                'refresh_token': self.account.refresh_token,
                'expires_at': time.mktime(self.account.expires_at.timetuple()),
                'token_type': 'bearer',
            })
        return self._client

    def _folder_tree(self, folder, flat_map):

        serialized = serialize_folder(folder[0].name, self.account.provider_id)
        serialized['children'] = [self._folder_tree(flat_map[f], flat_map) for f in folder[1]]
        serialized['kind'] = 'folder'
        return serialized

    @property
    def citation_folder_tree(self):
        """Nested list structure of serialized folders"""

        folders = self._get_folders()
        flat_map = {
            folder.id: (folder, [])
            for folder in folders
        }
        flat_map['root'] = (None, [])
        for folder in folders:
            if folder.parent_id:
                flat_map[folder.parent_id][1].append(folder.id)
            else:
                flat_map['root'][1].append(folder.id)
        tree = [
            serialize_folder(
                'All Documents',
                account_id=self.account.provider_id,
            )
        ]
        import pdb; pdb.set_trace()
        tree[0]['children'] = [self._folder_tree(flat_map[f], flat_map) for f in flat_map['root'][1]]
        tree[0]['kind'] = 'folder'
        return tree

    @property
    def citation_lists(self):
        """List of CitationList objects, derived from Mendeley folders"""

        folders = self._get_folders()

        # TODO: Verify OAuth access to each folder
        all_documents = serialize_folder(
            'All Documents',
            account_id=self.account.provider_id,
        )
        serialized_folders = [
            serialize_folder(
                each.name,
                account_id=self.account.provider_id,
                list_id=each.json['id'],
                parent_id=each.json.get('parent_id'),
            )
            for each in folders
        ]
        return [all_documents] + serialized_folders

    def get_list(self, list_id=None):
        """Get a single CitationList
        :param str list_id: ID for a Mendeley folder. Optional.
        :return CitationList: CitationList for the folder, or for all documents
        """
        folder = self.client.folders.get(list_id) if list_id else None
        if folder:
            return self._citations_for_mendeley_folder(folder)
        return self._citations_for_mendeley_user()

    def _citations_for_mendeley_folder(self, folder):
        return (
            self._citation_for_mendeley_document(document)
            for document in folder.documents.list().items
        )

    def _citations_for_mendeley_user(self):
        import pdb; pdb.set_trace()
        return (
            self._citation_for_mendeley_document(document)
            for document in self.client.documents.list().items
        )

    def _citation_for_mendeley_document(self, document):
        """Mendeley document to ``website.citations.models.Citation``
        :param BaseDocument document:
            An instance of ``mendeley.models.base_document.BaseDocument``
        :return Citation:
        """
        csl = {
            'id': document.json.get('id'),
            'type': document.json.get('type'),
        }

        if document.title:
            csl['title'] = document.title

        if document.json.get('authors'):
            csl['author'] = [
                {
                    'given': person.get('first_name'),
                    'family': person.get('last_name'),
                } for person in document.json.get('authors')
            ]

        if document.json.get('source'):
            csl['source'] = document.json.get('source')

        if document.year:
            csl['issued'] = {'date-parts': [[document.year]]}

        # gather identifiers
        idents = document.json.get('identifiers')
        if idents is not None:
            if idents.get('isbn'):
                csl['ISBN'] = idents.get('isbn')
            if idents.get('issn'):
                csl['ISSN'] = idents.get('issn')
            if idents.get('pmid'):
                csl['PMID'] = idents.get('pmid')
            if idents.get('doi'):
                csl['DOI'] = idents.get('doi')

        return csl
<commit_msg>iterative<commit_after># -*- coding: utf-8 -*-

import time

import mendeley
from modularodm import fields

from website import settings
from website.addons.base import AddonNodeSettingsBase
from website.addons.base import AddonUserSettingsBase
from website.oauth.models import ExternalProvider

from . import utils
from .api import APISession

def serialize_folder(name, account_id, parent_id=None, list_id=None):
    retval = {
        'name': name,
        'provider_account_id': account_id,
        'provider_list_id': list_id,
    }
    if parent_id:
        retval['parent_list_id'] = parent_id

    return retval

class AddonMendeleyUserSettings(AddonUserSettingsBase):

    def _get_connected_accounts(self):
        """Get user's connected Mendeley accounts"""
        return [
            x for x in self.owner.external_accounts if x.provider == 'mendeley'
        ]

    def to_json(self, user):
        ret = super(AddonMendeleyUserSettings, self).to_json(user)
        ret['accounts'] = [
            utils.serialize_account(each)
            for each in self._get_connected_accounts()
        ]
        return ret

class AddonMendeleyNodeSettings(AddonNodeSettingsBase):
    external_account = fields.ForeignField('externalaccount',
                                           backref='connected')

    mendeley_list_id = fields.StringField()

    # Keep track of all user settings that have been associated with this
    #   instance. This is so OAuth grants can be checked, even if the grant is
    #   not currently being used.
    associated_user_settings = fields.AbstractForeignField(list=True)

    _api = None

    @property
    def api(self):
        """authenticated ExternalProvider instance"""
        if self._api is None:
            self._api = Mendeley()
            self._api.account = self.external_account
        return self._api

    def grant_oauth_access(self, user, external_account, metadata=None):
        """Grant OAuth access, updates metadata on user settings
        :param User user:
        :param ExternalAccount external_account:
        :param dict metadata:
        """
        user_settings = user.get_addon('mendeley')

        # associate the user settings with this node's settings
        if user_settings not in self.associated_user_settings:
            self.associated_user_settings.append(user_settings)
            self.save()

        user_settings.grant_oauth_access(
            node=self.owner,
            external_account=external_account,
            metadata=metadata,
        )

        user_settings.save()

    def verify_oauth_access(self, external_account, list_id):
        """Determine if access to the ExternalAccount has been granted
        :param ExternalAccount external_account:
        :param str list_id: ID of the Mendeley list requested
        :rtype bool:
        """
        for user_settings in self.associated_user_settings:
            try:
                granted = user_settings.oauth_grants[self.owner._id][external_account._id]
            except KeyError:
                # no grant for this node, move along
                continue

            if list_id in granted.get('lists', []):
                return True
        return False

    def get_accounts(self, user):
        accounts = [
            account for account
            in user.external_accounts
            if account.provider == 'mendeley'
        ]
        if self.external_account and self.external_account not in accounts:
            accounts.append(self.external_account)
        return accounts

    def to_json(self, user):
        ret = super(AddonMendeleyNodeSettings, self).to_json(user)
        ret.update({
            'listId': self.mendeley_list_id,
            'accounts': [utils.serialize_account(each) for each in self.get_accounts(user)],
            'currentAccount': utils.serialize_account(self.external_account),
        })
        return ret

class Mendeley(ExternalProvider):
    name = "Mendeley"
    short_name = "mendeley"

    client_id = settings.MENDELEY_CLIENT_ID
    client_secret = settings.MENDELEY_CLIENT_SECRET

    auth_url_base = 'https://api.mendeley.com/oauth/authorize'
    callback_url = 'https://api.mendeley.com/oauth/token'
    default_scopes = ['all']

    _client = None

    def handle_callback(self, response):
        client = self._get_client(response)

        # make a second request for the Mendeley user's ID and name
        profile = client.profiles.me

        return {
            'provider_id': profile.id,
            'display_name': profile.display_name
        }

    def _get_client(self, credentials):
        if not self._client:
            partial = mendeley.Mendeley(
                client_id=self.client_id,
                client_secret=self.client_secret,
                redirect_uri='http://cos.ngrok.com/oauth/callback/mendeley/',
            )
            self._client = APISession(partial, credentials)

        return self._client

    def _get_folders(self):
        """Get a list of a user's folders"""

        client = self.client

        return client.folders.list().items

    @property
    def client(self):
        """An API session with Mendeley"""
        if not self._client:
            self._client = self._get_client({
                'access_token': self.account.oauth_key,
                'refresh_token': self.account.refresh_token,
                'expires_at': time.mktime(self.account.expires_at.timetuple()),
                'token_type': 'bearer',
            })
        return self._client

    def _folder_tree(self, folder, flat_map):

        serialized = serialize_folder(folder[0].name, self.account.provider_id)
        serialized['children'] = [self._folder_tree(flat_map[f], flat_map) for f in folder[1]]
        serialized['kind'] = 'folder'
        return serialized

    @property
    def citation_folder_tree(self):
        """Nested list structure of serialized folders"""

        folders = self._get_folders()
        flat_map = {
            folder.id: (folder, [])
            for folder in folders
        }
        flat_map['root'] = (None, [])
        for folder in folders:
            if folder.parent_id:
                flat_map[folder.parent_id][1].append(folder.id)
            else:
                flat_map['root'][1].append(folder.id)
        tree = [
            serialize_folder(
                'All Documents',
                account_id=self.account.provider_id,
            )
        ]
        tree[0]['children'] = [self._folder_tree(flat_map[f], flat_map) for f in flat_map['root'][1]]
        tree[0]['kind'] = 'folder'
        return tree

    @property
    def citation_lists(self):
        """List of CitationList objects, derived from Mendeley folders"""

        folders = self._get_folders()

        # TODO: Verify OAuth access to each folder
        all_documents = serialize_folder(
            'All Documents',
            account_id=self.account.provider_id,
        )
        serialized_folders = [
            serialize_folder(
                each.name,
                account_id=self.account.provider_id,
                list_id=each.json['id'],
                parent_id=each.json.get('parent_id'),
            )
            for each in folders
        ]
        return [all_documents] + serialized_folders

    def get_list(self, list_id=None):
        """Get a single CitationList
        :param str list_id: ID for a Mendeley folder. Optional.
        :return CitationList: CitationList for the folder, or for all documents
        """
        folder = self.client.folders.get(list_id) if list_id else None
        if folder:
            return self._citations_for_mendeley_folder(folder)
        return self._citations_for_mendeley_user()

    def _citations_for_mendeley_folder(self, folder):
        return (
            self._citation_for_mendeley_document(document)
            for document in folder.documents.list().items
        )

    def _citations_for_mendeley_user(self):
        import pdb; pdb.set_trace()
        return (
            self._citation_for_mendeley_document(document)
            for document in self.client.documents.list().items
        )

    def _citation_for_mendeley_document(self, document):
        """Mendeley document to ``website.citations.models.Citation``
        :param BaseDocument document:
            An instance of ``mendeley.models.base_document.BaseDocument``
        :return Citation:
        """
        csl = {
            'id': document.json.get('id'),
            'type': document.json.get('type'),
        }

        if document.title:
            csl['title'] = document.title

        if document.json.get('authors'):
            csl['author'] = [
                {
                    'given': person.get('first_name'),
                    'family': person.get('last_name'),
                } for person in document.json.get('authors')
            ]

        if document.json.get('source'):
            csl['source'] = document.json.get('source')

        if document.year:
            csl['issued'] = {'date-parts': [[document.year]]}

        # gather identifiers
        idents = document.json.get('identifiers')
        if idents is not None:
            if idents.get('isbn'):
                csl['ISBN'] = idents.get('isbn')
            if idents.get('issn'):
                csl['ISSN'] = idents.get('issn')
            if idents.get('pmid'):
                csl['PMID'] = idents.get('pmid')
            if idents.get('doi'):
                csl['DOI'] = idents.get('doi')

        return csl
<|endoftext|>


<commit_before>import foauth.providers

class Imgur(foauth.providers.OAuth1):
    # General info about the provider
    name = 'imgur'
    provider_url = 'http://imgur.com/'
    docs_url = 'http://api.imgur.com/'
    category = 'Pictures'

    # URLs to interact with the API
    request_token_url = 'https://api.imgur.com/oauth/request_token'
    authorize_url = 'https://api.imgur.com/oauth/authorize'
    access_token_url = 'https://api.imgur.com/oauth/access_token'
    api_domain = 'api.imgur.com'

    available_permissions = [
        (None, 'read and write to your images'),
    ]

    def get_user_id(self, key):
        r = self.api(key, self.api_domain, u'/2/account.json')
        return unicode(r.json[u'account'][u'url'])
<commit_msg>Update imgur to version 3 of their API, including OAuth 2<commit_after>import foauth.providers

class Imgur(foauth.providers.OAuth2):
    # General info about the provider
    name = 'imgur'
    provider_url = 'http://imgur.com/'
    docs_url = 'http://api.imgur.com/'
    category = 'Pictures'

    # URLs to interact with the API
    authorize_url = 'https://api.imgur.com/oauth2/authorize'
    access_token_url = 'https://api.imgur.com/oauth2/token'
    api_domain = 'api.imgur.com'

    available_permissions = [
        (None, 'read and write to your images'),
    ]

    # This is hopefully a short-term fix to a hostname mismatch on their end
    verify = False

    def get_user_id(self, key):
        r = self.api(key, self.api_domain, u'/3/account/me.json')
        return unicode(r.json[u'data'][u'id'])
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
#   -*- coding: utf-8 -*-
#
#   This file is part of PyBuilder
#
#   Copyright 2011-2015 PyBuilder Team
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import sys

sys.path.insert(0, 'src/main/python')  # This is only necessary in PyBuilder sources for bootstrap

from pybuilder import bootstrap
from pybuilder.core import Author, init, use_bldsup, use_plugin

bootstrap()

use_plugin("pypi:pybuilder_external_plugin_demo")
use_plugin("python.core")
use_plugin("python.pytddmon")
use_plugin("python.distutils")
use_plugin("python.install_dependencies")

use_plugin("copy_resources")
use_plugin("filter_resources")
use_plugin("source_distribution")

use_plugin("python.coverage")
use_plugin("python.unittest")
use_plugin("python.integrationtest")
use_plugin("python.flake8")
use_plugin("python.frosted")

if not sys.version_info[0:2] == (3, 2):
    use_plugin("python.cram")

use_plugin("python.pydev")
use_plugin("python.pycharm")
use_plugin("python.pytddmon")

use_bldsup()
use_plugin("pdoc")

summary = "An extensible, easy to use continuous build tool for Python"
description = """PyBuilder is a build automation tool for python.

PyBuilder is a software build tool written in pure Python which mainly targets Python applications.
It is based on the concept of dependency based programming but also comes along with powerful plugin mechanism that
allows the construction of build life cycles similar to those known from other famous build tools like Apache Maven.
"""

authors = [Author("Alexander Metzner", "alexander.metzner@gmail.com"),
           Author("Maximilien Riehl", "max@riehl.io"),
           Author("Michael Gruber", "aelgru@gmail.com"),
           Author("Udo Juettner", "udo.juettner@gmail.com")]
url = "http://pybuilder.github.io"
license = "Apache License"
version = "0.10.39"

default_task = ["analyze", "publish"]

@init
def initialize(project):
    project.build_depends_on("fluentmock")
    project.build_depends_on("mock")
    project.build_depends_on("mockito-without-hardcoded-distribute-version")
    project.build_depends_on("pyfix")  # required test framework
    project.build_depends_on("pyassert")
    project.build_depends_on("wheel")
    project.build_depends_on("pygments")
    if sys.version_info[0:2] == (2, 6):
        project.build_depends_on("importlib") # for fluentmock

    project.set_property("verbose", True)

    project.set_property("coverage_break_build", False)
    project.get_property("coverage_exceptions").append("pybuilder.cli")
    project.get_property("coverage_exceptions").append("pybuilder.plugins.core_plugin")

    project.set_property('flake8_break_build', True)
    project.set_property('flake8_include_test_sources', True)
    project.set_property('flake8_include_scripts', True)
    project.set_property('flake8_max_line_length', 130)

    project.set_property('frosted_include_test_sources', True)
    project.set_property('frosted_include_scripts', True)

    project.set_property("copy_resources_target", "$dir_dist")
    project.get_property("copy_resources_glob").append("LICENSE")
    project.get_property("filter_resources_glob").append("**/pybuilder/__init__.py")

    project.get_property("source_dist_ignore_patterns").append(".project")
    project.get_property("source_dist_ignore_patterns").append(".pydevproject")
    project.get_property("source_dist_ignore_patterns").append(".settings")

    # enable this to build a bdist on vagrant
    # project.set_property("distutils_issue8876_workaround_enabled", True)
    project.get_property("distutils_commands").append("bdist_wheel")
    project.set_property("distutils_console_scripts", ["pyb_ = pybuilder.cli:main"])
    project.set_property("distutils_classifiers", [
                         'Programming Language :: Python',
                         'Programming Language :: Python :: Implementation :: CPython',
                         'Programming Language :: Python :: Implementation :: PyPy',
                         'Programming Language :: Python :: 2.6',
                         'Programming Language :: Python :: 2.7',
                         'Programming Language :: Python :: 3',
                         'Programming Language :: Python :: 3.2',
                         'Programming Language :: Python :: 3.3',
                         'Programming Language :: Python :: 3.4',
                         'Development Status :: 4 - Beta',
                         'Environment :: Console',
                         'Intended Audience :: Developers',
                         'License :: OSI Approved :: Apache Software License',
                         'Topic :: Software Development :: Build Tools',
                         'Topic :: Software Development :: Quality Assurance',
                         'Topic :: Software Development :: Testing'])
<commit_msg>v0.10.40<commit_after>#!/usr/bin/env python
#
#   -*- coding: utf-8 -*-
#
#   This file is part of PyBuilder
#
#   Copyright 2011-2015 PyBuilder Team
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import sys

sys.path.insert(0, 'src/main/python')  # This is only necessary in PyBuilder sources for bootstrap

from pybuilder import bootstrap
from pybuilder.core import Author, init, use_bldsup, use_plugin

bootstrap()

use_plugin("pypi:pybuilder_external_plugin_demo")
use_plugin("python.core")
use_plugin("python.pytddmon")
use_plugin("python.distutils")
use_plugin("python.install_dependencies")

use_plugin("copy_resources")
use_plugin("filter_resources")
use_plugin("source_distribution")

use_plugin("python.coverage")
use_plugin("python.unittest")
use_plugin("python.integrationtest")
use_plugin("python.flake8")
use_plugin("python.frosted")

if not sys.version_info[0:2] == (3, 2):
    use_plugin("python.cram")

use_plugin("python.pydev")
use_plugin("python.pycharm")
use_plugin("python.pytddmon")

use_bldsup()
use_plugin("pdoc")

summary = "An extensible, easy to use continuous build tool for Python"
description = """PyBuilder is a build automation tool for python.

PyBuilder is a software build tool written in pure Python which mainly targets Python applications.
It is based on the concept of dependency based programming but also comes along with powerful plugin mechanism that
allows the construction of build life cycles similar to those known from other famous build tools like Apache Maven.
"""

authors = [Author("Alexander Metzner", "alexander.metzner@gmail.com"),
           Author("Maximilien Riehl", "max@riehl.io"),
           Author("Michael Gruber", "aelgru@gmail.com"),
           Author("Udo Juettner", "udo.juettner@gmail.com")]
url = "http://pybuilder.github.io"
license = "Apache License"
version = "0.10.40"

default_task = ["analyze", "publish"]

@init
def initialize(project):
    project.build_depends_on("fluentmock")
    project.build_depends_on("mock")
    project.build_depends_on("mockito-without-hardcoded-distribute-version")
    project.build_depends_on("pyfix")  # required test framework
    project.build_depends_on("pyassert")
    project.build_depends_on("wheel")
    project.build_depends_on("pygments")
    if sys.version_info[0:2] == (2, 6):
        project.build_depends_on("importlib") # for fluentmock

    project.set_property("verbose", True)

    project.set_property("coverage_break_build", False)
    project.get_property("coverage_exceptions").append("pybuilder.cli")
    project.get_property("coverage_exceptions").append("pybuilder.plugins.core_plugin")

    project.set_property('flake8_break_build', True)
    project.set_property('flake8_include_test_sources', True)
    project.set_property('flake8_include_scripts', True)
    project.set_property('flake8_max_line_length', 130)

    project.set_property('frosted_include_test_sources', True)
    project.set_property('frosted_include_scripts', True)

    project.set_property("copy_resources_target", "$dir_dist")
    project.get_property("copy_resources_glob").append("LICENSE")
    project.get_property("filter_resources_glob").append("**/pybuilder/__init__.py")

    project.get_property("source_dist_ignore_patterns").append(".project")
    project.get_property("source_dist_ignore_patterns").append(".pydevproject")
    project.get_property("source_dist_ignore_patterns").append(".settings")

    # enable this to build a bdist on vagrant
    # project.set_property("distutils_issue8876_workaround_enabled", True)
    project.get_property("distutils_commands").append("bdist_wheel")
    project.set_property("distutils_console_scripts", ["pyb_ = pybuilder.cli:main"])
    project.set_property("distutils_classifiers", [
                         'Programming Language :: Python',
                         'Programming Language :: Python :: Implementation :: CPython',
                         'Programming Language :: Python :: Implementation :: PyPy',
                         'Programming Language :: Python :: 2.6',
                         'Programming Language :: Python :: 2.7',
                         'Programming Language :: Python :: 3',
                         'Programming Language :: Python :: 3.2',
                         'Programming Language :: Python :: 3.3',
                         'Programming Language :: Python :: 3.4',
                         'Development Status :: 4 - Beta',
                         'Environment :: Console',
                         'Intended Audience :: Developers',
                         'License :: OSI Approved :: Apache Software License',
                         'Topic :: Software Development :: Build Tools',
                         'Topic :: Software Development :: Quality Assurance',
                         'Topic :: Software Development :: Testing'])
<|endoftext|>


<commit_before>#!/usr/bin/python

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

###############################################################################
# This build script is responsible for building and running all modules in
# this project. The following commands are supported:
#
#   clean : Remove all files in the output directory. This will not remove the
#           the output directory itself to allow the output directory to be
#           a symlink.
#
#   build : Build the full project. This will build all java files found in any
#           of the src directories.
#
#   rebuild : Call the clean and the build commands. For more information on
#             CLEAN and BUILD see the above entries.
#
#   run <class path> [ arguments ... ] : Run the specified class. All arguments
#                                        after the class path will be passed to
#                                        the java class when it runs.
###############################################################################

from __future__ import print_function

import os
import shutil
import subprocess
import sys

# Dictionary of settings that control java source compilation
CONFIG = {
  'out': 'bin',
  'src': [ 'src', 'test' ],
  'libraries': [
    'third_party/junit4-4.11.jar',
    'third_party/hamcrest-core-1.3.jar',
    'third_party/gson-2.8.1.jar'
  ],
  'separators': {
    'nt' : ';',
    'posix' : ':'
  }
}

# CLEAN
#
# Remove all files from the build output directory.
#
def clean(config):
  out = config['out']

  for entry in [ os.path.join(out, name) for name in os.listdir(out) ]:
    if os.path.isdir(entry):
      shutil.rmtree(entry)
    else :
      os.remove(entry)

  print('Clean PASSED')
  return 0

# BUILD
#
# Build the project defined by the config object. This will find all source
# files in the source directories, link all specified libraries, and write
# all output to the out directory.
#
def build(config):
  libraries = config['libraries']
  out = config['out']
  separator = config['separators'][os.name]
  src = config['src']

  # Find all the java source files in the given source directories.
  # Non-java source files are ignored.
  src_files = [ ]
  for src_path in src:
    for root, dirs, files in os.walk(src_path):
      src_files += [ os.path.join(root, file) for file in files if file.endswith('.java') ]

  # Take everything so far and construct a single command to build the project.
  command = [ ]
  command += [ 'javac' ]
  command += [ '-d', out ]
  command += [ '-cp', separator.join([ out ] + libraries) ]
  command += [ '-Xlint' ]
  command += src_files

  print('Running: {}'.format(command))
  exit_code = subprocess.call(command)
  print('Build {}'.format('PASSED' if exitCode == 0 else 'FAILED'))
  return exit_code

# RUN
#
# Run a class from within the project.
#
def run(config, start_class_path, arguments):
  libraries = config['libraries']
  out = config['out']
  separator = config['separators'][os.name]

  command = [ ]
  command += [ 'java' ]
  command += [ '-cp', separator.join([ out ] + libraries) ]
  command += [ start_class_path ]
  command += arguments

  print('Running: [', end=' ')
  for x in command:
    print(x, end=' ')
  print(']')
  exit_code = subprocess.call(command)
  print('Run {}'.format('PASSED' if exit_code == 0 else 'FAILED'))
  return exit_code

# USAGE
#
# Print basic usage info.
#
def usage():
  print('Usage: python build.py clean | build | rebuild | run | help')
  print('  clean   : Remove all files in the output directory.')
  print('            This does not remove the root of the output tree.')
  print('  build   : Build the full project. This will build all java files')
  print('            found in all of the src directories.')
  print('  rebuild : perform clean followed by build.')
  print('  run <class path> [ arguments ... ] : Run the specified class.')
  print('            All arguments after the class path will be passed to')
  print('            the java class when it runs.')
  print('  help    : Print this helpful message.')

# MAIN
def main(args):
  exit_code = 0
  if len(args) > 1:
    command = args[1]
    if 'help' == command:
      usage()
    elif 'clean' == command:
      exit_code += clean(CONFIG)
    elif 'build' == command:
      exit_code += build(CONFIG)
    elif 'rebuild' == command:
      exit_code += clean(CONFIG)
      exit_code += build(CONFIG)
    elif 'run' == command:
      if len(args) > 2:
        java_class = args[2]
        java_params = args[3:]
        exit_code += run(CONFIG, java_class, java_params)
      else:
        print('Run command requires a java class to run.')
        usage()
        exit_code = 1
    else:
      print('Unknown command: [', end=' ')
      for x in args:
        print(x, end=' ')
      print(']')
      usage()
      exit_code = 1
  else:
    print('No parameters provided.')
    usage()
    exit_code = 1
  return exit_code

if __name__ == '__main__':
  exit_code = main(sys.argv)
  sys.exit(exit_code)
<commit_msg>Small underscore typo on build script<commit_after>#!/usr/bin/python

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

###############################################################################
# This build script is responsible for building and running all modules in
# this project. The following commands are supported:
#
#   clean : Remove all files in the output directory. This will not remove the
#           the output directory itself to allow the output directory to be
#           a symlink.
#
#   build : Build the full project. This will build all java files found in any
#           of the src directories.
#
#   rebuild : Call the clean and the build commands. For more information on
#             CLEAN and BUILD see the above entries.
#
#   run <class path> [ arguments ... ] : Run the specified class. All arguments
#                                        after the class path will be passed to
#                                        the java class when it runs.
###############################################################################

from __future__ import print_function

import os
import shutil
import subprocess
import sys

# Dictionary of settings that control java source compilation
CONFIG = {
  'out': 'bin',
  'src': [ 'src', 'test' ],
  'libraries': [
    'third_party/junit4-4.11.jar',
    'third_party/hamcrest-core-1.3.jar',
    'third_party/gson-2.8.1.jar'
  ],
  'separators': {
    'nt' : ';',
    'posix' : ':'
  }
}

# CLEAN
#
# Remove all files from the build output directory.
#
def clean(config):
  out = config['out']

  for entry in [ os.path.join(out, name) for name in os.listdir(out) ]:
    if os.path.isdir(entry):
      shutil.rmtree(entry)
    else :
      os.remove(entry)

  print('Clean PASSED')
  return 0

# BUILD
#
# Build the project defined by the config object. This will find all source
# files in the source directories, link all specified libraries, and write
# all output to the out directory.
#
def build(config):
  libraries = config['libraries']
  out = config['out']
  separator = config['separators'][os.name]
  src = config['src']

  # Find all the java source files in the given source directories.
  # Non-java source files are ignored.
  src_files = [ ]
  for src_path in src:
    for root, dirs, files in os.walk(src_path):
      src_files += [ os.path.join(root, file) for file in files if file.endswith('.java') ]

  # Take everything so far and construct a single command to build the project.
  command = [ ]
  command += [ 'javac' ]
  command += [ '-d', out ]
  command += [ '-cp', separator.join([ out ] + libraries) ]
  command += [ '-Xlint' ]
  command += src_files

  print('Running: {}'.format(command))
  exit_code = subprocess.call(command)
  print('Build {}'.format('PASSED' if exit_code == 0 else 'FAILED'))
  return exit_code

# RUN
#
# Run a class from within the project.
#
def run(config, start_class_path, arguments):
  libraries = config['libraries']
  out = config['out']
  separator = config['separators'][os.name]

  command = [ ]
  command += [ 'java' ]
  command += [ '-cp', separator.join([ out ] + libraries) ]
  command += [ start_class_path ]
  command += arguments

  print('Running: [', end=' ')
  for x in command:
    print(x, end=' ')
  print(']')
  exit_code = subprocess.call(command)
  print('Run {}'.format('PASSED' if exit_code == 0 else 'FAILED'))
  return exit_code

# USAGE
#
# Print basic usage info.
#
def usage():
  print('Usage: python build.py clean | build | rebuild | run | help')
  print('  clean   : Remove all files in the output directory.')
  print('            This does not remove the root of the output tree.')
  print('  build   : Build the full project. This will build all java files')
  print('            found in all of the src directories.')
  print('  rebuild : perform clean followed by build.')
  print('  run <class path> [ arguments ... ] : Run the specified class.')
  print('            All arguments after the class path will be passed to')
  print('            the java class when it runs.')
  print('  help    : Print this helpful message.')

# MAIN
def main(args):
  exit_code = 0
  if len(args) > 1:
    command = args[1]
    if 'help' == command:
      usage()
    elif 'clean' == command:
      exit_code += clean(CONFIG)
    elif 'build' == command:
      exit_code += build(CONFIG)
    elif 'rebuild' == command:
      exit_code += clean(CONFIG)
      exit_code += build(CONFIG)
    elif 'run' == command:
      if len(args) > 2:
        java_class = args[2]
        java_params = args[3:]
        exit_code += run(CONFIG, java_class, java_params)
      else:
        print('Run command requires a java class to run.')
        usage()
        exit_code = 1
    else:
      print('Unknown command: [', end=' ')
      for x in args:
        print(x, end=' ')
      print(']')
      usage()
      exit_code = 1
  else:
    print('No parameters provided.')
    usage()
    exit_code = 1
  return exit_code

if __name__ == '__main__':
  exit_code = main(sys.argv)
  sys.exit(exit_code)
<|endoftext|>


<commit_before>from twisted.internet import reactor
from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

class Instantiate(Function):
    Help = "timer <time> - starts a countdown timer and notifies you when time's up."

    def GetResponse(self, HubbeBot, message):
        if message.Type != "PRIVMSG":
            return
        if message.Command == "timer":
            if len(message.ParameterList) != 1:
                return IRCResponse(ResponseType.Say, "Please use only 1 argument.", message.ReplyTo)
            try:
                delay = float(message.ParameterList[0])
                if delay > (60*60*24*365):
                    return IRCResponse(ResponseType.Say, "Do you really need a timer that long?", message.ReplyTo)
            except:
                return IRCResponse(ResponseType.Say, "That doesn't look like a number to me...", message.ReplyTo)
            reactor.callLater(delay, self.notifyUser, HubbeBot, delay, message)
            return IRCResponse(ResponseType.Say, message.User.Name + ": A timer has been started!", message.ReplyTo)

    def notifyUser(self, HubbeBot, message):
        HubbeBot.sendResponse(IRCResponse(ResponseType.Say, message.User.Name + ": Your " + message.ParameterList[0] + " second timer is up!" , message.ReplyTo))
<commit_msg>And yet again I'm a bit dense<commit_after>from twisted.internet import reactor
from IRCMessage import IRCMessage
from IRCResponse import IRCResponse, ResponseType
from Function import Function
import GlobalVars

class Instantiate(Function):
    Help = "timer <time> - starts a countdown timer and notifies you when time's up."

    def GetResponse(self, HubbeBot, message):
        if message.Type != "PRIVMSG":
            return
        if message.Command == "timer":
            if len(message.ParameterList) != 1:
                return IRCResponse(ResponseType.Say, "Please use only 1 argument.", message.ReplyTo)
            try:
                delay = float(message.ParameterList[0])
                if delay > (60*60*24*365):
                    return IRCResponse(ResponseType.Say, "Do you really need a timer that long?", message.ReplyTo)
            except:
                return IRCResponse(ResponseType.Say, "That doesn't look like a number to me...", message.ReplyTo)
            reactor.callLater(delay, self.notifyUser, HubbeBot, message)
            return IRCResponse(ResponseType.Say, message.User.Name + ": A timer has been started!", message.ReplyTo)

    def notifyUser(self, HubbeBot, message):
        HubbeBot.sendResponse(IRCResponse(ResponseType.Say, message.User.Name + ": Your " + message.ParameterList[0] + " second timer is up!" , message.ReplyTo))
<|endoftext|>


<commit_before>from theano import function
# The prediction function for pylearn2.models.*
def predict(model, x, U = 1000):
    a = model.get_input_space().make_theano_batch()
    b = model.fprop(a)
    f = function([a], b)
    n = x.shape[0]
    yhat = []
    for i in range(n // U + 1):
        yhat.append(f(x[(i * U):((i + 1) * U),:]))
    return np.vstack(yhat)

<commit_msg>import numpy as np<commit_after>from theano import function
import numpy as np
# The prediction function for pylearn2.models.*
def predict(model, x, U = 1000):
    a = model.get_input_space().make_theano_batch()
    b = model.fprop(a)
    f = function([a], b)
    n = x.shape[0]
    yhat = []
    for i in range(n // U + 1):
        yhat.append(f(x[(i * U):((i + 1) * U),:]))
    return np.vstack(yhat)

<|endoftext|>


<commit_before>import argparse
import subprocess
import json
import sys
import shutil
import os

def pythonCall(command, cwd=None):
	subprocess.call(command, shell=True)

def pythonRelCall(relCommand, command, cwd=None):
	pythonPath=os.path.dirname(sys.executable)
	fullPath=os.path.join(pythonPath, relCommand)
	pythonCall(fullPath + " " + command, cwd=cwd)

def main():
	parser=argparse.ArgumentParser(description="Rasterizer Builder", epilog="(C) Copyright 2013 by RSJ Software GmbH Germering. All rights resreved.")
	parms=parser.parse_args()

	with open("version.json", "r") as f:
		version=json.load(f)

	versionPart=version.split(".")

	version="%d.%02d.%04d" %(int(versionPart[0]), int(versionPart[1]), int(versionPart[2])+1)

	print "Building", version

	with open("version.json", "w") as f:
		json.dump(version, f)

	#Empty Distribution
	shutil.rmtree("dist", True)
	shutil.rmtree("build", True)
	shutil.rmtree("Output", True)

	# Py2Exe
	pythonCall("setup.py")

	# Inno Setup
	subprocess.call(r'"c:\Program Files (x86)\Inno Setup 5\iscc.exe" -dversion=' + version + ' rasterizer.iss')

if __name__=="__main__":
	main()<commit_msg>Update build.py<commit_after>import argparse
import subprocess
import json
import sys
import shutil
import os

def pythonCall(command, cwd=None):
	subprocess.call(command, shell=True)

def pythonRelCall(relCommand, command, cwd=None):
	pythonPath=os.path.dirname(sys.executable)
	fullPath=os.path.join(pythonPath, relCommand)
	pythonCall(fullPath + " " + command, cwd=cwd)

def main():
	parser=argparse.ArgumentParser(description="Rasterizer Builder", epilog="(C) Copyright 2013 by RSJ Software GmbH Germering. All rights reserved.")
	parms=parser.parse_args()

	with open("version.json", "r") as f:
		version=json.load(f)

	versionPart=version.split(".")

	version="%d.%02d.%04d" %(int(versionPart[0]), int(versionPart[1]), int(versionPart[2])+1)

	print "Building", version

	with open("version.json", "w") as f:
		json.dump(version, f)

	#Empty Distribution
	shutil.rmtree("dist", True)
	shutil.rmtree("build", True)
	shutil.rmtree("Output", True)

	# Py2Exe
	pythonCall("setup.py")

	# Inno Setup
	subprocess.call(r'"c:\Program Files (x86)\Inno Setup 5\iscc.exe" -dversion=' + version + ' rasterizer.iss')

if __name__=="__main__":
	main()
<|endoftext|>


<commit_before>from __future__ import with_statement
from fabric.api import local, settings, run, cd, env, prefix
from fabric.contrib.console import confirm

env.directory = '/home/URQA-Server'
env.activate = 'source ~/.virtualenvs/urqa/bin/activate'

def test():
    local("./manage.py test")

def deploy():
    with prefix(env.activate):
        with cd(env.directory):
            run('git checkout develop')
            run('git pull')
            install()

def setting():
    pip()
    git()
    code_dir = '~/.virtualenvs/urqa'
    with settings(warn_only=True):
        if run('test -d %s' % code_dir).failed:
            run('pip install virtualenv virtualenvwrapper')
            run('echo "source /usr/local/bin/virtualenvwrapper.sh">>~/.bashrc')
            run('export WORKON_HOME=$HOME/.virtualenvs')
            run('source ~/.bashrc')
            run('source /usr/local/bin/virtualenvwrapper.sh' +
                '&& mkvirtualenv urqa')
            run('source /usr/local/bin/virtualenvwrapper.sh && workon urqa')
        run('apt-get install -y python-dev libmysqlclient-dev')

        if run('test -d %s' % env.directory).failed:
                run('''git clone \
                    https://github.com/bongster/URQA-Server.git \
                    %s''' % env.directory)

def git():
    with settings(warn_only=True):
        result = run('git --version')
        if (result.failed and
           confirm('git are not exists, did you install git?')):
            run('apt-get install git')

def pip():
    with settings(warn_only=True):
        result = run('pip --version')
        if (result.failed and
           confirm('pip are not exists, did you install pip?')):
            run('apt-get install python-pip')

def install():
    with prefix(env.activate):
        with cd(env.directory):
            run('pip install -r requirements.txt')
<commit_msg>[#171] dbconfig file setting<commit_after>from __future__ import with_statement
from fabric.api import local, settings, run, cd, env, prefix
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from fabric.operations import prompt, put

env.directory = '/home/URQA-Server'
env.activate = 'source ~/.virtualenvs/urqa/bin/activate'

def config():
    env.config_file = prompt(
        'where is global config file: ',
        default='~/Documents/config.cfg')
    env.google_file = prompt(
        'where is google config file: ',
        default='~/Documents/google_client_secrets.json')
    env.db_config_file = prompt(
        'where is db config file: ',
        default='~/Documents/dbconfig.cfg')

    with prefix(env.activate):
        with cd(env.directory):
            if not exists('/data/etc/urqa.io/'):
                run('mkdir -p /data/etc/urqa.io')
            put(env.config_file, '/data/etc/urqa.io/config.cfg')
            put(env.google_file, '/data/etc/urqa.io/google_client_secrets.json')
            put(env.dbconfig, '/data/etc/urqa.io/dbconfig.cfg')

def test():
    local("./manage.py test")

def deploy():
    with prefix(env.activate):
        with cd(env.directory):
            run('git checkout develop')
            run('git pull')
            install()

def setting():
    pip()
    git()
    config()
    code_dir = '~/.virtualenvs/urqa'
    with settings(warn_only=True):
        if run('test -d %s' % code_dir).failed:
            run('pip install virtualenv virtualenvwrapper')
            run('echo "source /usr/local/bin/virtualenvwrapper.sh">>~/.bashrc')
            run('export WORKON_HOME=$HOME/.virtualenvs')
            run('source ~/.bashrc')
            run('source /usr/local/bin/virtualenvwrapper.sh' +
                '&& mkvirtualenv urqa')
            run('source /usr/local/bin/virtualenvwrapper.sh && workon urqa')
        run('apt-get install -y python-dev libmysqlclient-dev')

        if run('test -d %s' % env.directory).failed:
                run('''git clone \
                    https://github.com/bongster/URQA-Server.git \
                    %s''' % env.directory)

def git():
    with settings(warn_only=True):
        result = run('git --version')
        if (result.failed and
           confirm('git are not exists, did you install git?')):
            run('apt-get install git')

def pip():
    with settings(warn_only=True):
        result = run('pip --version')
        if (result.failed and
           confirm('pip are not exists, did you install pip?')):
            run('apt-get install python-pip')

def install():
    with prefix(env.activate):
        with cd(env.directory):
            run('pip install -r requirements.txt')
<|endoftext|>


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

from collections import Sequence  # noqa
import functools

from django.conf import settings

from horizon import exceptions

import semantic_version
import six

__all__ = ('APIResourceWrapper', 'APIDictWrapper',
           'get_service_from_catalog', 'url_for',)

@functools.total_ordering
class Version(object):
    def __init__(self, version):
        self.version = semantic_version.Version(str(version), partial=True)

    def __eq__(self, other):
        return self.version == Version(other).version

    def __lt__(self, other):
        return self.version < Version(other).version

    def __repr__(self):
        return "Version('%s')" % self.version

    def __str__(self):
        return str(self.version)

    def __hash__(self):
        return hash(self.version)

class APIVersionManager(object):
    """Object to store and manage API versioning data and utility methods."""

    SETTINGS_KEY = "OPENSTACK_API_VERSIONS"

    def __init__(self, service_type, preferred_version=None):
        self.service_type = service_type
        self.preferred = preferred_version
        self._active = None
        self.supported = {}
        # As a convenience, we can drop in a placeholder for APIs that we
        # have not yet needed to version. This is useful, for example, when
        # panels such as the admin metadata_defs wants to check the active
        # version even though it's not explicitly defined. Previously
        # this caused a KeyError.
        if self.preferred:
            self.supported[self.preferred] = {"version": self.preferred}

    @property
    def active(self):
        if self._active is None:
            self.get_active_version()
        return self._active

    def load_supported_version(self, version, data):
        version = Version(version)
        self.supported[version] = data

    def get_active_version(self):
        if self._active is not None:
            return self.supported[self._active]
        key = getattr(settings, self.SETTINGS_KEY, {}).get(self.service_type)
        if key is None:
            # TODO(gabriel): support API version discovery here; we'll leave
            # the setting in as a way of overriding the latest available
            # version.
            key = self.preferred
        version = Version(key)
        # Provide a helpful error message if the specified version isn't in the
        # supported list.
        if version not in self.supported:
            choices = ", ".join(str(k) for k in six.iterkeys(self.supported))
            msg = ('%s is not a supported API version for the %s service, '
                   ' choices are: %s' % (version, self.service_type, choices))
            raise exceptions.ConfigurationError(msg)
        self._active = version
        return self.supported[self._active]

    def clear_active_cache(self):
        self._active = None

class APIResourceWrapper(object):
    """Simple wrapper for api objects.

    Define _attrs on the child class and pass in the
    api object as the only argument to the constructor
    """
    _attrs = []
    _apiresource = None  # Make sure _apiresource is there even in __init__.

    def __init__(self, apiresource):
        self._apiresource = apiresource

    def __getattribute__(self, attr):
        try:
            return object.__getattribute__(self, attr)
        except AttributeError:
            if attr not in self._attrs:
                raise
            # __getattr__ won't find properties
            return getattr(self._apiresource, attr)

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__,
                             dict((attr, getattr(self, attr))
                                  for attr in self._attrs
                                  if hasattr(self, attr)))

    def to_dict(self):
        obj = {}
        for key in self._attrs:
            obj[key] = getattr(self._apiresource, key, None)
        return obj

class APIDictWrapper(object):
    """Simple wrapper for api dictionaries

    Some api calls return dictionaries.  This class provides identical
    behavior as APIResourceWrapper, except that it will also behave as a
    dictionary, in addition to attribute accesses.

    Attribute access is the preferred method of access, to be
    consistent with api resource objects from novaclient.
    """

    _apidict = {}  # Make sure _apidict is there even in __init__.

    def __init__(self, apidict):
        self._apidict = apidict

    def __getattribute__(self, attr):
        try:
            return object.__getattribute__(self, attr)
        except AttributeError:
            if attr not in self._apidict:
                raise
            return self._apidict[attr]

    def __getitem__(self, item):
        try:
            return getattr(self, item)
        except (AttributeError, TypeError) as e:
            # caller is expecting a KeyError
            raise KeyError(e)

    def __contains__(self, item):
        try:
            return hasattr(self, item)
        except TypeError:
            return False

    def get(self, item, default=None):
        try:
            return getattr(self, item)
        except (AttributeError, TypeError):
            return default

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self._apidict)

    def to_dict(self):
        return self._apidict

class Quota(object):
    """Wrapper for individual limits in a quota."""
    def __init__(self, name, limit):
        self.name = name
        self.limit = limit

    def __repr__(self):
        return "<Quota: (%s, %s)>" % (self.name, self.limit)

class QuotaSet(Sequence):
    """Wrapper for client QuotaSet objects which turns the individual quotas
    into Quota objects for easier handling/iteration.

    `QuotaSet` objects support a mix of `list` and `dict` methods; you can use
    the bracket notation (`qs["my_quota"] = 0`) to add new quota values, and
    use the `get` method to retrieve a specific quota, but otherwise it
    behaves much like a list or tuple, particularly in supporting iteration.
    """
    def __init__(self, apiresource=None):
        self.items = []
        if apiresource:
            if hasattr(apiresource, '_info'):
                items = apiresource._info.items()
            else:
                items = apiresource.items()

            for k, v in items:
                if k == 'id':
                    continue
                self[k] = v

    def __setitem__(self, k, v):
        v = int(v) if v is not None else v
        q = Quota(k, v)
        self.items.append(q)

    def __getitem__(self, index):
        return self.items[index]

    def __add__(self, other):
        """Merge another QuotaSet into this one. Existing quotas are
        not overridden.
        """
        if not isinstance(other, QuotaSet):
            msg = "Can only add QuotaSet to QuotaSet, " \
                  "but received %s instead" % type(other)
            raise ValueError(msg)

        for item in other:
            if self.get(item.name).limit is None:
                self.items.append(item)
        return self

    def __len__(self):
        return len(self.items)

    def __repr__(self):
        return repr(self.items)

    def get(self, key, default=None):
        match = [quota for quota in self.items if quota.name == key]
        return match.pop() if len(match) else Quota(key, default)

    def add(self, other):
        return self.__add__(other)

def get_service_from_catalog(catalog, service_type):
    if catalog:
        for service in catalog:
            if 'type' not in service:
                continue
            if service['type'] == service_type:
                return service
    return None

def get_version_from_service(service):
    if service and service.get('endpoints'):
        endpoint = service['endpoints'][0]
        if 'interface' in endpoint:
            return 3
        else:
            return 2.0
    return 2.0

# Mapping of V2 Catalog Endpoint_type to V3 Catalog Interfaces
ENDPOINT_TYPE_TO_INTERFACE = {
    'publicURL': 'public',
    'internalURL': 'internal',
    'adminURL': 'admin',
}

def get_url_for_service(service, region, endpoint_type):
    if 'type' not in service:
        return None

    identity_version = get_version_from_service(service)
    service_endpoints = service.get('endpoints', [])
    available_endpoints = [endpoint for endpoint in service_endpoints
                           if region == _get_endpoint_region(endpoint)]
    """if we are dealing with the identity service and there is no endpoint
    in the current region, it is okay to use the first endpoint for any
    identity service endpoints and we can assume that it is global
    """
    if service['type'] == 'identity' and not available_endpoints:
        available_endpoints = [endpoint for endpoint in service_endpoints]

    for endpoint in available_endpoints:
        try:
            if identity_version < 3:
                return endpoint.get(endpoint_type)
            else:
                interface = \
                    ENDPOINT_TYPE_TO_INTERFACE.get(endpoint_type, '')
                if endpoint.get('interface') == interface:
                    return endpoint.get('url')
        except (IndexError, KeyError):
            """it could be that the current endpoint just doesn't match the
            type, continue trying the next one
            """
            pass
    return None

def url_for(request, service_type, endpoint_type=None, region=None):
    endpoint_type = endpoint_type or getattr(settings,
                                             'OPENSTACK_ENDPOINT_TYPE',
                                             'publicURL')
    fallback_endpoint_type = getattr(settings, 'SECONDARY_ENDPOINT_TYPE', None)

    catalog = request.user.service_catalog
    service = get_service_from_catalog(catalog, service_type)
    if service:
        if not region:
            region = request.user.services_region
        url = get_url_for_service(service,
                                  region,
                                  endpoint_type)
        if not url and fallback_endpoint_type:
            url = get_url_for_service(service,
                                      region,
                                      fallback_endpoint_type)
        if url:
            return url
    raise exceptions.ServiceCatalogException(service_type)

def is_service_enabled(request, service_type):
    service = get_service_from_catalog(request.user.service_catalog,
                                       service_type)
    if service:
        region = request.user.services_region
        for endpoint in service.get('endpoints', []):
            if 'type' not in service:
                continue
            # ignore region for identity
            if service['type'] == 'identity' or \
               _get_endpoint_region(endpoint) == region:
                return True
    return False

def _get_endpoint_region(endpoint):
    """Common function for getting the region from endpoint.

    In Keystone V3, region has been deprecated in favor of
    region_id.

    This method provides a way to get region that works for
    both Keystone V2 and V3.
    """
    return endpoint.get('region_id') or endpoint.get('region')
<commit_msg>Consistently hash Version objects<commit_after># Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from collections import Sequence  # noqa
import functools

from django.conf import settings

from horizon import exceptions

import semantic_version
import six

__all__ = ('APIResourceWrapper', 'APIDictWrapper',
           'get_service_from_catalog', 'url_for',)

@functools.total_ordering
class Version(object):
    def __init__(self, version):
        self.version = semantic_version.Version(str(version), partial=True)

    def __eq__(self, other):
        return self.version == Version(other).version

    def __lt__(self, other):
        return self.version < Version(other).version

    def __repr__(self):
        return "Version('%s')" % self.version

    def __str__(self):
        return str(self.version)

    def __hash__(self):
        return hash(str(self.version))

class APIVersionManager(object):
    """Object to store and manage API versioning data and utility methods."""

    SETTINGS_KEY = "OPENSTACK_API_VERSIONS"

    def __init__(self, service_type, preferred_version=None):
        self.service_type = service_type
        self.preferred = preferred_version
        self._active = None
        self.supported = {}
        # As a convenience, we can drop in a placeholder for APIs that we
        # have not yet needed to version. This is useful, for example, when
        # panels such as the admin metadata_defs wants to check the active
        # version even though it's not explicitly defined. Previously
        # this caused a KeyError.
        if self.preferred:
            self.supported[self.preferred] = {"version": self.preferred}

    @property
    def active(self):
        if self._active is None:
            self.get_active_version()
        return self._active

    def load_supported_version(self, version, data):
        version = Version(version)
        self.supported[version] = data

    def get_active_version(self):
        if self._active is not None:
            return self.supported[self._active]
        key = getattr(settings, self.SETTINGS_KEY, {}).get(self.service_type)
        if key is None:
            # TODO(gabriel): support API version discovery here; we'll leave
            # the setting in as a way of overriding the latest available
            # version.
            key = self.preferred
        version = Version(key)
        # Provide a helpful error message if the specified version isn't in the
        # supported list.
        if version not in self.supported:
            choices = ", ".join(str(k) for k in six.iterkeys(self.supported))
            msg = ('%s is not a supported API version for the %s service, '
                   ' choices are: %s' % (version, self.service_type, choices))
            raise exceptions.ConfigurationError(msg)
        self._active = version
        return self.supported[self._active]

    def clear_active_cache(self):
        self._active = None

class APIResourceWrapper(object):
    """Simple wrapper for api objects.

    Define _attrs on the child class and pass in the
    api object as the only argument to the constructor
    """
    _attrs = []
    _apiresource = None  # Make sure _apiresource is there even in __init__.

    def __init__(self, apiresource):
        self._apiresource = apiresource

    def __getattribute__(self, attr):
        try:
            return object.__getattribute__(self, attr)
        except AttributeError:
            if attr not in self._attrs:
                raise
            # __getattr__ won't find properties
            return getattr(self._apiresource, attr)

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__,
                             dict((attr, getattr(self, attr))
                                  for attr in self._attrs
                                  if hasattr(self, attr)))

    def to_dict(self):
        obj = {}
        for key in self._attrs:
            obj[key] = getattr(self._apiresource, key, None)
        return obj

class APIDictWrapper(object):
    """Simple wrapper for api dictionaries

    Some api calls return dictionaries.  This class provides identical
    behavior as APIResourceWrapper, except that it will also behave as a
    dictionary, in addition to attribute accesses.

    Attribute access is the preferred method of access, to be
    consistent with api resource objects from novaclient.
    """

    _apidict = {}  # Make sure _apidict is there even in __init__.

    def __init__(self, apidict):
        self._apidict = apidict

    def __getattribute__(self, attr):
        try:
            return object.__getattribute__(self, attr)
        except AttributeError:
            if attr not in self._apidict:
                raise
            return self._apidict[attr]

    def __getitem__(self, item):
        try:
            return getattr(self, item)
        except (AttributeError, TypeError) as e:
            # caller is expecting a KeyError
            raise KeyError(e)

    def __contains__(self, item):
        try:
            return hasattr(self, item)
        except TypeError:
            return False

    def get(self, item, default=None):
        try:
            return getattr(self, item)
        except (AttributeError, TypeError):
            return default

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self._apidict)

    def to_dict(self):
        return self._apidict

class Quota(object):
    """Wrapper for individual limits in a quota."""
    def __init__(self, name, limit):
        self.name = name
        self.limit = limit

    def __repr__(self):
        return "<Quota: (%s, %s)>" % (self.name, self.limit)

class QuotaSet(Sequence):
    """Wrapper for client QuotaSet objects which turns the individual quotas
    into Quota objects for easier handling/iteration.

    `QuotaSet` objects support a mix of `list` and `dict` methods; you can use
    the bracket notation (`qs["my_quota"] = 0`) to add new quota values, and
    use the `get` method to retrieve a specific quota, but otherwise it
    behaves much like a list or tuple, particularly in supporting iteration.
    """
    def __init__(self, apiresource=None):
        self.items = []
        if apiresource:
            if hasattr(apiresource, '_info'):
                items = apiresource._info.items()
            else:
                items = apiresource.items()

            for k, v in items:
                if k == 'id':
                    continue
                self[k] = v

    def __setitem__(self, k, v):
        v = int(v) if v is not None else v
        q = Quota(k, v)
        self.items.append(q)

    def __getitem__(self, index):
        return self.items[index]

    def __add__(self, other):
        """Merge another QuotaSet into this one. Existing quotas are
        not overridden.
        """
        if not isinstance(other, QuotaSet):
            msg = "Can only add QuotaSet to QuotaSet, " \
                  "but received %s instead" % type(other)
            raise ValueError(msg)

        for item in other:
            if self.get(item.name).limit is None:
                self.items.append(item)
        return self

    def __len__(self):
        return len(self.items)

    def __repr__(self):
        return repr(self.items)

    def get(self, key, default=None):
        match = [quota for quota in self.items if quota.name == key]
        return match.pop() if len(match) else Quota(key, default)

    def add(self, other):
        return self.__add__(other)

def get_service_from_catalog(catalog, service_type):
    if catalog:
        for service in catalog:
            if 'type' not in service:
                continue
            if service['type'] == service_type:
                return service
    return None

def get_version_from_service(service):
    if service and service.get('endpoints'):
        endpoint = service['endpoints'][0]
        if 'interface' in endpoint:
            return 3
        else:
            return 2.0
    return 2.0

# Mapping of V2 Catalog Endpoint_type to V3 Catalog Interfaces
ENDPOINT_TYPE_TO_INTERFACE = {
    'publicURL': 'public',
    'internalURL': 'internal',
    'adminURL': 'admin',
}

def get_url_for_service(service, region, endpoint_type):
    if 'type' not in service:
        return None

    identity_version = get_version_from_service(service)
    service_endpoints = service.get('endpoints', [])
    available_endpoints = [endpoint for endpoint in service_endpoints
                           if region == _get_endpoint_region(endpoint)]
    """if we are dealing with the identity service and there is no endpoint
    in the current region, it is okay to use the first endpoint for any
    identity service endpoints and we can assume that it is global
    """
    if service['type'] == 'identity' and not available_endpoints:
        available_endpoints = [endpoint for endpoint in service_endpoints]

    for endpoint in available_endpoints:
        try:
            if identity_version < 3:
                return endpoint.get(endpoint_type)
            else:
                interface = \
                    ENDPOINT_TYPE_TO_INTERFACE.get(endpoint_type, '')
                if endpoint.get('interface') == interface:
                    return endpoint.get('url')
        except (IndexError, KeyError):
            """it could be that the current endpoint just doesn't match the
            type, continue trying the next one
            """
            pass
    return None

def url_for(request, service_type, endpoint_type=None, region=None):
    endpoint_type = endpoint_type or getattr(settings,
                                             'OPENSTACK_ENDPOINT_TYPE',
                                             'publicURL')
    fallback_endpoint_type = getattr(settings, 'SECONDARY_ENDPOINT_TYPE', None)

    catalog = request.user.service_catalog
    service = get_service_from_catalog(catalog, service_type)
    if service:
        if not region:
            region = request.user.services_region
        url = get_url_for_service(service,
                                  region,
                                  endpoint_type)
        if not url and fallback_endpoint_type:
            url = get_url_for_service(service,
                                      region,
                                      fallback_endpoint_type)
        if url:
            return url
    raise exceptions.ServiceCatalogException(service_type)

def is_service_enabled(request, service_type):
    service = get_service_from_catalog(request.user.service_catalog,
                                       service_type)
    if service:
        region = request.user.services_region
        for endpoint in service.get('endpoints', []):
            if 'type' not in service:
                continue
            # ignore region for identity
            if service['type'] == 'identity' or \
               _get_endpoint_region(endpoint) == region:
                return True
    return False

def _get_endpoint_region(endpoint):
    """Common function for getting the region from endpoint.

    In Keystone V3, region has been deprecated in favor of
    region_id.

    This method provides a way to get region that works for
    both Keystone V2 and V3.
    """
    return endpoint.get('region_id') or endpoint.get('region')
<|endoftext|>