Improve error codes

This commit is contained in:
Augusto Gunsch 2021-11-19 19:23:03 -03:00
parent ef868d83bf
commit 7e0df73933
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 26 additions and 27 deletions

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = fsub name = fsub
version = 1.0.1 version = 1.0.2
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

View File

@ -44,22 +44,22 @@ class TimeStamp:
h, m, s, ms = map(int, m.groups()) h, m, s, ms = map(int, m.groups())
self.time = h * 3600000 + m * 60000 + s * 1000 + ms self.time = h * 3600000 + m * 60000 + s * 1000 + ms
def getmilliseconds(self): def get_milliseconds(self):
return self.time % 1000 return self.time % 1000
def getseconds(self): def get_seconds(self):
return int((self.time % 60000) / 1000) return int((self.time % 60000) / 1000)
def getminutes(self): def get_minutes(self):
return int((self.time / 60000) % 60) return int((self.time / 60000) % 60)
def gethours(self): def get_hours(self):
return int(self.time / 3600000) return int(self.time / 3600000)
millisecods = property(getmilliseconds) millisecods = property(get_milliseconds)
seconds = property(getseconds) seconds = property(get_seconds)
minutes = property(getminutes) minutes = property(get_minutes)
hours = property(gethours) hours = property(get_hours)
def __int__(self): def __int__(self):
return self.time return self.time
@ -104,7 +104,7 @@ class SectionMarker:
try: try:
self.marker = int(arg) self.marker = int(arg)
except Exception: except Exception:
panic('Invalid section marker argument', 1) panic('Invalid section marker argument', 65)
def include_after(self, other): def include_after(self, other):
if type(self.marker) is TimeStamp: if type(self.marker) is TimeStamp:
@ -132,7 +132,7 @@ class Subtitle:
assert self.number > 0 assert self.number > 0
except Exception: except Exception:
panic('Invalid line number detected ({}:{})' panic('Invalid line number detected ({}:{})'
.format(file_name, line_number), 1) .format(file_name, line_number), 65)
line_number += 1 line_number += 1
@ -140,18 +140,18 @@ class Subtitle:
time_span = lines.pop(0).split(' --> ') time_span = lines.pop(0).split(' --> ')
except Exception: except Exception:
panic('Invalid time span format detected ({}:{})' panic('Invalid time span format detected ({}:{})'
.format(file_name, line_number), 1) .format(file_name, line_number), 65)
try: try:
self.time_start = TimeStamp(time_span[0]) self.time_start = TimeStamp(time_span[0])
self.time_end = TimeStamp(time_span[1]) self.time_end = TimeStamp(time_span[1])
except Exception: except Exception:
panic('Invalid time stamp detected ({}:{})' panic('Invalid time stamp detected ({}:{})'
.format(file_name, line_number), 1) .format(file_name, line_number), 65)
if self.time_start >= self.time_end: if self.time_start >= self.time_end:
panic('End time must be greater than start time ({}:{})' panic('End time must be greater than start time ({}:{})'
.format(file_name, line_number), 1) .format(file_name, line_number), 65)
self.content = lines self.content = lines
@ -204,7 +204,7 @@ class ConfigFile:
file = self.file_path.open(mode='r') file = self.file_path.open(mode='r')
except PermissionError: except PermissionError:
panic('Can\'t access file {}: Permission denied' panic('Can\'t access file {}: Permission denied'
.format(self.file_path), 1) .format(self.file_path), 77)
else: else:
self.file_path = Path(file.name) self.file_path = Path(file.name)
@ -218,7 +218,7 @@ class SubripFile:
def read_file(file): def read_file(file):
# Check extension # Check extension
if file.name[-4:] != '.srt': if file.name[-4:] != '.srt':
panic('File {} is not a SubRip file'.format(file.name), 1) panic('File {} is not a SubRip file'.format(file.name), 64)
# Read the input file # Read the input file
contents = file.read() contents = file.read()
@ -227,7 +227,7 @@ class SubripFile:
# Decode the file contents # Decode the file contents
encoding = chardet.detect(contents)['encoding'] encoding = chardet.detect(contents)['encoding']
if encoding is None: if encoding is None:
panic('Corrupt or empty file ({})'.format(file.name), 1) panic('Corrupt or empty file ({})'.format(file.name), 66)
return contents.decode(encoding) return contents.decode(encoding)
# This method parses the file # This method parses the file
@ -456,7 +456,7 @@ def parse_args(args):
# Flags that require section # Flags that require section
if args.cut_out: if args.cut_out:
if not args.begin and not args.end: if not args.begin and not args.end:
panic('You must specify a section to work with', 1) panic('You must specify a section to work with', 64)
# Make sure --clean is the default # Make sure --clean is the default
if not any((args.shift, args.no_html, args.join, args.cut_out)): if not any((args.shift, args.no_html, args.join, args.cut_out)):
@ -464,7 +464,7 @@ def parse_args(args):
# Validate options # Validate options
if not args.clean and args.config: if not args.clean and args.config:
panic('-f requires -c', 1) panic('-f requires -c', 64)
if args.begin: if args.begin:
args.begin = SectionMarker(args.begin) args.begin = SectionMarker(args.begin)
@ -494,14 +494,13 @@ def run(args):
if args.join: if args.join:
first = parsed_files.pop(0) first = parsed_files.pop(0)
while True:
try: for file in parsed_files:
file = parsed_files.pop(0) first += file
first += file if args.replace:
if args.replace: file.delete()
file.delete()
except IndexError: parsed_files.clear()
break
parsed_files.append(first) parsed_files.append(first)
for file in parsed_files: for file in parsed_files: