Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

munchie-0.0.19.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

munchie-0.0.19-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file munchie-0.0.19.tar.gz.

File metadata

  • Download URL: munchie-0.0.19.tar.gz
  • Upload date:
  • Size: 14.7 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

Hashes for munchie-0.0.19.tar.gz
Algorithm Hash digest
SHA256 1a48984edab3a8a9c5ffb1aaf9396ade33121d4d162af2d19cba6029e7ffdd6a
MD5 f7d7953deac1e4fe961a392d9de932ac
BLAKE2b-256 06042736d2b8dc95d6755bcdbe7facab1ae3a8d21a0ee50ba56fbdca19a9f0e7

See more details on using hashes here.

File details

Details for the file munchie-0.0.19-py3-none-any.whl.

File metadata

  • Download URL: munchie-0.0.19-py3-none-any.whl
  • Upload date:
  • Size: 13.6 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

Hashes for munchie-0.0.19-py3-none-any.whl
Algorithm Hash digest
SHA256 8d9cf6c521d27903772a4d7abbfd7798d681c298d59622c9dc0a64e4b3e6bf20
MD5 e3f180443418ece68ac7ddcdbd2c5f65
BLAKE2b-256 cc83af0cb03851d99dd09d7659984c73aa3fad7b473475506d7c9107dbc1678b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page