This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from python-excel/xlrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythondoc.py
1428 lines (1256 loc) · 49 KB
/
pythondoc.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
#
# $Id: pythondoc.py 3271 2007-09-09 09:45:14Z fredrik $
# pythondoc documentation generator
#
# history:
# 2003-10-19 fl first preview release (2.0a1)
# 2003-10-19 fl fix HTML in descriptor tags, 1.5.2 tweaks, etc (2.0a2)
# 2003-10-20 fl added encoding support, default HTML generator, etc (2.0a3)
# 2003-10-21 fl fixed some 1.5.2 issues, etc (2.0b1)
# 2003-10-22 fl HTML tweaks, pluggable output generators, etc (2.0b2)
# 2003-10-23 fl fixed encoding, added @author, @version, @since etc
# 2003-10-24 fl disable XML output by default
# 2003-10-25 fl moved info properties into an 'info' element
# 2003-10-26 fl expand wildcards on windows (2.0b3)
# 2003-10-30 fl added support for RISC OS
# 2003-10-31 fl (experimental) support module-level comments
# 2003-11-01 fl minor HTML tweaks (2.0b4)
# 2003-11-03 fl pythondoc 2.0 final
# 2003-11-15 fl added support for inline @link/@linkplain tags (2.1b1)
# 2003-11-20 fl fixed class attribute parsing bug
# 2004-03-27 fl handle multiple single-line methods
# 2004-09-01 fl support Python 2.4 decorators (2.1b2)
# 2004-09-21 fl fixed output filename for "pythondoc ."
# 2005-03-25 fl added docstring extraction for classes and methods (2.1b3)
# 2005-06-18 fl fixed correct HTML output when using ElementTree 1.3 (2.1b4)
# 2005-12-23 fl use xml.etree where available
# 2006-04-04 fl refactored comment parser code; added -s support (2.1b5)
# 2006-04-06 fl handle multiple params in docstrings correctly (2.1b6)
# 2007-09-09 fl moved HTML parser into pythondoc module itself
#
# Copyright (c) 2002-2007 by Fredrik Lundh.
#
##
# This is the PythonDoc tool. This tool parses Python source files
# and generates API descriptions in XML and HTML.
# <p>
# For more information on the PythonDoc tool and the markup format, see
# <a href="http://effbot.org/zone/pythondoc.htm">the PythonDoc page</a>
# at <a href="http://effbot.org/">effbot.org</a>.
##
# --------------------------------------------------------------------
# Software License
# --------------------------------------------------------------------
#
# Copyright (c) 2002-2007 by Fredrik Lundh
#
# By obtaining, using, and/or copying this software and/or its
# associated documentation, you agree that you have read, understood,
# and will comply with the following terms and conditions:
#
# Permission to use, copy, modify, and distribute this software and
# its associated documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice appears in
# all copies, and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of
# Secret Labs AB or the author not be used in advertising or publicity
# pertaining to distribution of the software without specific, written
# prior permission.
#
# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
# ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
#
# --------------------------------------------------------------------
# to do in later releases:
#
# TODO: test this release under 1.5.2 !
# TODO: better rendering of constructors/package modules
# TODO: check @param names against @def/define tags
# TODO: support recursive parsing (-R)
# TODO: warn for tags that doesn't make sense for a given target type
# TODO: HTML output localization (the %s module, returns, raises, etc)
# TODO: make compactHTML generate an element tree instead of raw HTML
#
# nice to have, maybe:
#
# IDEA: support multiple output handlers (multiple -O statements);
# make -x an alias for -Oxml
# IDEA: make pythondoc self-contained (include stub element implementation)
VERSION_DATE = "2.1b7-20070909"
VERSION = VERSION_DATE.split("-")[0]
COPYRIGHT = "(c) 2002-2007 by Fredrik Lundh"
# explicitly import site (for exemaker etc)
import site
# stuff we use in this module
import glob, os, re, string, sys, tokenize
# make sure elementtree is available
try:
try:
import xml.etree.ElementTree as ET
except ImportError:
import elementtree.ElementTree as ET
except ImportError:
raise RuntimeError(
"PythonDoc %s requires ElementTree 1.1 or later "
"(available from http://effbot.org/downloads)." % VERSION
)
# extension separator (not all systems use a period)
try:
EXTSEP = os.extsep
except AttributeError:
EXTSEP = "."
##
# Debug level. The higher the value, the more junk you'll see on
# standard output.
# <p>
# You can use the <b>-V</b> option to <b>pythondoc</b> to increase
# the debug level.
DEBUG = 0
##
# Whitespace tokens. These are ignored when the parser is scanning
# for a subject.
WHITESPACE_TOKEN = (
tokenize.NL, tokenize.NEWLINE, tokenize.DEDENT, tokenize.INDENT
)
##
# Default encoding. To override this for a module, put a "coding"
# directive in your Python module (see PEP 263 for details).
ENCODING = "iso-8859-1"
##
# Known tags. The parser generates warnings for tags that are not in
# this list, but it still copies them to the XML infoset.
TAGS = (
"def", "defreturn",
"param", "keyparam",
"return",
"throws", "exception",
# javadoc tags not used by the standard generator
"author", "deprecated", "see", "since", "version"
)
##
# (Helper) Combines filename prefix with extension part.
#
# @param prefix Filename prefix.
# @param ext Extension string, including a leading period. The
# period is replaced with a platform-specific separator, if
# necessary.
# @return The combined name.
def joinext(prefix, ext):
assert ext[0] == "." # require leading separator, to match os.path.splitext
return prefix + EXTSEP + ext[1:]
##
# (Helper) Extracts block tags from a PythonDoc comment.
#
# @param comment Comment text.
# @return A list of (lineno, tag, text) tuples, where the tag is None
# for the initial description.
# @defreturn List of tuples.
def gettags(comment):
tags = []
tag = None
tag_lineno = lineno = 0
tag_text = []
for line in comment:
if line[:1] == "@":
tags.append((tag_lineno, tag, string.join(tag_text, "\n")))
line = string.split(line, " ", 1)
tag = line[0][1:]
if len(line) > 1:
tag_text = [line[1]]
else:
tag_text = []
tag_lineno = lineno
else:
tag_text.append(line)
lineno = lineno + 1
tags.append((tag_lineno, tag, string.join(tag_text, "\n")))
return tags
##
# (Helper) Flattens an element tree, returning only the text contents.
#
# @param elem An element tree.
# @return A text string.
# @defreturn String.
def flatten(elem):
text = elem.text or ""
for e in elem:
text += flatten(e)
if e.tail:
text += e.tail
return text
##
# (Helper) Extracts summary from a PythonDoc comment. This function
# gets the first complete sentence from the description string.
#
# @param description An element containing the description.
# @return A summary string.
# @defreturn String.
def getsummary(description):
description = flatten(description)
# extract the first sentence from the description
m = re.search("(?s)(.+?\.)\s", description + " ")
if m:
return m.group(1)
return description # sorry
##
# (Helper) Parses HTML descriptor text into an XHTML structure.
#
# @param parser Parser instance (provides a warning method).
# @param text Text fragment.
# @return An element tree containing XHTML data.
# @defreturn Element.
def parsehtml(parser, tag, text, lineno):
# transcode
if parser.encoding != "ascii":
try:
text = unicode(text, parser.encoding)
except NameError:
pass # 1.5.2
# process inline links (@link, @linkplain)
# note that links are replaced with <a href='link:...> elements;
# the href's are resolved in a later step
if "{" in text:
def fixlink(m, parser=parser, lineno=lineno):
linkdef = string.split(m.group(1), None, 2)
if len(linkdef) == 2:
# default text is same as last part of target name
href = linkdef[1]
if href[:1] == "#":
href = href[1:]
linkdef.append(string.split(href, ".")[-1])
if len(linkdef) < 3 or linkdef[0] not in ("@link", "@linkplain"):
parser.warning(
(lineno, 0),
"malformed @link near this line",
)
return m.group(0)
type, href, text = linkdef
href = "link:" + html_encode(href)
if type == "@link":
return "<a href='%s' class='link'><b>%s</b></a>" % (href, text)
else:
return "<a href='%s' class='linkplain'>%s</a>" % (href, text)
text = re.sub("\{(@link[^}]+)\}", fixlink, text)
if "<" not in text and "&" not in text:
# plain text
elem = ET.Element(tag)
elem.text = string.strip(text)
return elem
p = HTMLTreeBuilder()
ix = 0
try:
p.feed("<%s>" % tag)
p.feed("<p>") # make sure everything's wrapped in a paragraph tag
# feed line by line
for line in string.split(text, "\n"):
p.feed(line + "\n")
ix = ix + 1
p.feed("</%s>" % tag)
tree = p.close()
except:
parser.warning(
(lineno+ix, 0),
"HTML parser error near this line (%s)",
sys.exc_value
)
return ET.Element("p")
return tree
# --------------------------------------------------------------------
# copied from ElementTree/HTMLTreeBuilder.py
import htmlentitydefs
AUTOCLOSE = "p", "li", "tr", "th", "td", "head", "body"
IGNOREEND = "img", "hr", "meta", "link", "br"
if sys.version[:3] == "1.5":
is_not_ascii = re.compile(r"[\x80-\xff]").search # 1.5.2
else:
is_not_ascii = re.compile(eval(r'u"[\u0080-\uffff]"')).search
try:
from HTMLParser import HTMLParser
except ImportError:
from sgmllib import SGMLParser
# hack to use sgmllib's SGMLParser to emulate 2.2's HTMLParser
class HTMLParser(SGMLParser):
# the following only works as long as this class doesn't
# provide any do, start, or end handlers
def unknown_starttag(self, tag, attrs):
self.handle_starttag(tag, attrs)
def unknown_endtag(self, tag):
self.handle_endtag(tag)
##
# ElementTree builder for HTML source code. This builder converts an
# HTML document or fragment to an ElementTree.
# <p>
# The parser is relatively picky, and requires balanced tags for most
# elements. However, elements belonging to the following group are
# automatically closed: P, LI, TR, TH, and TD. In addition, the
# parser automatically inserts end tags immediately after the start
# tag, and ignores any end tags for the following group: IMG, HR,
# META, and LINK.
#
# @keyparam builder Optional builder object. If omitted, the parser
# uses the standard <b>elementtree</b> builder.
# @keyparam encoding Optional character encoding, if known. If omitted,
# the parser looks for META tags inside the document. If no tags
# are found, the parser defaults to ISO-8859-1. Note that if your
# document uses a non-ASCII compatible encoding, you must decode
# the document before parsing.
class HTMLTreeBuilder(HTMLParser):
def __init__(self, encoding=None):
self.__stack = []
self.__builder = ET.TreeBuilder()
self.encoding = encoding or "iso-8859-1"
HTMLParser.__init__(self)
##
# Flushes parser buffers, and return the root element.
#
# @return An Element instance.
def close(self):
HTMLParser.close(self)
return self.__builder.close()
##
# (Internal) Handles start tags.
def handle_starttag(self, tag, attrs):
if tag == "meta":
# look for encoding directives
http_equiv = content = None
for k, v in attrs:
if k == "http-equiv":
http_equiv = string.lower(v)
elif k == "content":
content = v
if http_equiv == "content-type" and content:
# use mimetools to parse the http header
import mimetools, StringIO
header = mimetools.Message(
StringIO.StringIO("%s: %s\n\n" % (http_equiv, content))
)
encoding = header.getparam("charset")
if encoding:
self.encoding = encoding
if tag in AUTOCLOSE:
if self.__stack and self.__stack[-1] == tag:
self.handle_endtag(tag)
self.__stack.append(tag)
attrib = {}
if attrs:
for k, v in attrs:
attrib[string.lower(k)] = v
self.__builder.start(tag, attrib)
if tag in IGNOREEND:
self.__stack.pop()
self.__builder.end(tag)
##
# (Internal) Handles end tags.
def handle_endtag(self, tag):
if tag in IGNOREEND:
return
lasttag = self.__stack.pop()
if tag != lasttag and lasttag in AUTOCLOSE:
self.handle_endtag(lasttag)
self.__builder.end(tag)
##
# (Internal) Handles character references.
def handle_charref(self, char):
if char[:1] == "x":
char = int(char[1:], 16)
else:
char = int(char)
if 0 <= char < 128:
self.__builder.data(chr(char))
else:
self.__builder.data(unichr(char))
##
# (Internal) Handles entity references.
def handle_entityref(self, name):
entity = htmlentitydefs.entitydefs.get(name)
if entity:
if len(entity) == 1:
entity = ord(entity)
else:
entity = int(entity[2:-1])
if 0 <= entity < 128:
self.__builder.data(chr(entity))
else:
self.__builder.data(unichr(entity))
else:
self.unknown_entityref(name)
##
# (Internal) Handles character data.
def handle_data(self, data):
if isinstance(data, type('')) and is_not_ascii(data):
# convert to unicode, but only if necessary
data = unicode(data, self.encoding, "ignore")
self.__builder.data(data)
##
# (Hook) Handles unknown entity references. The default action
# is to ignore unknown entities.
def unknown_entityref(self, name):
pass # ignore by default; override if necessary
# --------------------------------------------------------------------
##
# (Helper) Parses a PythonDoc comment into an PythonDoc info structure.
#
# @param parser Parser instance (provides a warning method).
# @param lineno Line number where this comment starts.
# @param comment A list of text line making up the comment.
# @param dedent If true, strip leading whitespace from all comment
# lines except the first one.
# @return An element tree containing XHTML data.
# @defreturn Element.
def parsecomment(parser, lineno, comment, dedent=0):
subject_info = ET.Element("info")
# untabify
for ix in range(len(comment)):
comment[ix] = string.expandtabs(comment[ix])
if dedent:
margin = None
for ix in range(1, len(comment)):
s = string.lstrip(comment[ix])
if not s:
continue
m = len(comment[ix]) - len(s)
if margin is None:
margin = m
else:
margin = min(m, margin)
if margin:
for ix in range(1, len(comment)):
comment[ix] = comment[ix][margin:]
for ix, tag, text in gettags(comment):
pos = lineno + ix + 1, 0
# check tag name
if tag is None:
tag = "description"
else:
if tag not in TAGS:
parser.warning(
pos,
"unknown tag in description: @%s", tag
)
if tag in ("throws", "exception"):
tag = "exception" # PythonDoc extension
# deal with "named" tags
if tag in ("param", "keyparam", "exception"):
text = string.split(text, " ", 1)
name = text[0]
if len(text) > 1:
text = string.lstrip(text[1])
else:
text = ""
else:
name = None
tag_elem = parsehtml(parser, tag, text, pos[0])
# generate summaries
if tag == "description":
summary = getsummary(tag_elem)
if summary:
elem = ET.SubElement(subject_info, "summary")
elem.text = summary
subject_info.append(tag_elem)
if name:
tag_elem.set("name", name)
return subject_info
##
# Module parser.
# <p>
# This class implements the PythonDoc source code scanner. It reads
# source code from a file or a file-like object, and builds an element
# tree with information about the module.
# <p>
# Note that the constructor only sets things up for parsing. Use the
# {@link ModuleParser.parse} method to parse the file. Or for
# convenience, use the {@link parse} function to create a parser
# object and parse a given file.
#
# @param file Name of the module source file, or a file object. If a
# file object is used, it must provide a <b>name</b> attribute and
# a <b>readline</b> method.
# @param prefix Optional name prefix. If given, this is prepended to
# the module name. For example, if the prefix is set to "prefix"
# and the module filename is "name.py", the module is assumed to
# contain the "prefix.name" namespace.
class ModuleParser:
##
# Module name.
name = None
def __init__(self, file, prefix=None):
if hasattr(file, "readline"):
self.file = file
self.filename = file.name
else:
self.file = None
self.filename = file
name = os.path.splitext(os.path.basename(self.filename))[0]
if prefix and prefix != ".":
name = prefix + "." + name
self.name = name
self.stack = [
ET.Element(
"module",
name=name, filename=self.filename
)
]
self.indent = 0
self.scope = [] # list of (indent, tag, name, ...) tuples
self.handler = self.look_for_encoding
self.encoding = ENCODING
##
# Parses the file.
#
# @keyparam docstring If true, look for markup in docstrings.
# @return An element tree containing information about the module.
# @defreturn Element.
# @exception IOError If the file could not be opened.
def parse(self, docstring=0):
if self.file is None:
file = open(self.filename)
else:
file = self.file
try:
tokenize.tokenize(file.readline, self.handle_token)
except tokenize.TokenError, v:
message, lineno = v
self.warning(lineno, "exception in tokenizer: %s", message)
if len(self.stack) != 1:
pass # FIXME: print warning?
tree = self.stack[0] # may be incomplete
# fixup internal links
# 1) find all named elements
elems = {}
for elem in tree.getiterator():
name = elem.get("name")
if name:
elems[name] = elem
# 2) find all link anchors
for elem in tree.getiterator("a"):
href = elem.get("href")
if href[:5] == "link:":
# FIXME: add support for external links
href = href[5:]
if href[:1] == "#":
href = href[1:]
target = elems.get(self.name + "." + href)
if target:
href = "#" + target.get("name") + "-" + target.tag
elem.set("href", href)
if docstring:
# look for markup in docstrings
for info in tree.getiterator("info"):
docstring = info.findtext("docstring")
if not docstring:
continue
comment = docstring.split("\n")
newinfo = parsecomment(self, 0, comment, dedent=1)
for elem in info:
if newinfo.find(elem.tag) is None:
newinfo.append(elem)
info[:] = newinfo
return tree
##
# Prints a warning message to standard output.
#
# @param position A (line, column) tuple. The column can be set
# to None if not known (or not relevant).
# @param format Message or format string.
# @param *args Optional arguments.
def warning(self, position, format, *args):
line, column = position
message = "%s:%d: WARNING: %s" % (self.filename, line, format % args)
sys.stderr.write(message)
sys.stderr.write("\n")
##
# Dispatches tokens to the current handler. Each handler should
# return the handler to call for the next token.
# <p>
# This method also handles indentation and dedentation tokens,
# and manages the scope stack.
def handle_token(self, *args):
# dispatch incoming tokens to the current handler
if DEBUG > 1:
print self.handler.im_func.func_name, self.indent,
print tokenize.tok_name[args[0]], repr(args[1])
if args[0] == tokenize.DEDENT:
self.indent = self.indent - 1
while self.scope and self.scope[-1][0] >= self.indent:
del self.scope[-1]
del self.stack[-1]
self.handler = apply(self.handler, args)
if args[0] == tokenize.INDENT:
self.indent = self.indent + 1
##
# (Token handler) Scans for encoding directive.
def look_for_encoding(self, type, token, start, end, line):
if type == tokenize.COMMENT:
if string.rstrip(token) == "##":
return self.look_for_pythondoc(type, token, start, end, line)
m = re.search("coding[:=]\s*([-_.\w]+)", token)
if m:
self.encoding = m.group(1)
return self.look_for_pythondoc
if start[0] > 2:
return self.look_for_pythondoc
return self.look_for_encoding
##
# (Token handler) Scans for PythonDoc comments.
def look_for_pythondoc(self, type, token, start, end, line):
if type == tokenize.COMMENT and string.rstrip(token) == "##":
# found a comment: set things up for comment processing
self.comment_start = start
self.comment = []
return self.process_comment_body
else:
# deal with "bare" subjects
if token == "def" or token == "class":
self.subject_indent = self.indent
self.subject_parens = 0
self.subject_start = self.comment_start = None
self.subject = []
return self.process_subject(type, token, start, end, line)
return self.look_for_pythondoc
##
# (Token handler) Processes a comment body. This handler adds
# comment lines to the current comment.
def process_comment_body(self, type, token, start, end, line):
if type == tokenize.COMMENT:
if start[1] != self.comment_start[1]:
self.warning(
start,
"comment line should be aligned with marker"
)
line = string.rstrip(token)
if line == "##":
# handle module comments (experimental)
# FIXME: add more consistency checks?
if self.stack[0].find("info") is not None:
self.warning(
self.comment_start,
"multiple module comments are not allowed"
)
# FIXME: ignore additional comments?
self.process_subject_info(None, self.stack[0])
return self.look_for_pythondoc
elif line[:2] == "# ":
line = line[2:]
elif line[:1] == "#":
line = line[1:]
self.comment.append(line)
else:
if not self.comment:
self.warning(
self.comment_start,
"found pythondoc marker but no comment body"
)
return self.look_for_pythondoc
self.subject_start = None
self.subject = []
if type != tokenize.NL:
return self.process_subject(type, token, start, end, line)
return self.process_subject # end of comment
return self.process_comment_body
##
# (Token handler) Processes the comment subject. The subject can
# be either a plain variable, or a function/method or class
# definition.
# <p>
# This method is also used to process "bare" subjects; that is,
# functions, methods, and classes that don't have PythonDoc
# markup. In that case, the comment_start variable is set to
# None.
def process_subject(self, type, token, start, end, line):
# got an item; deal with it
if self.subject:
# method/function/class definition
definition = self.subject[0] in ("def", "class")
if definition:
if type not in WHITESPACE_TOKEN:
if token == "(":
self.subject_parens = self.subject_parens + 1
elif token == ")":
self.subject_parens = self.subject_parens - 1
if self.subject_parens or token != ":":
self.subject.append(token)
return self.process_subject
else:
# simple assignment
if token != "=":
self.warning(
self.subject_start,
"bad subject %s; ignoring description",
repr(self.subject[0])
)
# might be a pythondoc marker; pass it to the scanner
return self.look_for_pythondoc(
type, token, start, end, line
)
# FIXME: keep adding stuff until end of expression
else:
if type in WHITESPACE_TOKEN:
return self.process_subject
if type == tokenize.COMMENT:
self.warning(
start,
"comment between description and subject; " +
"ignoring description"
)
# might be a pythondoc marker; pass it to the scanner
return self.look_for_pythondoc(
type, token, start, end, line
)
# FIXME: check token type!
# the @ token type is currently tokenize.ERRORTOKEN; hopefully
# this will change before 2.4 final
if token == "@":
self.decorator_parens = 0
return self.skip_decorator
self.subject_start = start
self.subject.append(token)
if token in ("def", "class"):
# handle single-line subjects
while self.scope and self.scope[-1][0] >= self.indent:
self.scope.pop()
self.stack.pop()
self.subject_indent = self.indent
self.subject_parens = 0
return self.process_subject
# check if this is a method or a function
method = self.scope and self.scope[-1][1] == "class"
# calculate fully qualified subject name
name = [self.name]
for s in self.scope:
name.append(s[2])
if definition:
name.append(self.subject[1])
else:
name.append(self.subject[0])
# calculate subject definition statement
statement = []
for part in self.subject:
if part in ("class", "def"):
continue
statement.append(part)
if part == ",":
statement.append(" ")
if self.subject[0] == "def" and method:
# ignore the first argument for methods
# 'name', '(', 'self', ',', ' ', ...)
del statement[2:min(5, len(statement)-1)]
statement = string.join(statement, "")
# create subject element
if self.subject[0] == "class":
subject_elem = ET.Element("class")
elif self.subject[0] == "def":
if method:
subject_elem = ET.Element("method")
else:
subject_elem = ET.Element("function")
else:
subject_elem = ET.Element("variable")
self.stack[-1].append(subject_elem)
# add new subject to the scope and element stacks
if definition:
self.scope.append((self.subject_indent,) + tuple(self.subject))
self.stack.append(subject_elem)
subject_info = self.process_subject_info(name, subject_elem)
# add local name to info
elem = ET.Element("name")
elem.text = name[-1]
subject_info.insert(0, elem)
name = string.join(name, ".")
subject_elem.set("name", name)
subject_elem.set("lineno", str(self.subject_start[0]))
if subject_info.find("def") is None and statement:
# add subject definition (unless specified in comment)
elem = ET.Element("def")
elem.text = statement
# add to front, to make the XML easier to read
subject_info.insert(0, elem)
if definition:
return self.look_for_docstring(type, token, start, end, line)
else:
return self.look_for_pythondoc(type, token, start, end, line)
##
# (Token handler) Skips a decorator.
def skip_decorator(self, type, token, start, end, line):
if token == "(":
self.decorator_parens = self.decorator_parens + 1
elif token == ")":
self.decorator_parens = self.decorator_parens - 1
if self.decorator_parens or type != tokenize.NEWLINE:
return self.skip_decorator
return self.process_subject
##
# (Token handler helper) Processes a PythonDoc comment. This
# method creates an "info" element based on the current comment,
# and attaches it to the current subject element.
#
# @param subject_name Subject name (or None if the name is not known).
# @param subject_elem The current subject element.
# @return The info element. Note that this element has already
# been attached to the subject element.
# @defreturn Element
def process_subject_info(self, subject_name, subject_elem):
# process pythondoc comment (if any)
if self.comment_start:
subject_info = parsecomment(
self, self.comment_start[0], self.comment
)
else:
subject_info = ET.Element("info")
subject_elem.append(subject_info)
if DEBUG:
if subject_name:
subject_name = string.join(subject_name, ".")
else:
subject_name = "<module>"
print "---", subject_name
prefix = " " * len(self.scope)
for line in self.comment:
print prefix + line
return subject_info
##
# (Token handler) Look for docstring inside a definition.
def look_for_docstring(self, type, token, start, end, line):
if type in WHITESPACE_TOKEN or token == ":":
return self.look_for_docstring
if type == tokenize.STRING:
subject_elem = self.stack[-1]
subject_info = subject_elem.find("info")
if subject_info is not None:
elem = ET.SubElement(subject_info, "docstring")
# FIXME: add string sanity check here, before doing eval
elem.text = eval(token)
return self.skip_subject_body(type, token, start, end, line)
##
# (Token handler) Skips over the subject body.
def skip_subject_body(self, type, token, start, end, line):
# for now, just hand control back to the pythondoc scanner,
# and let it skip over the subject body while looking for the
# next marker.
return self.look_for_pythondoc(type, token, start, end, line)
##
# Parses a module.
# <p>
# This function creates a {@link #ModuleParser} instance, and uses it
# to parse the given file. For details, see {@linkplain #ModuleParser
# the <b>ModuleParser</b> documentation}.
#
# @param file Name of the module source file, or a file object.
# @param prefix Optional name prefix.
# @keyparam docstring If true, look for markup in docstrings.
# @return An element tree containing the module description.
# @defreturn Element.
# @exception IOError If the file could not be found, or could not
# be opened for reading.
def parse(file, prefix=None, docstring=0):
m = ModuleParser(file, prefix)
return m.parse(docstring=docstring)
# --------------------------------------------------------------------
# default formatter
if sys.version[:3] == "1.5":
_escape = re.compile(r"[&<>\"\x80-\xff]") # 1.5.2
else:
_escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]"'))
_escape_map = {
"&": "&",
"<": "<",
">": ">",
'"': """,
}
##
# Encodes reserved HTML characters and non-ASCII characters as HTML
# character references.
#
# @def html_encode(text)
# @param text Source text.
# @return An encoded string.