8000 paths: make subscription work for composing paths by koreno · Pull Request #455 · tomerfiliba/plumbum · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

paths: make subscription work for composing paths #455

8000
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

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
-----
* Paths: support composing paths using subscription operations (`#455 <https://github.com/tomerfiliba/plumbum/pull/455>`_)
* CLI: Improved 'Set' validator to allow non-string types, and CSV params (`#452 <https://github.com/tomerfiliba/plumbum/pull/452>`_)
* TypedEnv: Facility for modeling environment-variables into python data types (`#451 <https://github.com/tomerfiliba/plumbum/pull/450>`_)
* Commands: execute local/remote commands via a magic `.cmd` attribute (`#450 <https://github.com/tomerfiliba/plumbum/pull/450>`_)
Expand Down
8 changes: 6 additions & 2 deletions docs/paths.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ with a few improvements and extra features.
Paths now support more pathlib like syntax, several old names have been depreciated, like ``.basename``

The primary ways to create paths are from ``.cwd``, ``.env.home``, or ``.path(...)`` on a local
or remote machine, with ``/`` or ``//`` for composition.
or remote machine, with ``/``, ``//`` or ``[]`` for composition.

.. note::

Expand All @@ -43,14 +43,18 @@ Note that a name like ``file.name.10.15.tar.gz`` will have "5" suffixes.
Also available is ``.with_name(name)``, which will will replace the entire name.
``preferred_suffix(suffix)`` will add a suffix if one does not exist (for default suffix situations).

Paths can be composed using ``/``::
Paths can be composed using ``/`` or ``[]``::

>>> p / "notepad.exe"
<LocalPath c:\windows\notepad.exe>
>>> (p / "notepad.exe").is_file()
True
>>> (p / "notepad.exe").with_suffix(".dll")
<LocalPath c:\windows\notepad.dll>
>>> p["notepad.exe"].is_file()
True
>>> p["../some/path"]["notepad.exe"].with_suffix(".dll")
<LocalPath c:\windows\notepad.dll>


You can also iterate over directories to get the contents::
Expand Down
1 change: 1 addition & 0 deletions plumbum/path/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __div__(self, other):
return self.join(other)

__truediv__ = __div__
__getitem__ = __div__

def __floordiv__(self, expr):
"""Returns a (possibly empty) list of paths that matched the glob-pattern under this path"""
Expand Down
1 change: 1 addition & 0 deletions plumbum/path/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __new__(cls, remote, *parts):
windows = (remote.uname.lower() == "windows")
normed = []

parts = tuple(map(str, parts)) # force the paths into string, so subscription works properly
# Simple skip if path is absolute
if parts[0] and parts[0][0] not in ("/", "\\"):
cwd = (remote._cwd if hasattr(remote, '_cwd') else
Expand Down
22 changes: 12 additions & 10 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def test_iterdir(self):
files = list(cwd.iterdir())
assert cwd / 'test_local.py' in files
assert cwd / 'test_remote.py' in files
assert cwd['test_local.py'] in files
assert cwd['test_remote.py'] in files

def test_stem(self):
assert self.longpath.stem == "file"
Expand Down Expand Up @@ -228,18 +230,18 @@ def test_path_dir(self):
def test_mkdir(self):
# (identical to test_remote.TestRemotePath.test_mkdir)
with local.tempdir() as tmp:
(tmp / "a").mkdir(exist_ok=False, parents=False)
assert (tmp / "a").exists()
assert (tmp / "a").is_dir()
(tmp / "a").mkdir(exist_ok=True, parents=False)
(tmp / "a").mkdir(exist_ok=True, parents=True)
tmp["a"].mkdir(exist_ok=False, parents=False)
assert tmp["a"].exists()
assert tmp["a"].is_dir()
tmp["a"].mkdir(exist_ok=True, parents=False)
tmp["a"].mkdir(exist_ok=True, parents=True)
with pytest.raises(OSError):
(tmp / "a").mkdir(exist_ok=False, parents=False)
tmp["a"].mkdir(exist_ok=False, parents=False)
with pytest.raises(OSError):
(tmp / "a").mkdir(exist_ok=False, parents=True)
(tmp / "b" / "bb").mkdir(exist_ok=False, parents=True)
assert (tmp / "b" / "bb").exists()
assert (tmp / "b" / "bb").is_dir()
tmp["a"].mkdir(exist_ok=False, parents=True)
tmp["b"]["bb"].mkdir(exist_ok=False, parents=True)
assert tmp["b"]["bb"].exists()
assert tmp["b"]["bb"].is_dir()
assert not tmp.exists()

def test_mkdir_mode(self):
Expand Down
0