Skip to content

conestack/node.ext.directory

Repository files navigation

node.ext.directory

Latest PyPI version Number of PyPI downloads Test node.ext.directory

Maintenance mode

This package is in maintenance mode. The successor of this package is https://pypi.python.org/pypi/node.ext.fs.

Overview

node.ext.directory is a node implementation for file system directories.

For more information about node see https://pypi.python.org/pypi/node.

Usage

Create new file:

from node.ext.directory import File

file_path = 'file.txt'
f = File(name=file_path)

# set contents via data attribute
f.data = 'data\n'

# set contents via lines attribute
f.lines = ['data']

# set permissions
f.fs_mode = 0o644

# persist
f()

Read existing file:

file_path = 'file.txt'
f = File(name=file_path)

assert(f.data == 'data\n')
assert(f.lines == ['data'])
assert(f.fs_mode == 0o644)

Files with binary data:

from node.ext.directory import MODE_BINARY

file_path = 'file.txt'
f = File(name=file_path)
f.mode = MODE_BINARY

f.data = b'\x00\x00'

assert(f.data == b'\x00\x00')

# lines property won't work if file in binary mode
f.lines  # raises RuntimeError

Create directory:

from node.ext.directory import Directory

dir_path = '.'
d = Directory(name=dir_path)

# add subdirectories and files
d['sub'] = Directory()
d['file.txt'] = File()

# set permissions for directory
d['sub'].fs_mode = 0o755

# persist
d()

Read existing directory:

dir_path = '.'
d = Directory(name=dir_path)
>>> d.printtree()
<class 'node.ext.directory.directory.Directory'>: .
  <class 'node.ext.directory.directory.File'>: file.txt
  <class 'node.ext.directory.directory.Directory'>: sub

Define file factories:

from node.ext import directory

class PyFile(File):
    pass

# set global factories
directory.file_factories['.py'] = PyFile

# set local factories
d = Directory(name='.', factories={'.py': PyFile})

when reading .py files, PyFile is used to instanciate children:

>>> with open('foo.py', 'w') as f:
...     f.write('#')

>>> d = Directory(name='.', factories={'.py': PyFile})
>>> d.printtree()
<class 'node.ext.directory.directory.Directory'>: .
  <class '...PyFile'>: foo.py

Python Versions

  • Python 2.7, 3.7+
  • May work with other versions (untested)

Contributors

  • Robert Niederreiter (Author)

About

Filesystem Directory Abstraction based on Nodes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •