|
| 1 | +__all__ = ['get_search'] |
| 2 | + |
| 3 | +import flask |
| 4 | +import simplejson as json |
| 5 | + |
| 6 | +import signals |
| 7 | +import storage |
| 8 | +import toolkit |
| 9 | + |
| 10 | +from .app import app |
| 11 | + |
| 12 | + |
| 13 | +store = storage.load() |
| 14 | +#index = Index() |
| 15 | + |
| 16 | + |
| 17 | +class Index (dict): |
| 18 | + """Maintain an index of repository data |
| 19 | +
|
| 20 | + The index is a dictionary. The keys are |
| 21 | + '{namespace}/{repository}' strings, and the values are description |
| 22 | + strings. For example: |
| 23 | +
|
| 24 | + index['library/ubuntu'] = 'An ubuntu image...' |
| 25 | + """ |
| 26 | + def __init__(self): |
| 27 | + super(Index, self).__init__() |
| 28 | + self.version = 1 |
| 29 | + self.load() |
| 30 | + signals.repository_created.connect(self._handler_repository_created) |
| 31 | + signals.repository_updated.connect(self._handler_repository_created) |
| 32 | + signals.repository_deleted.connect(self._handler_repository_deleted) |
| 33 | + |
| 34 | + def load(self): |
| 35 | + regenerated = False |
| 36 | + try: |
| 37 | + index_content = store.get_content(store.index_path()) |
| 38 | + except (OSError, IOError): |
| 39 | + index_data = self._regenerate_index() |
| 40 | + regenerated = True |
| 41 | + else: |
| 42 | + data = json.loads(index_content) |
| 43 | + if data['version'] != self.version: |
| 44 | + raise NotImplementedError( |
| 45 | + 'unrecognized search index version {0}'.format( |
| 46 | + data['version'])) |
| 47 | + index_data = data['index'] |
| 48 | + self.clear() |
| 49 | + self.update(index_data) |
| 50 | + if regenerated: |
| 51 | + self.save() |
| 52 | + |
| 53 | + def save(self): |
| 54 | + index_data = { |
| 55 | + 'version': self.version, |
| 56 | + 'index': dict(self), |
| 57 | + } |
| 58 | + store.put_content(store.index_path(), json.dumps(index_data)) |
| 59 | + |
| 60 | + def _regenerate_index(self): |
| 61 | + index_data = {} |
| 62 | + description = '' # TODO(wking): store descriptions |
| 63 | + try: |
| 64 | + namespace_paths = list( |
| 65 | + store.list_directory(path=store.repositories)) |
| 66 | + except OSError: |
| 67 | + namespace_paths = [] |
| 68 | + for namespace_path in namespace_paths: |
| 69 | + namespace = namespace_path.rsplit('/', 1)[-1] |
| 70 | + try: |
| 71 | + repository_paths = list( |
| 72 | + store.list_directory(path=namespace_path)) |
| 73 | + except OSError: |
| 74 | + repository_paths = [] |
| 75 | + for path in repository_paths: |
| 76 | + repository = path.rsplit('/', 1)[-1] |
| 77 | + key = '{0}/{1}'.format(namespace, repository) |
| 78 | + index_data[key] = description |
| 79 | + return index_data |
| 80 | + |
| 81 | + def _handler_repository_created( |
| 82 | + self, sender, namespace, repository, value): |
| 83 | + key = '{0}/{1}'.format(namespace, repository) |
| 84 | + description = '' # TODO(wking): store descriptions |
| 85 | + self[key] = description |
| 86 | + self.save() |
| 87 | + |
| 88 | + def _handler_repository_deleted(self, sender, namespace, repository): |
| 89 | + key = '{0}/{1}'.format(namespace, repository) |
| 90 | + try: |
| 91 | + self.pop(key) |
| 92 | + except KeyError: |
| 93 | + pass |
| 94 | + else: |
| 95 | + self.save() |
| 96 | + |
| 97 | + |
| 98 | +index = Index() |
| 99 | + |
| 100 | + |
| 101 | +@app.route('/v1/search', methods=['GET']) |
| 102 | +def get_search(): |
| 103 | + search_term = flask.request.args.get('q', '') |
| 104 | + results = [ |
| 105 | + { |
| 106 | + 'name': name, |
| 107 | + 'description': description, |
| 108 | + } |
| 109 | + for name, description in index.items() |
| 110 | + if search_term in name |
| 111 | + or search_term in description] |
| 112 | + return toolkit.response({ |
| 113 | + 'query': search_term, |
| 114 | + 'num_results': len(results), |
| 115 | + 'results': results, |
| 116 | + }) |
0 commit comments