Conversation
| self._error_tmp = dict() | ||
| self._move_tmp = dict() | ||
| self._previous_stat = dict() | ||
| self._error_tmp = {} | ||
| self._move_tmp = {} | ||
| self._previous_stat = {} |
There was a problem hiding this comment.
Function SyncClient.__init__ refactored with the following changes:
- Replace dict() with {} (
dict-literal)
| if not handler in inotify.adapters._LOGGER.handlers: | ||
| if handler not in inotify.adapters._LOGGER.handlers: | ||
| inotify.adapters._LOGGER.addHandler(handler) | ||
| inotify.adapters._LOGGER.setLevel(self.logger.level) | ||
| notifyier = inotify.adapters.InotifyTree(base_dir, mask=mask) | ||
| for event in notifyier.event_gen(): | ||
| if event is not None: | ||
| header, type_names, watch_path, filename = event | ||
| if not any([tn in HANDLE_EVENTS for tn in type_names]): | ||
| if all(tn not in HANDLE_EVENTS for tn in type_names): | ||
| self.logger.debug("ignoring event type_names=%r", type_names) | ||
| continue | ||
| if any([re.search(regex, filename) for regex in FILE_IGNORE_REGEX]): | ||
| if any(re.search(regex, filename) for regex in FILE_IGNORE_REGEX): |
There was a problem hiding this comment.
Function SyncClient.watch_dir refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan) - Invert any/all to simplify comparisons (
invert-any-all) - Replace unneeded comprehension with generator (
comprehension-to-generator)
| if "IN_ISDIR" in action: | ||
| if "IN_CREATE" in action: | ||
| if "IN_CREATE" in action: | ||
| if "IN_ISDIR" in action: |
There was a problem hiding this comment.
Function SyncClient.handle_fs_event refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if)
This removes the following comments ( why? ):
# prevent unnecessary request by detecting IN_ATTRIB following
# file was moved away, noticed by request.add_stat() above
# IN_CREATE and IN_CLOSE_WRITE without stat change
# moved in from outside of inotify-observed tree
# prevent unnecessary request in case of an empty file
| processes = list() | ||
| processes = [] |
There was a problem hiding this comment.
Function main refactored with the following changes:
- Replace list() with [] (
list-literal)
| self._request[key] = self.encode_binary(value) | ||
| else: | ||
| self._request[key] = value | ||
| self._request[key] = self.encode_binary(value) if key == "data" else value |
There was a problem hiding this comment.
Function ZRequest.__setitem__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| self.diff_tree = dict() | ||
| self.diff_tree = {} |
There was a problem hiding this comment.
Function SyncServer.sync_tree refactored with the following changes:
- Replace dict() with {} (
dict-literal) - Replace list() with [] (
list-literal) - Use named expression to simplify assignment and conditional (
use-named-expression) - Simplify logical expression using De Morgan identities (
de-morgan)
| elif node["st_size"] == 0: | ||
| self.logger.info("truncate %r", file_path) | ||
| with open(file_path, "w") as fp: | ||
| fp.truncate(0) | ||
| warn = self.utime(file_path, node["st_atime"], node["st_mtime"]) | ||
| return dict(cmd="truncate", result="done", name=file_path, warn=warn) | ||
| elif node["st_blocks"] < MIN_BLOCK_DIFF_SIZE: | ||
| # not worth diffing on both ends and patching | ||
| self.logger.info("req full %r", file_path) | ||
| return dict(cmd="full", name=file_path) | ||
| else: | ||
| if node["st_size"] == 0: | ||
| self.logger.info("truncate %r", file_path) | ||
| with open(file_path, "w") as fp: | ||
| fp.truncate(0) | ||
| warn = self.utime(file_path, node["st_atime"], node["st_mtime"]) | ||
| return dict(cmd="truncate", result="done", name=file_path, warn=warn) | ||
| elif node["st_blocks"] < MIN_BLOCK_DIFF_SIZE: | ||
| # not worth diffing on both ends and patching | ||
| self.logger.info("req full %r", file_path) | ||
| return dict(cmd="full", name=file_path) | ||
| else: | ||
| self.logger.info("req diff %r", file_path) | ||
| sig = self.get_signature(file_path) | ||
| return dict(cmd="diff", name=file_path, data=ZRequest.encode_binary(sig.read())) | ||
| self.logger.info("req diff %r", file_path) | ||
| sig = self.get_signature(file_path) | ||
| return dict(cmd="diff", name=file_path, data=ZRequest.encode_binary(sig.read())) | ||
| else: | ||
| raise RuntimeError("We should not be here.") | ||
| else: | ||
| # src_st == dst_st | ||
| pass |
There was a problem hiding this comment.
Function SyncServer.cmp_node refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif) - Remove redundant pass statement (
remove-redundant-pass)
This removes the following comments ( why? ):
# src_st == dst_st
| if _node["cmd"] != "ignore": | ||
| if (not all([isinstance(_node[x], int) for x in ["st_mode", "st_uid", "st_gid", "st_size"]]) or | ||
| not isinstance(_node["st_mtime"], float)): | ||
| return (False, "Bad src_tree _node {}. Item 'st_mode', 'st_uid', 'st_gid', 'st_mtime' or " | ||
| "'st_size' has wrong type.".format(_node)) | ||
| if _node["cmd"] != "ignore" and ( | ||
| not all( | ||
| isinstance(_node[x], int) | ||
| for x in ["st_mode", "st_uid", "st_gid", "st_size"] | ||
| ) | ||
| or not isinstance(_node["st_mtime"], float) | ||
| ): | ||
| return (False, "Bad src_tree _node {}. Item 'st_mode', 'st_uid', 'st_gid', 'st_mtime' or " | ||
| "'st_size' has wrong type.".format(_node)) |
There was a problem hiding this comment.
Function SyncServer.validate_tree_structure.validate_node refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs) - Replace unneeded comprehension with generator (
comprehension-to-generator)
| if exc.errno == 2: | ||
| pass | ||
| else: | ||
| if exc.errno != 2: |
There was a problem hiding this comment.
Function SyncServer.run refactored with the following changes:
- Swap if/else to remove empty if body (
remove-pass-body)
| else: | ||
| self.logger.error("Checksum mismatch after patching %r.", request.name) | ||
| return {"status": 2, "reason": "Checksum mismatch for '{}'.".format(request.name)} | ||
| self.logger.error("Checksum mismatch after patching %r.", request.name) | ||
| return {"status": 2, "reason": "Checksum mismatch for '{}'.".format(request.name)} |
There was a problem hiding this comment.
Function SyncServer.post_file_write_action refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 2.85%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
masterrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run:Help us improve this pull request!