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