Skip to content

Commit

Permalink
[IMPR] use long regex flags which are more readable
Browse files Browse the repository at this point in the history
Change-Id: I2946c23d2ac0f0c31f2c9a7310cf478a90a8812b
  • Loading branch information
xqt committed Nov 2, 2024
1 parent 11b979d commit 78733ed
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pywikibot/comms/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def assign_user_agent(user_agent_format_string):
# Extract charset (from content-type header)
CHARSET_RE = re.compile(
r'charset\s*=\s*(?P<q>[\'"]?)(?P<charset>[^\'",;>/]+)(?P=q)',
flags=re.I,
flags=re.IGNORECASE,
)


Expand Down
4 changes: 2 additions & 2 deletions pywikibot/cosmetic_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ def replace_magicword(match: Match[str]) -> str:
exceptions = ['comment', 'nowiki', 'pre', 'syntaxhighlight']
regex = re.compile(
FILE_LINK_REGEX % '|'.join(self.site.namespaces[6]),
flags=re.X)
flags=re.VERBOSE)
return textlib.replaceExcept(
text, regex, replace_magicword, exceptions)

Expand Down Expand Up @@ -729,7 +729,7 @@ def removeEmptySections(self, text: str) -> str:
if self.site.code in skip_templates:
for template in skip_templates[self.site.code]:
skip_regexes.append(
re.compile(r'\{\{\s*%s\s*\}\}' % template, re.I))
re.compile(r'\{\{\s*%s\s*\}\}' % template, re.IGNORECASE))
# empty lists
skip_regexes.append(re.compile(r'(?m)^[\*#] *$'))

Expand Down
2 changes: 1 addition & 1 deletion pywikibot/pagegenerators/_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def titlefilter(cls,
quantifier = 'any'
elif quantifier is True:
quantifier = 'none'
reg = cls.__precompile(regex, re.I)
reg = cls.__precompile(regex, re.IGNORECASE)
for page in generator:
title = page.title(with_ns=not ignore_namespace)
if cls.__filter_match(reg, title, quantifier):
Expand Down
12 changes: 6 additions & 6 deletions pywikibot/textlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,10 +1571,10 @@ def replaceCategoryInPlace(oldtext, oldcat, newcat, site=None,
title = case_escape(site.namespaces[14].case, title, underscore=True)
categoryR = re.compile(
rf'\[\[\s*({catNamespace})\s*:\s*{title}[\s\u200e\u200f]*'
r'((?:\|[^]]+)?\]\])', re.I)
r'((?:\|[^]]+)?\]\])', re.IGNORECASE)
categoryRN = re.compile(
rf'^[^\S\n]*\[\[\s*({catNamespace})\s*:\s*{title}[\s\u200e\u200f]*'
r'((?:\|[^]]+)?\]\])[^\S\n]*\n', re.I | re.M)
r'((?:\|[^]]+)?\]\])[^\S\n]*\n', re.IGNORECASE | re.MULTILINE)
exceptions = ['comment', 'math', 'nowiki', 'pre', 'syntaxhighlight']
if newcat is None:
# First go through and try the more restrictive regex that removes
Expand Down Expand Up @@ -1621,7 +1621,7 @@ def replaceCategoryLinks(oldtext: str,
if site is None:
site = pywikibot.Site()
if re.search(r'\{\{ *(' + r'|'.join(site.getmagicwords('defaultsort'))
+ r')', oldtext, flags=re.I):
+ r')', oldtext, flags=re.IGNORECASE):
separator = '\n'
else:
separator = site.family.category_text_separator
Expand Down Expand Up @@ -1668,15 +1668,15 @@ def replaceCategoryLinks(oldtext: str,

if site.sitename == 'wikipedia:de':
personendaten = re.compile(r'\{\{ *Personendaten.*?\}\}',
re.I | re.DOTALL)
re.IGNORECASE | re.DOTALL)
under_categories.append(personendaten)

if site.sitename == 'wikipedia:yi':
stub = re.compile(r'\{\{.*?שטומף *\}\}', re.I)
stub = re.compile(r'\{\{.*?שטומף *\}\}', re.IGNORECASE)
under_categories.append(stub)

