Pre-packaged common functions for day-to-day
Project description
Munchie
Munchie is a simple file object manager to manipulate and store directories and files.
Compatibility
- Munchie works with Linux, OSX, and Windows.
- Requires Python 3.7 or later.
Installing
Install with pip3
or your favorite PyPi package manager.
pip install munchie
Using FileMuncher
To begin better controlling your files and directories it is as easy as importing and constructing a FileMuncher object.
from munchie.file_muncher import FileMuncher
file_muncher = FileMuncher()
Default attributes
By default FileMuncher loads the current working directory as well as the user home directory into respective attributes.
The attributes can be accessed by calling them from the constructed FileMuncher object.
file_muncher.base_dir # current working directory
file_muncher.home_dir # user home directory
Create a new directory
To create a new empty directory simply call the function and provide the path.
# create 'new_directory' in the user home folder
file_muncher.create_new_directory(f'{file_muncher.home_dir}/new_directory')
Create a new file
To create a new empty file simply call the function and provide the path.
# create 'new_file.test' in the user home folder
file_muncher.create_new_file(f'{file_muncher.home_dir}/new_file.test')
Get filesystem details
To get filesystem details about a directory or file:
# get filesystem details about a path
file_muncher.get_path_stats(f'{file_muncher.home_dir}/new_file.test')
Read file
FileMuncher is able to read in various file types and return the contents in a format that can be manipulated.
Supported extensions include:
Type | Extensions | Return Type |
---|---|---|
csv | .csv | list of dicts |
config | .cfg, .conf, .ini | dict |
json | .json | dict |
text | .nfo, .text, .txt | list |
yaml | .yaml, .yml | dict |
To read in the contents of a file all you need is the path to the file. FileMuncher will determine the file extension and how to open the file for you.
# read in 'new_file.json' and return the contents as dict
contents = file_muncher.read_file(f'{file_muncher.home_dir}/new_file.json')
Remove a directory
FileMuncher can also be used to cleanup directories. Provide the path the directory you wish to remove and all of the contents as well as the directory itself will be removed.
# prompt to remove directory 'new_dir'
file_muncher.remove_directory(f'{file_muncher.home_dir}/new_dir')
By default this command will require confirmation input from the user to verify prior to deletion. The confirmation requirement can be toggled off by setting the force
flag.
# remove directory 'new_dir' without confirmation check
file_muncher.remove_directory(f'{file_muncher.home_dir}/new_dir', force=True)
Remove a file
In addition to directories FileMuncher can also remove files by providing the path the file you wish to remove
# prompt to remove file 'new_file.test'
file_muncher.remove_file(f'{file_muncher.home_dir}/new_file.test')
By default this command will require confirmation input from the user to verify prior to deletion. The confirmation requirement can be toggled off by setting the force flag.
# remove file 'new_file.test' without confirmation check
file_muncher.remove_file(f'{file_muncher.home_dir}/new_file.test', force=True)
Rotate files
FileMuncher is capable of rotating old or stale files based on the their last modified date.
# prompt to rotate files in directory 'old_logs'
file_muncher.rotate_files(f'{file_muncher.home_dir}/old_logs')
You can specify how many days old files to rotate should be by setting the days_old
parameter. Default is 14.
# prompt to remove files in directory 'old_logs' older than 30 days
file_muncher.rotate_files(f'{file_muncher.home_dir}/old_logs', days_old=30)
In the same way removing directories and individual files works rotating also prompts for confirmation. This too can be toggled off with a force
flag.
# remove files in directory 'old_logs' older than 30 days without confirmation check
file_muncher.rotate_files(f'{file_muncher.home_dir}/old_logs', days_old=30, force=True)
Update Path
New directory and file paths can be added to the FileMuncher object. Existing paths can also be updated.
Whether creating a new path or updating an existing the only requirements are the attribute_name
and the attribute_path
# create a new attribute called 'new_file' that points to 'new_file.test' located in the user home directory
file_muncher.update_path(attribute_name='new_file', attribute_path=f'{file_muncher.home_dir}/new_file.test')
# update an existing path to point somewhere new
file_muncher.update_path(attribute_name='base_dir', attribute_path='/Users/username/Documents/testing')
When creating or updating a path you have the option to create a blank directory or empty file at the same time by specifying the is_dir
or is_file
flags. Only one flag may be chosen as a path cannot be both a directory and a file.
# create a new directory attribute and create the directory on the filesystem
file_muncher.update_path(attribute_name='new_dir', attribute_path=f'{file_muncher.home_dir}/new_dir', is_dir=True)
# create a new file attribute and create the file on the filesystem
file_muncher.update_path(attribute_name='new_file', attribute_path=f'{file_muncher.home_dir}/new_file.test', is_file=True)
Write file
Update or create a new file with contents. Supports the following file types:
- .csv
- .cfg, .conf, .ini
- .json
- .nfo, .text, .txt
- .yaml, .yml
To write contents to a file simply pass the contents you want to write and the path to save the contents to.
# prepare dict contents
contents = {
"file": "muncher"
}
# write the contents to a json file
file_muncher.write_file(contents, f'{file_muncher.home_dir}/new_file.json')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file munchie-0.0.17.tar.gz
.
File metadata
- Download URL: munchie-0.0.17.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5aae78eff4b0e7b7252d8c0995c3b0954a8e6420b5782574ff6bca74266f4b72 |
|
MD5 | fb3cd4e1198619421d799ed512a09fe9 |
|
BLAKE2b-256 | c348c796046a224556b61a2242a95350cdb056635bbcb94d8240a97d408f1b88 |
File details
Details for the file munchie-0.0.17-py3-none-any.whl
.
File metadata
- Download URL: munchie-0.0.17-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54ea6d26752ae3225374e7cf2efcf34520dfd27c259049692370137386d13a8a |
|
MD5 | 9009b0168ea06352bdb1be33077967f1 |
|
BLAKE2b-256 | 27870f95eb7cbdf6bfe9a3bd081f00b5b19d68f050efd6762302d1d41c42feb5 |