Import pygments only when needed
Sun Oct 5 15:53:16 UTC 2008 Alberto Bertogli <albertito@gmail.com>
* Import pygments only when needed
Otherwise, we pay for the pygments import (which is noticeable) on every
darcsweb page view.
This follows the lazy module loading that is already being done for other
costly modules (i.e. 're').
diff -rN -u old-darcsweb/darcsweb.cgi new-darcsweb/darcsweb.cgi
--- old-darcsweb/darcsweb.cgi 2015-10-20 03:59:27.000000000 +0000
+++ new-darcsweb/darcsweb.cgi 2015-10-20 03:59:27.000000000 +0000
@@ -19,13 +19,6 @@
import urllib
import xml.sax
from xml.sax.saxutils import escape as xml_escape
-try:
- import pygments
- import pygments.lexers
- import pygments.formatters
-except ImportError:
- pygments = False
-
time_imports = time.time() - time_begin
iso_datetime = '%Y-%m-%dT%H:%M:%SZ'
@@ -1277,6 +1270,11 @@
"""
return
+ try:
+ import pygments
+ except ImportError:
+ pygments = False
+
if not pygments:
print_blob_simple(fname)
return
@@ -1315,6 +1313,10 @@
print '</div>'
def print_blob_highlighted(fname, sample_code=False):
+ import pygments
+ import pygments.lexers
+ import pygments.formatters
+
code = open(realpath(fname), 'r').read()
if sample_code:
lexer = pygments.lexers.guess_lexer(code[:200],