summaryrefslogtreecommitdiff
path: root/pygments/formatters/__init__.py
diff options
context:
space:
mode:
authorTanner Prynn <tanner.prynn@nccgroup.trust>2016-02-24 17:46:32 -0600
committerTanner Prynn <tanner.prynn@nccgroup.trust>2016-02-24 17:46:32 -0600
commit1dcbeab604b4f6b9402a05895ceef1ce31177339 (patch)
tree6008ecde4c53170843898ae2f98cdd392ba2915c /pygments/formatters/__init__.py
parentcdec8b0e72a2716581d9e44174b754e2cb1b5c16 (diff)
downloadpygments-1dcbeab604b4f6b9402a05895ceef1ce31177339.tar.gz
Update pull request per comments by birkenfeld.
Add optional function parameter for the class name to instantiate, and update cli to support this. Move error handling to within the loading functions; they now only raise ClassNotFound. Modify doc with these updates and the version number. Test case clean up and additions.
Diffstat (limited to 'pygments/formatters/__init__.py')
-rw-r--r--pygments/formatters/__init__.py34
1 files changed, 24 insertions, 10 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py
index 369fb891..9034e435 100644
--- a/pygments/formatters/__init__.py
+++ b/pygments/formatters/__init__.py
@@ -80,24 +80,38 @@ def get_formatter_by_name(_alias, **options):
return cls(**options)
-def load_formatter_from_file(filename, **options):
+def load_formatter_from_file(filename, formattername="CustomFormatter",
+ **options):
"""Load a formatter from a file.
This method expects a file located relative to the current working
- directory, which contains a class named CustomFormatter.
+ directory, which contains a class named CustomFormatter. By default,
+ it expects the Formatter to be named CustomFormatter; you can specify
+ your own class name as the second argument to this function.
Users should be very careful with the input, because this method
is equivalent to running eval on the input file.
- Raises IOError if the file is not found/unreadable
- Raises ImportError if the file doesn't have a CustomFormatter class
- Raises whatever errors could happen when we eval(file)
+ Raises ClassNotFound if there are any problems importing the Formatter
"""
- # Load file as if calling import _ as customformatter
- load_source('customformatter', filename)
- # Instantiate the CustomFormatter from that file
- from customformatter import CustomFormatter
- return CustomFormatter(**options)
+ try:
+ # Load file as if calling import
+ customformatter = load_source('pygments.formatters.Custom_%s' %
+ formattername, filename)
+
+ if not hasattr(customformatter, formattername):
+ raise ClassNotFound('no valid %s class found in %s' %
+ (formattername, filename))
+
+ # Instantiate the CustomLexer from that file
+ formatter_class = getattr(customformatter, formattername)
+ return formatter_class(**options)
+ except IOError as err:
+ raise ClassNotFound('cannot read %s' % filename)
+ except ClassNotFound as err:
+ raise err
+ except Exception as err:
+ raise ClassNotFound('error when loading custom formatter: %s' % err)
def get_formatter_for_filename(fn, **options):