From 0a5be800b978f793625ec442fc055cbc97f17f54 Mon Sep 17 00:00:00 2001 From: Michael Bideau Date: Sun, 25 Oct 2020 21:51:05 +0100 Subject: [PATCH] Feature: added automatic escaping of comas by default (to prevent breaking the line), and option '--do-not-force-escape-comas' to disable it --- vcardlib.py | 7 +++++++ vcardtools.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/vcardlib.py b/vcardlib.py index 746396b..cd315c2 100644 --- a/vcardlib.py +++ b/vcardlib.py @@ -58,6 +58,7 @@ OPTION_MATCH_APPROX_RATIO = 100 OPTION_UPDATE_GROUP_KEY = True OPTION_FRENCH_TWEAKS = False +OPTION_DOT_NOT_FORCE_ESCAPE_COMAS = False SINGLE_INSTANCE_PROPERTIES = {'prodid', 'rev', 'uid'} @@ -886,6 +887,8 @@ def fix_and_convert_to_v3(file_path): logging.debug("\tmultiline value hack (joining this line with the previous)") if last_line.replace('\n', '')[-1:] == '=': last_line = re.sub('=$', '', last_line.replace('\n', '')) + '\n' + if not OPTION_DOT_NOT_FORCE_ESCAPE_COMAS: + line = re.sub('([^\\\\]|^),', '\\1\\,', line) last_line = last_line.replace('\n', '') + line.strip() + '\n' logging.debug("\tconcatened: '%s'", line.strip()) logging.debug("\t") @@ -912,8 +915,12 @@ def fix_and_convert_to_v3(file_path): # convert keys to upper case key_part = new_line.rsplit(':')[0].upper() logging.debug("\tkey part: '%s'", key_part) + rest_part = re.sub(r'^([^:]+):', '', new_line) logging.debug("\trest part: '%s'", rest_part) + if not OPTION_DOT_NOT_FORCE_ESCAPE_COMAS: + rest_part = re.sub('([^\\\\]|^),', '\\1\\,', rest_part) + new_line = key_part + ':' + rest_part logging.debug("\tbuilt new line: %s", new_line) diff --git a/vcardtools.py b/vcardtools.py index 897697a..b555357 100644 --- a/vcardtools.py +++ b/vcardtools.py @@ -85,6 +85,10 @@ def init_parser(): '--no-remove-name-in-email', dest='no_remove_name_in_email', action='store_true', \ help="Do not removes name in email, i.e.: keep email like the following untouched: \"John Doe\" " \ ) + parser.add_argument( \ + '--do-not-force-escape-comas', dest='do_not_force_escape_comas', action='store_true', \ + help="Disable automatically escaping comas." \ + ) parser.add_argument( \ '-l', '--log-level', dest='log_level', default='INFO', \ choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], \ @@ -143,6 +147,9 @@ def main(): # french tweaks vcardlib.OPTION_FRENCH_TWEAKS = args.french_tweaks + # coma auto escape + vcardlib.OPTION_DOT_NOT_FORCE_ESCAPE_COMAS = args.do_not_force_escape_comas + # check DESTDIR argument if exists(args.dest_dir):