summaryrefslogtreecommitdiff
path: root/src/scripts/gendoc/gendoc.py
blob: bee5161f0bb8d92cadae9e600a78e0fc2036d4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
# encoding: utf-8
"""
Efl documentation generator

Use this script without arguments to generate the full documentation of the Efl
namespace in a folder called 'dokuwiki' (-v to see all generated files)

  --help to see all other options

"""
import os
import sys
import argparse
import atexit


# Use .eo files from the source tree (not the installed ones)
script_path = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.abspath(os.path.join(script_path, '..', '..', '..'))
SCAN_FOLDER = os.path.join(root_path, 'src', 'lib')


# Use pyolian from source (not installed)
pyolian_path = os.path.join(root_path, 'src', 'scripts')
sys.path.insert(0, pyolian_path)
from pyolian import eolian
from pyolian.generator import Template


# parse args
parser = argparse.ArgumentParser(description='Pyolian DocuWiki generator.')
parser.add_argument('--root-path', '-r', metavar='FOLDER', default='dokuwiki',
                    help='where to write files to (root of dokuwiki) '
                         'default to: "./dokuwiki"')
parser.add_argument('--verbose', '-v', action='store_true',
                    help='print a line for each rendered file')
parser.add_argument('--namespace', '-n', metavar='ROOT', default='Efl',
                    help='root namespace of the docs. (default to "Efl")')
_choices = ['start', 'classes', 'enums', 'structs', 'aliases']
parser.add_argument('--step', '-s', metavar='STEP', default=None,
                    choices=_choices,
                    help='A single step to run (default to all), '
                         'valid choices: ' + ', '.join(_choices))
args = parser.parse_args()


# load the whole eolian db (from .eo files in source tree)
eolian_db = eolian.Eolian_State()
if not isinstance(eolian_db, eolian.Eolian_State):
    raise(RuntimeError('Eolian, failed to create Eolian state'))

if not eolian_db.directory_add(SCAN_FOLDER):
    raise(RuntimeError('Eolian, failed to scan source directory'))

if not eolian_db.all_eot_files_parse():
    raise(RuntimeError('Eolian, failed to parse all EOT files'))
    
if not eolian_db.all_eo_files_parse():
    raise(RuntimeError('Eolian, failed to parse all EO files'))


# cleanup the database on exit
def cleanup_db():
    global eolian_db
    del eolian_db


atexit.register(cleanup_db)


# calculate the full path for the txt page of the given object
def page_path_for_object(obj):
    path = ['data', 'pages', 'develop', 'api']
    for ns in obj.namespaces:
        path.append(ns.lower())
    output_filename = obj.short_name.lower() + '.txt'
    return os.path.join(args.root_path, *path, output_filename)


# render a (temporary) page for analizying the namespaces hierarchy
t = Template('namespaces.template')
nspaces = [ns for ns in eolian_db.all_namespaces
           if ns.name.startswith(args.namespace)]

tot_classes = tot_regulars = tot_abstracts = tot_mixins = tot_ifaces = 0
tot_enums = tot_structs = tot_aliases = 0
for ns in nspaces:
    for cls in ns.classes:
        tot_classes += 1
        if cls.type == eolian.Eolian_Class_Type.REGULAR:
            tot_regulars += 1
        elif cls.type == eolian.Eolian_Class_Type.ABSTRACT:
            tot_abstracts += 1
        elif cls.type == eolian.Eolian_Class_Type.MIXIN:
            tot_mixins += 1
        elif cls.type == eolian.Eolian_Class_Type.INTERFACE:
            tot_ifaces += 1
    tot_enums += len(ns.enums)
    tot_structs += len(ns.structs)
    tot_aliases += len(ns.aliases)


totals = [
    ('Namespaces', len(nspaces)),
    ('ALL Classes', tot_classes),
    ('Regular classes', tot_regulars),
    ('Abstract classes', tot_abstracts),
    ('Mixins', tot_mixins),
    ('Interfaces', tot_ifaces),
    ('Enums', tot_enums),
    ('Structs', tot_structs),
    ('Aliases', tot_aliases),
]

root_ns = eolian_db.namespace_get_by_name(args.namespace)

output_file = os.path.join(args.root_path, 'data', 'pages', 'develop', 'api', 'namespaces.txt')
t.render(output_file, args.verbose, root_ns=root_ns, totals=totals)


# render the main start.txt page
if args.step in ('start', None):
    t = Template('doc_start.template')

    nspaces = [ns for ns in eolian_db.all_namespaces
               if ns.name.startswith(args.namespace)]

    output_file = os.path.join(args.root_path, 'data', 'pages', 'develop', 'api', 'start.txt')
    t.render(output_file, args.verbose, nspaces=nspaces)


# render a page for each Class
if args.step in ('classes', None):
    t = Template('doc_class.template')
    for cls in eolian_db.classes:
        if cls.name.startswith(args.namespace):
            output_file = page_path_for_object(cls)
            t.render(output_file, args.verbose, cls=cls.name)

# render a page for each Enum
if args.step in ('enums', None):
    t = Template('doc_enum.template')
    for enum in eolian_db.enums:
        if enum.name.startswith(args.namespace):
            output_file = page_path_for_object(enum)
            t.render(output_file, args.verbose, enum=enum.name)

# render a page for each Struct
if args.step in ('structs', None):
    t = Template('doc_struct.template')
    for struct in eolian_db.structs:
        if struct.name.startswith(args.namespace):
            output_file = page_path_for_object(struct)
            t.render(output_file, args.verbose, struct=struct.name)

# render a page for each Alias
if args.step in ('aliases', None):
    t = Template('doc_alias.template')
    for alias in eolian_db.aliases:
        if alias.name.startswith(args.namespace):
            output_file = page_path_for_object(alias)
            t.render(output_file, args.verbose, alias=alias.name)