File Automation - run templated shell commands for each file matched by pattern.
Project description
File Automation
Run templated shell commands for each file matched by pattern.
Overview
Use YAML configuration file that defines how to list and match files. For each matched file, program will execute shell command that is templated using various variables. This program uses two concepts: targets and presets.
Targets: Each target contains a configration for file matching and list of presets to apply for each matched file. Use globbing syntax and additional modifiers like extensions to exclude or include, or minimum time passed since modification.
Presets: Preset is a shell command that will be executed for every file match.
Targets and presets will run in the order of their definition in YAML file.
Shell commands are templated using python string format syntax.
Use cases
Originally I developed this as a simple script to run FFMPEG on my video recordings. The general idea of file automation allowed me to setup a very powerful workflow:
- Automatically copy all video files from inserted SD card onto a new folder using files modification time
- Automatically encode those video files into format that is more suitable for video editing
- Remove encode files that are more than couple months old - I am not going to use them anyway past that
- Automatically transcode those 4k 120fps video files into smaller 1080p 30fps files optimized for long term storage
- Delete original files older than 1 year
- Automatically move any short fragments i cut from main videos, and transcode them to be suitable for telegram
- Automatically upload those transcoded short fragments into telegram channel
As you can see, this workflow is very powerful yet can be easily expressed and managed by file automation.
Currently, it runs on my NAS in docker container built for FFMPEG and hardware accelerated video encoding.
Configuration reference
presets:
# Order in which presets are defined does not matter. This is a "repository" of available presets.
move:
# Required. Command will be rendered using variables available in templating context. See "Templating context reference"
command: mv "{input_path}" "{output_path}"
# Optional. If your command creates new files, you can specify this parameter that will be used as new file name.
# Result of rendering this template will be available as "output_path" variable in command template.
# Command will not run if "output_path" file exists.
rename: {move_dir}/{name}
# Optional. Any additional variables to pass into command template. Will override all other built-in variables and target's variables too.
vars:
foo: bar
targets:
# Targets definition order matters. Targets are processed one by one in order they are defined.
move_docs:
# Required. Pattern for file matching. Supports standard globbing syntax like *, ?, **
glob: /docs_landing/*
# Optional. ONLY match files with these extensions. With dot!
include_ext:
- .txt
- .doc
# Optional. Exclude files that have these extensions. With dot!
exclude_ext:
- .mp4
# Optional. Exclude files that have these keywords in their name (case-insensitive)
exclude_keywords:
- billing
# Optional. Minimum time in seconds that passed since file modification to consider it as match.
min_age_s: 90
# Required. List of preset names from "presets" config that will be applied to each matched file.
presets:
- move
# Optional. Any additional variables to pass into command template. Will override all other built-in variables.
vars:
move_dir: /docs
Templating context reference
Available variables in rename
and command
parameters:
date
Current ISO date 2024-01-01time
Current ISO time 23:59:59created_at_date
ISO date when file was createdmodified_at_date
ISO date when file was last modifiedinput_path
Absolute path that is matched by target and passed through all filtersoutput_path
Available if preset'srename
parameter is set.stem
File name without extension and folders (for/foo/bar.txt
stem isbar
)suffix
Last file suffix (extension) dot includedext
Same assuffix
parent
Absolute path to a folder containing matched fileparent_parent
Absolute path to folder that contains folder with matched filetarget
Current target namepreset
Current preset namename
File name (with suffix but without folders)
vars
from preset and target will be also available. Preset's vars
will take precedence over any other variables defined.
Command line usage
This is available as command line script:
> file_automation --help
usage: file_automation [-h] [-c CONFIG] [--loop LOOP]
File Automation - run templated shell commands for each file matched by pattern.
options:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
Optional. Specify path to yaml config file. Can also use env FFM_CONFIG
--loop LOOP Instead of running one time and existing, this will make script to run every "loop" seconds.
It will try to load config every time it runs so changes to configuration do not require script restart.
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 file_automation-0.1.1.tar.gz
.
File metadata
- Download URL: file_automation-0.1.1.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d050c2b34789bcbb2093020738a5cde3f9ef3c3c67f84fdadf1fb855b9f31ef |
|
MD5 | 331f84fb8c4a2fa5c56f11a20e26436a |
|
BLAKE2b-256 | 22736b7ac1d3050f4c1edb93a27387c20ba64f340efa7efa1c6037836760c211 |
File details
Details for the file file_automation-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: file_automation-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28da696032e2fabc6c84d96a14d9cc10cfcbf6172ae367f9e3f26c92b950002e |
|
MD5 | c6ec2dbf5601035cf8465654db5f5031 |
|
BLAKE2b-256 | ba4bd94ed98421da69f7514f6b143d56c6a321389aeb853db9ce78987f6d897b |