Commit 41490c37 by Sébastien Eustace

Add search command

parent ed104cd0
......@@ -6,6 +6,7 @@
- Added support for Python 2.7.
- Added a fallback mechanism (opt-in) for missing dependencies.
- Added `search` command.
### Changes
......
......@@ -20,6 +20,7 @@ from .commands import PublishCommand
from .commands import RemoveCommand
from .commands import RunCommand
from .commands import ScriptCommand
from .commands import SearchCommand
from .commands import ShowCommand
from .commands import UpdateCommand
from .commands import VersionCommand
......@@ -100,6 +101,7 @@ class Application(BaseApplication):
RemoveCommand(),
RunCommand(),
ScriptCommand(),
SearchCommand(),
ShowCommand(),
UpdateCommand(),
VersionCommand(),
......
......@@ -10,6 +10,7 @@ from .publish import PublishCommand
from .remove import RemoveCommand
from .run import RunCommand
from .script import ScriptCommand
from .search import SearchCommand
from .show import ShowCommand
from .update import UpdateCommand
from .version import VersionCommand
from .command import Command
class SearchCommand(Command):
"""
Searches for packages on remote repositories.
search
{ tokens* : The tokens to search for. }
{ --N|only-name : Search only in name. }
"""
def handle(self):
from poetry.repositories.base_repository import BaseRepository
flags = BaseRepository.SEARCH_FULLTEXT
if self.option('only-name'):
flags = BaseRepository.SEARCH_FULLTEXT
results = self.poetry.pool.search(self.argument('tokens'), flags)
for result in results:
self.line('')
name = '<info>{}</>'.format(
result.name
)
name += ' (<comment>{}</>)'.format(result.version)
self.line(name)
if result.description:
self.line(
' {}'.format(result.description)
)
......@@ -52,6 +52,10 @@ class LegacyRepository(PyPiRepository):
}
})
@property
def name(self):
return self._name
def find_packages(self, name, constraint=None, extras=None):
packages = []
......
......@@ -4,6 +4,7 @@ from typing import Union
import poetry.packages
from .base_repository import BaseRepository
from .legacy_repository import LegacyRepository
from .repository import Repository
......@@ -79,4 +80,11 @@ class Pool(BaseRepository):
return []
def search(self, query, mode=BaseRepository.SEARCH_FULLTEXT):
raise NotImplementedError()
results = []
for repository in self._repositories:
if isinstance(repository, LegacyRepository):
continue
results += repository.search(query, mode=mode)
return results
from typing import List
from typing import Union
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
from cachy import CacheManager
......@@ -160,15 +165,13 @@ class PyPiRepository(Repository):
if mode == self.SEARCH_FULLTEXT:
search['summary'] = query
client = ServerProxy(self._url)
client = ServerProxy('https://pypi.python.org/pypi')
hits = client.search(search, 'or')
for hit in hits:
results.append({
'name': hit['name'],
'description': hit['summary'],
'version': hit['version']
})
result = Package(hit['name'], hit['version'], hit['version'])
result.description = hit['summary']
results.append(result)
return results
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment