From cdc2e21a77126cda8ff0b78abdf8a57a83a4ac91 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 9 Sep 2019 20:21:23 -0400 Subject: [PATCH] use raw strings where possible to avoid escaping --- src/calibre/library/catalogs/bibtex.py | 6 +++--- src/calibre/library/catalogs/csv_xml.py | 4 ++-- .../library/catalogs/epub_mobi_builder.py | 16 ++++++++-------- src/calibre/library/catalogs/utils.py | 8 ++++---- src/calibre/library/database.py | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/calibre/library/catalogs/bibtex.py b/src/calibre/library/catalogs/bibtex.py index e352900e65..7d3abd41c0 100644 --- a/src/calibre/library/catalogs/bibtex.py +++ b/src/calibre/library/catalogs/bibtex.py @@ -242,7 +242,7 @@ class BIBTEX(CatalogPlugin): # define a function to replace the template entry by its value def tpl_replace(objtplname) : - tpl_field = re.sub('[\\{\\}]', '', objtplname.group()) + tpl_field = re.sub(r'[\{\}]', '', objtplname.group()) if tpl_field in TEMPLATE_ALLOWED_FIELDS : if tpl_field in ['pubdate', 'timestamp'] : @@ -259,14 +259,14 @@ class BIBTEX(CatalogPlugin): if len(template_citation) >0 : tpl_citation = bibtexclass.utf8ToBibtex( - bibtexclass.ValidateCitationKey(re.sub('\\{[^{}]*\\}', + bibtexclass.ValidateCitationKey(re.sub(r'\{[^{}]*\}', tpl_replace, template_citation))) if len(tpl_citation) >0 : return tpl_citation if len(entry["isbn"]) > 0 : - template_citation = '%s' % re.sub('[\\D]','', entry["isbn"]) + template_citation = '%s' % re.sub(r'[\D]','', entry["isbn"]) else : template_citation = '%s' % unicode_type(entry["id"]) diff --git a/src/calibre/library/catalogs/csv_xml.py b/src/calibre/library/catalogs/csv_xml.py index 382b55ac44..e80c027384 100644 --- a/src/calibre/library/catalogs/csv_xml.py +++ b/src/calibre/library/catalogs/csv_xml.py @@ -156,9 +156,9 @@ class CSV_XML(CatalogPlugin): # Convert HTML to markdown text if isinstance(item, unicode_type): - opening_tag = re.search('<(\\w+)(\x20|>)', item) + opening_tag = re.search(r'<(\w+)( |>)', item) if opening_tag: - closing_tag = re.search('<\\/%s>$' % opening_tag.group(1), item) + closing_tag = re.search(r'<\/%s>$' % opening_tag.group(1), item) if closing_tag: item = html2text(item) diff --git a/src/calibre/library/catalogs/epub_mobi_builder.py b/src/calibre/library/catalogs/epub_mobi_builder.py index a12db3641f..3e8bfa5097 100644 --- a/src/calibre/library/catalogs/epub_mobi_builder.py +++ b/src/calibre/library/catalogs/epub_mobi_builder.py @@ -1228,11 +1228,11 @@ class CatalogBuilder(object): clipped to max_len """ - normalized = massaged = re.sub('\\s', '', ascii_text(tag).lower()) - if re.search('\\W', normalized): + normalized = massaged = re.sub(r'\s', '', ascii_text(tag).lower()) + if re.search(r'\W', normalized): normalized = '' for c in massaged: - if re.search('\\W', c): + if re.search(r'\W', c): normalized += self.generate_unicode_name(c) else: normalized += c @@ -1395,7 +1395,7 @@ class CatalogBuilder(object): Return: (str): asciized version of author """ - return re.sub("\\W", "", ascii_text(author)) + return re.sub(r"\W", "", ascii_text(author)) def generate_format_args(self, book): """ Generate the format args for template substitution. @@ -4228,9 +4228,9 @@ class CatalogBuilder(object): # Generate a legal XHTML id/href string if self.letter_or_symbol(series) == self.SYMBOLS: - return "symbol_%s_series" % re.sub('\\W', '', series).lower() + return "symbol_%s_series" % re.sub(r'\W', '', series).lower() else: - return "%s_series" % re.sub('\\W', '', ascii_text(series)).lower() + return "%s_series" % re.sub(r'\W', '', ascii_text(series)).lower() def generate_short_description(self, description, dest=None): """ Generate a truncated version of the supplied string. @@ -4311,7 +4311,7 @@ class CatalogBuilder(object): else: if re.match('[0-9]+', word[0]): word = word.replace(',', '') - suffix = re.search('[\\D]', word) + suffix = re.search(r'[\D]', word) if suffix: word = '%10.0f%s' % (float(word[:suffix.start()]), word[suffix.start():]) else: @@ -4327,7 +4327,7 @@ class CatalogBuilder(object): else: if re.search('[0-9]+', word[0]): word = word.replace(',', '') - suffix = re.search('[\\D]', word) + suffix = re.search(r'[\D]', word) if suffix: word = '%10.0f%s' % (float(word[:suffix.start()]), word[suffix.start():]) else: diff --git a/src/calibre/library/catalogs/utils.py b/src/calibre/library/catalogs/utils.py index 593b96cef7..b9e5d69200 100644 --- a/src/calibre/library/catalogs/utils.py +++ b/src/calibre/library/catalogs/utils.py @@ -90,8 +90,8 @@ class NumberToText(object): # {{{ # Special case ordinals if re.search('[st|nd|rd|th]',self.number): self.number = re.sub(',','',self.number) - ordinal_suffix = re.search('[\\D]', self.number) - ordinal_number = re.sub('\\D','',re.sub(',','',self.number)) + ordinal_suffix = re.search(r'[\D]', self.number) + ordinal_number = re.sub(r'\D','',re.sub(',','',self.number)) if self.verbose: self.log("Ordinal: %s" % ordinal_number) self.number_as_float = ordinal_number @@ -155,8 +155,8 @@ class NumberToText(object): # {{{ if self.verbose: self.log("Hybrid: %s" % self.number) # Split the token into number/text - number_position = re.search('\\d',self.number).start() - text_position = re.search('\\D',self.number).start() + number_position = re.search(r'\d',self.number).start() + text_position = re.search(r'\D',self.number).start() if number_position < text_position: number = self.number[:text_position] text = self.number[text_position:] diff --git a/src/calibre/library/database.py b/src/calibre/library/database.py index 01a29e3813..48b13667b1 100644 --- a/src/calibre/library/database.py +++ b/src/calibre/library/database.py @@ -60,7 +60,7 @@ def _connect(path): conn = sqlite.connect(path, factory=Connection, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) conn.row_factory = lambda cursor, row : list(row) conn.create_aggregate('concat', 1, Concatenate) - title_pat = re.compile('^(A|The|An)\\s+', re.IGNORECASE) + title_pat = re.compile(r'^(A|The|An)\s+', re.IGNORECASE) def title_sort(title): match = title_pat.search(title)