diff --git a/examples/smi2vtt.py b/examples/smi2vtt.py index 2196a58..9b9fd5a 100644 --- a/examples/smi2vtt.py +++ b/examples/smi2vtt.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # from sys import argv, stderr -from pysami import Converter, ConversionError +from pysami import SmiFile, ConversionError def throw(code): error = ConversionError(code) @@ -23,12 +23,13 @@ def throw(code): smi_filepath = argv[1] try: - converter = Converter(smi_filepath) - vtt = converter.convert('vtt') + smi = SmiFile(smi_filepath) + smi.parse() + vtt = smi.convert('vtt', 'KRCC') except ConversionError as e: throw(e.code) except: - throw(-5) + throw(-6) try: vtt_file = open(vtt_filepath, 'w') @@ -40,4 +41,4 @@ def throw(code): print('성공적으로 변환했습니다.') print(' WebVTT 파일 : '+vtt_filepath) -exit(1) \ No newline at end of file +exit(1) diff --git a/src/__init__.py b/src/__init__.py index 223be97..dca155e 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,14 +1,5 @@ # -*- coding: utf-8 -*- # -# 변환 작업 클래스 -class Converter: - def __init__(self, input_file, encoding=None, verbose=False): - self.smi_file = SmiFile(input_file, encoding) - self.smi_file.parse(verbose) - - def convert(self, output_type, lang='KRCC'): - return self.smi_file.convert(output_type, lang) - # SAMI 파일 파싱 클래스 from os.path import isfile from pysami.error import ConversionError @@ -19,7 +10,7 @@ def convert(self, output_type, lang='KRCC'): from pysami.subtitle import Subtitle class SmiFile: - def __init__(self, input_file, encoding): + def __init__(self, input_file, encoding=None): self.data = None if not isfile(input_file): @@ -47,7 +38,7 @@ def __init__(self, input_file, encoding): file.close() - def parse(self, verbose): + def parse(self, verbose=False): search = lambda string, pattern: re.search(pattern, string, flags=re.I) def split_content(string, tag): @@ -82,9 +73,9 @@ def parse_p(item): except: raise ConversionError(-3) - def convert(self, target, lang): + def convert(self, target, lang='ENCC'): if self.data == None: - self.parse() + raise ConversionError(-5) result = '' diff --git a/src/error.py b/src/error.py index 9ab8484..84b4736 100644 --- a/src/error.py +++ b/src/error.py @@ -1,24 +1,19 @@ # -*- coding: utf-8 -*- # -messages = [ - '원본 자막 파일이 존재하지 않습니다.', - - '원본 자막 파일의 인코딩을 감지할 수 없습니다.', - - """원본 자막 파일은 올바른 SMI 파일이 아닙니다. - (verbose 옵션은 오류의 발생 위치를 확인하는 데 도움을 줄 수 있습니다.)""", - - """지정한 파일 형식으로는 변환할 수 없습니다. - (지원 목록 : vtt)""", - - '알 수 없는 오류가 발생했습니다.' -] - # 변환 오류 클래스 class ConversionError(Exception): + messages = ( + 'Cannot access to the input file.', + 'Cannot find correct encoding for the input file.', + 'Cannot parse the input file. It seems not to be a valid SAMI file.\n(Verbose option may show you the position the error occured in)', + 'Cannot convert into the specified type. (Suppored types : vtt)', + 'Cannot convert the input file before parsing it.', + 'Unknown error occured.' + ) + def __init__(self, code): self.code = code - self.msg = messages[-(code+1)] + self.msg = self.messages[-(code+1)] def __str__(self): - return self.msg+' ('+str(self.code)+')' \ No newline at end of file + return self.msg+' ('+str(self.code)+')'