##// END OF EJS Templates
coding style: fix yield used as a function
Thomas Arendsen Hein -
r13077:6b8d2ee2 default
parent child Browse files
Show More
@@ -1,172 +1,172
1 1 import os, sys, textwrap
2 2 # import from the live mercurial repo
3 3 sys.path.insert(0, "..")
4 4 # fall back to pure modules if required C extensions are not available
5 5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
6 6 from mercurial import demandimport; demandimport.enable()
7 7 from mercurial import encoding
8 8 from mercurial.commands import table, globalopts
9 9 from mercurial.i18n import _
10 10 from mercurial.help import helptable
11 11 from mercurial import extensions
12 12
13 13 def get_desc(docstr):
14 14 if not docstr:
15 15 return "", ""
16 16 # sanitize
17 17 docstr = docstr.strip("\n")
18 18 docstr = docstr.rstrip()
19 19 shortdesc = docstr.splitlines()[0].strip()
20 20
21 21 i = docstr.find("\n")
22 22 if i != -1:
23 23 desc = docstr[i + 2:]
24 24 else:
25 25 desc = shortdesc
26 26
27 27 desc = textwrap.dedent(desc)
28 28
29 29 return (shortdesc, desc)
30 30
31 31 def get_opts(opts):
32 32 for opt in opts:
33 33 if len(opt) == 5:
34 34 shortopt, longopt, default, desc, optlabel = opt
35 35 else:
36 36 shortopt, longopt, default, desc = opt
37 37 allopts = []
38 38 if shortopt:
39 39 allopts.append("-%s" % shortopt)
40 40 if longopt:
41 41 allopts.append("--%s" % longopt)
42 42 desc += default and _(" (default: %s)") % default or ""
43 yield(", ".join(allopts), desc)
43 yield (", ".join(allopts), desc)
44 44
45 45 def get_cmd(cmd, cmdtable):
46 46 d = {}
47 47 attr = cmdtable[cmd]
48 48 cmds = cmd.lstrip("^").split("|")
49 49
50 50 d['cmd'] = cmds[0]
51 51 d['aliases'] = cmd.split("|")[1:]
52 52 d['desc'] = get_desc(attr[0].__doc__)
53 53 d['opts'] = list(get_opts(attr[1]))
54 54
55 55 s = 'hg ' + cmds[0]
56 56 if len(attr) > 2:
57 57 if not attr[2].startswith('hg'):
58 58 s += ' ' + attr[2]
59 59 else:
60 60 s = attr[2]
61 61 d['synopsis'] = s.strip()
62 62
63 63 return d
64 64
65 65 def section(ui, s):
66 66 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
67 67
68 68 def subsection(ui, s):
69 69 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
70 70
71 71 def subsubsection(ui, s):
72 72 ui.write("%s\n%s\n\n" % (s, "." * encoding.colwidth(s)))
73 73
74 74 def subsubsubsection(ui, s):
75 75 ui.write("%s\n%s\n\n" % (s, "#" * encoding.colwidth(s)))
76 76
77 77
78 78 def show_doc(ui):
79 79 # print options
80 80 section(ui, _("Options"))
81 81 for optstr, desc in get_opts(globalopts):
82 82 ui.write("%s\n %s\n\n" % (optstr, desc))
83 83
84 84 # print cmds
85 85 section(ui, _("Commands"))
86 86 commandprinter(ui, table, subsection)
87 87
88 88 # print topics
89 89 for names, sec, doc in helptable:
90 90 for name in names:
91 91 ui.write(".. _%s:\n" % name)
92 92 ui.write("\n")
93 93 section(ui, sec)
94 94 if hasattr(doc, '__call__'):
95 95 doc = doc()
96 96 ui.write(doc)
97 97 ui.write("\n")
98 98
99 99 section(ui, _("Extensions"))
100 100 ui.write(_("This section contains help for extensions that are distributed "
101 101 "together with Mercurial. Help for other extensions is available "
102 102 "in the help system."))
103 103 ui.write("\n\n"
104 104 ".. contents::\n"
105 105 " :class: htmlonly\n"
106 106 " :local:\n"
107 107 " :depth: 1\n\n")
108 108
109 109 for extensionname in sorted(allextensionnames()):
110 110 mod = extensions.load(None, extensionname, None)
111 111 subsection(ui, extensionname)
112 112 ui.write("%s\n\n" % mod.__doc__)
113 113 cmdtable = getattr(mod, 'cmdtable', None)
114 114 if cmdtable:
115 115 subsubsection(ui, _('Commands'))
116 116 commandprinter(ui, cmdtable, subsubsubsection)
117 117
118 118 def commandprinter(ui, cmdtable, sectionfunc):
119 119 h = {}
120 120 for c, attr in cmdtable.items():
121 121 f = c.split("|")[0]
122 122 f = f.lstrip("^")
123 123 h[f] = c
124 124 cmds = h.keys()
125 125 cmds.sort()
126 126
127 127 for f in cmds:
128 128 if f.startswith("debug"):
129 129 continue
130 130 d = get_cmd(h[f], cmdtable)
131 131 sectionfunc(ui, d['cmd'])
132 132 # synopsis
133 133 ui.write("::\n\n")
134 134 synopsislines = d['synopsis'].splitlines()
135 135 for line in synopsislines:
136 136 # some commands (such as rebase) have a multi-line
137 137 # synopsis
138 138 ui.write(" %s\n" % line)
139 139 ui.write('\n')
140 140 # description
141 141 ui.write("%s\n\n" % d['desc'][1])
142 142 # options
143 143 opt_output = list(d['opts'])
144 144 if opt_output:
145 145 opts_len = max([len(line[0]) for line in opt_output])
146 146 ui.write(_("options:\n\n"))
147 147 for optstr, desc in opt_output:
148 148 if desc:
149 149 s = "%-*s %s" % (opts_len, optstr, desc)
150 150 else:
151 151 s = optstr
152 152 ui.write("%s\n" % s)
153 153 ui.write("\n")
154 154 # aliases
155 155 if d['aliases']:
156 156 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
157 157
158 158
159 159 def allextensionnames():
160 160 extensionnames = []
161 161
162 162 extensionsdictionary = extensions.enabled()[0]
163 163 extensionnames.extend(extensionsdictionary.keys())
164 164
165 165 extensionsdictionary = extensions.disabled()[0]
166 166 extensionnames.extend(extensionsdictionary.keys())
167 167
168 168 return extensionnames
169 169
170 170
171 171 if __name__ == "__main__":
172 172 show_doc(sys.stdout)
@@ -1,95 +1,95
1 1 $ cat > correct.py <<EOF
2 2 > def toto(arg1, arg2):
3 3 > del arg2
4 4 > return (5 + 6, 9)
5 5 > EOF
6 6 $ cat > wrong.py <<EOF
7 7 > def toto( arg1, arg2):
8 8 > del(arg2)
9 9 > return ( 5+6, 9)
10 10 > EOF
11 11 $ cat > quote.py <<EOF
12 12 > # let's use quote in comments
13 13 > (''' ( 4x5 )
14 14 > but """\\''' and finally''',
15 15 > """let's fool checkpatch""", '1+2',
16 16 > '"""', 42+1, """and
17 17 > ( 4-1 ) """, "( 1+1 )\" and ")
18 18 > a, '\\\\\\\\', "\\\\\\" x-2", "c-1"
19 19 > EOF
20 20 $ cat > non-py24.py <<EOF
21 21 > # Using builtins that does not exist in Python 2.4
22 22 > if any():
23 23 > x = all()
24 24 > y = format(x)
25 25 >
26 26 > # Do not complain about our own definition
27 27 > def any(x):
28 28 > pass
29 29 > EOF
30 30 $ check_code="$TESTDIR"/../contrib/check-code.py
31 31 $ "$check_code" ./wrong.py ./correct.py ./quote.py ./non-py24.py
32 32 ./wrong.py:1:
33 33 > def toto( arg1, arg2):
34 34 gratuitous whitespace in () or []
35 35 ./wrong.py:2:
36 36 > del(arg2)
37 del isn't a function
37 Python keyword is not a function
38 38 ./wrong.py:3:
39 39 > return ( 5+6, 9)
40 40 missing whitespace in expression
41 41 gratuitous whitespace in () or []
42 42 ./quote.py:5:
43 43 > '"""', 42+1, """and
44 44 missing whitespace in expression
45 45 ./non-py24.py:2:
46 46 > if any():
47 47 any/all/format not available in Python 2.4
48 48 ./non-py24.py:3:
49 49 > x = all()
50 50 any/all/format not available in Python 2.4
51 51 ./non-py24.py:4:
52 52 > y = format(x)
53 53 any/all/format not available in Python 2.4
54 54 [1]
55 55
56 56 $ cat > is-op.py <<EOF
57 57 > # is-operator comparing number or string literal
58 58 > x = None
59 59 > y = x is 'foo'
60 60 > y = x is "foo"
61 61 > y = x is 5346
62 62 > y = x is -6
63 63 > y = x is not 'foo'
64 64 > y = x is not "foo"
65 65 > y = x is not 5346
66 66 > y = x is not -6
67 67 > EOF
68 68
69 69 $ "$check_code" ./is-op.py
70 70 ./is-op.py:3:
71 71 > y = x is 'foo'
72 72 object comparison with literal
73 73 ./is-op.py:4:
74 74 > y = x is "foo"
75 75 object comparison with literal
76 76 ./is-op.py:5:
77 77 > y = x is 5346
78 78 object comparison with literal
79 79 ./is-op.py:6:
80 80 > y = x is -6
81 81 object comparison with literal
82 82 ./is-op.py:7:
83 83 > y = x is not 'foo'
84 84 object comparison with literal
85 85 ./is-op.py:8:
86 86 > y = x is not "foo"
87 87 object comparison with literal
88 88 ./is-op.py:9:
89 89 > y = x is not 5346
90 90 object comparison with literal
91 91 ./is-op.py:10:
92 92 > y = x is not -6
93 93 object comparison with literal
94 94 [1]
95 95
General Comments 0
You need to be logged in to leave comments. Login now