Skip to content

Commit

Permalink
Move "Error:" prefix from exceptions to print()
Browse files Browse the repository at this point in the history
It seem unnecessary to add this to the description in an exception.
  • Loading branch information
lucc committed Feb 23, 2024
1 parent c848320 commit faeb4d4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
79 changes: 34 additions & 45 deletions khard/carddav_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,10 @@ def _add_phone_number(self, type: str, number: str) -> None:
string_to_list(type, ","), self.phone_types_v4 if
self.version == "4.0" else self.phone_types_v3)
if not standard_types and not custom_types and pref == 0:
raise ValueError("Error: label for phone number " + number +
" is missing.")
raise ValueError(f"Label for phone number {number} is missing.")
if len(custom_types) > 1:
raise ValueError("Error: phone number " + number + " got more "
"than one custom label: " +
raise ValueError(f"Phone number {number} got more than one "
"custom label: " +
list_to_string(custom_types, ", "))
phone_obj = self.vcard.add('tel')
if self.version == "4.0":
Expand Down Expand Up @@ -737,10 +736,9 @@ def add_email(self, type: str, address: str) -> None:
string_to_list(type, ","), self.email_types_v4 if
self.version == "4.0" else self.email_types_v3)
if not standard_types and not custom_types and pref == 0:
raise ValueError("Error: label for email address " + address +
" is missing.")
raise ValueError(f"Label for email address {address} is missing.")
if len(custom_types) > 1:
raise ValueError("Error: email address " + address + " got more "
raise ValueError(f"Email address {address} got more "
"than one custom label: " +
list_to_string(custom_types, ", "))
email_obj = self.vcard.add('email')
Expand Down Expand Up @@ -838,12 +836,10 @@ def _add_post_address(self, type: str, box: StrList, extended: StrList,
string_to_list(type, ","), self.address_types_v4
if self.version == "4.0" else self.address_types_v3)
if not standard_types and not custom_types and pref == 0:
raise ValueError(f"Error: label for post address {street} is "
"missing.")
raise ValueError(f"Label for post address {street} is missing.")
if len(custom_types) > 1:
raise ValueError(f"Error: post address {street} got more "
"than one custom " "label: " +
list_to_string(custom_types, ", "))
raise ValueError(f"Post address {street} got more than one custom "
"label: " + list_to_string(custom_types, ", "))
adr_obj = self.vcard.add('adr')
adr_obj.value = vobject.vcard.Address(
box=convert_to_vcard("box address field", box, ObjectType.both),
Expand Down Expand Up @@ -978,14 +974,13 @@ def _parse_yaml(input: str) -> Dict:
raise ValueError(err)
else:
if not contact_data:
raise ValueError("Error: Found no contact information")
raise ValueError("Found no contact information")

# check for available data
# at least enter name or organisation
if not (contact_data.get("First name") or contact_data.get("Last name")
or contact_data.get("Organisation")):
raise ValueError(
"Error: You must either enter a name or an organisation")
raise ValueError("You must either enter a name or an organisation")
return contact_data

@staticmethod
Expand Down Expand Up @@ -1023,31 +1018,31 @@ def _set_date(self, target: str, key: str, data: Dict) -> None:
if not new:
return
if not isinstance(new, str):
raise ValueError("Error: {} must be a string object.".format(key))
raise ValueError(f"{key} must be a string object.")
if re.match(r"^text[\s]*=.*$", new):
if self.version == "4.0":
v1 = ', '.join(x.strip() for x in re.split(r"text[\s]*=", new)
if x.strip())
if v1:
setattr(self, target, v1)
return
raise ValueError("Error: Free text format for {} only usable with "
"vcard version 4.0.".format(key.lower()))
raise ValueError(f"Free text format for {key.lower()} only usable "
"with vcard version 4.0.")
if re.match(r"^--\d\d-?\d\d$", new) and self.version != "4.0":
raise ValueError(
"Error: {} format --mm-dd and --mmdd only usable with "
f"{key} format --mm-dd and --mmdd only usable with "
"vcard version 4.0. You may use 1900 as placeholder, if "
"the year is unknown.".format(key))
"the year is unknown.")
try:
v2 = string_to_date(new)
if v2:
setattr(self, target, v2)
return
except ValueError:
pass
raise ValueError("Error: Wrong {} format or invalid date\n"
raise ValueError(f"Wrong {key.lower()} format or invalid date\n"
"Use format yyyy-mm-dd or "
"yyyy-mm-ddTHH:MM:SS".format(key.lower()))
"yyyy-mm-ddTHH:MM:SS")

def update(self, input: str) -> None:
"""Update this vcard with some yaml input
Expand Down Expand Up @@ -1106,11 +1101,10 @@ def update(self, input: str) -> None:
self._add_phone_number(type, number)
else:
raise ValueError(
"Error: got no number or list of numbers for the "
"phone number type " + type)
"Got no number or list of numbers for the "
f"phone number type {type}")
else:
raise ValueError(
"Error: missing type value for phone number field")
raise ValueError("Missing type value for phone number field")

# email
self._delete_vcard_object("EMAIL")
Expand All @@ -1126,11 +1120,10 @@ def update(self, input: str) -> None:
self.add_email(type, email)
else:
raise ValueError(
"Error: got no email or list of emails for the "
"email address type " + type)
"Got no email or list of emails for the "
f"email address type {type}")
else:
raise ValueError(
"Error: missing type value for email address field")
raise ValueError("Missing type value for email address field")

# post addresses
self._delete_vcard_object("ADR")
Expand Down Expand Up @@ -1161,16 +1154,13 @@ def update(self, input: str) -> None:
post_adr.get("Country", ""))
else:
raise ValueError(
"Error: one of the " + type + " type "
"address list items does not contain an "
"address")
f"One of the {type} type address "
"list items does not contain an address")
else:
raise ValueError(
"Error: got no address or list of addresses for "
"the post address type " + type)
raise ValueError("Got no address or list of addresses "
f"for the post address type {type}")
else:
raise ValueError(
"Error: missing type value for post address field")
raise ValueError("Missing type value for post address field")

# categories
self._delete_vcard_object("CATEGORIES")
Expand All @@ -1197,7 +1187,7 @@ def update(self, input: str) -> None:
self._add_category(sub_category)
else:
raise ValueError(
"Error: category must be a string or a list of strings")
"Category must be a string or a list of strings")

# urls
self._delete_vcard_object("URL")
Expand Down Expand Up @@ -1227,16 +1217,15 @@ def update(self, input: str) -> None:
if value:
self._add_private_object(key, value)
else:
raise ValueError(
"Error: got no value or list of values for "
"the private object " + key)
raise ValueError("Got no value or list of values "
f"for the private object {key}")
else:
raise ValueError(
"Error: private object key " + key + " was "
"changed.\nSupported private keys: " + ', '.join(
f"Private object key {key} was changed.\n"
"Supported private keys: " + ', '.join(
self.supported_private_objects))
else:
raise ValueError("Error: private objects must consist of a "
raise ValueError("Private objects must consist of a "
"key : value pair.")

# notes
Expand Down
2 changes: 1 addition & 1 deletion khard/helpers/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def edit_templates(self, yaml2card: Callable[[str], CarddavObject],
try:
return yaml2card(modified_template)
except ValueError as err:
print("\n{}\n".format(err))
print(f"\nError: {err}\n")
if not confirm("Do you want to open the editor again?"):
print("Canceled")
return None
Expand Down
12 changes: 5 additions & 7 deletions khard/helpers/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ def convert_to_vcard(name: str, value: StrList, constraint: ObjectType
return value.strip()
if isinstance(value, list):
if constraint == ObjectType.str:
raise ValueError("Error: " + name + " must contain a string.")
raise ValueError(f"{name} must contain a string.")
if not all(isinstance(entry, str) for entry in value):
raise ValueError("Error: " + name +
" must not contain a nested list")
raise ValueError(f"{name} must not contain a nested list")
# filter out empty list items and strip leading and trailing space
return [x.strip() for x in value if x.strip()]
if constraint == ObjectType.str:
raise ValueError("Error: " + name + " must be a string.")
raise ValueError(f"{name} must be a string.")
if constraint == ObjectType.list:
raise ValueError("Error: " + name + " must be a list with strings.")
raise ValueError("Error: " + name +
" must be a string or a list with strings.")
raise ValueError(f"{name} must be a list with strings.")
raise ValueError(f"{name} must be a string or a list with strings.")


def list_to_string(input: Union[str, List], delimiter: str) -> str:
Expand Down
6 changes: 3 additions & 3 deletions khard/khard.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def new_subcommand(abooks: AddressBookCollection, data: str, open_editor: bool
abook, data, config.private_objects,
config.preferred_vcard_version, config.localize_dates)
except ValueError as err:
sys.exit(str(err))
sys.exit(f"Error: {err}")
else:
new_contact.write_to_file()
if open_editor:
Expand Down Expand Up @@ -639,7 +639,7 @@ def add_email_to_contact(name: str, email_address: str,
try:
selected_vcard.add_email(label, email_address)
except ValueError as err:
print(err)
print(f"Error: {err}")
else:
break
# save to disk
Expand Down Expand Up @@ -951,7 +951,7 @@ def modify_subcommand(selected_vcard: CarddavObject,
selected_vcard, input_from_stdin_or_file,
config.localize_dates)
except ValueError as err:
sys.exit(str(err))
sys.exit(f"Error: {err}")
if selected_vcard == new_contact:
print("Nothing changed\n\n{}".format(new_contact.pretty()))
else:
Expand Down

0 comments on commit faeb4d4

Please sign in to comment.