Skip to content

Commit

Permalink
215f862 커밋에서 실수로 빠진 파일 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
조유성 authored and 조유성 committed May 11, 2014
1 parent 215f862 commit b8164e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
11 changes: 6 additions & 5 deletions examples/smi2vtt.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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')
Expand All @@ -40,4 +41,4 @@ def throw(code):

print('성공적으로 변환했습니다.')
print(' WebVTT 파일 : '+vtt_filepath)
exit(1)
exit(1)
17 changes: 4 additions & 13 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = ''

Expand Down
27 changes: 11 additions & 16 deletions src/error.py
Original file line number Diff line number Diff line change
@@ -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)+')'
return self.msg+' ('+str(self.code)+')'

0 comments on commit b8164e8

Please sign in to comment.