Mercurial > hg
comparison hgext/convert/subversion.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 | a8edcb9c1199 |
children | 216fa1ba9993 |
comparison
equal
deleted
inserted
replaced
24305:867c3649be5d | 24306:6ddc86eedc3b |
---|---|
869 # '2007-01-04T17:35:00.902377Z' | 869 # '2007-01-04T17:35:00.902377Z' |
870 date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"]) | 870 date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"]) |
871 if self.ui.configbool('convert', 'localtimezone'): | 871 if self.ui.configbool('convert', 'localtimezone'): |
872 date = makedatetimestamp(date[0]) | 872 date = makedatetimestamp(date[0]) |
873 | 873 |
874 log = message and self.recode(message) or '' | 874 if message: |
875 author = author and self.recode(author) or '' | 875 log = self.recode(message) |
876 else: | |
877 log = '' | |
878 | |
879 if author: | |
880 author = self.recode(author) | |
881 else: | |
882 author = '' | |
883 | |
876 try: | 884 try: |
877 branch = self.module.split("/")[-1] | 885 branch = self.module.split("/")[-1] |
878 if branch == self.trunkname: | 886 if branch == self.trunkname: |
879 branch = None | 887 branch = None |
880 except IndexError: | 888 except IndexError: |
1116 | 1124 |
1117 self.wc = wcpath | 1125 self.wc = wcpath |
1118 self.opener = scmutil.opener(self.wc) | 1126 self.opener = scmutil.opener(self.wc) |
1119 self.wopener = scmutil.opener(self.wc) | 1127 self.wopener = scmutil.opener(self.wc) |
1120 self.childmap = mapfile(ui, self.join('hg-childmap')) | 1128 self.childmap = mapfile(ui, self.join('hg-childmap')) |
1121 self.is_exec = util.checkexec(self.wc) and util.isexec or None | 1129 if util.checkexec(self.wc): |
1130 self.is_exec = util.isexec | |
1131 else: | |
1132 self.is_exec = None | |
1122 | 1133 |
1123 if created: | 1134 if created: |
1124 hook = os.path.join(created, 'hooks', 'pre-revprop-change') | 1135 hook = os.path.join(created, 'hooks', 'pre-revprop-change') |
1125 fp = open(hook, 'w') | 1136 fp = open(hook, 'w') |
1126 fp.write(pre_revprop_change) | 1137 fp.write(pre_revprop_change) |