~ubuntu-branches/ubuntu/oneiric/moin/oneiric-security

« back to all changes in this revision

Viewing changes to MoinMoin/support/pygments/formatters/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-03-30 12:55:34 UTC
  • mfrom: (0.1.17 sid)
  • Revision ID: james.westby@ubuntu.com-20100330125534-4c2ufc1rok24447l
Tags: 1.9.2-2ubuntu1
* Merge from Debian testing (LP: #521834). Based on work by Stefan Ebner.
  Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason for us
   to pull this in by default currently. Note: This isn't necessary anymore
   but needs a MIR for fckeditor, so postpone dropping this change until
   lucid+1
* debian/rules:
  - Replace hardcoded python2.5 with python* and hardcore python2.6 for ln
* debian/control.in: drop versioned depends on cdbs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
    pygments.formatters
 
4
    ~~~~~~~~~~~~~~~~~~~
 
5
 
 
6
    Pygments formatters.
 
7
 
 
8
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
 
9
    :license: BSD, see LICENSE for details.
 
10
"""
 
11
import os.path
 
12
import fnmatch
 
13
 
 
14
from pygments.formatters._mapping import FORMATTERS
 
15
from pygments.plugin import find_plugin_formatters
 
16
from pygments.util import docstring_headline, ClassNotFound
 
17
 
 
18
ns = globals()
 
19
for fcls in FORMATTERS:
 
20
    ns[fcls.__name__] = fcls
 
21
del fcls
 
22
 
 
23
__all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
 
24
           'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS]
 
25
 
 
26
 
 
27
_formatter_alias_cache = {}
 
28
_formatter_filename_cache = []
 
29
 
 
30
def _init_formatter_cache():
 
31
    if _formatter_alias_cache:
 
32
        return
 
33
    for cls in get_all_formatters():
 
34
        for alias in cls.aliases:
 
35
            _formatter_alias_cache[alias] = cls
 
36
        for fn in cls.filenames:
 
37
            _formatter_filename_cache.append((fn, cls))
 
38
 
 
39
 
 
40
def find_formatter_class(name):
 
41
    _init_formatter_cache()
 
42
    cls = _formatter_alias_cache.get(name, None)
 
43
    return cls
 
44
 
 
45
 
 
46
def get_formatter_by_name(name, **options):
 
47
    _init_formatter_cache()
 
48
    cls = _formatter_alias_cache.get(name, None)
 
49
    if not cls:
 
50
        raise ClassNotFound("No formatter found for name %r" % name)
 
51
    return cls(**options)
 
52
 
 
53
 
 
54
def get_formatter_for_filename(fn, **options):
 
55
    _init_formatter_cache()
 
56
    fn = os.path.basename(fn)
 
57
    for pattern, cls in _formatter_filename_cache:
 
58
        if fnmatch.fnmatch(fn, pattern):
 
59
            return cls(**options)
 
60
    raise ClassNotFound("No formatter found for file name %r" % fn)
 
61
 
 
62
 
 
63
def get_all_formatters():
 
64
    """Return a generator for all formatters."""
 
65
    for formatter in FORMATTERS:
 
66
        yield formatter
 
67
    for _, formatter in find_plugin_formatters():
 
68
        yield formatter