comparison mercurial/cmdutil.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 18b5b2c9d921
children fefcafda10b8
comparison
equal deleted inserted replaced
24305:867c3649be5d 24306:6ddc86eedc3b
396 pathname=None): 396 pathname=None):
397 397
398 writable = mode not in ('r', 'rb') 398 writable = mode not in ('r', 'rb')
399 399
400 if not pat or pat == '-': 400 if not pat or pat == '-':
401 fp = writable and repo.ui.fout or repo.ui.fin 401 if writable:
402 fp = repo.ui.fout
403 else:
404 fp = repo.ui.fin
402 if util.safehasattr(fp, 'fileno'): 405 if util.safehasattr(fp, 'fileno'):
403 return os.fdopen(os.dup(fp.fileno()), mode) 406 return os.fdopen(os.dup(fp.fileno()), mode)
404 else: 407 else:
405 # if this fp can't be duped properly, return 408 # if this fp can't be duped properly, return
406 # a dummy object that can be closed 409 # a dummy object that can be closed
472 dryrun = opts.get("dry_run") 475 dryrun = opts.get("dry_run")
473 wctx = repo[None] 476 wctx = repo[None]
474 477
475 def walkpat(pat): 478 def walkpat(pat):
476 srcs = [] 479 srcs = []
477 badstates = after and '?' or '?r' 480 if after:
481 badstates = '?'
482 else:
483 badstates = '?r'
478 m = scmutil.match(repo[None], [pat], opts, globbed=True) 484 m = scmutil.match(repo[None], [pat], opts, globbed=True)
479 for abs in repo.walk(m): 485 for abs in repo.walk(m):
480 state = repo.dirstate[abs] 486 state = repo.dirstate[abs]
481 rel = m.rel(abs) 487 rel = m.rel(abs)
482 exact = m.exact(abs) 488 exact = m.exact(abs)
691 runargs=None, appendpid=False): 697 runargs=None, appendpid=False):
692 '''Run a command as a service.''' 698 '''Run a command as a service.'''
693 699
694 def writepid(pid): 700 def writepid(pid):
695 if opts['pid_file']: 701 if opts['pid_file']:
696 mode = appendpid and 'a' or 'w' 702 if appendpid:
703 mode = 'a'
704 else:
705 mode = 'w'
697 fp = open(opts['pid_file'], mode) 706 fp = open(opts['pid_file'], mode)
698 fp.write(str(pid) + '\n') 707 fp.write(str(pid) + '\n')
699 fp.close() 708 fp.close()
700 709
701 if opts['daemon'] and not opts['daemon_pipefds']: 710 if opts['daemon'] and not opts['daemon_pipefds']:
927 node = ctx.node() 936 node = ctx.node()
928 parents = [p.node() for p in ctx.parents() if p] 937 parents = [p.node() for p in ctx.parents() if p]
929 branch = ctx.branch() 938 branch = ctx.branch()
930 if switch_parent: 939 if switch_parent:
931 parents.reverse() 940 parents.reverse()
932 prev = (parents and parents[0]) or nullid 941
942 if parents:
943 prev = parents[0]
944 else:
945 prev = nullid
933 946
934 shouldclose = False 947 shouldclose = False
935 if not fp and len(template) > 0: 948 if not fp and len(template) > 0:
936 desc_lines = ctx.description().rstrip().split('\n') 949 desc_lines = ctx.description().rstrip().split('\n')
937 desc = desc_lines[0] #Commit always has a first line. 950 desc = desc_lines[0] #Commit always has a first line.
1065 return 1078 return
1066 1079
1067 log = self.repo.changelog 1080 log = self.repo.changelog
1068 date = util.datestr(ctx.date()) 1081 date = util.datestr(ctx.date())
1069 1082
1070 hexfunc = self.ui.debugflag and hex or short 1083 if self.ui.debugflag:
1084 hexfunc = hex
1085 else:
1086 hexfunc = short
1071 1087
1072 parents = [(p, hexfunc(log.node(p))) 1088 parents = [(p, hexfunc(log.node(p)))
1073 for p in self._meaningful_parentrevs(log, rev)] 1089 for p in self._meaningful_parentrevs(log, rev)]
1074 1090
1075 # i18n: column positioning for "hg log" 1091 # i18n: column positioning for "hg log"
1864 } 1880 }
1865 1881
1866 opts = dict(opts) 1882 opts = dict(opts)
1867 # follow or not follow? 1883 # follow or not follow?
1868 follow = opts.get('follow') or opts.get('follow_first') 1884 follow = opts.get('follow') or opts.get('follow_first')
1869 followfirst = opts.get('follow_first') and 1 or 0 1885 if opts.get('follow_first'):
1886 followfirst = 1
1887 else:
1888 followfirst = 0
1870 # --follow with FILE behaviour depends on revs... 1889 # --follow with FILE behaviour depends on revs...
1871 it = iter(revs) 1890 it = iter(revs)
1872 startrev = it.next() 1891 startrev = it.next()
1873 try: 1892 try:
1874 followdescendants = startrev < it.next() 1893 followdescendants = startrev < it.next()