From f3b290881518aa9bbf713680fd93f9c514822e2c Mon Sep 17 00:00:00 2001 From: Rustam Zaitov Date: Mon, 28 Oct 2013 00:59:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20CopyLineParser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnitTests/CopyParser/test_copyParser.py | 23 +++++++++++ scripts/parser/CopyParser/CopyArguments.py | 12 +++--- scripts/parser/CopyParser/CopyLineParser.py | 39 +++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 scripts/UnitTests/CopyParser/test_copyParser.py create mode 100644 scripts/parser/CopyParser/CopyLineParser.py diff --git a/scripts/UnitTests/CopyParser/test_copyParser.py b/scripts/UnitTests/CopyParser/test_copyParser.py new file mode 100644 index 0000000..60e54ca --- /dev/null +++ b/scripts/UnitTests/CopyParser/test_copyParser.py @@ -0,0 +1,23 @@ +import unittest +from parser.CopyParser.CopyLineParser import CopyLineParser + + +class TestCopyParser(unittest.TestCase): + def setUp(self): + self.__parser = CopyLineParser() + + def test_validSrcDst(self): + cpArgs = self.__parser.parseLine("copy 'File1' to 'File2'") + self.assertEqual('File1', cpArgs.source) + self.assertEqual('File2', cpArgs.target) + + def test_withFolder(self): + cpArgs = self.__parser.parseLine("copy 'dir1/dir2/src.txt' to 'dir3/dir4/dst.txt'") + self.assertEqual('dir1/dir2/src.txt', cpArgs.source) + self.assertEqual('dir3/dir4/dst.txt', cpArgs.target) + + def test_withWiteSpace(self): + cpArgs = self.__parser.parseLine("copy 'dir1 with ws/dir2 with ws/s r c.txt' to 'dir3 with ws/dir4/d s t.txt'") + self.assertEqual('dir1 with ws/dir2 with ws/s r c.txt', cpArgs.source) + self.assertEqual('dir3 with ws/dir4/d s t.txt', cpArgs.target) + diff --git a/scripts/parser/CopyParser/CopyArguments.py b/scripts/parser/CopyParser/CopyArguments.py index f33d049..09f968d 100644 --- a/scripts/parser/CopyParser/CopyArguments.py +++ b/scripts/parser/CopyParser/CopyArguments.py @@ -3,15 +3,15 @@ __author__ = 'rzaitov' class CopyArguments(): def __init__(self): - self.__source = None - self.__target = None + self.source = None + self.target = None def setArguments(self, source, target): - self.__source = source - self.__target = target + self.source = source + self.target = target def isValid(self): - result = self.__source is not None - result &= self.__target is not None + result = self.source is not None + result &= self.target is not None return result \ No newline at end of file diff --git a/scripts/parser/CopyParser/CopyLineParser.py b/scripts/parser/CopyParser/CopyLineParser.py new file mode 100644 index 0000000..6e9c81e --- /dev/null +++ b/scripts/parser/CopyParser/CopyLineParser.py @@ -0,0 +1,39 @@ +from parser.CopyParser.CopyArguments import CopyArguments +from parser.LineParser import LineParser +import re + +class CopyLineParser(LineParser): + def __init__(self): + self.__copyArguments = CopyArguments() + + def parseLine(self, line): + assert line is not None + + srcFileNameRegexp = r"'(?P[^']+)'" + dstFileNameRegexp = r"'(?P[^']+)'$" + + regexpSource = self.startsWithKeywordToken('copy') + srcFileNameRegexp + self.keywordToken('to') + dstFileNameRegexp + regexp = re.compile(regexpSource, re.UNICODE) + + match = regexp.match(line) + self._guardMatch(match, line, regexpSource) + + src = match.group('src') + dst = match.group('dst') + + self.__copyArguments.setArguments(src, dst) + return self.__copyArguments + + def keywordToken(self, keyword): + assert keyword is not None + return r'\s+' + keyword + r'\s+' + + def startsWithKeywordToken(self, keyword): + assert keyword is not None + return r'^' + keyword + r'\s+' + + def isValidLine(self, line): + assert line is not None + + isValid = line.startswith("copy"); + return isValid \ No newline at end of file