From 6f6e09b1f6320b26def6c0df706aa78cd04bce00 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Tue, 7 Jul 2015 16:19:25 -0700 Subject: [PATCH] Properly handle multiple file patterns. --- autoapi/extension.py | 31 ++++++++++++++++++++++++++----- autoapi/mappers/base.py | 35 ++++++++++++++++++----------------- autoapi/mappers/dotnet.py | 6 +++--- autoapi/mappers/go.py | 2 +- autoapi/mappers/python.py | 2 ++ 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/autoapi/extension.py b/autoapi/extension.py index b1b2fe1..e468fdb 100644 --- a/autoapi/extension.py +++ b/autoapi/extension.py @@ -24,6 +24,14 @@ def run_autoapi(app): print "You must configure an autodapi_dir setting." return + # Make sure the paths are full + if os.path.isabs(app.config.autoapi_dir): + normalized_dir = app.config.autoapi_dir + else: + normalized_dir = os.path.normpath(os.path.join(app.confdir, app.config.autoapi_dir)) + + normalized_root = os.path.normpath(os.path.join(app.confdir, app.config.autoapi_root)) + app.env.autoapi_data = [] mapping = { @@ -33,13 +41,25 @@ def run_autoapi(app): 'javascript': JavaScriptSphinxMapper, } + default_file_mapping = { + 'python': ['*.py'], + 'dotnet': ['project.json', '*.csproj'], + 'go': ['*.go'], + 'javascript': ['*.js'], + } + domain = mapping[app.config.autoapi_type] domain_obj = domain(app, template_dir=app.config.autoapi_template_dir) + if app.config.autoapi_file_pattern: + file_patterns = app.config.autoapi_file_pattern + else: + file_patterns = default_file_mapping[app.config.autoapi_type] + app.info(bold('[AutoAPI] ') + darkgreen('Loading Data')) domain_obj.load( - pattern=app.config.autoapi_file_pattern, - dir=os.path.normpath(app.config.autoapi_dir), + patterns=file_patterns, + dir=normalized_dir, ignore=app.config.autoapi_ignore, ) @@ -48,7 +68,7 @@ def run_autoapi(app): app.info(bold('[AutoAPI] ') + darkgreen('Rendering Data')) domain_obj.output_rst( - root=app.config.autoapi_root, + root=normalized_root, # TODO: Better way to determine suffix? source_suffix=app.config.source_suffix[0], ) @@ -56,9 +76,10 @@ def run_autoapi(app): def build_finished(app, exception): if not app.config.autoapi_keep_files: + normalized_root = os.path.normpath(os.path.join(app.confdir, app.config.autoapi_root)) if app.verbosity > 1: app.info(bold('[AutoAPI] ') + darkgreen('Cleaning generated .rst files')) - shutil.rmtree(app.config.autoapi_root) + shutil.rmtree(normalized_root) def doctree_read(app, doctree): @@ -90,7 +111,7 @@ def setup(app): app.add_config_value('autoapi_root', 'autoapi', 'html') app.add_config_value('autoapi_ignore', ['*migrations*'], 'html') app.add_config_value('autoapi_options', default_options, 'html') - app.add_config_value('autoapi_file_pattern', '*', 'html') + app.add_config_value('autoapi_file_pattern', None, 'html') app.add_config_value('autoapi_dir', 'autoapi', 'html') app.add_config_value('autoapi_keep_files', False, 'html') app.add_config_value('autoapi_add_toctree_entry', True, 'html') diff --git a/autoapi/mappers/base.py b/autoapi/mappers/base.py index cac7657..3110886 100644 --- a/autoapi/mappers/base.py +++ b/autoapi/mappers/base.py @@ -145,35 +145,36 @@ class SphinxMapperBase(object): # Mapping of {namespace id -> Python Object} self.top_level_objects = OrderedDict() - def load(self, pattern, dir, ignore=[]): + def load(self, patterns, dir, ignore=[]): ''' Load objects from the filesystem into the ``paths`` dictionary. ''' - for path in self.find_files(pattern=pattern, dir=dir, ignore=ignore): + for path in self.find_files(patterns=patterns, dir=dir, ignore=ignore): data = self.read_file(path=path) if data: self.paths[path] = data - def find_files(self, pattern, dir, ignore): + def find_files(self, patterns, dir, ignore): files_to_read = [] for root, dirnames, filenames in os.walk(dir): - for filename in fnmatch.filter(filenames, pattern): - skip = False + for pattern in patterns: + for filename in fnmatch.filter(filenames, pattern): + skip = False - # Skip ignored files - for ignore_pattern in ignore: - if fnmatch.fnmatch(filename, ignore_pattern): - self.app.info("Ignoring %s/%s" % (root, filename)) - skip = True + # Skip ignored files + for ignore_pattern in ignore: + if fnmatch.fnmatch(filename, ignore_pattern): + self.app.info("Ignoring %s/%s" % (root, filename)) + skip = True - if skip: - continue - # Make sure the path is full - if os.path.isabs(filename): - files_to_read.append(os.path.join(filename)) - else: - files_to_read.append(os.path.join(root, filename)) + if skip: + continue + # Make sure the path is full + if os.path.isabs(filename): + files_to_read.append(filename) + else: + files_to_read.append(os.path.join(root, filename)) for _path in self.app.status_iterator( files_to_read, diff --git a/autoapi/mappers/dotnet.py b/autoapi/mappers/dotnet.py index 53a76a7..f74f600 100644 --- a/autoapi/mappers/dotnet.py +++ b/autoapi/mappers/dotnet.py @@ -20,15 +20,15 @@ class DotNetSphinxMapper(SphinxMapperBase): top_namespaces = {} - def load(self, pattern, dir, ignore=[]): + def load(self, patterns, dir, ignore=[]): ''' Load objects from the filesystem into the ``paths`` dictionary. ''' - for path in self.find_files(pattern='project.json', dir=dir, ignore=ignore): + for path in self.find_files(patterns=['project.json'], dir=dir, ignore=ignore): subprocess.check_output(['BuildMeta', '/target:Build', path]) # We now have yaml files - for xdoc_path in self.find_files(pattern='*.yml', dir='xdoc', ignore=ignore): + for xdoc_path in self.find_files(patterns=['*.yml'], dir='xdoc', ignore=ignore): data = self.read_file(path=xdoc_path) if data: self.paths[xdoc_path] = data diff --git a/autoapi/mappers/go.py b/autoapi/mappers/go.py index 4294619..c28fb30 100644 --- a/autoapi/mappers/go.py +++ b/autoapi/mappers/go.py @@ -13,7 +13,7 @@ class GoSphinxMapper(SphinxMapperBase): :param app: Sphinx application passed in as part of the extension ''' - def load(self, pattern, dir, ignore=[]): + def load(self, patterns, dir, ignore=[]): ''' Load objects from the filesystem into the ``paths`` dictionary. diff --git a/autoapi/mappers/python.py b/autoapi/mappers/python.py index 218bb02..7e7631f 100644 --- a/autoapi/mappers/python.py +++ b/autoapi/mappers/python.py @@ -29,6 +29,8 @@ class PythonSphinxMapper(SphinxMapperBase): self.app.warn('Error reading file: {0}'.format(path)) except TypeError: self.app.warn('Error reading file: {0}'.format(path)) + except ImportError: + self.app.warn('Error reading file: {0}'.format(path)) return None def create_class(self, data, options=None, **kwargs):