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,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

24
fsub
View File

@ -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':