From 141623c5e944e8618a0a68a0765f68ca1e991d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B8=D0=BB=D1=8F=D0=BD=20=D0=9F=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=D1=83=D0=B7=D0=BE=D0=B2?= Date: Mon, 8 Mar 2021 22:53:46 +0200 Subject: [PATCH] Some code simplifications with the return statement --- vdirsyncer/cli/config.py | 5 ++--- vdirsyncer/cli/discover.py | 11 +++++------ vdirsyncer/cli/utils.py | 3 +-- vdirsyncer/http.py | 4 ++-- vdirsyncer/storage/dav.py | 8 +++----- vdirsyncer/utils.py | 6 ++---- vdirsyncer/vobject.py | 18 +++++++----------- 7 files changed, 22 insertions(+), 33 deletions(-) diff --git a/vdirsyncer/cli/config.py b/vdirsyncer/cli/config.py index f9884d6d..bbfec2c8 100644 --- a/vdirsyncer/cli/config.py +++ b/vdirsyncer/cli/config.py @@ -257,7 +257,7 @@ def _process_conflict_resolution_param( ): 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 @@ def resolve(a, b): 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 diff --git a/vdirsyncer/cli/discover.py b/vdirsyncer/cli/discover.py index ebd4e161..62495a73 100644 --- a/vdirsyncer/cli/discover.py +++ b/vdirsyncer/cli/discover.py @@ -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}") diff --git a/vdirsyncer/cli/utils.py b/vdirsyncer/cli/utils.py index 37c27c41..8fa51ee2 100644 --- a/vdirsyncer/cli/utils.py +++ b/vdirsyncer/cli/utils.py @@ -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) diff --git a/vdirsyncer/http.py b/vdirsyncer/http.py index 51d451c2..0ecd6d2a 100644 --- a/vdirsyncer/http.py +++ b/vdirsyncer/http.py @@ -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" diff --git a/vdirsyncer/storage/dav.py b/vdirsyncer/storage/dav.py index b9110827..65463223 100644 --- a/vdirsyncer/storage/dav.py +++ b/vdirsyncer/storage/dav.py @@ -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 @@ def _check_collection_resource_type(self, response): 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 : %s", response) return False if props.find("{DAV:}resourcetype/" + self._resourcetype) is None: @@ -626,7 +624,7 @@ def _parse_prop_responses(self, root, handled_hrefs=None): 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: diff --git a/vdirsyncer/utils.py b/vdirsyncer/utils.py index f5a55812..2d4b5f27 100644 --- a/vdirsyncer/utils.py +++ b/vdirsyncer/utils.py @@ -24,8 +24,7 @@ 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): diff --git a/vdirsyncer/vobject.py b/vdirsyncer/vobject.py index 8fdb2abc..08d05225 100644 --- a/vdirsyncer/vobject.py +++ b/vdirsyncer/vobject.py @@ -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 @@ def parse(cls, lines, multiple=False): 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 @@ def __delitem__(self, key): for line in lineiter: if line.startswith(prefix): break - else: - new_lines.append(line) + new_lines.append(line) else: break @@ -347,10 +344,9 @@ def __contains__(self, obj): 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 @@ def __getitem__(self, key): 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: