Refactor some settings and jinja env.

pull/8/head
Eric Holscher 9 years ago
parent bd92f55fc7
commit 1d3bd2012e

@ -3,9 +3,11 @@ import yaml
import json
import fnmatch
from jinja2 import Environment, FileSystemLoader
from sphinx.util.console import darkgreen
from ..settings import env
from ..settings import TEMPLATE_DIR
class AutoAPIBase(object):
@ -16,10 +18,20 @@ class AutoAPIBase(object):
def __init__(self, obj):
self.obj = obj
TEMPLATE_PATHS = [TEMPLATE_DIR]
USER_TEMPLATE_DIR = self.get_config('autoapi_template_dir')
if USER_TEMPLATE_DIR:
# Put at the front so it's loaded first
TEMPLATE_PATHS.insert(0, USER_TEMPLATE_DIR)
self.jinja_env = Environment(
loader=FileSystemLoader(TEMPLATE_PATHS)
)
def render(self, ctx=None):
if not ctx:
ctx = {}
template = env.get_template(
template = self.jinja_env.get_template(
'{language}/{type}.rst'.format(language=self.language, type=self.type)
)
ctx.update(**self.get_context_data())
@ -42,12 +54,9 @@ class AutoAPIBase(object):
return self.id < other.id
return super(AutoAPIBase, self).__lt__(other)
class UnknownType(AutoAPIBase):
def render(self, ctx=None):
print "Unknown Type: %s" % (self.obj['type'])
super(UnknownType, self).render(ctx=ctx)
def __str__(self):
return '<{cls} {id}>'.format(cls=self.__class__.__name__,
id=self.id)
class AutoAPIDomain(object):
@ -94,9 +103,9 @@ class AutoAPIDomain(object):
self.app.env.autoapi_data.append(obj)
self.objects[obj.name] = obj
def get_config(self, key):
def get_config(self, key, default=None):
if self.app.config is not None:
return getattr(self.app.config, key, None)
return getattr(self.app.config, key, default)
def find_files(self, pattern='*.yaml'):
'''Find YAML/JSON files to parse for namespace information'''
@ -139,3 +148,11 @@ class AutoAPIDomain(object):
:param obj: Instance of a AutoAPI object
'''
raise NotImplementedError
def write_indexes(self):
# Write Index
top_level_index = os.path.join(self.get_config('autoapi_root'),
'index.rst')
with open(top_level_index, 'w+') as top_level_file:
content = self.jinja_env.get_template('index.rst')
top_level_file.write(content.render())

@ -172,7 +172,7 @@ class DotNetDomain(AutoAPIDomain):
top_level_index = os.path.join(self.get_config('autoapi_root'),
'index.rst')
with open(top_level_index, 'w+') as top_level_file:
content = env.get_template('index.rst')
content = self.jinja_env.get_template('index.rst')
top_level_file.write(content.render(pages=self.namespaces.values()))

@ -49,11 +49,11 @@ class GoDomain(AutoAPIDomain):
'''
obj_map = dict(
(cls.type, cls) for cls
in [GoConstant, GoFunction, GoPackage, GoVariable, GoType, GoMethod]
in ALL_CLASSES
)
try:
cls = obj_map[data['type']]
except KeyError as e:
except KeyError:
self.app.warn('Unknown Type: %s' % data)
else:
if cls.inverted_names and 'names' in data:
@ -100,14 +100,6 @@ class GoDomain(AutoAPIDomain):
with open(path, 'w+') as detail_file:
detail_file.write(rst.encode('utf-8'))
def write_indexes(self):
# Write Index
top_level_index = os.path.join(self.get_config('autoapi_root'),
'index.rst')
with open(top_level_index, 'w+') as top_level_file:
content = env.get_template('index.rst')
top_level_file.write(content.render())
class GoBase(AutoAPIBase):
@ -161,12 +153,6 @@ class GoBase(AutoAPIBase):
def methods(self):
return self.obj.get('methods', [])
def __lt__(self, other):
'''Sort object by name'''
if isinstance(other, GoBase):
return self.name.lower() < other.name.lower()
return self.name < other
class GoVariable(GoBase):
type = 'var'
@ -195,3 +181,13 @@ class GoPackage(GoBase):
class GoType(GoBase):
type = 'type'
ALL_CLASSES = [
GoConstant,
GoFunction,
GoPackage,
GoVariable,
GoType,
GoMethod,
]

@ -101,14 +101,6 @@ class JavaScriptDomain(AutoAPIDomain):
with open(path, 'w+') as detail_file:
detail_file.write(rst.encode('utf-8'))
def write_indexes(self):
# Write Index
top_level_index = os.path.join(self.get_config('autoapi_root'),
'index.rst')
with open(top_level_index, 'w+') as top_level_file:
content = env.get_template('index.rst')
top_level_file.write(content.render())
class JavaScriptBase(AutoAPIBase):
@ -134,9 +126,6 @@ class JavaScriptBase(AutoAPIBase):
# Language Specific
pass
def __str__(self):
return '<{cls} {id}>'.format(cls=self.__class__.__name__,
id=self.id)
@property
def short_name(self):
@ -161,12 +150,6 @@ class JavaScriptBase(AutoAPIBase):
def methods(self):
return self.obj.get('methods', [])
def __lt__(self, other):
'''Sort object by name'''
if isinstance(other, JavaScriptBase):
return self.name.lower() < other.name.lower()
return self.name < other
class JavaScriptClass(JavaScriptBase):
type = 'class'

@ -93,7 +93,7 @@ class PythonDomain(AutoAPIDomain):
ns_obj = self.app.env.autoapi_data[n]
if ns_obj is None:
ns_obj = list(self.create_class({'id': namespace,
'type': 'module'}))[0]
'type': 'module'}))[0]
self.app.env.autoapi_data.append(ns_obj)
self.namespaces[ns_obj.id] = ns_obj
if obj.id not in (child.id for child in ns_obj.children):
@ -131,14 +131,6 @@ class PythonDomain(AutoAPIDomain):
with open(path, 'w+') as detail_file:
detail_file.write(rst)
def write_indexes(self):
# Write Index
top_level_index = os.path.join(self.get_config('autoapi_root'),
'index.rst')
with open(top_level_index, 'w+') as top_level_file:
content = env.get_template('index.rst')
top_level_file.write(content.render())
class PythonBase(AutoAPIBase):

@ -1,9 +1,4 @@
import os
from jinja2 import Environment, FileSystemLoader
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIR = os.path.join(SITE_ROOT, 'templates')
env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))

@ -19,6 +19,12 @@ AutoAPI Configuration
Directory to output the AutoAPI files into
.. confval:: autoapi_template_dir
Default: ``''``
A directory that has user-defined templates to override our default templates.
.. confval:: autoapi_ignore
Default: ``[]``

Loading…
Cancel
Save