Skip to content

Commit

Permalink
fix #123, ScribusGenerator crash when document requires more data row…
Browse files Browse the repository at this point in the history
…s than CSV provides
  • Loading branch information
berteh committed Feb 5, 2019
1 parent ac44f83 commit d473f67
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ScribusGeneratorBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def run(self):
rootStr = ET.tostring(root, encoding='utf8', method='xml')
# number of data records appearing in source document
recordsInDocument = 1 + string.count(rootStr, CONST.NEXT_RECORD)
logging.info("source document consumes %s data record(s)." %
recordsInDocument)
logging.info("source document consumes %s data record(s) from %s." %
(recordsInDocument, dataC))
dataBuffer = []
for row in csvData:
if(index == 0): # first line is the Header-Row of the CSV-File
Expand All @@ -186,15 +186,15 @@ def run(self):
else: # index > 0, row is one data entry
# accumulate row in buffer
dataBuffer.append(row)

# buffered data for all document records OR reached last data record
if (index % recordsInDocument == 0) or index == dataC:
# subsitute
outContent = self.substituteData(varNamesForReplacingVariables, self.handleAmpersand(dataBuffer),
ET.tostring(templateElt, method='xml').split('\n'), keepTabsLF=CONST.KEEP_TAB_LINEBREAK)
if (self.__dataObject.getSingleOutput()):
# first substitution, update DOCUMENT properties
if (index == recordsInDocument):
if (index == min(recordsInDocument,dataC)):
logging.debug(
"generating reference content from dataBuffer #1")
outputElt = ET.fromstring(outContent)
Expand Down Expand Up @@ -484,12 +484,12 @@ def substituteData(self, varNames, rows, lines, clean=CONST.CLEAN_UNUSED_EMPTY_V

# done in string instead of XML for lack of efficient attribute-value-based substring-search in ElementTree
for idx, line in enumerate(lines):
# logging.debug("replacing vars in (out of %s): %s"%(len(line), line[:25]))
# logging.debug("replacing vars in (out of %s): %s"%(len(line), line[:30]))

# skip un-needed computations and preserve colors declarations
if (re.search('%VAR_|'+CONST.NEXT_RECORD, line) == None) or (re.search('\s*<COLOR\s+', line) != None):
result = result + line
# logging.debug(" keeping intact %s"%line[:25])
# logging.debug(" keeping intact %s"%line[:30])
continue

# detect NEXT_RECORD
Expand All @@ -505,9 +505,9 @@ def substituteData(self, varNames, rows, lines, clean=CONST.CLEAN_UNUSED_EMPTY_V
logging.debug("next record reached last data entry")

# replace with data
logging.debug("replacing VARS_* in %s" % line[:25].strip())
logging.debug("replacing VARS_* in %s" % line[:30].strip())
line = self.multiple_replace(line, replacements)
logging.debug("replaced in line: %s" % line)
#logging.debug("replaced in line: %s" % line)

# remove (& trim) any (unused) %VAR_\w*% like string.
if (clean):
Expand Down

0 comments on commit d473f67

Please sign in to comment.