Mercurial > public > mercurial-scm > hg
comparison mercurial/commands.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 | 0b94b68aace9 |
children | fefcafda10b8 |
comparison
equal
deleted
inserted
replaced
24305:867c3649be5d | 24306:6ddc86eedc3b |
---|---|
275 # --follow is deprecated and now just an alias for -f/--file | 275 # --follow is deprecated and now just an alias for -f/--file |
276 # to mimic the behavior of Mercurial before version 1.5 | 276 # to mimic the behavior of Mercurial before version 1.5 |
277 opts['file'] = True | 277 opts['file'] = True |
278 | 278 |
279 fm = ui.formatter('annotate', opts) | 279 fm = ui.formatter('annotate', opts) |
280 datefunc = ui.quiet and util.shortdate or util.datestr | 280 if ui.quiet: |
281 datefunc = util.shortdate | |
282 else: | |
283 datefunc = util.datestr | |
281 hexfn = fm.hexfunc | 284 hexfn = fm.hexfunc |
282 | 285 |
283 opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser), | 286 opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser), |
284 ('number', ' ', lambda x: x[0].rev(), str), | 287 ('number', ' ', lambda x: x[0].rev(), str), |
285 ('changeset', ' ', lambda x: hexfn(x[0].node()), str), | 288 ('changeset', ' ', lambda x: hexfn(x[0].node()), str), |
662 def extendbisectrange(nodes, good): | 665 def extendbisectrange(nodes, good): |
663 # bisect is incomplete when it ends on a merge node and | 666 # bisect is incomplete when it ends on a merge node and |
664 # one of the parent was not checked. | 667 # one of the parent was not checked. |
665 parents = repo[nodes[0]].parents() | 668 parents = repo[nodes[0]].parents() |
666 if len(parents) > 1: | 669 if len(parents) > 1: |
667 side = good and state['bad'] or state['good'] | 670 if good: |
671 side = state['bad'] | |
672 else: | |
673 side = state['good'] | |
668 num = len(set(i.node() for i in parents) & set(side)) | 674 num = len(set(i.node() for i in parents) & set(side)) |
669 if num == 1: | 675 if num == 1: |
670 return parents[0].ancestor(parents[1]) | 676 return parents[0].ancestor(parents[1]) |
671 return None | 677 return None |
672 | 678 |
3668 for i in xrange(blo, bhi): | 3674 for i in xrange(blo, bhi): |
3669 yield ('+', b[i]) | 3675 yield ('+', b[i]) |
3670 | 3676 |
3671 def display(fn, ctx, pstates, states): | 3677 def display(fn, ctx, pstates, states): |
3672 rev = ctx.rev() | 3678 rev = ctx.rev() |
3673 datefunc = ui.quiet and util.shortdate or util.datestr | 3679 if ui.quiet: |
3680 datefunc = util.shortdate | |
3681 else: | |
3682 datefunc = util.datestr | |
3674 found = False | 3683 found = False |
3675 @util.cachefunc | 3684 @util.cachefunc |
3676 def binary(): | 3685 def binary(): |
3677 flog = getfile(fn) | 3686 flog = getfile(fn) |
3678 return util.binary(flog.read(ctx.filenode(fn))) | 3687 return util.binary(flog.read(ctx.filenode(fn))) |
3944 | 3953 |
3945 if not repo and not source: | 3954 if not repo and not source: |
3946 raise util.Abort(_("there is no Mercurial repository here " | 3955 raise util.Abort(_("there is no Mercurial repository here " |
3947 "(.hg not found)")) | 3956 "(.hg not found)")) |
3948 | 3957 |
3949 hexfunc = ui.debugflag and hex or short | 3958 if ui.debugflag: |
3959 hexfunc = hex | |
3960 else: | |
3961 hexfunc = short | |
3950 default = not (num or id or branch or tags or bookmarks) | 3962 default = not (num or id or branch or tags or bookmarks) |
3951 output = [] | 3963 output = [] |
3952 revs = [] | 3964 revs = [] |
3953 | 3965 |
3954 if source: | 3966 if source: |
4340 | 4352 |
4341 See :hg:`help files` for a more versatile command. | 4353 See :hg:`help files` for a more versatile command. |
4342 | 4354 |
4343 Returns 0 if a match is found, 1 otherwise. | 4355 Returns 0 if a match is found, 1 otherwise. |
4344 """ | 4356 """ |
4345 end = opts.get('print0') and '\0' or '\n' | 4357 if opts.get('print0'): |
4358 end = '\0' | |
4359 else: | |
4360 end = '\n' | |
4346 rev = scmutil.revsingle(repo, opts.get('rev'), None).node() | 4361 rev = scmutil.revsingle(repo, opts.get('rev'), None).node() |
4347 | 4362 |
4348 ret = 1 | 4363 ret = 1 |
4349 ctx = repo[rev] | 4364 ctx = repo[rev] |
4350 m = scmutil.match(ctx, pats, opts, default='relglob') | 4365 m = scmutil.match(ctx, pats, opts, default='relglob') |
4504 copies = [] | 4519 copies = [] |
4505 for fn in ctx.files(): | 4520 for fn in ctx.files(): |
4506 rename = getrenamed(fn, rev) | 4521 rename = getrenamed(fn, rev) |
4507 if rename: | 4522 if rename: |
4508 copies.append((fn, rename[0])) | 4523 copies.append((fn, rename[0])) |
4509 revmatchfn = filematcher and filematcher(ctx.rev()) or None | 4524 if filematcher: |
4525 revmatchfn = filematcher(ctx.rev()) | |
4526 else: | |
4527 revmatchfn = None | |
4510 displayer.show(ctx, copies=copies, matchfn=revmatchfn) | 4528 displayer.show(ctx, copies=copies, matchfn=revmatchfn) |
4511 if displayer.flush(rev): | 4529 if displayer.flush(rev): |
4512 count += 1 | 4530 count += 1 |
4513 | 4531 |
4514 displayer.close() | 4532 displayer.close() |
5548 | 5566 |
5549 # this way we can check if something was given in the command-line | 5567 # this way we can check if something was given in the command-line |
5550 if opts.get('port'): | 5568 if opts.get('port'): |
5551 opts['port'] = util.getport(opts.get('port')) | 5569 opts['port'] = util.getport(opts.get('port')) |
5552 | 5570 |
5553 baseui = repo and repo.baseui or ui | 5571 if repo: |
5572 baseui = repo.baseui | |
5573 else: | |
5574 baseui = ui | |
5554 optlist = ("name templates style address port prefix ipv6" | 5575 optlist = ("name templates style address port prefix ipv6" |
5555 " accesslog errorlog certificate encoding") | 5576 " accesslog errorlog certificate encoding") |
5556 for o in optlist.split(): | 5577 for o in optlist.split(): |
5557 val = opts.get(o, '') | 5578 val = opts.get(o, '') |
5558 if val in (None, ''): # should check against default options instead | 5579 if val in (None, ''): # should check against default options instead |
5698 node2 = scmutil.revsingle(repo, change, None).node() | 5719 node2 = scmutil.revsingle(repo, change, None).node() |
5699 node1 = repo[node2].p1().node() | 5720 node1 = repo[node2].p1().node() |
5700 else: | 5721 else: |
5701 node1, node2 = scmutil.revpair(repo, revs) | 5722 node1, node2 = scmutil.revpair(repo, revs) |
5702 | 5723 |
5703 cwd = (pats and repo.getcwd()) or '' | 5724 if pats: |
5704 end = opts.get('print0') and '\0' or '\n' | 5725 cwd = repo.getcwd() |
5726 else: | |
5727 cwd = '' | |
5728 | |
5729 if opts.get('print0'): | |
5730 end = '\0' | |
5731 else: | |
5732 end = '\n' | |
5705 copy = {} | 5733 copy = {} |
5706 states = 'modified added removed deleted unknown ignored clean'.split() | 5734 states = 'modified added removed deleted unknown ignored clean'.split() |
5707 show = [k for k in states if opts.get(k)] | 5735 show = [k for k in states if opts.get(k)] |
5708 if opts.get('all'): | 5736 if opts.get('all'): |
5709 show += ui.quiet and (states[:4] + ['clean']) or states | 5737 show += ui.quiet and (states[:4] + ['clean']) or states |
5710 if not show: | 5738 if not show: |
5711 show = ui.quiet and states[:4] or states[:5] | 5739 if ui.quiet: |
5740 show = states[:4] | |
5741 else: | |
5742 show = states[:5] | |
5712 | 5743 |
5713 stat = repo.status(node1, node2, scmutil.match(repo[node2], pats, opts), | 5744 stat = repo.status(node1, node2, scmutil.match(repo[node2], pats, opts), |
5714 'ignored' in show, 'clean' in show, 'unknown' in show, | 5745 'ignored' in show, 'clean' in show, 'unknown' in show, |
5715 opts.get('subrepos')) | 5746 opts.get('subrepos')) |
5716 changestates = zip(states, 'MAR!?IC', stat) | 5747 changestates = zip(states, 'MAR!?IC', stat) |
6027 raise util.Abort(_("--rev and --remove are incompatible")) | 6058 raise util.Abort(_("--rev and --remove are incompatible")) |
6028 if opts.get('rev'): | 6059 if opts.get('rev'): |
6029 rev_ = opts['rev'] | 6060 rev_ = opts['rev'] |
6030 message = opts.get('message') | 6061 message = opts.get('message') |
6031 if opts.get('remove'): | 6062 if opts.get('remove'): |
6032 expectedtype = opts.get('local') and 'local' or 'global' | 6063 if opts.get('local'): |
6064 expectedtype = 'local' | |
6065 else: | |
6066 expectedtype = 'global' | |
6067 | |
6033 for n in names: | 6068 for n in names: |
6034 if not repo.tagtype(n): | 6069 if not repo.tagtype(n): |
6035 raise util.Abort(_("tag '%s' does not exist") % n) | 6070 raise util.Abort(_("tag '%s' does not exist") % n) |
6036 if repo.tagtype(n) != expectedtype: | 6071 if repo.tagtype(n) != expectedtype: |
6037 if expectedtype == 'global': | 6072 if expectedtype == 'global': |