Back to index

moin  1.9.0~rc2
Functions | Variables
MoinMoin.support.pygments.cmdline Namespace Reference

Functions

def _parse_options
def _parse_filters
def _print_help
def _print_list
def main

Variables

string USAGE

Function Documentation

Definition at line 105 of file cmdline.py.

00105 
00106 def _parse_filters(f_strs):
00107     filters = []
00108     if not f_strs:
00109         return filters
00110     for f_str in f_strs:
00111         if ':' in f_str:
00112             fname, fopts = f_str.split(':', 1)
00113             filters.append((fname, _parse_options([fopts])))
00114         else:
00115             filters.append((f_str, {}))
00116     return filters
00117 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 84 of file cmdline.py.

00084 
00085 def _parse_options(o_strs):
00086     opts = {}
00087     if not o_strs:
00088         return opts
00089     for o_str in o_strs:
00090         if not o_str:
00091             continue
00092         o_args = o_str.split(',')
00093         for o_arg in o_args:
00094             o_arg = o_arg.strip()
00095             try:
00096                 o_key, o_val = o_arg.split('=')
00097                 o_key = o_key.strip()
00098                 o_val = o_val.strip()
00099             except ValueError:
00100                 opts[o_arg] = True
00101             else:
00102                 opts[o_key] = o_val
00103     return opts
00104 

Here is the caller graph for this function:

def MoinMoin.support.pygments.cmdline._print_help (   what,
  name 
) [private]

Definition at line 118 of file cmdline.py.

00118 
00119 def _print_help(what, name):
00120     try:
00121         if what == 'lexer':
00122             cls = find_lexer_class(name)
00123             print "Help on the %s lexer:" % cls.name
00124             print dedent(cls.__doc__)
00125         elif what == 'formatter':
00126             cls = find_formatter_class(name)
00127             print "Help on the %s formatter:" % cls.name
00128             print dedent(cls.__doc__)
00129         elif what == 'filter':
00130             cls = find_filter_class(name)
00131             print "Help on the %s filter:" % name
00132             print dedent(cls.__doc__)
00133     except AttributeError:
00134         print >>sys.stderr, "%s not found!" % what
00135 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 136 of file cmdline.py.

00136 
00137 def _print_list(what):
00138     if what == 'lexer':
00139         print
00140         print "Lexers:"
00141         print "~~~~~~~"
00142 
00143         info = []
00144         for fullname, names, exts, _ in get_all_lexers():
00145             tup = (', '.join(names)+':', fullname,
00146                    exts and '(filenames ' + ', '.join(exts) + ')' or '')
00147             info.append(tup)
00148         info.sort()
00149         for i in info:
00150             print ('* %s\n    %s %s') % i
00151 
00152     elif what == 'formatter':
00153         print
00154         print "Formatters:"
00155         print "~~~~~~~~~~~"
00156 
00157         info = []
00158         for cls in get_all_formatters():
00159             doc = docstring_headline(cls)
00160             tup = (', '.join(cls.aliases) + ':', doc, cls.filenames and
00161                    '(filenames ' + ', '.join(cls.filenames) + ')' or '')
00162             info.append(tup)
00163         info.sort()
00164         for i in info:
00165             print ('* %s\n    %s %s') % i
00166 
00167     elif what == 'filter':
00168         print
00169         print "Filters:"
00170         print "~~~~~~~~"
00171 
00172         for name in get_all_filters():
00173             cls = find_filter_class(name)
00174             print "* " + name + ':'
00175             print "    %s" % docstring_headline(cls)
00176 
00177     elif what == 'style':
00178         print
00179         print "Styles:"
00180         print "~~~~~~~"
00181 
00182         for name in get_all_styles():
00183             cls = get_style_by_name(name)
00184             print "* " + name + ':'
00185             print "    %s" % docstring_headline(cls)
00186 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.cmdline.main (   args = sys.argv)
Main command line entry point.

Definition at line 187 of file cmdline.py.

