Реализовал класс который подставляет содержимое другого файла
This commit is contained in:
parent
db015acedf
commit
dea4af4339
|
|
@ -0,0 +1,9 @@
|
|||
class FileContentProvider:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def fetchContent(self, path):
|
||||
file = open(path)
|
||||
content = file.read()
|
||||
|
||||
return content
|
||||
|
|
@ -1,6 +1,20 @@
|
|||
class TextInclude:
|
||||
def __init__(self):
|
||||
pass
|
||||
def __init__(self, includeProcessor, contentProvider):
|
||||
assert includeProcessor is not None
|
||||
assert contentProvider is not None
|
||||
|
||||
self.includeProcessor = includeProcessor
|
||||
self.contentProvider = contentProvider
|
||||
|
||||
def processText(self, text):
|
||||
pass
|
||||
assert text is not None
|
||||
includesInfo = self.includeProcessor.getIncludesInfo(text)
|
||||
|
||||
for info in includesInfo:
|
||||
includeStatement = info[0]
|
||||
path = info[1]
|
||||
|
||||
content = self.contentProvider.fetchContent(path)
|
||||
text.replace(includeStatement, content)
|
||||
|
||||
return text
|
||||
|
|
@ -0,0 +1 @@
|
|||
__author__ = 'rzaitov'
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import unittest
|
||||
from utils.IncludeProcessor import IncludeProcessor
|
||||
|
||||
|
||||
class TestIncludeProcessor(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.processor = IncludeProcessor()
|
||||
|
||||
def test_getPathByIncludeStatement(self):
|
||||
statement = "< include 'Some Path'>"
|
||||
path = self.processor.getPathByIncludeStatement(statement)
|
||||
|
||||
self.assertEqual('Some Path', path)
|
||||
|
||||
def test_getInfos(self):
|
||||
text = """
|
||||
< include 'path1'>
|
||||
bla bla
|
||||
<include 'path2'>
|
||||
some text
|
||||
"""
|
||||
includeInfo = self.processor.getIncludesInfo(text)
|
||||
|
||||
self.assertEqual(2, len(includeInfo))
|
||||
|
||||
info0 = includeInfo[0]
|
||||
self.assertEqual("< include 'path1'>", info0[0])
|
||||
self.assertEqual('path1', info0[1])
|
||||
|
||||
info1 = includeInfo[1]
|
||||
self.assertEqual("<include 'path2'>", info1[0])
|
||||
self.assertEqual('path2', info1[1])
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import re
|
||||
from parser.LineParser import LineParser
|
||||
|
||||
|
||||
class IncludeProcessor(LineParser):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def getIncludesInfo(self, text):
|
||||
assert text is not None
|
||||
|
||||
regexpSource = '<\s*' + self.than('include') + r"'[^']+'" + '\s*>'
|
||||
|
||||
regexp = re.compile(regexpSource, re.UNICODE)
|
||||
results = regexp.findall(text)
|
||||
|
||||
includesInfo = []
|
||||
if results:
|
||||
for r in results:
|
||||
path = self.getPathByIncludeStatement(r)
|
||||
includesInfo.append((r, path))
|
||||
|
||||
return includesInfo
|
||||
|
||||
def getPathByIncludeStatement(self, includeStatement):
|
||||
assert includeStatement is not None
|
||||
|
||||
regexpSource = r"'([^']+)'"
|
||||
regexp = re.compile(regexpSource, re.UNICODE)
|
||||
|
||||
results = regexp.findall(includeStatement)
|
||||
path = results[0]
|
||||
|
||||
return path
|
||||
Loading…
Reference in New Issue