From d68437d5f5eb091a0a9c734c0c3fb971f93f1985 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 18 Apr 2020 03:37:30 -0700 Subject: [PATCH] Rewrite some strings using raw string syntax. Signed-off-by: Anders Kaseorg --- tools/custom_check.py | 40 +++++++++---------- zulip/integrations/log2zulip/log2zulip | 2 +- zulip/integrations/perforce/git_p4.py | 12 +++--- .../bots/link_shortener/link_shortener.py | 8 ++-- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/tools/custom_check.py b/tools/custom_check.py index 0b36693..092ba63 100644 --- a/tools/custom_check.py +++ b/tools/custom_check.py @@ -22,7 +22,7 @@ markdown_whitespace_rules = list([rule for rule in whitespace_rules if rule['pat {'pattern': r'((?~"]', + {'pattern': r" =" + r'[^ =>~"]', 'description': 'Missing whitespace after "="'}, {'pattern': r'":\w[^"]*$', 'description': 'Missing whitespace after ":"'}, @@ -44,25 +44,25 @@ python_rules = RuleList( {'pattern': r"^\s+[#]\w", 'strip': '\n', 'description': 'Missing whitespace after "#"'}, - {'pattern': "assertEquals[(]", + {'pattern': r"assertEquals[(]", 'description': 'Use assertEqual, not assertEquals (which is deprecated).'}, - {'pattern': 'self: Any', + {'pattern': r'self: Any', 'description': 'you can omit Any annotation for self', 'good_lines': ['def foo (self):'], 'bad_lines': ['def foo(self: Any):']}, - {'pattern': "== None", + {'pattern': r"== None", 'description': 'Use `is None` to check whether something is None'}, - {'pattern': "type:[(]", + {'pattern': r"type:[(]", 'description': 'Missing whitespace after ":" in type annotation'}, - {'pattern': "# type [(]", + {'pattern': r"# type [(]", 'description': 'Missing : after type in type annotation'}, - {'pattern': "#type", + {'pattern': r"#type", 'description': 'Missing whitespace after "#" in type annotation'}, - {'pattern': 'if[(]', + {'pattern': r'if[(]', 'description': 'Missing space between if and ('}, - {'pattern': ", [)]", + {'pattern': r", [)]", 'description': 'Unnecessary whitespace between "," and ")"'}, - {'pattern': "% [(]", + {'pattern': r"% [(]", 'description': 'Unnecessary whitespace between "%" and "("'}, # This next check could have false positives, but it seems pretty # rare; if we find any, they can be added to the exclude list for @@ -71,17 +71,17 @@ python_rules = RuleList( 'description': 'Used % comprehension without a tuple'}, {'pattern': r'.*%s.* % \([a-zA-Z0-9_.]*\)$', 'description': 'Used % comprehension without a tuple'}, - {'pattern': '__future__', + {'pattern': r'__future__', 'include_only': {'zulip_bots/zulip_bots/bots/'}, 'description': 'Bots no longer need __future__ imports.'}, - {'pattern': '#!/usr/bin/env python$', + {'pattern': r'#!/usr/bin/env python$', 'include_only': {'zulip_bots/'}, 'description': 'Python shebangs must be python3'}, {'pattern': r'(^|\s)open\s*\(', 'description': 'open() should not be used in Zulip\'s bots. Use functions' ' provided by the bots framework to access the filesystem.', 'include_only': {'zulip_bots/zulip_bots/bots/'}}, - {'pattern': 'pprint', + {'pattern': r'pprint', 'description': 'Used pprint, which is most likely a debugging leftover. For user output, use print().'}, {'pattern': r'\(BotTestCase\)', 'bad_lines': ['class TestSomeBot(BotTestCase):'], @@ -97,7 +97,7 @@ python_rules = RuleList( bash_rules = RuleList( langs=['sh'], rules=[ - {'pattern': '#!.*sh [-xe]', + {'pattern': r'#!.*sh [-xe]', 'description': 'Fix shebang line with proper call to /usr/bin/env for Bash path, change -x|-e switches' ' to set -x|set -e'}, ] + whitespace_rules[0:1], @@ -117,15 +117,15 @@ json_rules = RuleList( ) prose_style_rules = [ - {'pattern': '[^\\/\\#\\-\"]([jJ]avascript)', # exclude usage in hrefs/divs + {'pattern': r'[^\/\#\-"]([jJ]avascript)', # exclude usage in hrefs/divs 'description': "javascript should be spelled JavaScript"}, - {'pattern': '[^\\/\\-\\.\"\'\\_\\=\\>]([gG]ithub)[^\\.\\-\\_\"\\<]', # exclude usage in hrefs/divs + {'pattern': r'''[^\/\-\."'\_\=\>]([gG]ithub)[^\.\-\_"\<]''', # exclude usage in hrefs/divs 'description': "github should be spelled GitHub"}, - {'pattern': '[oO]rganisation', # exclude usage in hrefs/divs + {'pattern': r'[oO]rganisation', # exclude usage in hrefs/divs 'description': "Organization is spelled with a z"}, - {'pattern': '!!! warning', + {'pattern': r'!!! warning', 'description': "!!! warning is invalid; it's spelled '!!! warn'"}, - {'pattern': '[^-_]botserver(?!rc)|bot server', + {'pattern': r'[^-_]botserver(?!rc)|bot server', 'description': "Use Botserver instead of botserver or Botserver."}, ] # type: Rule diff --git a/zulip/integrations/log2zulip/log2zulip b/zulip/integrations/log2zulip/log2zulip index e36e740..bdce5c6 100755 --- a/zulip/integrations/log2zulip/log2zulip +++ b/zulip/integrations/log2zulip/log2zulip @@ -52,7 +52,7 @@ def process_lines(raw_lines, file_name): lines = [] for line in raw_lines: # Add any filtering or modification code here - if re.match(".*upstream timed out.*while reading upstream.*", line): + if re.match(r".*upstream timed out.*while reading upstream.*", line): continue lines.append(line) diff --git a/zulip/integrations/perforce/git_p4.py b/zulip/integrations/perforce/git_p4.py index 670af2a..17e57b3 100755 --- a/zulip/integrations/perforce/git_p4.py +++ b/zulip/integrations/perforce/git_p4.py @@ -338,7 +338,7 @@ def setP4ExecBit(file, mode): if not isModeExec(mode): p4Type = getP4OpenedType(file) - p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type) + p4Type = re.sub(r'^([cku]?)x(.*)', '\\1\\2', p4Type) p4Type = re.sub(r'(.*?\+.*?)x(.*?)', '\\1\\2', p4Type) if p4Type[-1] == "+": p4Type = p4Type[0:-1] @@ -817,7 +817,7 @@ def wildcard_encode(path): return path def wildcard_present(path): - m = re.search("[*#@%]", path) + m = re.search(r"[*#@%]", path) return m is not None class Command: @@ -1979,7 +1979,7 @@ class P4Sync(Command, P4UserMap): # Preserve everything in relative path name except leading # //depot/; just look at first prefix as they all should # be in the same depot. - depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0]) + depot = re.sub(r"^(//[^/]+/).*", r'\1', prefixes[0]) if p4PathStartsWith(path, depot): path = path[len(depot):] @@ -3049,7 +3049,7 @@ class P4Rebase(Command): die("Cannot find upstream branchpoint for rebase") # the branchpoint may be p4/foo~3, so strip off the parent - upstream = re.sub("~[0-9]+$", "", upstream) + upstream = re.sub(r"~[0-9]+$", "", upstream) print("Rebasing the current branch onto %s" % upstream) oldHead = read_pipe("git rev-parse HEAD").strip() @@ -3085,8 +3085,8 @@ class P4Clone(P4Sync): def defaultDestination(self, args): ## TODO: use common prefix of args? depotPath = args[0] - depotDir = re.sub("(@[^@]*)$", "", depotPath) - depotDir = re.sub("(#[^#]*)$", "", depotDir) + depotDir = re.sub(r"(@[^@]*)$", "", depotPath) + depotDir = re.sub(r"(#[^#]*)$", "", depotDir) depotDir = re.sub(r"\.\.\.$", "", depotDir) depotDir = re.sub(r"/$", "", depotDir) return os.path.split(depotDir)[1] diff --git a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py index 0ef0675..c14b785 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py @@ -32,12 +32,12 @@ class LinkShortenerHandler: def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: REGEX_STR = ( - '(' + r'(' r'(?:http|https):\/\/' # This allows for the HTTP or HTTPS # protocol. - '[^"<>\\{\\}|\\^~[\\]` ]+' # This allows for any character except - # for certain non-URL-safe ones. - ')' + r'[^"<>\{\}|\^~[\]` ]+' # This allows for any character except + # for certain non-URL-safe ones. + r')' ) HELP_STR = (