File indexing completed on 2025-01-05 04:00:27

0001 import sys
0002 import os
0003 import pkgutil
0004 import importlib
0005 import inspect
0006 import re
0007 import collections
0008 sys.path.insert(0, os.path.join(
0009     os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
0010     "lib"
0011 ))
0012 import lottie.objects
0013 from lottie.objects.base import LottieEnum, LottieObject, PseudoList, LottieBase
0014 
0015 
0016 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
0017 doxpath = os.path.join(root, "docs", "dox")
0018 dox_classdoc = os.path.join(doxpath, "lottie_class.dox")
0019 dox_summary = os.path.join(doxpath, "lottie.dox")
0020 
0021 
0022 type_modules = {}
0023 modules = []
0024 Module = collections.namedtuple("Module", "name fullname module classes")
0025 
0026 
0027 def extract_type_classdoc(name):
0028     return "\\ref {1}::{0} \"{0}\"".format(name, type_modules[name])
0029 
0030 
0031 def extract_type_summary(name):
0032     return "<a href='#lottie_{0}'>{0}</a>".format(name)
0033 
0034 
0035 def extract_type(prop, linkmode):
0036     name = prop.type.__name__
0037 
0038     if name in type_modules:
0039         name = linkmode(name)
0040 
0041     if linkmode is extract_type_summary and name == "NVector":
0042         name = "list of float"
0043 
0044     if prop.list is True:
0045         name = "list of %s" % name
0046     elif prop.list is PseudoList:
0047         name = "{0} or list of {0}".format(name)
0048     return name
0049 
0050 
0051 def class_summary(cls):
0052     #out_summary.write(
0053         #"""\n\\section lottie_{0} {0}\nPython class: \\ref {1}::{0} "{0}"\n\n\\copybrief {1}::{0}\n\n"""
0054         #.format(clsname, type_modules[clsname])
0055     #)
0056     clsname = cls.__name__
0057     out_summary.write(
0058         r"""
0059 <h2><a name='lottie_{0}'></a><a href='#lottie_{0}'>{0}</a></h2>
0060 \par
0061 Python class: \ref {1}::{0} "{0}"
0062 \par
0063 {2}
0064 \par
0065         """
0066         .format(clsname, type_modules[clsname], (inspect.getdoc(cls) or "").lstrip("!").lstrip().replace("@brief", ""))
0067     )
0068 
0069 
0070 for _, modname, _ in pkgutil.iter_modules(lottie.objects.__path__):
0071     if modname == "base":
0072         continue
0073 
0074     full_modname = "lottie.objects." + modname
0075     module = importlib.import_module(full_modname)
0076 
0077     classes = []
0078 
0079     for clsname, cls in inspect.getmembers(module):
0080         if inspect.isclass(cls):
0081             if issubclass(cls, LottieBase) and cls not in {LottieObject, LottieEnum} and cls.__module__ == full_modname:
0082                 type_modules[clsname] = full_modname.replace(".", "::")
0083                 classes.append(cls)
0084 
0085     modules.append(Module(name=modname, fullname=full_modname, module=module, classes=classes))
0086 
0087 
0088 def proptable(out, cls, module, mode):
0089     out.write("Lottie name|Type|Description|Attribute\n")
0090     out.write("-----------|----|-----------|---------\n")
0091     for prop in cls._props:
0092         fqn = "%s::%s::%s" % (module.fullname.replace(".", "::"), cls.__name__, prop.name)
0093         type = extract_type(prop, mode)
0094         if hasattr(cls, prop.name):
0095             t = getattr(cls, prop.name)
0096             if isinstance(t, prop.type):
0097                 type += " = %r" % t
0098         extra = ""
0099         if prop.lottie == "ef" and hasattr(cls, "_effects"):
0100             extra = "[%s]" % ", ".join(
0101                 "<a href='#lottie_{0}'>{1}</a>".format(efcls.__name__, name)
0102                 if mode is extract_type_summary
0103                 else name
0104                 for name, efcls in cls._effects
0105             )
0106 
0107         out.write(
0108             "{lottie} | {type} | \\copybrief {fqn} {extra} &nbsp; | \\ref {fqn} \"{name}\" \n"
0109             .format(
0110                 lottie=prop.lottie,
0111                 type=type,
0112                 fqn=fqn,
0113                 name=prop.name,
0114                 extra=extra,
0115             )
0116         )
0117 
0118 
0119 os.makedirs(doxpath, exist_ok=True)
0120 
0121 with open(dox_classdoc, "w") as out_classdoc, open(dox_summary, "w") as out_summary:
0122     out_classdoc.write("/**\n")
0123     out_summary.write("""/**
0124 \\page lottie_json Lottie JSON Format
0125 \\ingroup Lottie
0126     """)
0127 
0128     out_summary.write("""\n\\section lottie_index Index\n""")
0129     for module in modules:
0130         out_summary.write(" - %s\n" % module.name)
0131         for cls in module.classes:
0132             out_summary.write("   - <a href='#lottie_{name}'>{name}</a>\n".format(name=cls.__name__))
0133         out_summary.write("\n")
0134 
0135     for module in modules:
0136         for cls in module.classes:
0137             clsname = cls.__name__
0138             if issubclass(cls, LottieObject):
0139                 props = getattr(cls, "_props", None)
0140                 class_summary(cls)
0141                 sub = cls.__subclasses__()
0142                 if sub:
0143                     out_summary.write("Subclasses:\n")
0144                     for sc in sub:
0145                         out_summary.write(" - <a href='#lottie_{name}'>{name}</a>\n".format(
0146                             name=sc.__name__
0147                         ))
0148                     out_summary.write("\n\par\n")
0149                 if props:
0150                     out_classdoc.write("\\class %s.%s\n\\par Lottie JSON\n" % (module.fullname, clsname))
0151                     proptable(out_classdoc, cls, module, extract_type_classdoc)
0152                     out_classdoc.write("\n\n")
0153 
0154                     proptable(out_summary, cls, module, extract_type_summary)
0155                 out_summary.write("\n\n")
0156             elif issubclass(cls, LottieEnum):
0157                 class_summary(cls)
0158                 out_summary.write("Lottie Value|Name|Description| Attribute\n")
0159                 out_summary.write("------------|----|-----------|---------\n")
0160                 for name, val in cls.__members__.items():
0161                     fqn = "%s::%s::%s" % (module.fullname.replace(".", "::"), clsname, name)
0162                     out_summary.write("{value} | {name} | \\copybrief {fqn} &nbsp; | \\ref {fqn} \"{name}\"\n".format(
0163                         value=val.value,
0164                         name=name,
0165                         fqn=fqn,
0166                     ))
0167                 out_summary.write("\n\n")
0168 
0169     out_classdoc.write("*/")
0170     out_summary.write("*/")