Add --replace flag
This commit is contained in:
parent
790c21f6fe
commit
3b9dcee49f
25
README.md
25
README.md
|
@ -9,25 +9,23 @@ pip install fsub
|
||||||
|
|
||||||
# Usage
|
# 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.
|
Fix, edit and clean SubRip (.srt) files.
|
||||||
|
|
||||||
positional arguments:
|
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:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-c, --clean remove subtitles matching regular expressions listed in the config
|
-c, --clean remove subtitles matching regular expressions listed in the config file
|
||||||
file (this is the default behavior if no other flag is passed)
|
(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
|
-s MS, --shift MS shift all subtitles by MS milliseconds, which may be positive or negative
|
||||||
negative
|
-n, --no-html strip HTML tags from subtitles content
|
||||||
-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:
|
||||||
-f FILE, --config-file FILE
|
$HOME/.config/fsubrc; on Windows: %APPDATA%\fsubrc)
|
||||||
overwrite the default config file (Unix: $HOME/.config/fsubrc,
|
-j, --join join all files into the first, shifting their time accordingly
|
||||||
Windows: %APPDATA%\fsubrc)
|
-r, --replace edit files in-place (-j will delete joined files too)
|
||||||
-j, --join join all files into the first, shifting their time accordingly (this
|
|
||||||
will delete files)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
|
@ -49,3 +47,4 @@ python -m unittest tests.integration
|
||||||
- May shift the time of all subtitles
|
- May shift the time of all subtitles
|
||||||
- May strip HTML
|
- May strip HTML
|
||||||
- May join files together
|
- May join files together
|
||||||
|
- May edit files in-place
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[metadata]
|
[metadata]
|
||||||
name = fsub
|
name = fsub
|
||||||
version = 0.1.2
|
version = 0.1.3
|
||||||
author = Augusto Lenz Gunsch
|
author = Augusto Lenz Gunsch
|
||||||
author_email = augustogunsch@tutanota.com
|
author_email = augustogunsch@tutanota.com
|
||||||
description = CLI SubRip editor
|
description = CLI SubRip editor
|
||||||
|
|
|
@ -160,12 +160,12 @@ class ConfigFile:
|
||||||
def __init__(self, args):
|
def __init__(self, args):
|
||||||
# No reason to continue
|
# No reason to continue
|
||||||
if not args.clean:
|
if not args.clean:
|
||||||
if args.config_file:
|
if args.config:
|
||||||
args.config_file.close()
|
args.config.close()
|
||||||
self.expressions = []
|
self.expressions = []
|
||||||
return
|
return
|
||||||
|
|
||||||
file = args.config_file
|
file = args.config
|
||||||
# Set default config file if not specified
|
# Set default config file if not specified
|
||||||
if not file:
|
if not file:
|
||||||
home = Path.home()
|
home = Path.home()
|
||||||
|
@ -272,15 +272,27 @@ class SubripFile:
|
||||||
self.strip_html()
|
self.strip_html()
|
||||||
|
|
||||||
self.renumber()
|
self.renumber()
|
||||||
self.write_file()
|
self.write_file(args.replace)
|
||||||
|
|
||||||
def write_file(self):
|
def write_file(self, in_place=False, stdout=False):
|
||||||
output = open(self.file_name, 'w', encoding='utf-8')
|
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))
|
output.write(repr(self))
|
||||||
|
|
||||||
if len(self.subs) > 0:
|
if len(self.subs) > 0:
|
||||||
output.write('\n')
|
output.write('\n')
|
||||||
|
|
||||||
output.close()
|
output.close()
|
||||||
|
|
||||||
|
def delete(self):
|
||||||
|
os.remove(self.file_name)
|
||||||
|
del self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '\n\n'.join(map(repr, self.subs))
|
return '\n\n'.join(map(repr, self.subs))
|
||||||
|
|
||||||
|
@ -316,18 +328,23 @@ def parse_args(args):
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-f', '--config-file',
|
'-f', '--config',
|
||||||
help='overwrite the default config file (Unix: $HOME/.config/fsubrc,' +
|
help='use F as the config file (by default, F is: ' +
|
||||||
r' Windows: %%APPDATA%%\fsubrc)',
|
r'on Unix: $HOME/.config/fsubrc; on Windows: %%APPDATA%%\fsubrc)',
|
||||||
metavar='FILE',
|
metavar='F',
|
||||||
action='store',
|
action='store',
|
||||||
type=argparse.FileType('r')
|
type=argparse.FileType('r')
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-j', '--join',
|
'-j', '--join',
|
||||||
help='join all files into the first, shifting their time ' +
|
help='join all files into the first, shifting their time accordingly',
|
||||||
'accordingly (this will delete files)',
|
action='store_true'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-r', '--replace',
|
||||||
|
help='edit files in-place (-j will delete joined files too)',
|
||||||
action='store_true'
|
action='store_true'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -342,11 +359,12 @@ def parse_args(args):
|
||||||
args = parser.parse_args(args)
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
# Make sure --clean is the default
|
# Make sure --clean is the default
|
||||||
|
# TODO: account for new options
|
||||||
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
|
# Validate options
|
||||||
if not args.clean and args.config_file:
|
if not args.clean and args.config:
|
||||||
panic('-f requires -c', 1)
|
panic('-f requires -c', 1)
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
@ -364,7 +382,9 @@ def run(args):
|
||||||
first = parsed_files.pop(0)
|
first = parsed_files.pop(0)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
first += parsed_files.pop(0)
|
file = parsed_files.pop(0)
|
||||||
|
first += file
|
||||||
|
file.delete()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
break
|
break
|
||||||
parsed_files.append(first)
|
parsed_files.append(first)
|
||||||
|
|
|
@ -9,7 +9,7 @@ from pathlib import Path
|
||||||
class TestFsub(unittest.TestCase):
|
class TestFsub(unittest.TestCase):
|
||||||
samples = Path('tests/samples')
|
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]
|
caller = inspect.stack()[1][3]
|
||||||
ifiles = []
|
ifiles = []
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ class TestFsub(unittest.TestCase):
|
||||||
limit = len(ofiles)
|
limit = len(ofiles)
|
||||||
for i, ifile in enumerate(ifiles):
|
for i, ifile in enumerate(ifiles):
|
||||||
if i < limit:
|
if i < limit:
|
||||||
|
if not replace:
|
||||||
|
os.remove(ifile)
|
||||||
|
ifile = 'out-' + ifile
|
||||||
out = open(ifile)
|
out = open(ifile)
|
||||||
result = out.read()
|
result = out.read()
|
||||||
out.close()
|
out.close()
|
||||||
|
@ -37,7 +40,10 @@ class TestFsub(unittest.TestCase):
|
||||||
cmp_file.close()
|
cmp_file.close()
|
||||||
|
|
||||||
self.assertEqual(result, cmp)
|
self.assertEqual(result, cmp)
|
||||||
os.remove(ifile)
|
try:
|
||||||
|
os.remove(ifile)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_cleaned(self):
|
def test_cleaned(self):
|
||||||
args = ['-f', str(self.samples / 'blacklist')]
|
args = ['-f', str(self.samples / 'blacklist')]
|
||||||
|
|
Loading…
Reference in New Issue