pykd

Форк
0
/
pytowiki.py 
192 строки · 6.1 Кб
1
#
2
#
3
#
4

5
import sys
6

7

8
def usage():
9
    print("python pytowiki.py module_name output_file")
10

11

12
class CodeplexFormatter:
13

14
    def endl( self ):
15
        return "\n"
16

17
    def header1( self, s ):
18
        return "! " + s + self.endl()
19

20
    def header2( self, s ):
21
        return "!! " + s + self.endl()
22

23
    def header3( self, s ):
24
        return "!!! " + s + self.endl()
25

26
    def header4( self, s ):
27
        return "!!!! " + s + self.endl()
28

29
    def bulletItem( self, s ):
30
        return "* " + s + self.endl()
31

32
    def escapeMarkup( self, s ):
33
        return "{\"" + s + "\"}"
34

35
    def link( self, text, link ):
36
        return "[" + text + "|#" + link + "]"
37

38
    def anchor( self, link ):
39
        return "{anchor:" + link + "}" + self.endl()
40
        
41
class MarkdownFormatter:
42

43
    def endl( self ):
44
        return "\n"
45

46
    def header1( self, s ):
47
        return "# " + s + self.endl()
48

49
    def header2( self, s ):
50
        return "## " + s + self.endl()
51

52
    def header3( self, s ):
53
        return "### " + s + self.endl()
54

55
    def header4( self, s ):
56
        return "#### " + s + self.endl()
57

58
    def bulletItem( self, s ):
59
        return "* " + s + self.endl()
60

61
    def escapeMarkup( self, s ):
62
        return self.endl() + "```"  + self.endl() + s + self.endl() + "```" + self.endl()
63

64
    def link( self, text, link ):
65
        return "[" + text + "](#" + link + ")"
66

67
    def anchor( self, link ):
68
        return "<a name=\"" + link + "\"></a>"  + self.endl()
69
        
70
    def escapeSymbols(self, text):
71
        output = ""
72
        escapeSyms = ['_'] 
73
        for s in text:
74
            if s in escapeSyms:
75
                output += "\\" + s
76
            else:
77
                output += s
78
        return output
79
           
80

81

82

83
class ModuleInfo:
84

85
    def __init__ (self, module):
86
        self.funcs = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ ) 
87
        self.classes = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "class" or type(item).__name__ == "type" ],  key=lambda x: x.__name__ )
88

89
        for cls in  self.classes:
90
            cls.methods = sorted( [ item for item in cls.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ ) 
91
            cls.properties = sorted( [ item for item in cls.__dict__.items() if type(item[1]).__name__ == "property" ], key=lambda x: x[0] )
92
            cls.enum = cls.__dict__.get("names", [] )
93

94

95
def buildDoc( ioStream, formatter, apiInfo ):
96
  
97
    ioStream.write( formatter.header2( "Functions" ) )
98

99
    for func in apiInfo.funcs:
100
        ioStream.write( formatter.bulletItem( formatter.link( func.__name__, func.__name__ ) ) )
101
        
102
    ioStream.write( formatter.endl() )
103

104
    ioStream.write( formatter.header2( "Classes" ) )
105

106
    for cls in apiInfo.classes:
107
        ioStream.write( formatter.bulletItem( formatter.link( cls.__name__, cls.__name__ ) ) )
108

109
    ioStream.write( formatter.endl() )     
110

111
    for func in apiInfo.funcs:
112
        ioStream.write( formatter.anchor( func.__name__ ) )
113
        ioStream.write( formatter.header3( "Function " +  func.__name__ ) )
114
        if func.__doc__ != None:
115
            ioStream.write( formatter.escapeMarkup( func.__doc__) + formatter.endl() )
116

117

118
    for cls in apiInfo.classes:
119
        ioStream.write( formatter.anchor( cls.__name__ ) )
120
        ioStream.write( formatter.header3( "Class " + cls.__name__ ) )
121
        if cls.__doc__ != None:
122
            ioStream.write( formatter.escapeMarkup( cls.__doc__)  + formatter.endl() )
123

124
        ioStream.write( formatter.header4( "Base classes:") )
125
        for b in cls.__bases__:
126
            if b in apiInfo.classes:
127
                ioStream.write( formatter.link( b.__name__,  b.__name__ ) + formatter.endl() )
128
            else:
129
                ioStream.write( b.__name__ + formatter.endl() )
130

131
        if cls.properties:
132
            ioStream.write( formatter.header4( "Properties:") )
133
            for p in cls.properties:  
134
                ioStream.write( formatter.bulletItem( formatter.link( p[0],  cls.__name__ + "." + p[0]) ) )
135
            ioStream.write( formatter.endl() )
136

137
        methods = list(filter( lambda m: m.__doc__ != None, cls.methods ) )
138
            
139
        if methods:
140
            ioStream.write( formatter.header4( "Methods:") )
141
            for m in methods:
142
               ioStream.write( formatter.bulletItem( formatter.link( formatter.escapeSymbols(m.__name__), cls.__name__ + "." + m.__name__)) )
143
            ioStream.write( formatter.endl() )
144

145
        if cls.enum:
146
            ioStream.write( formatter.header4( "Values:") )
147
            for v in cls.enum.items():
148
                ioStream.write( formatter.bulletItem( "%s: %d" % v ) )
149
            ioStream.write( formatter.endl() )
150
        
151
        if cls.properties:
152
            for p in cls.properties:  
153
                if p[1].__doc__ != None:
154
                    ioStream.write( formatter.anchor( cls.__name__ + "." + p[0] ) )
155
                    ioStream.write( formatter.header4( "Property  " + cls.__name__  + "."  +  p[0] ) )
156
                    ioStream.write( formatter.escapeMarkup( p[1].__doc__ ) + formatter.endl() )
157

158
        if methods:
159
            for m in methods:
160
                ioStream.write( formatter.anchor( cls.__name__ + "." + m.__name__ ) )
161
                ioStream.write( formatter.header4( formatter.escapeSymbols("Method  " + cls.__name__  + "."  + m.__name__ )) )
162
                ioStream.write( formatter.escapeMarkup( m.__doc__ ) + formatter.endl() )
163

164
def main():
165

166
    if len(sys.argv) < 3:
167
        usage()
168
        return
169

170
    moduleName = sys.argv[1]
171
    fileName = sys.argv[2]
172

173
    try:
174

175
        module = __import__( moduleName )
176

177
        with open( fileName, "w" ) as wikiIo:
178

179
            apiInfo = ModuleInfo( module )
180

181
            #formatter = CodeplexFormatter()
182
            formatter = MarkdownFormatter()
183

184
            buildDoc( wikiIo, formatter, apiInfo )
185

186
    except ImportWarning:
187

188
        print("failed to import module ", moduleName)
189

190

191
if __name__ == "__main__":
192
    main()
193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.