# Path: poet/repositories/pypi_repository.py
# class PyPiRepository(object):
# 
#     DEFAULT_URL = PyPI.pypi_url
# 
#     SEARCH_FULLTEXT = 0
#     SEARCH_NAME = 1
# 
#     def __init__(self, url=DEFAULT_URL):
#         self._url = url
# 
#     def find_packages(self, name, constraint=None):
#         packages = []
# 
#         if constraint is not None:
#             version_parser = VersionParser()
#             constraint = version_parser.parse_constraints(constraint)
# 
#         with ServerProxy(self._url) as client:
#             versions = client.package_releases(name, True)
# 
#         if constraint:
#             versions = constraint.select([Version.coerce(v) for v in versions])
# 
#         for version in versions:
#             try:
#                 packages.append(Package(name, version))
#             except ValueError:
#                 continue
# 
#         return packages
# 
#     def search(self, query, mode=0):
#         results = []
# 
#         search = {
#             'name': query
#         }
# 
#         if mode == self.SEARCH_FULLTEXT:
#             search['summary'] = query
# 
#         client = ServerProxy(self._url)
#         hits = client.search(search, 'or')
# 
#         for hit in hits:
#             results.append({
#                 'name': hit['name'],
#                 'description': hit['summary'],
#                 'version': hit['version']
#             })
# 
#         return results
# 
#     def package_name(self, name):
#         url = 'https://pypi.python.org/pypi/{}/json'.format(name)
# 
#         response = requests.get(url)
# 
#         if response.status_code == 404:
#             raise Exception('Package [{}] not found'.format(name))
# 
#         response.raise_for_status()
# 
#         info = response.json()['info']
# 
#         return info['name']
# 
# Path: poet/console/commands/index_command.py
# class IndexCommand(Command):
# 
#     def __init__(self):
#         super(IndexCommand, self).__init__()
# 
#         self._repository = PyPiRepository()
# 
#     def configure(self):
#         super(IndexCommand, self).configure()
# 
#         # Adding --i|index option
#         self.add_option(
#             'index', 'i',
#             InputOption.VALUE_REQUIRED,
#             'The index to use'
#         )
# 
#     def execute(self, i, o):
#         super(IndexCommand, self).execute(i, o)
# 
#         index = self.option('index')
# 
#         if index:
#             self._repository = PyPiRepository(index)


from ...repositories import PyPiRepository
from .index_command import IndexCommand
# -*- coding: utf-8 -*-



class SearchCommand(IndexCommand):
    """
    Search for packages.

    search
        {tokens* : The tokens to search for.}
        {--N|only-name : Search only in name.}
    """

    help = """The search command searches for packages by its name
<info>poet search requests pendulum</info>
"""

    def handle(self):
        flags = PyPiRepository.SEARCH_FULLTEXT
        if self.option('only-name'):

# Path: poet/repositories/pypi_repository.py
# class PyPiRepository(object):
# 
#     DEFAULT_URL = PyPI.pypi_url
# 
#     SEARCH_FULLTEXT = 0
#     SEARCH_NAME = 1
# 
#     def __init__(self, url=DEFAULT_URL):
#         self._url = url
# 
#     def find_packages(self, name, constraint=None):
#         packages = []
# 
#         if constraint is not None:
#             version_parser = VersionParser()
#             constraint = version_parser.parse_constraints(constraint)
# 
#         with ServerProxy(self._url) as client:
#             versions = client.package_releases(name, True)
# 
#         if constraint:
#             versions = constraint.select([Version.coerce(v) for v in versions])
# 
#         for version in versions:
#             try:
#                 packages.append(Package(name, version))
#             except ValueError:
#                 continue
# 
#         return packages
# 
#     def search(self, query, mode=0):
#         results = []
# 
#         search = {
#             'name': query
#         }
# 
#         if mode == self.SEARCH_FULLTEXT:
#             search['summary'] = query
# 
#         client = ServerProxy(self._url)
#         hits = client.search(search, 'or')
# 
#         for hit in hits:
#             results.append({
#                 'name': hit['name'],
#                 'description': hit['summary'],
#                 'version': hit['version']
#             })
# 
#         return results
# 
#     def package_name(self, name):
#         url = 'https://pypi.python.org/pypi/{}/json'.format(name)
# 
#         response = requests.get(url)
# 
#         if response.status_code == 404:
#             raise Exception('Package [{}] not found'.format(name))
# 
#         response.raise_for_status()
# 
#         info = response.json()['info']
# 
#         return info['name']
# 
# Path: poet/console/commands/index_command.py
# class IndexCommand(Command):
# 
#     def __init__(self):
#         super(IndexCommand, self).__init__()
# 
#         self._repository = PyPiRepository()
# 
#     def configure(self):
#         super(IndexCommand, self).configure()
# 
#         # Adding --i|index option
#         self.add_option(
#             'index', 'i',
#             InputOption.VALUE_REQUIRED,
#             'The index to use'
#         )
# 
#     def execute(self, i, o):
#         super(IndexCommand, self).execute(i, o)
# 
#         index = self.option('index')
# 
#         if index:
#             self._repository = PyPiRepository(index)


# Path: poet/console/commands/search.py
from ...repositories import PyPiRepository
from .index_command import IndexCommand

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



class SearchCommand(IndexCommand):
    """
    Search for packages.

    search
        {tokens* : The tokens to search for.}
        {--N|only-name : Search only in name.}
    """

    help = """The search command searches for packages by its name
<info>poet search requests pendulum</info>
"""

    def handle(self):
        flags = PyPiRepository.SEARCH_FULLTEXT
        if self.option('only-name'):

            flags = PyPiRepository.SEARCH_NAME