~ubuntu-branches/ubuntu/quantal/wxwidgets2.8/quantal

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/extern/pygments/lexers/__init__.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
    Pygments lexers.
7
7
 
8
 
    :copyright: 2006-2007 by Georg Brandl.
9
 
    :license: BSD, see LICENSE for more details.
 
8
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
 
9
    :license: BSD, see LICENSE for details.
10
10
"""
 
11
 
 
12
import sys
 
13
import types
11
14
import fnmatch
12
 
import types
13
15
from os.path import basename
14
16
 
15
 
try:
16
 
    set
17
 
except NameError:
18
 
    from sets import Set as set
19
 
 
20
17
from pygments.lexers._mapping import LEXERS
21
18
from pygments.plugin import find_plugin_lexers
22
 
from pygments.util import ClassNotFound
 
19
from pygments.util import ClassNotFound, bytes
23
20
 
24
21
 
25
22
__all__ = ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
83
80
    raise ClassNotFound('no lexer for alias %r found' % _alias)
84
81
 
85
82
 
86
 
def get_lexer_for_filename(_fn, **options):
87
 
    """
88
 
    Get a lexer for a filename.
89
 
    """
 
83
def get_lexer_for_filename(_fn, code=None, **options):
 
84
    """
 
85
    Get a lexer for a filename.  If multiple lexers match the filename
 
86
    pattern, use ``analyze_text()`` to figure out which one is more
 
87
    appropriate.
 
88
    """
 
89
    matches = []
90
90
    fn = basename(_fn)
91
91
    for modname, name, _, filenames, _ in LEXERS.itervalues():
92
92
        for filename in filenames:
93
93
            if fnmatch.fnmatch(fn, filename):
94
94
                if name not in _lexer_cache:
95
95
                    _load_lexers(modname)
96
 
                return _lexer_cache[name](**options)
 
96
                matches.append(_lexer_cache[name])
97
97
    for cls in find_plugin_lexers():
98
98
        for filename in cls.filenames:
99
99
            if fnmatch.fnmatch(fn, filename):
100
 
                return cls(**options)
 
100
                matches.append(cls)
 
101
 
 
102
    if sys.version_info > (3,) and isinstance(code, bytes):
 
103
        # decode it, since all analyse_text functions expect unicode
 
104
        code = code.decode('latin1')
 
105
 
 
106
    def get_rating(cls):
 
107
        # The class _always_ defines analyse_text because it's included in
 
108
        # the Lexer class.  The default implementation returns None which
 
109
        # gets turned into 0.0.  Run scripts/detect_missing_analyse_text.py
 
110
        # to find lexers which need it overridden.
 
111
        d = cls.analyse_text(code)
 
112
        #print "Got %r from %r" % (d, cls)
 
113
        return d
 
114
 
 
115
    if code:
 
116
        matches.sort(key=get_rating)
 
117
    if matches:
 
118
        #print "Possible lexers, after sort:", matches
 
119
        return matches[-1](**options)
101
120
    raise ClassNotFound('no lexer for filename %r found' % _fn)
102
121
 
103
122
 
200
219
        raise AttributeError(name)
201
220
 
202
221
 
203
 
import sys
204
222
oldmod = sys.modules['pygments.lexers']
205
223
newmod = _automodule('pygments.lexers')
206
224
newmod.__dict__.update(oldmod.__dict__)