if site.family.name == 'wikipedia' and site.code in ('simple', 'en'):
stub = re.compile(r'\{\{.*?stub *\}\}', re.I)
stub = re.compile(r'\{\{.*?stub *\}\}', re.IGNORECASE)
under_categories.append(stub)

if under_categories:
Expand Down
2 changes: 1 addition & 1 deletion pywikibot/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __init__(
the start of the path to the warning module must match.
(case-sensitive)
"""
self.message_match = re.compile(message, re.I).match
self.message_match = re.compile(message, re.IGNORECASE).match
self.category = category
self.filename_match = re.compile(filename).match
super().__init__(record=True)
Expand Down
4 changes: 2 additions & 2 deletions scripts/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@

CFD_TEMPLATE_REGEX = re.compile(r'<!--\s*BEGIN CFD TEMPLATE\s*-->.*?'
r'<!--\s*END CFD TEMPLATE\s*-->\n?',
flags=re.I | re.M | re.S)
flags=re.IGNORECASE | re.MULTILINE | re.DOTALL)

cfd_templates = {
'wikipedia': {
Expand Down Expand Up @@ -531,7 +531,7 @@ def treat(self, page) -> None:
if self.includeonly == ['includeonly']:
tagname = 'includeonly'
tagnameregexp = re.compile(fr'(.*)(<\/{tagname}>)',
re.I | re.DOTALL)
re.IGNORECASE | re.DOTALL)
categorytitle = catpl.title(
as_link=True, allow_interwiki=False)
if tagnameregexp.search(text):
Expand Down
2 changes: 1 addition & 1 deletion scripts/category_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def setup_soft_redirect(self):
template='|'.join(item.replace(' ', '[ _]+')
for item in self.template_list),
catns=self.site.namespace(14)),
re.I | re.X)
re.IGNORECASE | re.VERBOSE)

nonemptypages = []
catpages = set()
Expand Down
2 changes: 1 addition & 1 deletion scripts/checkimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ def is_tagged(self) -> bool:
if '{{' in i:
regex_pattern = re.compile(
r'\{\{(?:template)?%s ?(?:\||\r?\n|\}|<|/) ?'
% i.split('{{')[1].replace(' ', '[ _]'), re.I)
% i.split('{{')[1].replace(' ', '[ _]'), re.IGNORECASE)
result = regex_pattern.findall(self.image_check_text)
if result:
return True
Expand Down
2 changes: 1 addition & 1 deletion scripts/commonscat.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def checkCommonscatLink(self, name: str = ''):
r'(?P<newcat1>[^\|\}]+)(\|[^\}]+)?\]\]|'
r'Robot: Changing Category:(.+) '
r'to Category:(?P<newcat2>.+)')
m = re.search(regex, logcomment, flags=re.I)
m = re.search(regex, logcomment, flags=re.IGNORECASE)

if not m:
pywikibot.info(
Expand Down
2 changes: 1 addition & 1 deletion scripts/solve_disambiguation.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def setup(self) -> None:
\[\[ (?P<title> [^\[\]\|#]*)
(?P<section> \#[^\]\|]*)?
(\|(?P<label> [^\]]*))? \]\]
(?P<linktrail>{linktrail})""", flags=re.X)
(?P<linktrail>{linktrail})""", flags=re.VERBOSE)

@staticmethod
def firstlinks(page) -> Generator[str]:
Expand Down
2 changes: 1 addition & 1 deletion scripts/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@


CHUNK_SIZE_REGEX = re.compile(
r'-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?', re.I)
r'-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?', re.IGNORECASE)


def get_chunk_size(match) -> int:
Expand Down
2 changes: 1 addition & 1 deletion scripts/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def define_sign(self, force: bool = False) -> list[str]:
return self._random_signature

sign_text = ''
creg = re.compile(r'^\* ?(.*?)$', re.M)
creg = re.compile(r'^\* ?(.*?)$', re.MULTILINE)
if not globalvar.sign_file_name:
sign_page_name = i18n.translate(self.site, RANDOM_SIGN)
if not sign_page_name:
Expand Down

0 comments on commit 78733ed

Please sign in to comment.