From 37fe35cb9ab97717bb4156fb17feb9fff80b93a9 Mon Sep 17 00:00:00 2001 From: Ingo Schmiegel Date: Tue, 27 Oct 2015 10:00:01 +0100 Subject: [PATCH] Add section option "no_remote = true" to allow passports for repos without remotes --- git-passport.py | 8 ++++-- passport/case.py | 21 +++++++++++----- passport/configuration.py | 52 ++++++++++++++++++++++++++------------- passport/dialog.py | 18 +++++++++----- 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/git-passport.py b/git-passport.py index 172df2b..e6f461e 100755 --- a/git-passport.py +++ b/git-passport.py @@ -72,8 +72,12 @@ candidates = case.url_exists(config, local_url) else: candidates = case.no_url_exists(config) - - selected_id = dialog.get_input(candidates.keys()) + + if args.select or len(candidates) > 1: + dialog.print_choice(candidates) + selected_id = dialog.get_input(candidates.keys()) + else: + selected_id = next(iter(candidates.keys())) if selected_id is not None: git.config_set(config, candidates[selected_id]["email"], "email") git.config_set(config, candidates[selected_id]["name"], "name") diff --git a/passport/case.py b/passport/case.py index e7f779e..2f0445e 100644 --- a/passport/case.py +++ b/passport/case.py @@ -97,7 +97,6 @@ def gen_candidates(ids, url): print(util.dedented(msg, "lstrip")) configuration.add_global_id(config, candidates) - dialog.print_choice(candidates) return candidates @@ -112,11 +111,21 @@ def no_url_exists(config): Returns: candidates (dict): Contains preselected Git ID candidates """ - candidates = config["git_passports"] - msg = "«remote.origin.url» is not set, listing all passports:\n" + def gen_candidates(ids): + for key, value in ids.items(): + if value.get('no_remote', False): + yield (key, value) + + local_passports = config["git_passports"] + candidates = dict(gen_candidates(local_passports)) + if candidates: + msg = "«remote.origin.url» is not set, listing all passports with «no_remote» set:\n" + else: + msg = "«remote.origin.url» is not set, listing all passports:\n" + candidates = local_passports + configuration.add_global_id(config, candidates) - print(msg) - configuration.add_global_id(config, candidates) - dialog.print_choice(candidates) + if len(candidates) > 1: + print(msg) return candidates diff --git a/passport/configuration.py b/passport/configuration.py index 74f9a85..aab332d 100644 --- a/passport/configuration.py +++ b/passport/configuration.py @@ -43,6 +43,12 @@ def preset(filename): preset["passport 1"]["name"] = "name_1" preset["passport 1"]["service"] = "gitlab.com" + preset["passport 2"] = {} + preset["passport 2"]["email"] = "email_2@example.com" + preset["passport 2"]["name"] = "name_2" + preset["passport 2"]["no_remote"] = "true" + preset["passport 2"]["service"] = ".*" + try: msg = """ No configuration file found ~/. @@ -87,7 +93,8 @@ def validate_scheme(filename): "enable_hook", "name", "service", - "sleep_duration" + "sleep_duration", + "no_remote" ]) # Create sets containing non-whitelisted section and option names @@ -148,6 +155,7 @@ def validate_values(filename): email: E-Mail scheme sleep_duration: Float enable_hook: Boolean + no_remote: Boolean Args: filename (str): The complete `filepath` of the configuration file @@ -156,33 +164,41 @@ def validate_values(filename): True (bool): If the configfile contains valid values False (bool): If the configfile contains invalid values """ - def filter_email(config): + def filter_section(config): pattern_section = r"^(passport)\s[0-9]+$" pattern_email = r"[^@]+@[^@]+\.[^@]+" for section in config.sections(): if re.match(pattern_section, section): + # Check email email = config.get(section, "email") if not re.match(pattern_email, email): - yield email + yield ('email address', email) + # Check no_remote + try: + # cannot use default of config.get because it overwrites + # fallbacks ('DEFAULT' section) + no_remote = config.get(section, "no_remote") + except configparser.NoOptionError: + pass + else: + # no_remote exists, now check valye + try: + no_remote = config.getboolean(section, "no_remote") + except ValueError: + yield ("no_remote", no_remote) raw_config = configparser.ConfigParser() raw_config.read(filename) - false_email = set(filter_email(raw_config)) - - # Quit if we have wrong email addresses - if len(false_email): - msg = """ - E > Configuration > Invalid email address: - >>> {} - """.format(", ".join(false_email)) - - print(util.dedented(msg, "strip")) + # Quit if we have wrong section config + for option_name, value in filter_section(raw_config): + msg = "E > Configuration > Invalid {}: {}".format(option_name, value) + print(msg) return False # Quit if we have wrong boolean values try: - raw_config.getboolean("general", "enable_hook") + raw_config.getboolean("general", 'enable_hook') except ValueError: msg = "E > Configuration > enable_hook: Expecting True or False." @@ -213,9 +229,11 @@ def release(filename): """ def passport(config): pattern_section = r"^(passport)\s[0-9]+$" - for passport in config.items(): - if re.match(pattern_section, passport[0]): - yield dict(passport[1]) + for name, section in config.items(): + if re.match(pattern_section, name): + d = dict(section) + d["no_remote"] = section.getboolean("no_remote", fallback=False) + yield d raw_config = configparser.ConfigParser() raw_config.read(filename) diff --git a/passport/dialog.py b/passport/dialog.py index beeb9ff..b757dee 100644 --- a/passport/dialog.py +++ b/passport/dialog.py @@ -59,11 +59,13 @@ def print_choice(choice): msg = """ ~:Global ID: {} . User: {} - . E-Mail: {} + . E-Mail: {}{} """.format( key, value["name"], - value["email"] + value["email"], + """ + . no_remote: true""" if value["no_remote"] else '' ) print(util.dedented(msg, "lstrip")) @@ -72,12 +74,14 @@ def print_choice(choice): ~Passport ID: {} . User: {} . E-Mail: {} - . Service: {} + . Service: {}{} """.format( key, value["name"], value["email"], - value["service"] + value["service"], + """ + . no_remote: true""" if value["no_remote"] else '' ) print(util.dedented(msg, "lstrip")) @@ -85,11 +89,13 @@ def print_choice(choice): msg = """ ~:Passport ID: {} . User: {} - . E-Mail: {} + . E-Mail: {}{} """.format( key, value["name"], - value["email"] + value["email"], + """ + . no_remote: true""" if value["no_remote"] else '' ) print(util.dedented(msg, "lstrip"))