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"):
return conflict_resolution
elif (
if (
isinstance(conflict_resolution, list)
and len(conflict_resolution) > 1
and conflict_resolution[0] == "command"
@ -271,8 +271,7 @@ class PairConfig:
return _resolve_conflict_via_command(a, b, command, a_name, b_name)
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
# self.config_a would expand all `x.fetch` parameters. This is costly and

View file

@ -59,21 +59,20 @@ async def collections_for_pair(
cache_key = _get_collections_cache_key(pair)
if from_cache:
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(
_expand_collections_cache(
rv["collections"], pair.config_a, pair.config_b
)
)
elif rv:
if rv:
raise exceptions.UserError(
"Detected change in config file, "
f"please run `vdirsyncer discover {pair.name}`."
)
else:
raise exceptions.UserError(
f"Please run `vdirsyncer discover {pair.name}` before synchronization."
)
raise exceptions.UserError(
f"Please run `vdirsyncer discover {pair.name}` before synchronization."
)
logger.info(f"Discovering collections for pair {pair.name}")

View file

@ -294,8 +294,7 @@ async def storage_instance_from_config(
create=False,
connector=connector,
)
else:
raise
raise
except Exception:
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 auth == "basic" or auth is None:
return BasicAuthMethod(username, password)
elif auth == "digest":
if auth == "digest":
return DigestAuthMethod(username, password)
elif auth == "guess":
if auth == "guess":
raise exceptions.UserError(
"'Guess' authentication is not supported in this version of vdirsyncer. \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
mediatype, subtype = strict.split("/")
if subtype in weak:
return True
return False
return subtype in weak
class Discover:
@ -237,7 +235,7 @@ class Discover:
return True
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)
return False
if props.find("{DAV:}resourcetype/" + self._resourcetype) is None:
@ -626,7 +624,7 @@ class DAVStorage(Storage):
continue
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.")
continue
else:

View file

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

View file

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