File: python_coot_docs.py

package info (click to toggle)
coot 1.1.18%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 219,964 kB
  • sloc: cpp: 495,934; python: 35,043; ansic: 26,143; lisp: 22,768; sh: 13,186; makefile: 2,746; awk: 441; xml: 245; csh: 14
file content (151 lines) | stat: -rw-r--r-- 4,361 bytes parent folder | download | duplicates (2)
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
#    python_coot_docs.py
#    Copyright (C) 2008  Bernhard Lohkamp, The University of York
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# BL script to make texi files from coot python modules

def is_empty_file(file_name):

    print("to be done?")

def make_texi_file(python_file, doc_file):

    import os

    def extracted_function(line_no):
        fcn = lines[line_no][4:-1]
        lb = fcn.find("(")
        # to get a space between the funcn and '('
        # will result in better readability of html output
        if (fcn[lb-1] != " "):
            fcn = fcn.replace("(", " (")
        # check for multiple line functions
        count = 0
        while ("(" in fcn and not ")" in fcn):
            count += 1
            # remove white space too (maybe - FIXME - check)
            lb = lines[line_no+count].lstrip()
            # remove EOL '\n' if exists from fcn
            fcn = fcn[0:-1]
            fcn += lb
        return fcn
    
    def extract_info(line_no):
        extracted_function = lines[line_no][4:-1]
        for i in range(line_no - 1, 0, -1):
            if '#' in lines[i][0]:
                pass
            else:
                write_info(i, line_no)
                break

    def write_info(start_line, stop_line):
        docout.write("\n")
        docout.write("@c BL python scripted from " + python_file + ":" + str(start_line) + "\n")
        docout.write("@deffn procedure " + extracted_function(stop_line) + "\n")
        for i in range(start_line, stop_line):
            line = lines[i][2:]
            if line == "":
                docout.write("\n")
            else:
                docout.write(line)
        docout.write("\n")
        docout.write("@end deffn\n")

    pyin = open(python_file, 'r')
    docout = open(doc_file, 'w')
    lines = pyin.readlines()
    for i in range(len(lines)):
        line = str(lines[i])
        if "def" in line[0:3]:
            extract_info(i)
        # This is for xtra documentations!
        elif "# deftexi" in line[0:9]:
            lines[i] = "def" + line[9:]
            extract_info(i)
        else:
            pass

    pyin.close()
    docout.close()

def make_section_file(section_file, base_name):

    import os

    so = open(section_file, 'w')
    so.write("@node    " + base_name + "\n")
    so.write("@section " + base_name + "\n")
    so.write("@cindex  " + base_name + "\n")
    so.close()

def append_file(master_file_name, append_file_name):

    master = open(master_file_name, 'a')
    append = open(append_file_name, 'r')
    lines = append.readlines()
    master.writelines(lines)
    master.close()
    append.close()

def remove_file(file_name):

    import os

    os.remove(file_name)

import glob, os

source_files = glob.glob("*.py")
# take out coot.py if there
try:
    source_files.remove("coot.py")
    source_files.remove("python_coot_docs.py")
except: 
    pass

all_doc_file_name = "coot-python-functions.texi"

# first make the menu
def make_menu(file_name):

    doco = open(file_name, 'w')
    doco.write("@menu\n")  
    
    for source_file in source_files:
        base_name, ext = os.path.splitext(source_file)
        line = "* " + base_name + "::\n"
        doco.write(line)

    doco.write("@end menu\n")

    doco.close()

make_menu(all_doc_file_name)

# make the texi files and sections
for source_file in source_files:

    base_name, ext = os.path.splitext(source_file)
    doc_file =  base_name + ".texi"
    section_file = base_name + "-section.texi"
    make_texi_file(source_file, doc_file)
    make_section_file(section_file, base_name)

    append_file(all_doc_file_name, section_file)
    append_file(all_doc_file_name, doc_file)

    remove_file(section_file)
    remove_file(doc_file)