перенес функциональность связанную с построением регулярных выражений из класса LineParser в класс RegexpBuilder

This commit is contained in:
rzaitov 2013-11-14 14:33:05 +04:00
parent 6953034312
commit e0e6ed61eb
15 changed files with 89 additions and 57 deletions

View File

@ -1,21 +1,37 @@
import os
from commands.CopyCommand import CopyCommand
from parsers.CopyParser.CopyArguments import CopyArguments
from parsers.InstallProfileParser import InstallProfileParser
class InstallProfileCommandBuilder:
def __init__(self):
pass
self.profileStorageDir = '~/Library/MobileDevice/Provisioning Profiles/'
def isInstallProfile(self, line):
assert line is not None
return line.startswith('install profile')
parser = InstallProfileParser()
isValid = parser.isValidLine(line)
return isValid
def getCommandFor(self, line):
assert line is not None
parser = InstallProfileParser()
cpArgs = parser.parseLine(line)
srcPath = parser.parseLine(line)
dstPath = self.getDestinationPath(srcPath)
cpArgs = CopyArguments()
cpArgs.setArguments(srcPath, dstPath)
command = CopyCommand(cpArgs)
return command
def getDestinationPath(self, sourcePath):
profileFileName = os.path.basename(sourcePath)
destination = os.path.join(self.profileStorageDir, profileFileName)
return destination

View File

@ -8,7 +8,6 @@ class TestInstallProfile(unittest.TestCase):
def test_parse(self):
line = "install profile 'Path/To/Profile.mobileprovision'"
copyArgs = self.parser.parseLine(line)
src = self.parser.parseLine(line)
self.assertEqual('Path/To/Profile.mobileprovision', copyArgs.source)
self.assertEqual('~/Library/MobileDevice/Provisioning Profiles/Profile.mobileprovision', copyArgs.target)
self.assertEqual('Path/To/Profile.mobileprovision', src)

View File

@ -1,6 +1,7 @@
import re
from parsers.LineParser import LineParser
from parsers.RegexpBuilder import RegexpBuilder
class CleanBuildParser(LineParser):
@ -8,7 +9,7 @@ class CleanBuildParser(LineParser):
LineParser.__init__(self)
assert commandToken is not None
self.__commandToken = commandToken
self.commandToken = commandToken
def parseLine(self, line):
assert line is not None
@ -16,7 +17,8 @@ class CleanBuildParser(LineParser):
filePathRegexp = r"'(?P<path>[./ a-zA-Z]+\.sln)'"
slnConfigRegexp = r"'(?P<config>[a-zA-Z|]+)'$"
regexpSource = self.startsWith(self.__commandToken) + filePathRegexp + self.keywordToken('for') + slnConfigRegexp
rb = RegexpBuilder()
regexpSource = rb.startsWith(self.commandToken) + filePathRegexp + rb.keywordToken('for') + slnConfigRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
@ -30,5 +32,5 @@ class CleanBuildParser(LineParser):
def isValidLine(self, line):
assert line is not None
isValid = line.startswith(self.__commandToken)
isValid = line.startswith(self.commandToken)
return isValid

View File

@ -2,6 +2,7 @@ import re
from parsers.CopyParser.CopyArguments import CopyArguments
from parsers.LineParser import LineParser
from parsers.RegexpBuilder import RegexpBuilder
class CopyLineParser(LineParser):
@ -15,7 +16,8 @@ class CopyLineParser(LineParser):
srcFileNameRegexp = r"'(?P<src>[^']+)'"
dstFileNameRegexp = r"'(?P<dst>[^']+)'$"
regexpSource = self.startsWith('copy') + srcFileNameRegexp + self.keywordToken('to') + dstFileNameRegexp
rb = RegexpBuilder()
regexpSource = rb.startsWith('copy') + srcFileNameRegexp + rb.keywordToken('to') + dstFileNameRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -1,14 +1,10 @@
import re
import os
from parsers.CopyParser.CopyArguments import CopyArguments
from parsers.LineParser import LineParser
class InstallProfileParser(LineParser):
def __init__(self):
LineParser.__init__(self)
self.__copyArguments = CopyArguments()
self.__profileStorageDir = '~/Library/MobileDevice/Provisioning Profiles/'
def parseLine(self, line):
assert line is not None
@ -22,13 +18,4 @@ class InstallProfileParser(LineParser):
self._guardMatch(match, line, regexpSource)
srcPath = match.group('path')
dstPath = self.getDestinationPath(srcPath)
self.__copyArguments.setArguments(srcPath, dstPath)
return self.__copyArguments
def getDestinationPath(self, sourcePath):
profileFileName = os.path.basename(sourcePath)
destination = os.path.join(self.__profileStorageDir, profileFileName)
return destination
return srcPath

View File

@ -1,35 +1,20 @@
import abc
class LineParser:
__metaclass__ = abc.ABCMeta
def __init__(self):
pass
@abc.abstractmethod
def parseLine(self, line):
assert line is not None
pass
@abc.abstractmethod
def isValidLine(self, line):
assert line is not None
return False
def keywordToken(self, keyword):
assert keyword is not None
return r'\s+' + keyword + r'\s+'
def startsWith(self, keyword):
assert keyword is not None
return r'^' + keyword + r'\s+'
def spaceEndsWith(self, keyword):
assert keyword is not None
return r'\s+' + keyword + '$'
def endsWith(self, keyword):
assert keyword is not None
return keyword + '$'
def than(self, keyword):
assert keyword is not None
return keyword + r'\s+'
def _guardMatch(self, match_object, source, regexpSource = None):
if match_object is None:
msg = 'Recognition exception: "{0}" for "{1}"'.format(source, regexpSource)

View File

@ -1,6 +1,7 @@
import re
from parsers.LineParser import LineParser
from parsers.RegexpBuilder import RegexpBuilder
class MakeDirsParser(LineParser):
@ -10,7 +11,8 @@ class MakeDirsParser(LineParser):
def parseLine(self, line):
pathRegexp = r"'(?P<path>[^']+)'$"
regexpSource = self.startsWith('create dirs') + pathRegexp
rb = RegexpBuilder()
regexpSource = rb.startsWith('create dirs') + pathRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -1,6 +1,7 @@
import re
from parsers.ParserBackup.ParserBackupBase import ParserBackupBase
from parsers.RegexpBuilder import RegexpBuilder
class CreateBackupParser(ParserBackupBase):
@ -10,7 +11,8 @@ class CreateBackupParser(ParserBackupBase):
def getMatchInfo(self, line):
assert line is not None
regexpSource = self.startsWith('create') + self.endsWith('backup')
rb = RegexpBuilder()
regexpSource = rb.startsWith('create') + rb.endsWith('backup')
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -1,6 +1,7 @@
import re
from parsers.ParserBackup.ParserBackupBase import ParserBackupBase
from parsers.RegexpBuilder import RegexpBuilder
class DeleteBackupParser(ParserBackupBase):
@ -10,7 +11,8 @@ class DeleteBackupParser(ParserBackupBase):
def getMatchInfo(self, line):
assert line is not None
regexpSource = self.startsWith('delete') + self.endsWith('backup')
rb = RegexpBuilder()
regexpSource = rb.startsWith('delete') + rb.endsWith('backup')
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -1,6 +1,7 @@
import re
from parsers.ParserBackup.ParserBackupBase import ParserBackupBase
from parsers.RegexpBuilder import RegexpBuilder
class RestoreBackupParser(ParserBackupBase):
@ -10,7 +11,8 @@ class RestoreBackupParser(ParserBackupBase):
def getMatchInfo(self, line):
assert line is not None
regexpSource = self.startsWith('restore') + self.than('from') + self.endsWith('backup')
rb = RegexpBuilder()
regexpSource = rb.startsWith('restore') + rb.than('from') + rb.endsWith('backup')
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -0,0 +1,20 @@
class RegexpBuilder:
def keywordToken(self, keyword):
assert keyword is not None
return r'\s+' + keyword + r'\s+'
def startsWith(self, keyword):
assert keyword is not None
return r'^' + keyword + r'\s+'
def spaceEndsWith(self, keyword):
assert keyword is not None
return r'\s+' + keyword + '$'
def endsWith(self, keyword):
assert keyword is not None
return keyword + '$'
def than(self, keyword):
assert keyword is not None
return keyword + r'\s+'

View File

@ -26,6 +26,11 @@ class SettingsLineParser(LineParser):
return result
def isValidLine(self, line):
assert line is not None
return '=' in line
def splitToPathAndValue(self, line):
# some.path = some_value
result = line.split('=')

View File

@ -1,6 +1,7 @@
import re
from parsers.LineParser import LineParser
from parsers.RegexpBuilder import RegexpBuilder
class ShParser(LineParser):
@ -10,9 +11,10 @@ class ShParser(LineParser):
def parseLine(self, line):
assert line
rb = RegexpBuilder()
cmdTextRegexp = r'(?P<text>.*)'
regexpSource = self.startsWith('sh') + cmdTextRegexp
regexpSource = rb.startsWith('sh') + cmdTextRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)

View File

@ -1,5 +1,7 @@
from parsers.LineParser import LineParser
import re
from parsers.RegexpBuilder import RegexpBuilder
class TestflightParser(LineParser):
def __init__(self):
@ -8,15 +10,17 @@ class TestflightParser(LineParser):
def parseLine(self, line):
assert line is not None
rb = RegexpBuilder()
notesRegexp = r"'(?P<notes>[^']+)'"
apiTokenRegexp = r"'(?P<api_token>[^']+)'"
teamTokenRegexp = r"'(?P<team_token>[^']+)'"
filePathRegexp = r"'(?P<path>[^']+)'"
regexpSource = self.startsWith('publish') + filePathRegexp + self.keywordToken('to') + self.than('testflight') + \
self.than('notes') + self.than('=') + notesRegexp + \
self.keywordToken('api_token') + self.than('=') + apiTokenRegexp + \
self.keywordToken('team_token') + self.than('=') + teamTokenRegexp
regexpSource = rb.startsWith('publish') + filePathRegexp + rb.keywordToken('to') + rb.than('testflight') + \
rb.than('notes') + rb.than('=') + notesRegexp + \
rb.keywordToken('api_token') + rb.than('=') + apiTokenRegexp + \
rb.keywordToken('team_token') + rb.than('=') + teamTokenRegexp
regexp = re.compile(regexpSource, re.UNICODE)

View File

@ -1,5 +1,6 @@
import re
from parsers.LineParser import LineParser
from parsers.RegexpBuilder import RegexpBuilder
class IncludeProcessor(LineParser):
@ -9,7 +10,8 @@ class IncludeProcessor(LineParser):
def getIncludesInfo(self, text):
assert text is not None
regexpSource = '<\s*' + self.than('include') + r"'[^']+'" + '\s*>'
rb = RegexpBuilder()
regexpSource = '<\s*' + rb.than('include') + r"'[^']+'" + '\s*>'
regexp = re.compile(regexpSource, re.UNICODE)
results = regexp.findall(text)