From 7b625264c929b10b38e878118d5ed1386edd2dd2 2014-12-11 23:18:28 From: Min RK Date: 2014-12-11 23:18:28 Subject: [PATCH] Merge pull request #7181 from jhamrick/extension-traitlet Add extension traitlet to force dotted names --- diff --git a/IPython/nbconvert/exporters/__init__.py b/IPython/nbconvert/exporters/__init__.py index 5518850..c3fee7c 100644 --- a/IPython/nbconvert/exporters/__init__.py +++ b/IPython/nbconvert/exporters/__init__.py @@ -8,4 +8,4 @@ from .notebook import NotebookExporter from .pdf import PDFExporter from .python import PythonExporter from .rst import RSTExporter -from .exporter import Exporter +from .exporter import Exporter, FilenameExtension diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 33991db..38d9ce5 100644 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -14,7 +14,7 @@ import datetime from IPython.config.configurable import LoggingConfigurable from IPython.config import Config from IPython import nbformat -from IPython.utils.traitlets import MetaHasTraits, Unicode, List +from IPython.utils.traitlets import MetaHasTraits, Unicode, List, TraitError from IPython.utils.importstring import import_item from IPython.utils import text, py3compat @@ -24,6 +24,24 @@ class ResourcesDict(collections.defaultdict): return '' +class FilenameExtension(Unicode): + """A trait for filename extensions.""" + + default_value = u'' + info_text = 'a filename extension, beginning with a dot' + + def validate(self, obj, value): + # cast to proper unicode + value = super(FilenameExtension, self).validate(obj, value) + + # check that it starts with a dot + if value and not value.startswith('.'): + msg = "FileExtension trait '{}' does not begin with a dot: {!r}" + raise TraitError(msg.format(self.name, value)) + + return value + + class Exporter(LoggingConfigurable): """ Class containing methods that sequentially run a list of preprocessors on a @@ -31,7 +49,7 @@ class Exporter(LoggingConfigurable): accompanying resources dict. """ - file_extension = Unicode( + file_extension = FilenameExtension( '.txt', config=True, help="Extension of the file that should be written to disk" )