Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions git-passport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 15 additions & 6 deletions passport/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
52 changes: 35 additions & 17 deletions passport/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~/.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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."

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 12 additions & 6 deletions passport/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -72,24 +74,28 @@ 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"))
else:
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"))
Expand Down