Fix Windows compatibility + bugs

This commit is contained in:
Augusto Gunsch 2021-11-14 20:32:35 -03:00
parent 7d04cfcce9
commit cf9cf79da2
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
3 changed files with 50 additions and 27 deletions

View File

@ -9,7 +9,7 @@ pip install fsub
# Usage
```
usage: fsub [-h] [-c] [-s MS] [-n] [-f FILE] file [file ...]
usage: fsub.py [-h] [-c] [-s MS] [-n] [-f FILE] file [file ...]
Fix, edit and clean SubRip (.srt) files.
@ -18,20 +18,20 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
-c, --clean remove subtitles matching regular expressions listed in
~/.config/fsubrc (this is the default behavior if no other flag is
passed)
-c, --clean remove subtitles matching regular expressions listed in the config
file (this is the default behavior if no other flag is passed)
-s MS, --shift MS shift all subtitles by MS milliseconds, which may be positive or
negative
-n, --no-html strip HTML tags from subtitles content
-f FILE, --config-file FILE
overwrite the default config file (~/.config/fsubrc)
overwrite the default config file (Unix: $HOME/.config/fsubrc,
Windows: %APPDATA%\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 the config file (by default `~/.config/fsubrc`)
- May remove subtitles containing lines that match any regular expression listed in the config file (by default on Unix: `$HOME/.config/fsubrc`; on Windows: `%APPDATA%\fsubrc`)
- May shift the time of all subtitles
- May strip HTML

View File

@ -1,6 +1,6 @@
[metadata]
name = fsub
version = 0.0.5
version = 0.1.0
author = Augusto Lenz Gunsch
author_email = augustogunsch@tutanota.com
description = CLI SubRip editor

View File

@ -26,6 +26,7 @@ import argparse
import re
import chardet
import os
import pathlib
class Time:
@ -91,24 +92,17 @@ class Subtitle:
return False
def __repr__(self):
return '{}{}{} --> {}{}{}'.format(
self.number, os.linesep,
return '{}\n{} --> {}\n{}'.format(
self.number,
self.time_start, self.time_end,
os.linesep, os.linesep.join(self.content)
'\n'.join(self.content)
)
def clean(subs, cfg):
# Read expressions in ~/.config/fsubrc
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))
cfg.close()
def clean(subs, expressions):
# Cancel if no expression
if len(expressions) == 0:
return
return subs
# Remove lines matching any expression
for regexp in expressions:
@ -126,10 +120,10 @@ def shift(subs, ms):
def strip_html(subs):
for sub in subs:
for i in range(0, len(sub.content)):
sub.content[i] = re.sub('<.+>', '', sub.content[i])
sub.content[i] = re.sub('<.+?>', '', sub.content[i])
def process_file(args, file):
def process_file(args, file, expressions):
# Read the input file
contents = file.read()
file.close()
@ -163,7 +157,7 @@ def process_file(args, file):
# Clean if --clean is passed
if args.clean:
subs_objs = clean(subs_objs, args.config_file)
subs_objs = clean(subs_objs, expressions)
# Shift if --shift is passed
if args.shift:
@ -180,15 +174,41 @@ def process_file(args, file):
i += 1
# Join Subtitle objects back to a string
contents = (os.linesep + os.linesep).join(map(repr, subs_objs))
contents = '\n\n'.join(map(repr, subs_objs))
# Write output
output = open(file.name, 'w', encoding='utf-8')
output.write(contents)
output.write(os.linesep)
output.write('\n')
output.close()
def read_expressions(args):
if args.clean:
cfg = args.config_file
# Open default config file if not specified
if not args.config_file:
home = pathlib.Path.home()
try:
if type(home) is pathlib.PosixPath:
cfg = open(str(home) + '/.config/fsubrc', 'r')
elif type(home) is pathlib.WindowsPath:
cfg = open(os.getenv('APPDATA') + r'\fsubrc', 'r')
else:
print('Unsupported operating system', file=sys.stderr)
sys.exit(1)
except FileNotFoundError:
return []
# Read expressions
lines = re.split(r'\r?\n', cfg.read().strip())
expressions = list(map(re.compile, lines))
cfg.close()
return expressions
return []
def main():
parser = argparse.ArgumentParser(
description='Fix, edit and clean SubRip (.srt) files.',
@ -198,7 +218,7 @@ def main():
parser.add_argument(
'-c', '--clean',
help='remove subtitles matching regular expressions ' +
'listed in ~/.config/fsubrc (this is the default ' +
'listed in the config file (this is the default ' +
'behavior if no other flag is passed)',
action='store_true'
)
@ -220,7 +240,8 @@ def main():
parser.add_argument(
'-f', '--config-file',
help='overwrite the default config file (~/.config/fsubrc)',
help='overwrite the default config file (Unix: $HOME/.config/fsubrc,' +
r' Windows: %%APPDATA%%\fsubrc)',
metavar='FILE',
action='store',
type=argparse.FileType('r')
@ -252,8 +273,10 @@ def main():
file=sys.stderr)
sys.exit(1)
expressions = read_expressions(args)
for file in args.files:
process_file(args, file)
process_file(args, file, expressions)
if __name__ == '__main__':