00187 
00188 def main(args=sys.argv):
00189     """
00190     Main command line entry point.
00191     """
00192     # pylint: disable-msg=R0911,R0912,R0915
00193 
00194     usage = USAGE % ((args[0],) * 6)
00195 
00196     try:
00197         popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHg")
00198     except getopt.GetoptError, err:
00199         print >>sys.stderr, usage
00200         return 2
00201     opts = {}
00202     O_opts = []
00203     P_opts = []
00204     F_opts = []
00205     for opt, arg in popts:
00206         if opt == '-O':
00207             O_opts.append(arg)
00208         elif opt == '-P':
00209             P_opts.append(arg)
00210         elif opt == '-F':
00211             F_opts.append(arg)
00212         opts[opt] = arg
00213 
00214     if not opts and not args:
00215         print usage
00216         return 0
00217 
00218     if opts.pop('-h', None) is not None:
00219         print usage
00220         return 0
00221 
00222     if opts.pop('-V', None) is not None:
00223         print 'Pygments version %s, (c) 2006-2008 by Georg Brandl.' % __version__
00224         return 0
00225 
00226     # handle ``pygmentize -L``
00227     L_opt = opts.pop('-L', None)
00228     if L_opt is not None:
00229         if opts:
00230             print >>sys.stderr, usage
00231             return 2
00232 
00233         # print version
00234         main(['', '-V'])
00235         if not args:
00236             args = ['lexer', 'formatter', 'filter', 'style']
00237         for arg in args:
00238             _print_list(arg.rstrip('s'))
00239         return 0
00240 
00241     # handle ``pygmentize -H``
00242     H_opt = opts.pop('-H', None)
00243     if H_opt is not None:
00244         if opts or len(args) != 2:
00245             print >>sys.stderr, usage
00246             return 2
00247 
00248         what, name = args
00249         if what not in ('lexer', 'formatter', 'filter'):
00250             print >>sys.stderr, usage
00251             return 2
00252 
00253         _print_help(what, name)
00254         return 0
00255 
00256     # parse -O options
00257     parsed_opts = _parse_options(O_opts)
00258     opts.pop('-O', None)
00259 
00260     # parse -P options
00261     for p_opt in P_opts:
00262         try:
00263             name, value = p_opt.split('=', 1)
00264         except ValueError:
00265             parsed_opts[p_opt] = True
00266         else:
00267             parsed_opts[name] = value
00268     opts.pop('-P', None)
00269 
00270     # handle ``pygmentize -N``
00271     infn = opts.pop('-N', None)
00272     if infn is not None:
00273         try:
00274             lexer = get_lexer_for_filename(infn, **parsed_opts)
00275         except ClassNotFound, err:
00276             lexer = TextLexer()
00277         except OptionError, err:
00278             print >>sys.stderr, 'Error:', err
00279             return 1
00280 
00281         print lexer.aliases[0]
00282         return 0
00283 
00284     # handle ``pygmentize -S``
00285     S_opt = opts.pop('-S', None)
00286     a_opt = opts.pop('-a', None)
00287     if S_opt is not None:
00288         f_opt = opts.pop('-f', None)
00289         if not f_opt:
00290             print >>sys.stderr, usage
00291             return 2
00292         if opts or args:
00293             print >>sys.stderr, usage
00294             return 2
00295 
00296         try:
00297             parsed_opts['style'] = S_opt
00298             fmter = get_formatter_by_name(f_opt, **parsed_opts)
00299         except ClassNotFound, err:
00300             print >>sys.stderr, err
00301             return 1
00302 
00303         arg = a_opt or ''
00304         try:
00305             print fmter.get_style_defs(arg)
00306         except Exception, err:
00307             print >>sys.stderr, 'Error:', err
00308             return 1
00309         return 0
00310 
00311     # if no -S is given, -a is not allowed
00312     if a_opt is not None:
00313         print >>sys.stderr, usage
00314         return 2
00315 
00316     # parse -F options
00317     F_opts = _parse_filters(F_opts)
00318     opts.pop('-F', None)
00319 
00320     # select formatter
00321     outfn = opts.pop('-o', None)
00322     fmter = opts.pop('-f', None)
00323     if fmter:
00324         try:
00325             fmter = get_formatter_by_name(fmter, **parsed_opts)
00326         except (OptionError, ClassNotFound), err:
00327             print >>sys.stderr, 'Error:', err
00328             return 1
00329 
00330     if outfn:
00331         if not fmter:
00332             try:
00333                 fmter = get_formatter_for_filename(outfn, **parsed_opts)
00334             except (OptionError, ClassNotFound), err:
00335                 print >>sys.stderr, 'Error:', err
00336                 return 1
00337         try:
00338             outfile = open(outfn, 'wb')
00339         except Exception, err:
00340             print >>sys.stderr, 'Error: cannot open outfile:', err
00341             return 1
00342     else:
00343         if not fmter:
00344             fmter = TerminalFormatter(**parsed_opts)
00345         outfile = sys.stdout
00346 
00347     # select lexer
00348     lexer = opts.pop('-l', None)
00349     if lexer:
00350         try:
00351             lexer = get_lexer_by_name(lexer, **parsed_opts)
00352         except (OptionError, ClassNotFound), err:
00353             print >>sys.stderr, 'Error:', err
00354             return 1
00355 
00356     if args:
00357         if len(args) > 1:
00358             print >>sys.stderr, usage
00359             return 2
00360 
00361         infn = args[0]
00362         try:
00363             code = open(infn, 'rb').read()
00364         except Exception, err:
00365             print >>sys.stderr, 'Error: cannot read infile:', err
00366             return 1
00367 
00368         if not lexer:
00369             try:
00370                 lexer = get_lexer_for_filename(infn, code, **parsed_opts)
00371             except ClassNotFound, err:
00372                 if '-g' in opts:
00373                     try:
00374                         lexer = guess_lexer(code)
00375                     except ClassNotFound:
00376                         lexer = TextLexer()
00377                 else:
00378                     print >>sys.stderr, 'Error:', err
00379                     return 1
00380             except OptionError, err:
00381                 print >>sys.stderr, 'Error:', err
00382                 return 1
00383 
00384     else:
00385         if '-g' in opts:
00386             code = sys.stdin.read()
00387             try:
00388                 lexer = guess_lexer(code)
00389             except ClassNotFound:
00390                 lexer = TextLexer()
00391         elif not lexer:
00392             print >>sys.stderr, 'Error: no lexer name given and reading ' + \
00393                                 'from stdin (try using -g or -l <lexer>)'
00394             return 2
00395         else:
00396             code = sys.stdin.read()
00397 
00398     # No encoding given? Use latin1 if output file given,
00399     # stdin/stdout encoding otherwise.
00400     # (This is a compromise, I'm not too happy with it...)
00401     if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts:
00402         if outfn:
00403             # encoding pass-through
00404             fmter.encoding = 'latin1'
00405         else:
00406             if sys.version_info < (3,):
00407                 # use terminal encoding; Python 3's terminals already do that
00408                 lexer.encoding = getattr(sys.stdin, 'encoding',
00409                                          None) or 'ascii'
00410                 fmter.encoding = getattr(sys.stdout, 'encoding',
00411                                          None) or 'ascii'
00412 
00413     # ... and do it!
00414     try:
00415         # process filters
00416         for fname, fopts in F_opts:
00417             lexer.add_filter(fname, **fopts)
00418         highlight(code, lexer, fmter, outfile)
00419     except Exception, err:
00420         import traceback
00421         info = traceback.format_exception(*sys.exc_info())
00422         msg = info[-1].strip()
00423         if len(info) >= 3:
00424             # extract relevant file and position info
00425             msg += '\n   (f%s)' % info[-2].split('\n')[0].strip()[1:]
00426         print >>sys.stderr
00427         print >>sys.stderr, '*** Error while highlighting:'
00428         print >>sys.stderr, msg
00429         return 1
00430 
00431     return 0

Here is the call graph for this function:


Variable Documentation

Definition at line 26 of file cmdline.py.