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.16.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

munchie-0.0.16-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for munchie-0.0.16.tar.gz
Algorithm Hash digest
SHA256 04e17ad8b84a7ac138da53a4b6cf4904a79888cbacfec5d2c29131bef53d6e73
MD5 bc722ce7d79e58d03269173fca7e9e21
BLAKE2b-256 071b6345d8ebe97e50e3fdc8d00d535a1cdbba61e0f9fe41b459c8625bb92222

See more details on using hashes here.

File details

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

File metadata

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

Hashes for munchie-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 bfebf84bc5327469efaef27d23e93e36f9681ac429aa5b81a530dd4eee0a9ad0
MD5 f36fb245293cf727c27a3b37500e6178
BLAKE2b-256 23a422b9875a637fcd44f8472ed21132477e72fb00aececd190de6f0f92d0f8b

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