kivy/doc/autobuild.py

287 lines
7.0 KiB
Python
Raw Normal View History

2010-11-03 21:05:03 +00:00
'''
Script to generate Kivy API from source code.
Code is messy, but working.
Be careful if you change anything in !
'''
ignore_list = (
2016-07-11 19:48:30 +00:00
'kivy._clock',
2012-03-25 11:20:45 +00:00
'kivy._event',
'kivy.factory_registers',
'kivy.graphics.buffer',
'kivy.graphics.vbo',
'kivy.graphics.vertex',
'kivy.uix.recycleview.__init__',
2017-03-01 21:51:59 +00:00
'kivy.setupconfig',
'kivy.version',
'kivy._version'
)
2010-11-03 21:05:03 +00:00
import os
import sys
from glob import glob
import kivy
# force loading of kivy modules
import kivy.app
2012-11-02 12:38:39 +00:00
import kivy.metrics
import kivy.atlas
import kivy.context
import kivy.core.audio
import kivy.core.camera
import kivy.core.clipboard
import kivy.core.gl
import kivy.core.image
import kivy.core.spelling
import kivy.core.text
import kivy.core.text.markup
import kivy.core.video
import kivy.core.window
import kivy.geometry
2010-11-03 21:05:03 +00:00
import kivy.graphics
2011-06-15 11:22:59 +00:00
import kivy.graphics.shader
import kivy.graphics.tesselator
2011-01-04 11:44:00 +00:00
import kivy.animation
import kivy.modules.console
import kivy.modules.keybinding
import kivy.modules.monitor
import kivy.modules.touchring
import kivy.modules.inspector
import kivy.modules.recorder
import kivy.modules.screen
2017-04-23 11:43:03 +00:00
import kivy.modules.joycursor
2013-08-26 19:49:51 +00:00
import kivy.storage
import kivy.storage.dictstore
import kivy.storage.jsonstore
import kivy.storage.redisstore
import kivy.network.urlrequest
import kivy.modules.webdebugger
import kivy.support
try:
import kivy.tools.packaging.pyinstaller_hooks
except ImportError:
pass
import kivy.input.recorder
import kivy.interactive
2013-05-02 20:44:03 +00:00
import kivy.garden
from kivy.factory import Factory
2018-10-06 14:47:06 +00:00
from kivy.lib import ddsfile, mtdev
# check for silenced build
2016-08-28 03:19:03 +00:00
BE_QUIET = True
if os.environ.get('BE_QUIET') == 'False':
BE_QUIET = False
# force loading of all classes from factory
for x in list(Factory.classes.keys())[:]:
getattr(Factory, x)
2010-11-03 21:05:03 +00:00
# Directory of doc
base_dir = os.path.dirname(__file__)
dest_dir = os.path.join(base_dir, 'sources')
examples_framework_dir = os.path.join(base_dir, '..', 'examples', 'framework')
2016-08-28 03:19:03 +00:00
# Check touch file
base = 'autobuild.py-done'
with open(os.path.join(base_dir, base), 'w') as f:
f.write('')
2014-02-03 16:34:06 +00:00
2010-11-03 21:05:03 +00:00
def writefile(filename, data):
global dest_dir
# avoid to rewrite the file if the content didn't change
2010-11-03 21:05:03 +00:00
f = os.path.join(dest_dir, filename)
2015-12-27 15:27:58 +00:00
if not BE_QUIET:
print('write', filename)
if os.path.exists(f):
with open(f) as fd:
if fd.read() == data:
return
2010-11-03 21:05:03 +00:00
h = open(f, 'w')
h.write(data)
h.close()
# Activate Kivy modules
2010-11-11 13:18:14 +00:00
'''
2010-11-03 21:05:03 +00:00
for k in kivy.kivy_modules.list().keys():
kivy.kivy_modules.import_module(k)
2010-11-11 13:18:14 +00:00
'''
2010-11-03 21:05:03 +00:00
2014-02-03 16:34:06 +00:00
2010-11-03 21:05:03 +00:00
# Search all kivy module
2014-02-03 16:34:06 +00:00
l = [(x, sys.modules[x],
os.path.basename(sys.modules[x].__file__).rsplit('.', 1)[0])
for x in sys.modules if x.startswith('kivy') and sys.modules[x]]
2010-11-03 21:05:03 +00:00
# Extract packages from modules
packages = []
modules = {}
api_modules = []
2010-11-03 21:05:03 +00:00
for name, module, filename in l:
if name in ignore_list:
continue
if not any([name.startswith(x) for x in ignore_list]):
api_modules.append(name)
2010-11-03 21:05:03 +00:00
if filename == '__init__':
packages.append(name)
else:
if hasattr(module, '__all__'):
modules[name] = module.__all__
else:
modules[name] = [x for x in dir(module) if not x.startswith('__')]
packages.sort()
# Create index
2014-02-03 16:34:06 +00:00
api_index = '''API Reference
-------------
The API reference is a lexicographic list of all the different classes,
methods and features that Kivy offers.
2010-11-03 21:05:03 +00:00
.. toctree::
:maxdepth: 1
2010-11-03 21:05:03 +00:00
'''
api_modules.sort()
for package in api_modules:
2010-11-03 21:05:03 +00:00
api_index += " api-%s.rst\n" % package
writefile('api-index.rst', api_index)
2014-02-03 16:34:06 +00:00
# Create index for all packages
# Note on displaying inherited members;
# Adding the directive ':inherited-members:' to automodule achieves this
# but is not always desired. Please see
# https://github.com/kivy/kivy/pull/3870
2014-02-03 16:34:06 +00:00
template = '\n'.join((
'=' * 100,
'$SUMMARY',
'=' * 100,
'''
2010-11-03 21:05:03 +00:00
$EXAMPLES_REF
.. automodule:: $PACKAGE
:members:
:show-inheritance:
.. toctree::
$EXAMPLES
2014-02-03 16:34:06 +00:00
'''))
2010-11-03 21:05:03 +00:00
2014-02-03 16:34:06 +00:00
template_examples = '''.. _example-reference%d:
2010-11-03 21:05:03 +00:00
Examples
--------
%s
'''
2014-02-03 16:34:06 +00:00
template_examples_ref = ('# :ref:`Jump directly to Examples'
' <example-reference%d>`')
2010-11-03 21:05:03 +00:00
def extract_summary_line(doc):
"""
2015-12-27 15:27:58 +00:00
:param doc: the __doc__ field of a module
:return: a doc string suitable for a header or empty string
"""
if doc is None:
return ''
for line in doc.split('\n'):
line = line.strip()
# don't take empty line
if len(line) < 1:
continue
# ref mark
if line.startswith('.. _'):
continue
return line
2010-11-03 21:05:03 +00:00
for package in packages:
summary = extract_summary_line(sys.modules[package].__doc__)
if summary is None or summary == '':
2010-11-03 21:05:03 +00:00
summary = 'NO DOCUMENTATION (package %s)' % package
t = template.replace('$SUMMARY', summary)
t = t.replace('$PACKAGE', package)
t = t.replace('$EXAMPLES_REF', '')
2010-12-16 23:02:30 +00:00
t = t.replace('$EXAMPLES', '')
2010-11-03 21:05:03 +00:00
# search packages
for subpackage in packages:
packagemodule = subpackage.rsplit('.', 1)[0]
if packagemodule != package or len(subpackage.split('.')) <= 2:
continue
t += " api-%s.rst\n" % subpackage
# search modules
m = list(modules.keys())
2016-05-07 22:36:52 +00:00
m.sort(key=lambda x: extract_summary_line(sys.modules[x].__doc__).upper())
2010-11-03 21:05:03 +00:00
for module in m:
packagemodule = module.rsplit('.', 1)[0]
if packagemodule != package:
continue
t += " api-%s.rst\n" % module
writefile('api-%s.rst' % package, t)
# Create index for all module
m = list(modules.keys())
2010-11-03 21:05:03 +00:00
m.sort()
refid = 0
for module in m:
summary = extract_summary_line(sys.modules[module].__doc__)
if summary is None or summary == '':
summary = 'NO DOCUMENTATION (module %s)' % module
2010-11-03 21:05:03 +00:00
# search examples
example_output = []
example_prefix = module
if module.startswith('kivy.'):
example_prefix = module[5:]
example_prefix = example_prefix.replace('.', '_')
# try to found any example in framework directory
2014-02-03 16:34:06 +00:00
list_examples = glob('%s*.py' % os.path.join(
examples_framework_dir, example_prefix))
2010-11-03 21:05:03 +00:00
for x in list_examples:
# extract filename without directory
xb = os.path.basename(x)
# add a section !
example_output.append('File :download:`%s <%s>` ::' % (
xb, os.path.join('..', x)))
# put the file in
with open(x, 'r') as fd:
d = fd.read().strip()
d = '\t' + '\n\t'.join(d.split('\n'))
example_output.append(d)
t = template.replace('$SUMMARY', summary)
t = t.replace('$PACKAGE', module)
if len(example_output):
refid += 1
2014-02-03 16:34:06 +00:00
example_output = template_examples % (
refid, '\n\n\n'.join(example_output))
2010-11-03 21:05:03 +00:00
t = t.replace('$EXAMPLES_REF', template_examples_ref % refid)
t = t.replace('$EXAMPLES', example_output)
else:
t = t.replace('$EXAMPLES_REF', '')
t = t.replace('$EXAMPLES', '')
writefile('api-%s.rst' % module, t)
# Generation finished
print('Auto-generation finished')