-
Notifications
You must be signed in to change notification settings - Fork 0
/
docgen.py
executable file
·1449 lines (1235 loc) · 45.9 KB
/
docgen.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# encoding: utf-8
"""
Python documentation with Markdown
Given a Python module whose docstrings use the [Markdown syntax][markdown],
print the full Markdown documentation with:
$ docgen [MODULE-NAME]
This script depends on the Python 2.7 standard library as well as the
subprocess wrapper [`sh`][sh] and the [`pandoc`][pandoc] universal
document converter.
[markdown]: http://daringfireball.net/projects/markdown/syntax/
[sh]: https://github.com/amoffat/sh
[pandoc]: http://johnmacfarlane.net/pandoc/
"""
# Python 2.7 Standard Library
import copy
import importlib
import inspect
import json
import os
import pydoc
import re
import shutil
import sys
import tempfile
import types
# Third-Party Libraries
import script
import sh
#
# Metadata
# ------------------------------------------------------------------------------
#
__author__ = u"Sébastien Boisgérault <Sebastien.Boisgerault@mines-paristech.fr>"
__license__ = "MIT License"
__version__ = "0.0.0a1"
#
# TODOs
# ------------------------------------------------------------------------------
#
# - use syntaxtic (source) analysis to produce the function signatures ... ok.
#
# - manage to documention Cython extension classes ? ..................... ok.
#
# - handle `wrapper_descriptor` type as a function (Cython). Get the type
# as type(str.__dict__['__add__']). Do the same with method_descriptor
# obtained as type(str.center)
#
# - handle assignment of class and function differently from their definition.
# (examine the type info and not only the type of the object). Use cases:
# `__str__ = __repr__` in a class, multiple names of classes/functions to
# preserve a legacy API, etc.
#
# - manage the documentation / docstrings of properties.
#
# - decorators.
#
# - flag to hide "private" fields. Or at least, their doc should be deactivated
# by default.
#
# - generalize the trick used to compose the document title ? Combine the
# object tt name with the one-liner description ? So that the titles are
# not 100% tt (code) anymore but (bold) text, made for the human ? Or even,
# hide the code, signature, etc one level below ? Would be ok for classes,
# where the short doc is a title, not such much for functions for which the
# one-liner is a sentence (action performed by the function).
#
# - Test specifically for pandoc 1.9 (or <= ?) ? Pinpoint the last version
# with the "classic" JSON model that is compatible with my implementation.
# Study the (apparently unstable and whose doc does not macth the reality
# of the JSON document model)
#
# Pandoc Document Model
# ------------------------------------------------------------------------------
#
# Rk: need to stick with pandoc 1.9 for now, the later versions have been
# messing seriously with the JSON representation (and it's still not
# stable or works as advertised AFAICT).
def _tree_iter(item):
"Tree iterator"
yield item
if not isinstance(item, basestring):
try:
it = iter(item)
for subitem in it:
for subsubitem in _tree_iter(subitem):
yield subsubitem
except TypeError:
pass
class PandocType(object):
"""
Pandoc types base class
Refer to the [Pandoc data structure definition](http://hackage.haskell.org/packages/archive/pandoc-types/1.8/doc/html/Text-Pandoc-Definition.html) (in Haskell) for details.
"""
def __init__(self, *args):
self.args = list(args)
def __iter__(self):
"Child iterator"
return iter(self.args)
def iter(self):
"Tree iterator"
return _tree_iter(self)
def apply(self, transform):
apply(transform)(self)
def __json__(self):
"""
Convert the `PandocType instance` into a native Python structure that
may be encoded into text by `json.dumps`.
"""
return {type(self).__name__: to_json(list(self.args))}
def __repr__(self):
typename = type(self).__name__
args = ", ".join(repr(arg) for arg in self.args)
return "{0}({1})".format(typename, args)
class Pandoc(PandocType):
def __json__(self):
meta, blocks = self.args[0], self.args[1]
return [meta, [to_json(block) for block in blocks]]
@staticmethod
def read(text):
return read(text)
def write(self):
return write(self)
class Block(PandocType):
pass
class Header(Block):
pass
class Table(Block):
pass
class DefinitionList(Block):
pass
class BulletList(Block):
pass
class OrderedList(Block):
pass
class Plain(Block):
pass
class CodeBlock(Block):
pass
class BlockQuote(Block):
pass
class RawBlock(Block):
pass
class Inline(PandocType):
pass
class Emph(Inline):
pass
class Para(Inline):
pass
class Code(Inline):
pass
class Link(Inline):
pass
class Str(Inline):
def __init__(self, *args):
self.args = [u"".join(args)]
def __repr__(self):
text = self.args[0]
return "{0}({1!r})".format("Str", text)
def __json__(self):
return {"Str": self.args[0]}
#
# **Remark:** `Space` is encoded as a string in the json exported by pandoc.
# That's kind of a problem because we won't typematch it like the other
# instances and searching for the string "Space" may lead to false positive.
# The only way to deal with it is to be aware of the context where the Space
# atom (inline) may appear but here we typically are not aware of that.
#
class Strong(Inline):
pass
class Math(Inline):
pass
# TODO: check Pandoc version: in 1.12(.1 ?), change in the json output
# structure ... Need to handle both kind of outputs ... selection
# of the format as a new argument to __json__ ? The Text.Pandoc.Definition
# has been moved to pandoc-types <http://hackage.haskell.org/package/pandoc-types>.
# Detect the format used by the conversion of a simple document ? Fuck,
# In need to be able to access an "old" version of pandoc (the one packaged
# for ubuntu 12.04 ?). Ah, fuck, all this is a moving target. In 12.1,
# that's "tag" and "contents", but changelog of 12.1 stated that it is
# "t" and "c" ... I don't even know what version I am really using.
# What is supposed to be stable ? There is probably 3 target: the packaged
# ubuntu 12.04, the 1.12 installed as latest by cabal ... and the current
# git version ... 1.12.3 ouch. What's in Ubuntu 13.04 ? 13.10 ? The 1.11.1
# Errr ... Try to build from git the git version and see if there is
# really a change in the JSON format ?
def to_pandoc(json):
def is_doc(item):
return isinstance(item, list) and \
len(item) == 2 and \
isinstance(item[0], dict) and \
"docTitle" in item[0].keys()
if is_doc(json):
return Pandoc(*[to_pandoc(item) for item in json])
elif isinstance(json, list):
return [to_pandoc(item) for item in json]
elif isinstance(json, dict) and len(json) == 1:
key, args = json.items()[0]
pandoc_type = eval(key)
return pandoc_type(*to_pandoc(args))
else:
return json
def to_json(doc_item):
if hasattr(doc_item, "__json__"):
return doc_item.__json__()
elif isinstance(doc_item, list):
return [to_json(item) for item in doc_item]
else:
return doc_item
def read(text):
"""
Read a markdown text as a Pandoc instance.
"""
#print "***text:", text
json_text = str(sh.pandoc(read="markdown", write="json", _in=text))
json_ = json.loads(json_text)
#import pprint
#pp = pprint.PrettyPrinter(indent=2).pprint
#print "***json:"
#pp(json_)
return to_pandoc(json_)
def write(doc):
"""
Write a Pandoc instance as a markdown text.
"""
json_text = json.dumps(to_json(doc))
return str(sh.pandoc(read="json", write="markdown", _in=json_text))
#
# Pandoc Transforms
# ------------------------------------------------------------------------------
#
def apply(transform):
def doc_transform(doc_item):
for elt in doc_item.iter():
transform(elt)
return doc_transform
def increase_header_level(doc, delta=1):
def _increase_header_level(delta):
def _increase(doc_item):
if isinstance(doc_item, Header):
doc_item.args[0] = doc_item.args[0] + delta
return _increase
return doc.apply(_increase_header_level(delta))
def set_min_header_level(doc, minimum=1):
levels = [item.args[0] for item in doc.iter() if isinstance(item, Header)]
if not levels:
return
else:
min_ = min(levels)
if minimum > min_:
delta = minimum - min_
increase_header_level(doc, delta)
#
# **TODO:** insert HorizontalRule before every level 2 section. Unless I do that
# at the LaTeX level ? Or don't do it generally, just before functions
# and classes (no transform, do it directly during markdown generation) ?
#
#
# ------------------------------------------------------------------------------
#
#
# **TODO:**
# hierarchical structure: class method docs should be browsed too.
# return a list that contains [name, item, docstring, children] ?
# add extra stuff such as line / file, source code, etc. ? Put this
# stuff into an info dict ?
#
_targets = "module dict weakref doc builtins file name package"
_hidden_magic = ["__{0}__".format(name) for name in _targets.split()]
def get_star_imports(module):
try:
source = inspect.getsource(module)
except TypeError:
source = ""
pattern = r"\s*from\s*(\S*)\s*import\s*\*"
lines = source.split("\n")
modules = []
for line in lines:
match = re.match(pattern, line)
if match:
modules.append(match.groups()[0])
return modules
def is_external(item, name, star_imports):
last_name = name.split(".")[-1]
if last_name.startswith("_") and not (last_name.startswith("__") and last_name.endswith("__")):
return False
for module_name in star_imports:
module = importlib.import_module(module_name)
if hasattr(module, last_name) and getattr(module, last_name) is item:
return True
else:
return False
# TODO
# : when found an external module, register somewhere (for the dependency
# analysis ...)
# TODO
# : need to find the star-imports and for every object that has no
# `__module__`, check that's there no such name in the star-imported
# modules.
def object_tree(item, name=None, module=None, _cache=None):
"""
Return the tree of items contained in `item`.
The return value is a `(name, item, children)` where `children`
has the same structure.
"""
if name is None:
if hasattr(item, "__module__"):
name = item.__module__ + "." + item.__name__
else:
name = item.__name__
if module is None and isinstance(item, types.ModuleType):
module = item
tree = (name, item, [])
if _cache is None:
_cache = ([], [])
if id(item) not in [id(x) for x in _cache[0]]:
_cache[0].append(item)
_cache[1].append(tree[2])
if isinstance(item, types.ModuleType):
children = inspect.getmembers(item)
elif isinstance(item, type):
children = item.__dict__.items()
else:
children = []
MethodWrapper = type((lambda: None).__call__)
def is_local(item, name):
if module:
return getattr(_item, "__module__", module.__name__) == module.__name__
else:
return True
star_imports = get_star_imports(module)
for _name, _item in children:
# exclude private and foreign objects as well as (sub)modules.
# exclude __call__ for anything but classes (nah, detect wrapper instead)
# some extra magic stuff should be excluded too (__class__, __base__, etc.)
# OH, C'MON, even strings are a nested problem ! "a.__doc__.__doc__", etc ...
if (not _name.startswith("_") or (_name.startswith("__") and _name.endswith("__") and not _name in _hidden_magic)) and \
not isinstance(_item, types.ModuleType) and \
is_local(item, name) and \
not is_external(_item, _name, star_imports) and \
not isinstance(_item, MethodWrapper):
# import time; time.sleep(1.0)
_name = name + "." + _name
# print "*", _name, "|||", _item, "|||", type(_item), "|||", isinstance(_item, types.ModuleType)
# BUG: Numpy issue: when an array is "=="'d to SOME items (such as
# a numeric value, a boolean, etc.), the result is an array.
if id(_item) in [id(x) for x in _cache[0]]:
index = [id(x) for x in _cache[0]].index(id(_item))
new = (_name, _item, _cache[1][index])
else:
new = object_tree(_item, _name, module, _cache)
tree[2].append(new)
return tree
def tt(text):
"""
Turn `text` into fixed-font text (or *teletype*).
"""
return "`{0}`".format(text)
INF = 1e300000
def line_number_finder(container):
def line_number(info):
qname, item, children = info
name = qname.split(".")[-1]
try:
_line_number = inspect.getsourcelines(item)[1]
except (IOError, TypeError):
try:
source = inspect.getsource(container)
pattern = r"\s*{0}\s*=".format(name)
_line_number = 1
for line in source.split("\n"):
if re.match(pattern, line):
return _line_number
else:
_line_number += 1
else:
_line_number = INF
except (IOError, TypeError):
_line_number = INF
return _line_number
return line_number
# Having issue with signature when the function is a built-in ...
# need to fallback on source syntax analysis. (TODO).
def signature(function, name=None):
"""
Return the function signature as found in Python source:
>>> def f(x, y=1, *args, **kwargs):
... pass
>>> print signature(f)
f(x, y=1, *args, **kwargs)
>>> print signature(f, name="g")
g(x, y=1, *args, **kwargs)
"""
argspec = inspect.getargspec(function)
if name is None:
name = function.__name__
nargs = len(argspec.args)
args = ""
defaults = argspec.defaults or []
for i, arg in enumerate(argspec.args):
try:
default = defaults[i - nargs]
args += "{0}={1!r}, ".format(arg, default)
except IndexError:
args += "{0}, ".format(arg)
if argspec.varargs:
args += "*{0}, ".format(argspec.varargs)
if argspec.keywords:
args += "**{0}, ".format(argspec.keywords)
if args:
args = args[:-2]
return name + "({0})".format(args)
#def _get_comments(module):
# comments = []
# try:
# source = inspect.getsource(module)
# except IOError:
# return comment
# pattern = "^#\s*\n(# [^\n]*\n)*#\s*\n"
# for match in re.finditer(pattern, source, re.MULTILINE):
# line_number = source.count("\n", 0, match.start()) + 1
# comment = match.group(0)
# comment = "\n".join(line[2:] for line in comment.split("\n"))
# comments.append((line_number, comment))
# return comments
def last_header_level(markdown):
doc = Pandoc.read(markdown)
levels = [item.args[0] for item in doc.iter() if isinstance(item, Header)]
if levels:
return levels[-1]
#
# Source Code Analysis
# ------------------------------------------------------------------------------
#
# TODO: take source as an argument, create a list of "objects" with names
# and other info such as type, plus some type-specific info and
# chidren. Add some sources / lineno info into the mix so that the
# original source can be reconstructed easily.
# How to deal with indentation and blanklines ? Factor indentation
# when we can ? How do we deal with line continuations ?
# TODO: start with the generation of a stream of (types, with data) events
# that all have a lineno, THEN create the appropriate structure ? Use
# a state when doing so that to begin with, has a stack of all indents ?
# Have a INDENT, DEINDENT (?). START_DECL, END_DECL, START/END-COMMENT,
# same for docstrings, etc. etc.
# DEPRECATED:
#
# def is_blank(line):
# """Identify blank lines"""
# whitespace = "\s*"
# return re.match(whitespace, line).group(0) == line
# TODO: single function call, convert one way or another based on the
# number of arguments ? And therefore, turn the Locator into a
# closure ...
class Locator(object):
"""Convert locations in text.
Locations are described either by
`offset`, an absolute character offset with respect to the
start of `text`
or
a pair `(lineno, rel_offset)` of a line number offset
(starts at `0`) and an offset relative to the start of the line.
"""
def __init__(self, text):
"""
Create a `Locator` instance for the string `text`.
"""
self._offsets = [0]
for line in text.split("\n"):
self._offsets.append(self._offsets[-1] + len(line) + 1)
def __call__(self, offset):
"""
Compute the location `(lineno, rel_offset)`
"""
for i, _offset in enumerate(self._offsets):
if offset < _offset:
return (i - 1, offset - self._offsets[i-1])
def offset(self, lineno, rel_offset):
"""
Compute the location `offset`
"""
return sum([len for len in self._offsets[:lineno]] + [rel_offset], 0)
#
# -----
#
def finder(symbol, pattern=None, *flags):
"""
Create a function that searches for locations of a a pattern in a text.
Arguments
---------
- `symbol`: the name of the symbol to search,
- `pattern`: a regular expression, defaults to `re.escape(symbol)`,
- `flags`: extra flags passed to `re.search` function internally.
Returns
-------
- a finder function whose arguments are :
- `text`: the text to be searched,
- `start`: the start index, defaults to `0`.
that returns:
- `(symbol, start, end)` or `None` when no match is found.
"""
if pattern is None:
pattern = "({0})".format(re.escape(symbol))
pattern = re.compile(pattern, *flags)
def finder_(text, start=0):
match = pattern.search(text, start)
if match is None:
return None
else:
start, end = match.span(1)
return symbol, start, end
finder_.__name__ = symbol
return finder_
# We don't need a First then Longest then "First in pattern list" sorter ?
# This is what is done implicitly ? Can we trust list.sort to return the
# first item in the list among those that are equally sorted ? Yes, this
# is a stable sort ...
def sort_items(list):
"""First-then-longest sorter
This function sorts in-place a list of `(symbol, start, end)` items,
in a way that the items with the lowest `start` index appear
first, and when such indices are equal, the item with the highest `end`
index appears first.
"""
first_then_longest = lambda item: (item[1], -item[2])
list.sort(key=first_then_longest)
def tokenize(text):
"""
Tokenizer
Produce a sequence of `(symbol, start, end)` items where `symbol`
belongs to the following list of strings:
( ) [ ] { } BLANKLINE COMMENT LINECONT STRING
"""
finders = []
finders += [finder(symbol) for symbol in "( [ { ) ] }".split()]
finders += [finder("BLANKLINE", r"(^[ \t\r\f\v]*\n)", re.MULTILINE)]
finders += [finder("COMMENT" , r"([ \t\r\f\v]*#.*\n?(?:[ \t\r\f\v]*#.*\n?)*)")]
finders += [finder("LINECONT" , r"(\\\n)")]
finders += [finder("STRING" , r'("(?:[^"]|\\")*")')]
finders += [finder("STRING" , r'("""(?:[^"]|\\"|"{1,2}(?!"))*""")')]
finders += [finder("STRING" , r"('(?: [^']|\\')*')")]
finders += [finder("STRING" , r"('''(?:[^']|\\'|'{1,2}(?!'))*''')")]
start = 0
items = []
while start < len(text):
results = []
for find in finders:
result = find(text, start)
if result is not None:
results.append(result)
if results:
sort_items(results)
result = results[0]
items.append(result)
start = result[2]
else:
break
return items
# Rk: now the "largest" objects (enclosing braces) are returned AFTER the
# enclosed objects. Maybe we don't care ? But it's contrary to the
# classic linearization of the hierarchy.
def scan(text):
"""
Scan the source code `text` for atomic and scoping patterns.
Produce a a sequence of `(symbol, start, end)` items where `symbol`
belongs to the following list of strings:
() [] {} BLANKLINE COMMENT LINECONT STRING
The items may be overlapping.
"""
match = {"(": ")", "[": "]", "{": "}", ")": "(", "]": "[", "}": "{"}
wait_for = []
items = []
for symbol, start, end in tokenize(text):
if name in ["(", "[", "{"]:
wait_for.append((match[symbol], start))
elif wait_for and symbol == wait_for[-1][0]:
_, start = wait_for.pop()
items.append((match[symbol] + symbol, start, end))
else:
items.append((symbol, start, end))
sort_items(items)
return items
def skip_lines(text):
"""
Lines to skip during the indentation analysis.
"""
lines = []
locator = Locator(text)
for name, start, end in scan(text):
start, end = locator(start), locator(end)
if name == "BLANKLINE":
lines.append(start[0])
if name == "COMMENT":
start_line = start[0] + (start[1] != 0)
end_line = end[0] - 1
lines += [line for line in range(start_line, end_line + 1)]
if name == "LINECONT":
lines.append(start[0] + 1)
if name in "() [] {} STRING".split():
start_line = start[0] + 1
end_line = end[0]
lines += [line for line in range(start_line, end_line + 1)]
return set(lines)
def tab_match(line, tabs):
"""
Analyze the indentation of a line with respect to a sequence of indents.
Arguments
---------
- `line`: a text string,
- `tabs`: a sequence of non-empty whitespace strings.
Returns
-------
- `match`: the largest starting sequence of `tabs` found at the
start of the line,
- `extra`: an extra whitespace element found after the full `tabs` sequence,
or otherwise `None`.
Raises
------
A `ValueError` exception is raised if the `tabs` list is matched only
partially but there is some extra whitespace found after it.
"""
tab_search = re.compile("^[ \t\r\f\v]+", re.MULTILINE).search
_tabs = tabs[:]
matched = []
while _tabs:
tab = _tabs.pop(0)
if line.startswith(tab):
matched.append(tab)
line = line[len(tab):]
else:
break
match = tab_search(line)
if match:
extra = match.group(0)
else:
extra = None
if matched == tabs or not extra:
return matched, extra
else:
raise ValueError("indentation error")
def indents(text):
"""
Return the indents of a source code.
The result is a list of `(lineno, delta)` where:
- `lineno` is a line number offset (starts with `0`),
- `delta` is the number of extra indents (it may be negative).
"""
skip = skip_lines(text)
tabs = []
indents = []
for i, line in enumerate(text.split("\n")):
if i not in skip:
match, extra = tab_match(line, tabs)
if extra:
indents.append((i, +1))
tabs.append(extra)
else:
indents.append((i, len(match) - len(tabs)))
tabs = tabs[:len(match)]
return indents
def parse_declaration(line):
finders = []
finders += [finder("function" , r"^\s*c?p?def\s+([_0-9a-zA-Z]+)\s*\(")]
finders += [finder("assignment", r"^\s*([_0-9a-zA-Z]+)\s*=\s*")]
finders += [finder("class" , r"^\s*(?:cdef)?\s*class\s+([_0-9a-zA-Z]+)")]
results, result = [], None
for find in finders:
result = find(line)
if result is not None:
results.append(result)
if results:
sort_items(results)
result = results[0]
result = result[0], line[result[1]:result[2]]
return result
else:
return None, None
# ------------------------------------------------------------------------------
# TODO: tree (or make_tree) function that produces a [lineno, info, children]
# (or even [info, children] ?) hierarchical structure. All the relevant
# information (type, name, etc.) is stored in the struct info. Later
# -- beyond syntax analysis -- introspection-based information can be
# added to the info object, such as the object itself, the docstring,
# etc. The special markdown comments should also be intertwined.
# In info, use None (for lineno, name, type) when it is required.
#
# Q: how should line continuations be handled ? In a first approach,
# we don't do anything but later, maybe the lineno should be
# replaced with a RANGE of lineno ?
class Info(object):
"""
Lighweight Records
"""
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
return "Info(**{0})".format(self.__dict__)
__str__ = __repr__
# How to handle line cont ? Markdown comments and more generally multiple
# line stuff ? replace lineno with a range ? start and end lineno + offset ?
# OR (simpler): create a lineno -> lines function and use it :) (instead of
# the skipping mecanism). Arf, not that simple ... Instead an iterator that
# returns (lineno, lines) ? But to work without a major redesign, the lineno
# would have to return something that is subject to tab analysis ... (that
# does not suspend the identation state). And that cannot always be done,
# YES IT CAN BE DONE
# if the file start with comments for example. ... BULL ! Just attribute
# this chunk to the module itself.
# TODO: insert markdown comments at this level ?
# TODO: lookahead for the next lineno: the missing lines in the iteration
# should be stacked and added to the node info.
#
# Rk: splitlines does not honor trailing newlines, use split("\n") instead.
# TODO: check that all blanklines are here.
def make_tree(text):
"""
Create a nested structure based on the indentation of source code.
Argument
--------
- `text`: a source code string
Returns
-------
- `tree = (info, children)` where:
- `info` has `lineno`, `name`, `type` and `source` attributes,
- `children` is a list of `tree` items.
"""
lines = text.split("\n")
items = [(Info(lineno=0, name=None, type=None), [])]
item = items[0] # current item
prev_lineno = 0
def push(item):
items.append(item)
def fold():
item = items.pop()
items[-1][-1].append(item)
for lineno, tab in indents(text):
text = "\n".join(lines[prev_lineno:lineno])
item[0].source = text
type, name = parse_declaration(lines[lineno])
if tab <= 0 and len(items) >= 2:
for _ in range(-tab + 1):
fold()
info = Info(lineno=lineno, name=name, type=type)
item = (info, [])
push(item)
prev_lineno = lineno
else:
lineno = len(lines)
source = "\n".join(lines[prev_lineno:lineno]) # no trailing newline.
item[0].source = source
while len(items) >= 2:
fold()
return items[0]
# TODO: Sometimes there are extra BLANKLINEs, get rid of them. It maybe an
# issue with comments ???
def display_tree(tree, nest=""):
template = "{info.lineno:>5} {nest:>9} | {info.name:>15} {object_type:>12} {info.type:>12}"
try:
object = getattr(tree[0], "object")
object_type = type(object).__name__
except AttributeError:
object_type = None
left = template.format(info=tree[0], nest=nest, object_type=object_type)
lines = tree[0].source.split("\n")
if lines:
print left, "|", lines[0]
for line in lines[1:]:
print len(left) * " ", "|", line
children = tree[1]
for child in children:
display_tree(child, nest=nest+"+")
#
# Documentation Formatting
# ------------------------------------------------------------------------------
#
# TODO: manage the body of docgen as yet another formatter function.
def docgen(module, source, debug=False):
module_name = module.__name__
tree = make_tree(source)
tree[0].name = module_name
objectify(tree)
commentify(tree)
decoratify(tree)
if debug:
display_tree(tree)
print 5*"\n"
markdown = ""
docstring = inspect.getdoc(module) or ""
doclines = docstring.split("\n")
if len(doclines) == 1:
short, long = doclines[0].strip(), ""
elif len(doclines) >= 2 and not doclines[1].strip():
short, long = doclines[0].strip(), "\n".join(doclines[2:])
else:
short, long = "\n".join(doclines)
# TODO: refactor into `format_module`.
markdown = "#" + " " + tt(module_name)
markdown += (" -- " + short + "\n\n") if short else "\n\n"
markdown += long + "\n\n" if long else ""
level = 2
state = {"level": level,
"namespace": module_name,
"restore": True}
for child in tree[1]:
markdown += format(child, state)
return markdown
def load_object(qualified_name):
"""
Load an object by qualified (dotted) name.
"""
parts = qualified_name.split(".")
object = None
base = ""
while parts:
part = parts.pop(0)
base = (base + "." if base else "") + part
try:
object = importlib.import_module(base)
except ImportError:
parts.insert(0, part)
break
if object is None:
raise ValueError()
for part in parts:
try:
object = getattr(object, part)
except AttributeError:
raise ValueError()
return object
def objectify(tree, ns=None):
"""
Annotate a tree with objects instances.
Add `object` fields to the tree `info` structures when it makes sense.
"""
name = tree[0].name
if name:
qname = (ns + "." if ns else "") + name
try:
tree[0].object = load_object(qname)
except ValueError:
pass
for child in tree[1]:
objectify(child, ns=qname)
class Markdown(object):
def __init__(self, markdown):