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:
  • path (str) – the path string
  • is_file (bool) – if True is file, else folder
  • content (str) – content of the file
  • structure – structure of the directory

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")
absolute()[source]
add_child(child)[source]
children
chmod(mode)[source]

Change the mode (permissions) of a file

Parameters:
  • path (str) –
  • mode (int) – new permissions (see os.chmod)

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_path_obj()[source]

copy mock path (removing path and parent)

copy_to(target)[source]

copy from a mock path to a target

Parameters:target (str, pathlib.Path or MockPath) –
exists()[source]
file_content
glob(regex, recurse=False, toplevel=True)[source]
Parameters:
  • regex (str) – the path regex, with * to match 0 or more (non-recursive) paths and ** to match zero or more (recursive) directories
  • recurse (bool) –
Yields:

path (MockPath)

is_dir()[source]
is_file()[source]
iterdir()[source]
joinpath(*paths)[source]
maketemp(getoutput=False, dir=None)[source]

make a named temporary file or folder containing the path contents

Parameters:
  • getoutput (bool) – if True, (on exit) new paths will be read/added to the path
  • dir (None or str) – directory to place temp in (see tempfile.mkstemp)
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
open(mode='r', encoding=None)[source]

context manager for opening a file

Parameters:
parent
path
relative_to(other)[source]
rename(target)[source]
rmdir()[source]
samefile(other)[source]
stat()[source]

Retrieve information about a file

Parameters:path (str) –
Returns:attr – see os.stat, includes st_mode, st_size, st_uid, st_gid, st_atime, and st_mtime attributes
Return type:object
to_string(indentlvl=2, file_content=False, color=False)[source]

convert to string

touch()[source]
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)
Hello, World!