Добавил базовый класс для Inside парсеров и вынес туда значительную часть функциональности

This commit is contained in:
rzaitov 2013-11-12 19:36:13 +04:00
parent 60901c462f
commit 31e938809d
5 changed files with 93 additions and 65 deletions

View File

@ -1,21 +1,13 @@
import re
from parsers.LineParser import LineParser
from parsers.InsideParser.InsideParserBase import InsideParserBase
class InsideCsprojSetParser(LineParser):
class InsideCsprojSetParser(InsideParserBase):
def __init__(self, fileExt):
LineParser.__init__(self)
self.__extension = fileExt
InsideParserBase.__init__(self, fileExt)
def parseLine(self, line):
assert line is not None
matchInfo = self.getMatchInfo(line)
match = matchInfo[0]
regexpSource = matchInfo[1]
self._guardMatch(match, line, regexpSource)
match = self.fetchMatchFor(line)
filePath = match.group('file')
key = match.group('key')
@ -27,22 +19,13 @@ class InsideCsprojSetParser(LineParser):
def getMatchInfo(self, line):
assert line is not None
filePathRegexp = r"'(?P<file>[./ a-zA-Z]+\.{0})'".format(self.__extension)
keyRegexp = r'(?P<key>[a-zA-Z]+)'
valueRegexp = r"'(?P<value>[^']+)'"
slnConfigRegexp = r"'(?P<config>[a-zA-Z|]+)'$"
regexpSource = self.startsWith('inside') + filePathRegexp + self.keywordToken('set') + keyRegexp + \
regexpSource = self.startsWith('inside') + self.filePathRegexp + self.keywordToken('set') + keyRegexp + \
self.keywordToken('to') + valueRegexp + self.keywordToken('for') + slnConfigRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
return match, regexpSource
def isValidLine(self, line):
assert line is not None
matchInfo = self.getMatchInfo(line)
match = matchInfo[0]
return match is not None
return match, regexpSource

View File

@ -0,0 +1,38 @@
import abc
from parsers.LineParser import LineParser
class InsideParserBase(object, LineParser):
__metaclass__ = abc.ABCMeta
def __init__(self, fileExt):
LineParser.__init__(self)
assert fileExt is not None
self.fileExt = fileExt
self.filePathRegexp = r"'(?P<file>[./ a-zA-Z]+\.{0})'".format(self.fileExt)
@abc.abstractmethod
def getMatchInfo(self, line):
"Not implemented"
return None, None
def fetchMatchFor(self, text):
assert text is not None
matchInfo = self.getMatchInfo(text)
match = matchInfo[0]
regexpSource = matchInfo[1]
self._guardMatch(match, text, regexpSource)
return match
def isValidLine(self, line):
assert line is not None
matchInfo = self.getMatchInfo(line)
match = matchInfo[0]
return match is not None

View File

@ -1,35 +1,27 @@
import re
from parsers.LineParser import LineParser
from parsers.InsideParser.InsideParserBase import InsideParserBase
class InsideRemoveParser(LineParser):
class InsideRemoveParser(InsideParserBase):
def __init__(self, fileExt):
LineParser.__init__(self)
assert fileExt is not None
self.__extension = fileExt
InsideParserBase.__init__(self, fileExt)
def parseLine(self, line):
assert line is not None
filePathRegexp = r"'(?P<file>[./ a-zA-Z]+\.{0})'".format(self.__extension)
projectNameRegexp = r'(?P<project>[.a-zA-Z]+)'
regexpSource = self.startsWith('inside') + filePathRegexp + self.keywordToken('remove') + projectNameRegexp + self.spaceEndsWith('project')
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
self._guardMatch(match, line, regexpSource)
match = self.fetchMatchFor(line)
filePath = match.group('file')
projectName = match.group('project')
return filePath, projectName
def isValidLine(self, line):
regexpSrc = r"inside\s+'[./ a-zA-Z]+\.{0}'\s+remove".format(self.__extension)
regexp = re.compile(regexpSrc, re.UNICODE)
def getMatchInfo(self, line):
assert line is not None
filePathRegexp = r"'(?P<file>[./ a-zA-Z]+\.{0})'".format(self.fileExt)
projectNameRegexp = r'(?P<project>[.a-zA-Z]+)'
regexpSource = self.startsWith('inside') + filePathRegexp + self.keywordToken('remove') + projectNameRegexp + self.spaceEndsWith('project')
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
return match is not None
return match, regexpSource

View File

@ -0,0 +1,30 @@
import re
from parsers.InsideParser.InsideParserBase import InsideParserBase
class InsideSetArrayParser(InsideParserBase):
def __init__(self, fileExt):
InsideParserBase.__init__(self, fileExt)
def parseLine(self, line):
match = self.fetchMatchFor(line)
filePath = match.group('file')
key = match.group('key')
value = match.group('value')
return filePath, key, value
def getMatchInfo(self, line):
assert line is not None
keyRegexp = r'(?P<key>[a-zA-Z]+)'
valueRegexp = r"'(?P<value>[^']+)'$"
regexpSource = self.startsWith('inside') + self.filePathRegexp + self.keywordToken('set') + keyRegexp + \
self.keywordToken('to') + valueRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
return match, regexpSource

View File

@ -1,20 +1,13 @@
import re
from parsers.LineParser import LineParser
from parsers.InsideParser.InsideParserBase import InsideParserBase
class InsideSetParser(LineParser):
class InsideSetParser(InsideParserBase):
def __init__(self, fileExt):
LineParser.__init__(self)
self.__extension = fileExt
InsideParserBase.__init__(self, fileExt)
def parseLine(self, line):
assert line is not None
matchInfo = self.getMatchInfo(line)
match = matchInfo[0]
regexpSource = matchInfo[1]
self._guardMatch(match, line, regexpSource)
match = self.fetchMatchFor(line)
filePath = match.group('file')
key = match.group('key')
@ -25,21 +18,13 @@ class InsideSetParser(LineParser):
def getMatchInfo(self, line):
assert line is not None
filePathRegexp = r"'(?P<file>[./ a-zA-Z]+\.{0})'".format(self.__extension)
keyRegexp = r'(?P<key>[a-zA-Z]+)'
valueRegexp = r"'(?P<value>[^']+)'$"
regexpSource = self.startsWith('inside') + filePathRegexp + self.keywordToken('set') + keyRegexp + \
regexpSource = self.startsWith('inside') + self.filePathRegexp + self.keywordToken('set') + keyRegexp + \
self.keywordToken('to') + valueRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
return match, regexpSource
def isValidLine(self, line):
assert line is not None
matchInfo = self.getMatchInfo(line)
match = matchInfo[0]
return match is not None
return match, regexpSource