Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hgweb/webcommands.py @ 24306:6ddc86eedc3b
style: kill ersatz if-else ternary operators
Although Python supports `X = Y if COND else Z`, this was only
introduced in Python 2.5. Since we have to support Python 2.4, it was
a very common thing to write instead `X = COND and Y or Z`, which is a
bit obscure at a glance. It requires some intricate knowledge of
Python to understand how to parse these one-liners.
We change instead all of these one-liners to 4-liners. This was
executed with the following perlism:
find -name "*.py" -exec perl -pi -e 's,(\s*)([\.\w]+) = \(?(\S+)\s+and\s+(\S*)\)?\s+or\s+(\S*)$,$1if $3:\n$1 $2 = $4\n$1else:\n$1 $2 = $5,' {} \;
I tweaked the following cases from the automatic Perl output:
prev = (parents and parents[0]) or nullid
port = (use_ssl and 443 or 80)
cwd = (pats and repo.getcwd()) or ''
rename = fctx and webutil.renamelink(fctx) or []
ctx = fctx and fctx or ctx
self.base = (mapfile and os.path.dirname(mapfile)) or ''
I also added some newlines wherever they seemd appropriate for readability
There are probably a few ersatz ternary operators still in the code
somewhere, lurking away from the power of a simple regex.
author | Jordi Guti?rrez Hermoso <jordigh@octave.org> |
---|---|
date | Fri, 13 Mar 2015 17:00:06 -0400 |
parents | f53b7174facf |
children | bbf1ae6b6a44 |
comparison
equal
deleted
inserted
replaced
24305:867c3649be5d | 24306:6ddc86eedc3b |
---|---|
88 text = fctx.data() | 88 text = fctx.data() |
89 mt = 'application/binary' | 89 mt = 'application/binary' |
90 if guessmime: | 90 if guessmime: |
91 mt = mimetypes.guess_type(path)[0] | 91 mt = mimetypes.guess_type(path)[0] |
92 if mt is None: | 92 if mt is None: |
93 mt = util.binary(text) and 'application/binary' or 'text/plain' | 93 if util.binary(text): |
94 mt = 'application/binary' | |
95 else: | |
96 mt = 'text/plain' | |
94 if mt.startswith('text/'): | 97 if mt.startswith('text/'): |
95 mt += '; charset="%s"' % encoding.encoding | 98 mt += '; charset="%s"' % encoding.encoding |
96 | 99 |
97 req.respond(HTTP_OK, mt, path, body=text) | 100 req.respond(HTTP_OK, mt, path, body=text) |
98 return [] | 101 return [] |
363 | 366 |
364 entry = webutil.changelistentry(web, web.repo[rev], tmpl) | 367 entry = webutil.changelistentry(web, web.repo[rev], tmpl) |
365 entry['parity'] = parity.next() | 368 entry['parity'] = parity.next() |
366 yield entry | 369 yield entry |
367 | 370 |
368 revcount = shortlog and web.maxshortchanges or web.maxchanges | 371 if shortlog: |
372 revcount = web.maxshortchanges | |
373 else: | |
374 revcount = web.maxchanges | |
375 | |
369 if 'revcount' in req.form: | 376 if 'revcount' in req.form: |
370 try: | 377 try: |
371 revcount = int(req.form.get('revcount', [revcount])[0]) | 378 revcount = int(req.form.get('revcount', [revcount])[0]) |
372 revcount = max(revcount, 1) | 379 revcount = max(revcount, 1) |
373 tmpl.defaults['sessionvars']['revcount'] = revcount | 380 tmpl.defaults['sessionvars']['revcount'] = revcount |
781 style = web.config('web', 'style', 'paper') | 788 style = web.config('web', 'style', 'paper') |
782 if 'style' in req.form: | 789 if 'style' in req.form: |
783 style = req.form['style'][0] | 790 style = req.form['style'][0] |
784 | 791 |
785 diffs = webutil.diffs(web.repo, tmpl, ctx, None, [path], parity, style) | 792 diffs = webutil.diffs(web.repo, tmpl, ctx, None, [path], parity, style) |
786 rename = fctx and webutil.renamelink(fctx) or [] | 793 if fctx: |
787 ctx = fctx and fctx or ctx | 794 rename = webutil.renamelink(fctx) |
795 ctx = fctx | |
796 else: | |
797 rename = [] | |
798 ctx = ctx | |
788 return tmpl("filediff", | 799 return tmpl("filediff", |
789 file=path, | 800 file=path, |
790 node=hex(n), | 801 node=hex(n), |
791 rev=ctx.rev(), | 802 rev=ctx.rev(), |
792 date=ctx.date(), | 803 date=ctx.date(), |