source: tagsplugin/branches/0.8-stable/tractags/macros.py

Last change on this file was 14787, checked in by Steffen Hoffmann, 8 years ago

TagsPlugin: Added exclude argument for ListTagged macro to allow resources selection using shell-style patterns, refs 12202.

File size: 18.0 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006 Alec Thomas <alec@swapoff.org>
4# Copyright (C) 2011 Itamar Ostricher <itamarost@gmail.com>
5# Copyright (C) 2011-2015 Steffen Hoffmann <hoff.st@web.de>
6# Copyright (C) 2015 Ryan J Ollos <ryan.j.ollos@gmail.com>
7#
8# This software is licensed as described in the file COPYING, which
9# you should have received as part of this distribution.
10#
11
12import re
13
14from fnmatch import fnmatchcase
15from genshi.builder import tag as builder
16from pkg_resources import resource_filename
17
18from trac.config import BoolOption, ListOption, Option
19from trac.core import Component, TracError, implements
20from trac.resource import Resource, get_resource_description, \
21                          get_resource_url, render_resource_link
22from trac.ticket.api import TicketSystem
23from trac.ticket.model import Ticket
24from trac.util import embedded_numbers
25from trac.util.compat import any, sorted, set
26from trac.util.presentation import Paginator
27from trac.util.text import shorten_line, to_unicode
28from trac.web.chrome import Chrome, ITemplateProvider, \
29                            add_link, add_stylesheet
30from trac.wiki.api import IWikiMacroProvider, parse_args
31from trac.wiki.formatter import format_to_oneliner
32
33from tractags.api import Counter, TagSystem, N_, _, gettext
34from tractags.util import query_realms
35
36try:
37    from trac.util  import as_int
38except ImportError:
39    def as_int(s, default, min=None, max=None):
40        """Convert s to an int and limit it to the given range, or
41        return default if unsuccessful (copied verbatim from Trac0.12dev)."""
42        try:
43            value = int(s)
44        except (TypeError, ValueError):
45            return default
46        if min is not None and value < min:
47            value = min
48        if max is not None and value > max:
49            value = max
50        return value
51
52
53# Check for unsupported pre-tags-0.6 macro keyword arguments.
54_OBSOLETE_ARGS_RE = re.compile(r"""
55    (expression|operation|showheadings|tagspace|tagspaces)=
56    """, re.VERBOSE)
57
58
59class TagTemplateProvider(Component):
60    """Provides templates and static resources for the tags plugin."""
61
62    implements(ITemplateProvider)
63
64    abstract = True
65
66    # ITemplateProvider methods
67    def get_templates_dirs(self):
68        """Return the absolute path of the directory containing the provided
69        Genshi templates.
70        """
71        return [resource_filename(__name__, 'templates')]
72
73    def get_htdocs_dirs(self):
74        """Return the absolute path of a directory containing additional
75        static resources (such as images, style sheets, etc).
76        """
77        return [('tags', resource_filename(__name__, 'htdocs'))]
78
79
80class TagWikiMacros(TagTemplateProvider):
81    """[opt] Provides macros, that utilize the tag system in wiki markup."""
82
83    implements(IWikiMacroProvider)
84
85    caseless_sort = BoolOption('tags', 'cloud_caseless_sort', default=False,
86        doc="Whether the tag cloud should be sorted case-sensitive.")
87    default_cols = Option('tags', 'listtagged_default_table_cols',
88        'id|description|tags',
89        doc="Select columns and column order for table format.\n\n"
90            "See `ListTagged` description (WikiMacros) for supported values.")
91    default_format = Option('tags', 'listtagged_default_format', 'oldlist',
92        doc="Set default format for the handler of the `/tags` domain.\n\n"
93            "See `ListTagged` description (WikiMacros) for supported values.")
94    exclude_realms = ListOption('tags', 'listtagged_exclude_realms', [],
95        doc="Comma-separated list of realms to exclude from tags queries "
96            "by default, unless specifically included using 'realm:<realm>' "
97            "in a query.")
98    items_per_page = Option('tags', 'listtagged_items_per_page', 100,
99        doc="Number of tagged resources displayed per page of tag query "
100            "results requested by `ListTagged` macros and from `/tags`.")
101    items_per_page = as_int(items_per_page, 100)
102    supported_cols = frozenset(['realm', 'id', 'description', 'tags'])
103
104    def __init__(self):
105        # TRANSLATOR: Keep macro doc style formatting here, please.
106        self.doc_cloud = N_("""Display a tag cloud.
107
108    Show a tag cloud for all tags on resources matching query.
109
110    Usage:
111
112    {{{
113    [[TagCloud(<query>[,caseless_sort=<bool>][,mincount=<n>])]]
114    }}}
115    caseless_sort::
116      Whether the tag cloud should be sorted case-sensitive.
117    mincount::
118      Optional integer threshold to hide tags with smaller count.
119
120    See tags documentation for the query syntax.
121    """)
122        self.doc_listtagged = N_("""List tagged resources.
123
124    Usage:
125
126    {{{
127    [[ListTagged(<query>[,exclude=<list>],[[format=<format>],cols=<columns>])]]
128    }}}
129    format::
130      result list presentation; supported values:
131
132    || `compact` || comma-separated inline list of "linked-description" ||
133    || `oldlist` (default) || " * linked-id description (tags)" list ||
134    || `table` || table... (see corresponding column option too) ||
135    || `short` or other value || bulleted list of "linked-description" ||
136    cols::
137      columns for 'table' format using a "|"-separated list of column names
138      (order matters); supported columns: realm, id, description, tags
139    exclude::
140      exclude tagged resources that match a name in the colon-separated list
141      of resource ids, accepts shell-style patterns
142
143    See tags documentation for the query syntax.
144    """)
145
146    # IWikiMacroProvider
147
148    def get_macros(self):
149        yield 'ListTagged'
150        yield 'TagCloud'
151
152    def get_macro_description(self, name):
153        if name == 'ListTagged':
154            return gettext(self.doc_listtagged)
155        if name == 'TagCloud':
156            return gettext(self.doc_cloud)
157
158    def expand_macro(self, formatter, name, content, realms=[]):
159        """Evaluate macro call and render results.
160
161        Calls from web-UI come with pre-processed realm selection.
162        """
163        env = self.env
164        req = formatter.req
165        tag_system = TagSystem(env)
166
167        all_realms = tag_system.get_taggable_realms()
168        if not all_realms:
169            # Tag providers are required, no result without at least one.
170            return ''
171        args, kw = parse_args(content)
172
173        query = args and args[0].strip() or None
174        if not realms:
175            # Check macro arguments for realms (typical wiki macro call).
176            realms = 'realm' in kw and kw['realm'].split('|') or []
177        if query:
178            # Add realms from query expression.
179            realms.extend(query_realms(query, all_realms))
180            # Remove redundant realm selection for performance.
181            if set(realms) == all_realms:
182                query = re.sub('(^|\W)realm:\S+(\W|$)', ' ', query).strip()
183        if name == 'TagCloud':
184            # Set implicit 'all tagged realms' as default.
185            if not realms:
186                realms = all_realms
187            if query:
188                all_tags = Counter()
189                # Require per resource query including view permission checks.
190                for resource, tags in tag_system.query(req, query):
191                    all_tags.update(tags)
192            else:
193                # Allow faster per tag query, side steps permission checks.
194                all_tags = tag_system.get_all_tags(req, realms=realms)
195            mincount = 'mincount' in kw and kw['mincount'] or None
196            return self.render_cloud(req, all_tags,
197                                     caseless_sort=self.caseless_sort,
198                                     mincount=mincount, realms=realms)
199        elif name == 'ListTagged':
200            if content and _OBSOLETE_ARGS_RE.search(content):
201                data = {'warning': 'obsolete_args'}
202            else:
203                data = {'warning': None}
204            context = formatter.context
205            # Use TagsQuery arguments (most likely wiki macro calls).
206            cols = 'cols' in kw and kw['cols'] or self.default_cols
207            format = 'format' in kw and kw['format'] or self.default_format
208            if not realms:
209                # Apply ListTagged defaults to macro call w/o realm.
210                realms = list(set(all_realms)-set(self.exclude_realms))
211                if not realms:
212                    return ''
213            query = '(%s) (%s)' % (query or '', ' or '.join(['realm:%s' % (r)
214                                                             for r in realms]))
215            query_result = tag_system.query(req, query)
216            excludes = [exc.strip()
217                        for exc in kw.get('exclude', '' ).split(':')
218                        if exc.strip()]
219            if excludes and query_result:
220                filtered_result = [(resource, tags)
221                                   for resource, tags in query_result
222                                   if not any(fnmatchcase(resource.id, exc)
223                                              for exc in excludes)]
224                query_result = filtered_result
225            if not query_result:
226                return ''
227
228            def _link(resource):
229                if resource.realm == 'tag':
230                    # Keep realm selection in tag links.
231                    return builder.a(resource.id,
232                                     href=self.get_href(req, realms,
233                                                        tag=resource))
234                elif resource.realm == 'ticket':
235                    # Return resource link including ticket status dependend
236                    #   class to allow for common Trac ticket link style.
237                    ticket = Ticket(env, resource.id)
238                    return builder.a('#%s' % ticket.id,
239                                     class_=ticket['status'],
240                                     href=formatter.href.ticket(ticket.id),
241                                     title=shorten_line(ticket['summary']))
242                return render_resource_link(env, context, resource, 'compact')
243
244            if format == 'table':
245                cols = [col for col in cols.split('|')
246                        if col in self.supported_cols]
247                # Use available translations from Trac core.
248                try:
249                    labels = TicketSystem(env).get_ticket_field_labels()
250                    labels['id'] = _('Id')
251                except AttributeError:
252                    # Trac 0.11 neither has the attribute nor uses i18n.
253                    labels = {'id': 'Id', 'description': 'Description'}
254                labels['realm'] = _('Realm')
255                labels['tags'] = _('Tags')
256                headers = [{'label': labels.get(col)}
257                           for col in cols]
258                data.update({'cols': cols,
259                             'headers': headers})
260
261            results = sorted(query_result, key=lambda r: \
262                             embedded_numbers(to_unicode(r[0].id)))
263            results = self._paginate(req, results, realms)
264            rows = []
265            for resource, tags in results:
266                desc = tag_system.describe_tagged_resource(req, resource)
267                tags = sorted(tags)
268                wiki_desc = format_to_oneliner(env, context, desc)
269                if tags:
270                    rendered_tags = [_link(Resource('tag', tag))
271                                     for tag in tags]
272                    if 'oldlist' == format:
273                        resource_link = _link(resource)
274                    else:
275                        resource_link = builder.a(wiki_desc,
276                                                  href=get_resource_url(
277                                                  env, resource, context.href))
278                        if 'table' == format:
279                            cells = []
280                            for col in cols:
281                                if col == 'id':
282                                    cells.append(_link(resource))
283                                # Don't duplicate links to resource in both.
284                                elif col == 'description' and 'id' in cols:
285                                    cells.append(wiki_desc)
286                                elif col == 'description':
287                                    cells.append(resource_link)
288                                elif col == 'realm':
289                                    cells.append(resource.realm)
290                                elif col == 'tags':
291                                    cells.append(
292                                        builder([(tag, ' ')
293                                                 for tag in rendered_tags]))
294                            rows.append({'cells': cells})
295                            continue
296                rows.append({'desc': wiki_desc,
297                             'rendered_tags': None,
298                             'resource_link': _link(resource)})
299            data.update({'format': format,
300                         'paginator': results,
301                         'results': rows,
302                         'tags_url': req.href('tags')})
303
304            # Work around a bug in trac/templates/layout.html, that causes a
305            # TypeError for the wiki macro call, if we use add_link() alone.
306            add_stylesheet(req, 'common/css/search.css')
307
308            return Chrome(env).render_template(
309                req, 'listtagged_results.html', data, 'text/html', True)
310
311    def get_href(self, req, realms, query=None, per_page=None, page=None,
312                 tag=None, **kwargs):
313        """Prepare href objects for tag links and pager navigation.
314
315        Generate form-related arguments, strip arguments with default values.
316        """
317        # Prepare realm arguments to keep form data consistent.
318        form_realms = dict((realm, 'on') for realm in realms)
319        if not page and not per_page:
320            # We're not serving pager navigation here.
321            return get_resource_url(self.env, tag, req.href,
322                                    form_realms=form_realms, **kwargs)
323        if page == 1:
324            page = None
325        if per_page == self.items_per_page:
326            per_page = None
327        return req.href(req.path_info, form_realms, q=query,
328                        realms=realms, listtagged_per_page=per_page,
329                        listtagged_page=page, **kwargs)
330
331    def render_cloud(self, req, cloud, renderer=None, caseless_sort=False,
332                     mincount=None, realms=()):
333        """Render a tag cloud.
334
335        :cloud: Dictionary of {object: count} representing the cloud.
336        :param renderer: A callable with signature (tag, count, percent)
337                         used to render the cloud objects.
338        :param caseless_sort: Boolean, whether tag cloud should be sorted
339                              case-sensitive.
340        :param mincount: Integer threshold to hide tags with smaller count.
341        """
342        min_px = 10.0
343        max_px = 30.0
344        scale = 1.0
345
346        if renderer is None:
347            def default_renderer(tag, count, percent):
348                href = self.get_href(req, realms, tag=Resource('tag', tag))
349                return builder.a(tag, rel='tag', title='%i' % count,
350                                 href=href, style='font-size: %ipx'
351                                 % int(min_px + percent * (max_px - min_px)))
352            renderer = default_renderer
353
354        # A LUT from count to n/len(cloud)
355        size_lut = dict([(c, float(i)) for i, c in
356                         enumerate(sorted(set([r for r in cloud.values()])))])
357        if size_lut:
358            scale = 1.0 / len(size_lut)
359
360        if caseless_sort:
361            # Preserve upper-case precedence within similar tags.
362            items = reversed(sorted(cloud.iteritems(),
363                                    key=lambda t: t[0].lower(), reverse=True))
364        else:
365            items = sorted(cloud.iteritems())
366        ul = li = None
367        for i, (tag, count) in enumerate(items):
368            percent = size_lut[count] * scale
369            if mincount and count < as_int(mincount, 1):
370                # Tag count is too low.
371                continue
372            if ul:
373                # Found new tag for cloud; now add previously prepared one.
374                ul('\n', li)
375            else:
376                # Found first tag for cloud; now create the list.
377                ul = builder.ul(class_='tagcloud')
378            # Prepare current tag entry.
379            li = builder.li(renderer(tag, count, percent))
380        if li:
381            # All tags checked; mark latest tag as last one (no tailing colon).
382            li(class_='last')
383            ul('\n', li, '\n')
384        return ul and ul or _("No tags found")
385
386    def _paginate(self, req, results, realms):
387        query = req.args.get('q', None)
388        current_page = as_int(req.args.get('listtagged_page'), 1, min=1)
389        items_per_page = as_int(req.args.get('listtagged_per_page'),
390                                self.items_per_page)
391        if items_per_page < 1:
392            items_per_page = self.items_per_page
393        try:
394            result = Paginator(results, current_page - 1, items_per_page)
395        except (AssertionError, TracError), e:
396            self.log.warn("ListTagged macro: %s", e)
397            current_page = 1
398            result = Paginator(results, current_page - 1, items_per_page)
399
400        pagedata = []
401        shown_pages = result.get_shown_pages(21)
402        for page in shown_pages:
403            page_href = self.get_href(req, realms, query, items_per_page, page)
404            pagedata.append([page_href, None, str(page),
405                             _("Page %(num)d", num=page)])
406
407        attributes = ['href', 'class', 'string', 'title']
408        result.shown_pages = [dict(zip(attributes, p)) for p in pagedata]
409
410        result.current_page = {'href': None, 'class': 'current',
411                               'string': str(result.page + 1), 'title': None}
412
413        if result.has_next_page:
414            next_href = self.get_href(req, realms, query, items_per_page,
415                                      current_page + 1)
416            add_link(req, 'next', next_href, _('Next Page'))
417
418        if result.has_previous_page:
419            prev_href = self.get_href(req, realms, query, items_per_page,
420                                      current_page - 1)
421            add_link(req, 'prev', prev_href, _('Previous Page'))
422        return result
Note: See TracBrowser for help on using the repository browser.