Some code simplifications with the return statement

This commit is contained in:
Дилян Палаузов 2021-03-08 22:53:46 +02:00 committed by Hugo
parent 5d343264f3
commit 2f548e048d
7 changed files with 22 additions and 33 deletions

View file

@ -257,7 +257,7 @@ class PairConfig:
): ):
if conflict_resolution in (None, "a wins", "b wins"): if conflict_resolution in (None, "a wins", "b wins"):
return conflict_resolution return conflict_resolution
elif ( if (
isinstance(conflict_resolution, list) isinstance(conflict_resolution, list)
and len(conflict_resolution) > 1 and len(conflict_resolution) > 1
and conflict_resolution[0] == "command" and conflict_resolution[0] == "command"
@ -271,7 +271,6 @@ class PairConfig:
return _resolve_conflict_via_command(a, b, command, a_name, b_name) return _resolve_conflict_via_command(a, b, command, a_name, b_name)
return resolve return resolve
else:
raise ValueError("Invalid value for `conflict_resolution`.") raise ValueError("Invalid value for `conflict_resolution`.")
# The following parameters are lazily evaluated because evaluating # The following parameters are lazily evaluated because evaluating

View file

@ -59,18 +59,17 @@ async def collections_for_pair(
cache_key = _get_collections_cache_key(pair) cache_key = _get_collections_cache_key(pair)
if from_cache: if from_cache:
rv = load_status(status_path, pair.name, data_type="collections") rv = load_status(status_path, pair.name, data_type="collections")
if rv.get("cache_key", None) == cache_key: if rv and rv.get("cache_key", None) == cache_key:
return list( return list(
_expand_collections_cache( _expand_collections_cache(
rv["collections"], pair.config_a, pair.config_b rv["collections"], pair.config_a, pair.config_b
) )
) )
elif rv: if rv:
raise exceptions.UserError( raise exceptions.UserError(
"Detected change in config file, " "Detected change in config file, "
f"please run `vdirsyncer discover {pair.name}`." f"please run `vdirsyncer discover {pair.name}`."
) )
else:
raise exceptions.UserError( raise exceptions.UserError(
f"Please run `vdirsyncer discover {pair.name}` before synchronization." f"Please run `vdirsyncer discover {pair.name}` before synchronization."
) )

View file

@ -294,7 +294,6 @@ async def storage_instance_from_config(
create=False, create=False,
connector=connector, connector=connector,
) )
else:
raise raise
except Exception: except Exception:
return handle_storage_init_error(cls, new_config) return handle_storage_init_error(cls, new_config)

View file

@ -100,9 +100,9 @@ def prepare_auth(auth, username, password):
if username and password: if username and password:
if auth == "basic" or auth is None: if auth == "basic" or auth is None:
return BasicAuthMethod(username, password) return BasicAuthMethod(username, password)
elif auth == "digest": if auth == "digest":
return DigestAuthMethod(username, password) return DigestAuthMethod(username, password)
elif auth == "guess": if auth == "guess":
raise exceptions.UserError( raise exceptions.UserError(
"'Guess' authentication is not supported in this version of vdirsyncer. \n" "'Guess' authentication is not supported in this version of vdirsyncer. \n"
"Please explicitly specify either 'basic' or 'digest' auth instead. \n" "Please explicitly specify either 'basic' or 'digest' auth instead. \n"

View file

@ -114,9 +114,7 @@ def _fuzzy_matches_mimetype(strict, weak):
return True return True
mediatype, subtype = strict.split("/") mediatype, subtype = strict.split("/")
if subtype in weak: return subtype in weak
return True
return False
class Discover: class Discover:
@ -237,7 +235,7 @@ class Discover:
return True return True
props = _merge_xml(response.findall("{DAV:}propstat/{DAV:}prop")) props = _merge_xml(response.findall("{DAV:}propstat/{DAV:}prop"))
if props is None or not len(props): if props is None or not props:
dav_logger.debug("Skipping, missing <prop>: %s", response) dav_logger.debug("Skipping, missing <prop>: %s", response)
return False return False
if props.find("{DAV:}resourcetype/" + self._resourcetype) is None: if props.find("{DAV:}resourcetype/" + self._resourcetype) is None:
@ -626,7 +624,7 @@ class DAVStorage(Storage):
continue continue
props = response.findall("{DAV:}propstat/{DAV:}prop") props = response.findall("{DAV:}propstat/{DAV:}prop")
if props is None or not len(props): if props is None or not props:
dav_logger.debug(f"Skipping {href!r}, properties are missing.") dav_logger.debug(f"Skipping {href!r}, properties are missing.")
continue continue
else: else:

View file

@ -24,8 +24,7 @@ _missing = object()
def expand_path(p: str) -> str: def expand_path(p: str) -> str:
"""Expand $HOME in a path and normalise slashes.""" """Expand $HOME in a path and normalise slashes."""
p = os.path.expanduser(p) p = os.path.expanduser(p)
p = os.path.normpath(p) return os.path.normpath(p)
return p
def split_dict(d: dict, f: Callable): def split_dict(d: dict, f: Callable):
@ -177,7 +176,6 @@ def generate_href(ident=None, safe=SAFE_UID_CHARS):
""" """
if not ident or not href_safe(ident, safe): if not ident or not href_safe(ident, safe):
return str(uuid.uuid4()) return str(uuid.uuid4())
else:
return ident return ident

View file

@ -231,7 +231,6 @@ def _get_item_type(components, wrappers):
if not i: if not i:
return None, None return None, None
else:
raise ValueError("Not sure how to join components.") raise ValueError("Not sure how to join components.")
@ -303,9 +302,8 @@ class _Component:
if multiple: if multiple:
return rv return rv
elif len(rv) != 1: if len(rv) != 1:
raise ValueError(f"Found {len(rv)} components, expected one.") raise ValueError(f"Found {len(rv)} components, expected one.")
else:
return rv[0] return rv[0]
def dump_lines(self): def dump_lines(self):
@ -323,7 +321,6 @@ class _Component:
for line in lineiter: for line in lineiter:
if line.startswith(prefix): if line.startswith(prefix):
break break
else:
new_lines.append(line) new_lines.append(line)
else: else:
break break
@ -347,9 +344,8 @@ class _Component:
return obj not in self.subcomponents and not any( return obj not in self.subcomponents and not any(
obj in x for x in self.subcomponents obj in x for x in self.subcomponents
) )
elif isinstance(obj, str): if isinstance(obj, str):
return self.get(obj, None) is not None return self.get(obj, None) is not None
else:
raise ValueError(obj) raise ValueError(obj)
def __getitem__(self, key): def __getitem__(self, key):
@ -360,7 +356,7 @@ class _Component:
if line.startswith(prefix_without_params): if line.startswith(prefix_without_params):
rv = line[len(prefix_without_params) :] rv = line[len(prefix_without_params) :]
break break
elif line.startswith(prefix_with_params): if line.startswith(prefix_with_params):
rv = line[len(prefix_with_params) :].split(":", 1)[-1] rv = line[len(prefix_with_params) :].split(":", 1)[-1]
break break
else: else: