~jcsackett/charmworld/bac-tag-constraints

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from datetime import datetime
from email.utils import parseaddr
from inspect import getargspec
import json
from os.path import (
    basename,
    join,
)
import re
from urllib import unquote

from gridfs.errors import NoFile
from os.path import dirname
from pyramid.httpexceptions import HTTPNotFound
from webob import Response

from charmworld.models import (
    CharmFileSet,
    getfs,
    QAData,
)
from charmworld.search import (
    InvalidCharmType,
    NegativeLimit,
)


def json_error(status, params):
    """Return a JSON error response.

    :param status: The HTTP status code to use.
    :param params: A json-serializable value to use as the body.
    """
    return Response(json.dumps(params),
                    headerlist=[
                        ('Content-Type', 'application/json'),
                        ("Access-Control-Allow-Origin", "*"),
                        ("Access-Control-Allow-Headers", "X-Requested-With"),
                    ],
                    status_code=status)


class API0:
    """Implementation of API 0.

    All methods whose names do not begin with an underscore are exposed.
    Methods may return a webob Response, which is returned directly, or a
    json-serializable value, which will be returned as a json HTTP response.
    """
    def __init__(self, request):
        self.request = request

    def __call__(self):
        """Dispatch the request to the appropriate method.

        Find a method with the same name as 'endpoint'.  Raise HTTPNotFound if
        the method doesn't exist or starts with '_'.

        Return a 406 response if the query parameters are not supported by the
        method.
        """
        endpoint = self.request.matchdict['endpoint']
        if endpoint.startswith('_'):
            raise HTTPNotFound(self.request.path)
        handler = getattr(self, endpoint, None)
        remainder = self.request.matchdict.get('remainder')
        if handler is None or remainder == '/':
            raise HTTPNotFound(self.request.path)
        if remainder == '':
            args = ()
        else:
            args = (remainder,)
        kwargs = {}
        spec = getargspec(handler)
        kwargs = self.request.GET.dict_of_lists()
        try:
            kwargs['type_'] = kwargs.pop('type')
        except KeyError:
            pass
        for key in set(kwargs).difference(spec.args):
            return json_error(406, {
                'type': 'parameter_not_supported',
                'parameter': key})
        result = handler(*args, **kwargs)
        if isinstance(result, Response):
            return result
        return Response(
            json.dumps(result, indent=2, sort_keys=True),
            headerlist=[
                ('Content-Type', 'application/json'),
                ("Access-Control-Allow-Origin", "*"),
                ("Access-Control-Allow-Headers", "X-Requested-With"),
            ]
        )

    @staticmethod
    def _get_api_id(charm):
        """Return the API id for a Mongo-formatted charm."""
        return re.sub('^cs:', '', charm['store_url'])

    @staticmethod
    def _parsed_email(address):
        name, address = parseaddr(address)
        return {
            'name': name,
            'email': address,
        }

    @classmethod
    def _format_revision(cls, revision):
        created = datetime.utcfromtimestamp(round(revision['created']))
        authors = [cls._parsed_email(author)
                   for author in revision['authors']]
        return{
            'message': revision['message'],
            'revno': revision['revno'],
            'date': created.isoformat(),
            'authors': authors,
        }

    @classmethod
    def _format_charm(cls, charm):
        """Format the charm for API consumers."""
        mapping = {
            'summary': 'summary',
            'name': 'name',
            'description': 'description',
            'owner': 'owner',
            'distro_series': 'series',
            'revision': 'revision',
            'url': 'store_url',
        }
        output = dict((key, charm[value]) for key, value in mapping.items())
        maintainer = charm.get('maintainer', '')
        bugs_link = (
            'https://bugs.launchpad.net/charms/%(series)s/+source/%(name)s'
            % charm)
        revisions = []
        for revision in charm['changes']:
            revisions.append(cls._format_revision(revision))
        output.update({
            'id': cls._get_api_id(charm),
            'rating_numerator': 0,
            'rating_denominator': 0,
            'is_new': False,
            'is_popular': False,
            'date_created': 0,
            'code_source': {
                'type': 'bzr',
                'location': 'lp:' + charm['branch_spec'],
                'revision': str(charm['last_change']['revno']),
                'last_log': charm['last_change']['message'],
                'bugs_link': bugs_link,
                'revisions': revisions,
            },
            'maintainer': cls._parsed_email(maintainer),
            'relations': {
                'provides': charm.get('provides', {}),
                'requires': charm.get('requires', {}),
            },
            'options': charm.get('config', {'options': {}})['options'],
            'files': [join(entry['subdir'], entry['filename'])
                      for entry in charm['files'].values()],
            'is_approved': charm['owner'] == 'charmers',
            'tested_providers': [],
            'downloads_in_past_30_days': 0,
            'is_subordinate': charm.get('subordinate', False)
        })
        if 'icon' in charm:
            output['icon'] = charm['icon']
        return output

    @staticmethod
    def _extract_charm_id(path):
        """Extract the charm_id from a path.

        charmids begining with a '~' are considered to be 3 elements long,
        and other charmids are considered to be 2 elements long.
        """
        elements = path.lstrip('/').split('/')
        if unquote(elements[0])[0] == '~':
            split_at = 3
        else:
            split_at = 2
        charm_id = '/'.join(elements[:split_at])
        if len(elements) > split_at:
            trailing = '/'.join(elements[split_at:])
        else:
            trailing = None
        return charm_id, trailing

    @staticmethod
    def _parse_charm_id(charm_id):
        """Split a charm id into its component parts.

        :return: A tuple of (owner, series, name, revision).
        """
        elements = [unquote(element) for element in charm_id.split('/')]
        name, revision = elements[-1].rsplit('-', 1)
        if len(elements) == 3:
            owner = elements[0]
            if not owner.startswith('~'):
                raise ValueError('Owner should begin with a "~".')
            else:
                owner = owner[1:]
        else:
            owner = 'charmers'
        return owner, elements[-2], name, revision

    def _find_charm(self, path):
        charm_id, trailing = self._extract_charm_id(path)
        try:
            owner, series, name, revision = self._parse_charm_id(charm_id)
        except ValueError:
            charm = None
        else:
            charm = self.request.db.charms.find_one({
                'owner': owner,
                'series': series,
                'name': name,
            })
        if charm is not None:
            api_id = self._get_api_id(charm)
            # Charm id should match up to revision.
            if not api_id.startswith(charm_id.split('-', 1)[0]):
                charm = None
        return charm_id, trailing, charm

    def charm(self, path):
        """Retrieve a charm according to its charm_id."""
        charm_id, trailing, charm = self._find_charm(path)
        if charm is None:
            return json_error(
                404, {'type': 'no_such_charm', 'charm_id': charm_id})
        if trailing is None:
            return self._format_charm(charm)
        elif trailing.startswith('file/'):
            return self._charm_file(charm, trailing)
        elif trailing == ('qa'):
            return self._charm_qa(charm)
        else:
            raise HTTPNotFound(self.request.path)

    def _charm_file(self, charm, trailing):
        path = trailing.split('/', 1)[1]
        fs = getfs(self.request.db)
        charm_file = CharmFileSet.make_charm_file(fs, charm, path,
                                                  basename(path))
        try:
            return Response(
                charm_file.read(),
                headerlist=[
                    ("Access-Control-Allow-Origin", "*"),
                    ("Access-Control-Allow-Headers", "X-Requested-With"),
                ],
                status_code=200)
        except NoFile:
            return json_error(404, {'type': 'no_such_file', 'path': path})

    @staticmethod
    def _format_category(category):
        return dict((key, value) for key, value in category.items()
                    if key != '_id')

    def _charm_qa(self, charm):
        qa_data = QAData(self.request.db, charm.get('qa'))
        categories = [self._format_category(category) for category
                      in qa_data.qa_categories.values()]
        scores = qa_data.filtered_scores()
        return {'result': {'questions': categories}, 'scores': scores}

    def charms(self, limit=None, name=None, series=None, owner=None,
               provides=None, requires=None, type_=None, provider=None,
               scope=None, category=None, text=None):
        """Search for charms matching parameters.

        :limit: A query limit.  (max number of results)
        """
        params = dict((key, value) for key, value in locals().items()
                      if key in ('series', 'owner', 'name'))
        if text is None:
            text = ['']
        params['i_provides'] = provides
        params['i_requires'] = requires
        filters = dict(item for item in params.items() if item[1] is not None)
        if limit is not None:
            if len(limit) > 1:
                return json_error(406, {
                                  'type': 'multiple_values',
                                  'parameter': 'limit'})
            limit = int(limit[0])
        try:
            results = self.request.index_client.api_search(
                text[0], filters, type_, limit)
        except InvalidCharmType as e:
            return json_error(406, {
                              'type': 'unsupported_value',
                              'parameter': 'type',
                              'value': e[0]})
        except NegativeLimit:
            return json_error(406, {
                              'type': 'negative_value',
                              'parameter': 'limit'})
        return {'result': [self._format_charm(result) for result in results]}

    def sidebar_editorial(self):
        """Generate a JSON structure of editorial content for the sidebar.

        Includes data for the initial 5 charm slider widget, popular charms,
        and new.

        This is temporary hard coded data that should be replaced by some real
        backend that's editable by other users.
        """
        sidebar_json = "{0}/static/sidebar.json".format(
            dirname(dirname(__file__)))
        with open(sidebar_json) as handle:
            sidebar_dict = json.loads(handle.read())
        return sidebar_dict