Properly handle multiple file patterns.

This commit is contained in:
Eric Holscher 2015-07-07 16:19:25 -07:00
parent 136bb7c986
commit 6f6e09b1f6
5 changed files with 50 additions and 26 deletions

View File

@ -24,6 +24,14 @@ def run_autoapi(app):
print "You must configure an autodapi_dir setting." print "You must configure an autodapi_dir setting."
return 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 = [] app.env.autoapi_data = []
mapping = { mapping = {
@ -33,13 +41,25 @@ def run_autoapi(app):
'javascript': JavaScriptSphinxMapper, 'javascript': JavaScriptSphinxMapper,
} }
default_file_mapping = {
'python': ['*.py'],
'dotnet': ['project.json', '*.csproj'],
'go': ['*.go'],
'javascript': ['*.js'],
}
domain = mapping[app.config.autoapi_type] domain = mapping[app.config.autoapi_type]
domain_obj = domain(app, template_dir=app.config.autoapi_template_dir) 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')) app.info(bold('[AutoAPI] ') + darkgreen('Loading Data'))
domain_obj.load( domain_obj.load(
pattern=app.config.autoapi_file_pattern, patterns=file_patterns,
dir=os.path.normpath(app.config.autoapi_dir), dir=normalized_dir,
ignore=app.config.autoapi_ignore, ignore=app.config.autoapi_ignore,
) )
@ -48,7 +68,7 @@ def run_autoapi(app):
app.info(bold('[AutoAPI] ') + darkgreen('Rendering Data')) app.info(bold('[AutoAPI] ') + darkgreen('Rendering Data'))
domain_obj.output_rst( domain_obj.output_rst(
root=app.config.autoapi_root, root=normalized_root,
# TODO: Better way to determine suffix? # TODO: Better way to determine suffix?
source_suffix=app.config.source_suffix[0], source_suffix=app.config.source_suffix[0],
) )
@ -56,9 +76,10 @@ def run_autoapi(app):
def build_finished(app, exception): def build_finished(app, exception):
if not app.config.autoapi_keep_files: 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: if app.verbosity > 1:
app.info(bold('[AutoAPI] ') + darkgreen('Cleaning generated .rst files')) app.info(bold('[AutoAPI] ') + darkgreen('Cleaning generated .rst files'))
shutil.rmtree(app.config.autoapi_root) shutil.rmtree(normalized_root)
def doctree_read(app, doctree): 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_root', 'autoapi', 'html')
app.add_config_value('autoapi_ignore', ['*migrations*'], 'html') app.add_config_value('autoapi_ignore', ['*migrations*'], 'html')
app.add_config_value('autoapi_options', default_options, '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_dir', 'autoapi', 'html')
app.add_config_value('autoapi_keep_files', False, 'html') app.add_config_value('autoapi_keep_files', False, 'html')
app.add_config_value('autoapi_add_toctree_entry', True, 'html') app.add_config_value('autoapi_add_toctree_entry', True, 'html')

View File

@ -145,35 +145,36 @@ class SphinxMapperBase(object):
# Mapping of {namespace id -> Python Object} # Mapping of {namespace id -> Python Object}
self.top_level_objects = OrderedDict() 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. 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) data = self.read_file(path=path)
if data: if data:
self.paths[path] = data self.paths[path] = data
def find_files(self, pattern, dir, ignore): def find_files(self, patterns, dir, ignore):
files_to_read = [] files_to_read = []
for root, dirnames, filenames in os.walk(dir): for root, dirnames, filenames in os.walk(dir):
for filename in fnmatch.filter(filenames, pattern): for pattern in patterns:
skip = False for filename in fnmatch.filter(filenames, pattern):
skip = False
# Skip ignored files # Skip ignored files
for ignore_pattern in ignore: for ignore_pattern in ignore:
if fnmatch.fnmatch(filename, ignore_pattern): if fnmatch.fnmatch(filename, ignore_pattern):
self.app.info("Ignoring %s/%s" % (root, filename)) self.app.info("Ignoring %s/%s" % (root, filename))
skip = True skip = True
if skip: if skip:
continue continue
# Make sure the path is full # Make sure the path is full
if os.path.isabs(filename): if os.path.isabs(filename):
files_to_read.append(os.path.join(filename)) files_to_read.append(filename)
else: else:
files_to_read.append(os.path.join(root, filename)) files_to_read.append(os.path.join(root, filename))
for _path in self.app.status_iterator( for _path in self.app.status_iterator(
files_to_read, files_to_read,

View File

@ -20,15 +20,15 @@ class DotNetSphinxMapper(SphinxMapperBase):
top_namespaces = {} top_namespaces = {}
def load(self, pattern, dir, ignore=[]): def load(self, patterns, dir, ignore=[]):
''' '''
Load objects from the filesystem into the ``paths`` dictionary. 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]) subprocess.check_output(['BuildMeta', '/target:Build', path])
# We now have yaml files # 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) data = self.read_file(path=xdoc_path)
if data: if data:
self.paths[xdoc_path] = data self.paths[xdoc_path] = data

View File

@ -13,7 +13,7 @@ class GoSphinxMapper(SphinxMapperBase):
:param app: Sphinx application passed in as part of the extension :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. Load objects from the filesystem into the ``paths`` dictionary.

View File

@ -29,6 +29,8 @@ class PythonSphinxMapper(SphinxMapperBase):
self.app.warn('Error reading file: {0}'.format(path)) self.app.warn('Error reading file: {0}'.format(path))
except TypeError: except TypeError:
self.app.warn('Error reading file: {0}'.format(path)) self.app.warn('Error reading file: {0}'.format(path))
except ImportError:
self.app.warn('Error reading file: {0}'.format(path))
return None return None
def create_class(self, data, options=None, **kwargs): def create_class(self, data, options=None, **kwargs):