Add config file overwriting

This commit is contained in:
Augusto Gunsch 2021-11-14 16:11:05 -03:00
parent f6183e99bd
commit 7120d76aad
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 31 additions and 14 deletions

View File

@ -3,7 +3,7 @@
# Usage # 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. Fix, edit and clean SubRip (.srt) files.
@ -12,17 +12,20 @@ positional arguments:
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
-c, --clean removes subtitles matching regular expressions listed in ~/.config/fsubrc -c, --clean removes subtitles matching regular expressions listed in
(this is the default behavior if no other flag is passed) ~/.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 -s MS, --shift MS shifts all subtitles by MS milliseconds, which may be positive or
negative negative
-n, --no-html strips HTML tags from subtitles content -n, --no-html strips HTML tags from subtitles content
-f FILE, --config-file FILE
overwrites the default config file (~/.config/fsubrc)
``` ```
# Features # Features
- Fixes subtitle numbering - Fixes subtitle numbering
- Converts files to UTF-8 encoding - Converts files to UTF-8 encoding
- Validates file structure - 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 shift the time of all subtitles
- May strip HTML - May strip HTML

24
fsub
View File

@ -98,12 +98,13 @@ class Subtitle:
) )
def clean(subs): def clean(subs, cfg):
# Read expressions in ~/.config/fsubrc # Read expressions in ~/.config/fsubrc
fsubrc = open(os.getenv('HOME') + '/.config/fsubrc', 'r') if not cfg:
lines = re.split(r'\r?\n', fsubrc.read().strip()) cfg = open(os.getenv('HOME') + '/.config/fsubrc', 'r')
lines = re.split(r'\r?\n', cfg.read().strip())
expressions = list(map(re.compile, lines)) expressions = list(map(re.compile, lines))
fsubrc.close() cfg.close()
# Cancel if no expression # Cancel if no expression
if len(expressions) == 0: if len(expressions) == 0:
@ -162,7 +163,7 @@ def process_file(args, file):
# Clean if --clean is passed # Clean if --clean is passed
if args.clean: if args.clean:
subs_objs = clean(subs_objs) subs_objs = clean(subs_objs, args.config_file)
# Shift if --shift is passed # Shift if --shift is passed
if args.shift: if args.shift:
@ -215,6 +216,14 @@ parser.add_argument(
action='store_true' 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( parser.add_argument(
'files', 'files',
help='list of input files (they all must be SubRip 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: if not args.shift and not args.no_html:
args.clean = True 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 # Check if all files are .srt
for file in args.files: for file in args.files:
if file.name[-4:] != '.srt': if file.name[-4:] != '.srt':