jsonextended.mockpath module¶
-
class
jsonextended.mockpath.
MockPath
(path='root', is_file=False, exists=True, structure=(), content='', parent=None)[source]¶ Bases:
object
a mock path, mimicking pathlib.Path, supporting context open method for read/write
Parameters: Examples
>>> file_obj = MockPath("path/to/test.txt",is_file=True, ... content="line1\nline2\nline3") ... >>> file_obj MockFile("path/to/test.txt") >>> file_obj.name 'test.txt' >>> file_obj.parent MockFolder("path/to") >>> print(str(file_obj)) path/to/test.txt >>> print(file_obj.to_string()) File("test.txt") Contents: line1 line2 line3 >>> file_obj.is_file() True >>> file_obj.is_dir() False >>> with file_obj.open('r') as f: ... print(f.readline().strip()) line1 >>> with file_obj.open('w') as f: ... f.write('newline1\nnewline2') >>> print(file_obj.to_string()) File("test.txt") Contents: newline1 newline2
>>> with file_obj.maketemp() as temp: ... with temp.open() as f: ... print(f.readline().strip()) newline1
>>> dir_obj = MockPath( ... structure=[{'dir1':[{'subdir':[file_obj.copy_path_obj()]},file_obj.copy_path_obj()]}, ... {'dir2':[file_obj.copy_path_obj()]},file_obj.copy_path_obj()] ... ) >>> dir_obj MockFolder("root") >>> dir_obj.name 'root' >>> dir_obj.is_file() False >>> dir_obj.is_dir() True >>> print(dir_obj.to_string()) Folder("root") Folder("dir1") Folder("subdir") File("test.txt") File("test.txt") Folder("dir2") File("test.txt") File("test.txt")
>>> "dir1/test.txt" in dir_obj True
>>> dir_obj["dir1/test.txt"] MockFile("root/dir1/test.txt")
>>> list(dir_obj.iterdir()) [MockFolder("root/dir1"), MockFolder("root/dir2"), MockFile("root/test.txt")]
>>> list(dir_obj.glob("*")) [MockFolder("root/dir1"), MockFolder("root/dir2"), MockFile("root/test.txt")]
>>> list(dir_obj.glob("dir1/*")) [MockFolder("root/dir1/subdir"), MockFile("root/dir1/test.txt")]
>>> list(dir_obj.glob("**")) [MockFolder("root/dir1"), MockFolder("root/dir1/subdir"), MockFolder("root/dir2")]
>>> list(dir_obj.glob("**/*")) [MockFolder("root/dir1"), MockFolder("root/dir1/subdir"), MockFile("root/dir1/subdir/test.txt"), MockFile("root/dir1/test.txt"), MockFolder("root/dir2"), MockFile("root/dir2/test.txt"), MockFile("root/test.txt")]
# >>> list(dir_obj.glob(“**/dir1”)) # [MockFolder(“root/dir1”)]
>>> new = dir_obj.joinpath('dir3') >>> new.mkdir() >>> list(dir_obj.iterdir()) [MockFolder("root/dir1"), MockFolder("root/dir2"), MockFolder("root/dir3"), MockFile("root/test.txt")]
>>> dir_obj.joinpath("test.txt").unlink() >>> list(dir_obj.iterdir()) [MockFolder("root/dir1"), MockFolder("root/dir2"), MockFolder("root/dir3")]
>>> dir_obj.joinpath("dir3").rmdir() >>> list(dir_obj.iterdir()) [MockFolder("root/dir1"), MockFolder("root/dir2")]
>>> print(dir_obj.to_string()) Folder("root") Folder("dir1") Folder("subdir") File("test.txt") File("test.txt") Folder("dir2") File("test.txt")
>>> dir_obj.joinpath("dir1/subdir") MockFolder("root/dir1/subdir")
>>> dir_obj.joinpath("dir1", "subdir") MockFolder("root/dir1/subdir")
>>> new = dir_obj.joinpath("dir1/subdir/other") >>> new MockVirtualPath("root/dir1/subdir/other")
>>> new.touch() >>> new MockFile("root/dir1/subdir/other")
>>> new.unlink() >>> new MockVirtualPath("root/dir1/subdir/other")
>>> new.mkdir() >>> new MockFolder("root/dir1/subdir/other")
>>> newfile = MockPath('newfile.txt', is_file=True) >>> new.copy_from(newfile) >>> print(new.to_string()) Folder("other") File("newfile.txt")
>>> file_obj = MockPath("newfile2.txt",is_file=True, content='test') >>> file_obj.copy_to(new) >>> print(new.to_string()) Folder("other") File("newfile.txt") File("newfile2.txt")
>>> file_obj.name = "newfile3.txt" >>> with file_obj.maketemp() as temp: ... new.copy_from(temp) >>> print(new.to_string()) Folder("other") File("newfile.txt") File("newfile2.txt") File("newfile3.txt")
>>> print(new.copy_path_obj().to_string()) Folder("other") File("newfile.txt") File("newfile2.txt") File("newfile3.txt")
>>> with new.maketemp(getoutput=True) as tempdir: ... tempdir.joinpath("new").mkdir() ... tempdir.joinpath("new/file.txt").touch() >>> print(new.to_string()) Folder("other") Folder("new") File("file.txt") File("newfile.txt") File("newfile2.txt") File("newfile3.txt")
-
children
¶
-
chmod
(mode)[source]¶ Change the mode (permissions) of a file
Parameters: Examples
To make a file executable cur_mode = folder.stat(“exec.sh”).st_mode folder.chmod(“exec.sh”, cur_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH )
-
copy_from
(source)[source]¶ copy from a source to a mock directory
Parameters: source (str or pathlib.Path or MockPath) – any file like object or path to a file
-
copy_to
(target)[source]¶ copy from a mock path to a target
Parameters: target (str, pathlib.Path or MockPath) –
-
file_content
¶
-
maketemp
(getoutput=False, dir=None)[source]¶ make a named temporary file or folder containing the path contents
Parameters: Yields: temppath (path.Path) – path to temporary
-
mkdir
(parents=False)[source]¶ Parameters: - mode –
- parents (bool) – If True, any missing parents of this path are created as needed If False, a missing parent raises FileNotFoundError.
-
name
¶
-
parent
¶
-
path
¶
-
-
jsonextended.mockpath.
colortxt
(text, color=None, on_color=None, attrs=None)[source]¶ Colorize text.
- Available text colors:
- red, green, yellow, blue, magenta, cyan, white.
- Available text highlights:
- on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
- Available attributes:
- bold, dark, underline, blink, reverse, concealed.
Examples
>>> txt = colortxt('Hello, World!', 'red', 'on_grey', ['bold']) >>> print(txt) [1m[40m[31mHello, World![0m