-
Notifications
You must be signed in to change notification settings - Fork 25
Add code to link remote and local paths. #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rsekman
wants to merge
7
commits into
rndusr:master
Choose a base branch
from
rsekman:links
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
062910b
Add code to link remote and local paths.
rsekman 3bd5dba
links: only allow absolute paths
rsekman f32b60e
links: fix a typo in an error message
rsekman dc5962c
links: convert path argument to translation to pathlib.Path
rsekman c1f6312
links: use links when adding torrents from cli
rsekman a1660b9
add: check if path is not None before translating
rsekman 6dad974
links: linting
rsekman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
'pyxdg', | ||
'blinker', | ||
'natsort', | ||
'bidict', | ||
], | ||
extras_require = { | ||
'setproctitle': ['setproctitle'], | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details | ||
# http://www.gnu.org/licenses/gpl-3.0.txt | ||
|
||
from pathlib import Path | ||
from bidict import bidict, ValueDuplicationError | ||
|
||
from ..logging import make_logger # isort:skip | ||
|
||
log = make_logger(__name__) | ||
|
||
|
||
class PathTranslator: | ||
def __init__(self): | ||
# links: a dict of (remote_root, local_root) pairs | ||
self.links = bidict() | ||
|
||
def link(self, remote, local, force=False): | ||
remote = Path(remote) | ||
local = Path(local) | ||
if not local.exists(): | ||
raise ValueError("Local path %s does not exist." % local) | ||
if not local.is_absolute(): | ||
raise ValueError("Local path %s must be absolute." % local) | ||
if not remote.is_absolute(): | ||
raise ValueError("Remote path %s must be absolute." % remote) | ||
if force: | ||
self.links.forceput(remote, local) | ||
if remote in self.links.keys(): | ||
raise ValueError( | ||
"Remote path %s is already linked to local path %s." | ||
% (remote, self.links[remote]) | ||
) | ||
try: | ||
self.links[remote] = local | ||
except ValueDuplicationError: | ||
raise ValueError( | ||
"Local path %s is already linked to remote path %s." | ||
% (local, self.links[remote]) | ||
) | ||
log.debug("Linked remote path %s to local path %s" % (remote, local)) | ||
|
||
def _translate_path(self, path, links): | ||
try: | ||
path = Path(path) | ||
except Exception: | ||
raise ValueError("Could not convert %s to Path" % path) | ||
for roots in links.items(): | ||
try: | ||
return roots[1] / path.relative_to(roots[0]) | ||
except ValueError: | ||
pass | ||
return path | ||
|
||
def to_local(self, p_rem): | ||
return self._translate_path(p_rem, self.links) | ||
|
||
def to_remote(self, p_loc): | ||
return self._translate_path(p_loc, self.links.inv) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason this needs to be
async
? Every nested scope this coroutine would generate appears to use blocking methods.