Add --replace flag

This commit is contained in:
Augusto Gunsch 2021-11-15 19:30:59 -03:00
parent 790c21f6fe
commit 3b9dcee49f
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
4 changed files with 55 additions and 30 deletions

View File

@ -9,25 +9,23 @@ pip install fsub
# Usage
```
usage: fsub [-h] [-c] [-s MS] [-n] [-f FILE] [-j] file [file ...]
usage: fsub [-h] [-c] [-s MS] [-n] [-f F] [-j] [-r] 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 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 (Unix: $HOME/.config/fsubrc,
Windows: %APPDATA%\fsubrc)
-j, --join join all files into the first, shifting their time accordingly (this
will delete files)
-h, --help show this help message and exit
-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 F, --config F use F as the config file (by default, F is: on Unix:
$HOME/.config/fsubrc; on Windows: %APPDATA%\fsubrc)
-j, --join join all files into the first, shifting their time accordingly
-r, --replace edit files in-place (-j will delete joined files too)
```
# Testing
@ -49,3 +47,4 @@ python -m unittest tests.integration
- May shift the time of all subtitles
- May strip HTML
- May join files together
- May edit files in-place

View File

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

View File

@ -160,12 +160,12 @@ class ConfigFile:
def __init__(self, args):
# No reason to continue
if not args.clean:
if args.config_file:
args.config_file.close()
if args.config:
args.config.close()
self.expressions = []
return
file = args.config_file
file = args.config
# Set default config file if not specified
if not file:
home = Path.home()
@ -272,15 +272,27 @@ class SubripFile:
self.strip_html()
self.renumber()
self.write_file()
self.write_file(args.replace)
def write_file(self):
output = open(self.file_name, 'w', encoding='utf-8')
def write_file(self, in_place=False, stdout=False):
if stdout:
print(self)
return
file = self.file_name if in_place else 'out-' + self.file_name
output = open(file, 'w', encoding='utf-8')
output.write(repr(self))
if len(self.subs) > 0:
output.write('\n')
output.close()
def delete(self):
os.remove(self.file_name)
del self
def __repr__(self):
return '\n\n'.join(map(repr, self.subs))
@ -316,18 +328,23 @@ def parse_args(args):
)
parser.add_argument(
'-f', '--config-file',
help='overwrite the default config file (Unix: $HOME/.config/fsubrc,' +
r' Windows: %%APPDATA%%\fsubrc)',
metavar='FILE',
'-f', '--config',
help='use F as the config file (by default, F is: ' +
r'on Unix: $HOME/.config/fsubrc; on Windows: %%APPDATA%%\fsubrc)',
metavar='F',
action='store',
type=argparse.FileType('r')
)
parser.add_argument(
'-j', '--join',
help='join all files into the first, shifting their time ' +
'accordingly (this will delete files)',
help='join all files into the first, shifting their time accordingly',
action='store_true'
)
parser.add_argument(
'-r', '--replace',
help='edit files in-place (-j will delete joined files too)',
action='store_true'
)
@ -342,11 +359,12 @@ def parse_args(args):
args = parser.parse_args(args)
# Make sure --clean is the default
# TODO: account for new options
if not args.shift and not args.no_html:
args.clean = True
# Validate options
if not args.clean and args.config_file:
if not args.clean and args.config:
panic('-f requires -c', 1)
return args
@ -364,7 +382,9 @@ def run(args):
first = parsed_files.pop(0)
while True:
try:
first += parsed_files.pop(0)
file = parsed_files.pop(0)
first += file
file.delete()
except IndexError:
break
parsed_files.append(first)

View File

@ -9,7 +9,7 @@ from pathlib import Path
class TestFsub(unittest.TestCase):
samples = Path('tests/samples')
def run_on(self, args, samples, ofiles):
def run_on(self, args, samples, ofiles, replace=False):
caller = inspect.stack()[1][3]
ifiles = []
@ -27,6 +27,9 @@ class TestFsub(unittest.TestCase):
limit = len(ofiles)
for i, ifile in enumerate(ifiles):
if i < limit:
if not replace:
os.remove(ifile)
ifile = 'out-' + ifile
out = open(ifile)
result = out.read()
out.close()
@ -37,7 +40,10 @@ class TestFsub(unittest.TestCase):
cmp_file.close()
self.assertEqual(result, cmp)
os.remove(ifile)
try:
os.remove(ifile)
except FileNotFoundError:
pass
def test_cleaned(self):
args = ['-f', str(self.samples / 'blacklist')]