Move parsing code from gui/qt to tomblib.parser

This commit is contained in:
boyska 2011-11-03 00:00:48 +01:00
parent d2120486ca
commit ab706fc639
3 changed files with 26 additions and 7 deletions

View File

@ -1,6 +1,5 @@
import sys,os
import time
import re
from tempfile import NamedTemporaryFile
from PyQt4 import QtCore
@ -8,6 +7,7 @@ from PyQt4 import QtCore
parentdir = sys.path[0].split(os.sep)[:-1]
sys.path.append(os.sep.join(parentdir))
from tomblib.tomb import Tomb
from tomblib.parser import parse_line
class TombCreateThread(QtCore.QThread):
line_received = QtCore.pyqtSignal(QtCore.QString)
@ -58,7 +58,6 @@ class TombOutputThread(QtCore.QThread):
#This could be simplified, and s/search/match, if --no-color supported
#see #59
#TODO: this should be moved to tomblib.parse
err_regex = re.compile(r'\[!\][^ ]* +(.+)$')
match = err_regex.search(line)
if match:
self.error_received.emit(match.group(1))
parsed = parse_line(line)
if parsed and parsed['type'] == 'error':
self.error_received.emit(parsed.content)

20
src/gui/tomblib/parser.py Normal file
View File

@ -0,0 +1,20 @@
'''
Utilities to analyze tomb output
'''
import re
_err_regex = re.compile(r'\[!\][^ ]* +(.+)$')
def parse_line(line):
'''Analyze a single line.
Return None if no standard format is detected, a dict otherwise.
The fields 'type' and 'content' are always in the dict; 'content' may be
empty
'type' can be 'error', 'progress'
'''
match = _err_regex.search(line)
if match:
return { 'type': 'error', 'content': match.group(1) }
return None

View File

@ -28,7 +28,7 @@ class Tomb(object):
Returns None on error, the path on success.
'''
try:
path=subprocess.check_output(['which', 'tomb'])
path = subprocess.check_output(['which', 'tomb'])
except subprocess.CalledProcessError:
return None
return path
@ -47,7 +47,7 @@ class Tomb(object):
return True
@classmethod
def create(cls, tombpath,tombsize,keypath, stdout=None, stderr=None, no_color=True, ignore_swap=False):
def create(cls, tombpath, tombsize,keypath, stdout=None, stderr=None, no_color=True, ignore_swap=False):
'''If keypath is None, it will be created adjacent to the tomb.
This is unsafe, and you should NOT do it.