From 7120d76aad2f7c45ed2dec513be20aa663d06565 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sun, 14 Nov 2021 16:11:05 -0300 Subject: [PATCH] Add config file overwriting --- README.md | 21 ++++++++++++--------- fsub | 24 +++++++++++++++++++----- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 398c68d..92efae0 100644 --- a/README.md +++ b/README.md @@ -3,26 +3,29 @@ # Usage ``` -usage: fsub [-h] [-c] [-s MS] [-n] file [file ...] +usage: fsub [-h] [-c] [-s MS] [-n] [-f FILE] file [file ...] Fix, edit and clean SubRip (.srt) files. positional arguments: - file list of input files (they all must be SubRip files) + file list of input files (they all must be SubRip files) optional arguments: - -h, --help show this help message and exit - -c, --clean removes subtitles matching regular expressions listed in ~/.config/fsubrc - (this is the default behavior if no other flag is passed) - -s MS, --shift MS shifts all subtitles by MS milliseconds, which may be positive or - negative - -n, --no-html strips HTML tags from subtitles content + -h, --help show this help message and exit + -c, --clean removes subtitles matching regular expressions listed in + ~/.config/fsubrc (this is the default behavior if no other flag is + passed) + -s MS, --shift MS shifts all subtitles by MS milliseconds, which may be positive or + negative + -n, --no-html strips HTML tags from subtitles content + -f FILE, --config-file FILE + overwrites the default config file (~/.config/fsubrc) ``` # Features - Fixes subtitle numbering - Converts files to UTF-8 encoding - Validates file structure -- May remove subtitles containing lines that match any regular expression listed in `~/.config/fsubrc` +- May remove subtitles containing lines that match any regular expression listed in the config file (by default `~/.config/fsubrc`) - May shift the time of all subtitles - May strip HTML diff --git a/fsub b/fsub index 2d2e6a8..767065f 100755 --- a/fsub +++ b/fsub @@ -98,12 +98,13 @@ class Subtitle: ) -def clean(subs): +def clean(subs, cfg): # Read expressions in ~/.config/fsubrc - fsubrc = open(os.getenv('HOME') + '/.config/fsubrc', 'r') - lines = re.split(r'\r?\n', fsubrc.read().strip()) + if not cfg: + cfg = open(os.getenv('HOME') + '/.config/fsubrc', 'r') + lines = re.split(r'\r?\n', cfg.read().strip()) expressions = list(map(re.compile, lines)) - fsubrc.close() + cfg.close() # Cancel if no expression if len(expressions) == 0: @@ -162,7 +163,7 @@ def process_file(args, file): # Clean if --clean is passed if args.clean: - subs_objs = clean(subs_objs) + subs_objs = clean(subs_objs, args.config_file) # Shift if --shift is passed if args.shift: @@ -215,6 +216,14 @@ parser.add_argument( action='store_true' ) +parser.add_argument( + '-f', '--config-file', + help='overwrites the default config file (~/.config/fsubrc)', + metavar='FILE', + action='store', + type=argparse.FileType('r') +) + parser.add_argument( 'files', help='list of input files (they all must be SubRip files)', @@ -229,6 +238,11 @@ args = parser.parse_args() if not args.shift and not args.no_html: args.clean = True +# Validate options +if not args.clean and args.config_file: + print('-f requires -c', file=sys.stderr) + exit(1) + # Check if all files are .srt for file in args.files: if file.name[-4:] != '.srt':