2018-01-07 01:08:35 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#
|
2019-09-03 16:58:50 +00:00
|
|
|
|
# CanvasImportStore.py -- XXX
|
2018-01-07 01:08:35 +00:00
|
|
|
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2018-11-26 09:35:48 +00:00
|
|
|
|
# This project is licensed under the terms of the MIT licence.
|
2018-01-07 01:08:35 +00:00
|
|
|
|
#
|
|
|
|
|
|
2019-09-03 16:58:50 +00:00
|
|
|
|
class CanvasImportStore():
|
2018-01-07 01:08:35 +00:00
|
|
|
|
"""XXX"""
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# _CellState(): Cell state
|
|
|
|
|
class _CellState():
|
|
|
|
|
CS_NONE = 0x00
|
|
|
|
|
CS_BOLD = 0x01
|
|
|
|
|
CS_ITALIC = 0x02
|
|
|
|
|
CS_UNDERLINE = 0x04
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# _ParseState(): Parsing loop state
|
|
|
|
|
class _ParseState():
|
|
|
|
|
PS_CHAR = 1
|
|
|
|
|
PS_COLOUR_DIGIT0 = 2
|
|
|
|
|
PS_COLOUR_DIGIT1 = 3
|
|
|
|
|
|
2018-01-07 22:49:57 +00:00
|
|
|
|
# {{{ _flipCellStateBit(self, cellState, bit): XXX
|
|
|
|
|
def _flipCellStateBit(self, cellState, bit):
|
2019-09-04 13:52:54 +00:00
|
|
|
|
return cellState & ~bit if cellState & bit else cellState | bit
|
2018-01-07 22:49:57 +00:00
|
|
|
|
# }}}
|
2018-01-07 01:08:35 +00:00
|
|
|
|
# {{{ _parseCharAsColourSpec(self, colourSpec, curColours): XXX
|
|
|
|
|
def _parseCharAsColourSpec(self, colourSpec, curColours):
|
|
|
|
|
if len(colourSpec) > 0:
|
|
|
|
|
colourSpec = colourSpec.split(",")
|
2019-09-04 13:52:54 +00:00
|
|
|
|
if len(colourSpec) == 2 \
|
2018-01-20 08:17:52 +00:00
|
|
|
|
and len(colourSpec[1]) > 0:
|
2019-09-04 13:52:54 +00:00
|
|
|
|
return (int(colourSpec[0] or curColours[0]), int(colourSpec[1]))
|
|
|
|
|
elif len(colourSpec) == 1 \
|
2018-01-20 08:17:52 +00:00
|
|
|
|
or len(colourSpec[1]) == 0:
|
2018-01-11 22:13:46 +00:00
|
|
|
|
return (int(colourSpec[0]), curColours[1])
|
2018-01-07 01:08:35 +00:00
|
|
|
|
else:
|
|
|
|
|
return (15, 1)
|
|
|
|
|
# }}}
|
2018-01-07 03:19:38 +00:00
|
|
|
|
|
|
|
|
|
# {{{ importIntoPanel(self): XXX
|
|
|
|
|
def importIntoPanel(self):
|
2018-01-07 15:43:41 +00:00
|
|
|
|
self.parentCanvas.onStoreUpdate(self.inSize, self.outMap)
|
2018-01-07 03:19:38 +00:00
|
|
|
|
# }}}
|
2019-09-04 13:52:54 +00:00
|
|
|
|
# {{{ importNew(self, newCanvasSize=None): XXX
|
|
|
|
|
def importNew(self, newCanvasSize=None):
|
|
|
|
|
newMap = [[[1, 1, 0, " "] \
|
|
|
|
|
for x in range(newCanvasSize[0])] \
|
|
|
|
|
for y in range(newCanvasSize[1])]
|
|
|
|
|
self.parentCanvas.onStoreUpdate(newCanvasSize, newMap)
|
|
|
|
|
# }}}
|
2018-01-07 03:19:38 +00:00
|
|
|
|
# {{{ importTextFile(self, pathName): XXX
|
|
|
|
|
def importTextFile(self, pathName):
|
2019-09-04 10:50:21 +00:00
|
|
|
|
return self.importTextFileBuffer(open(pathName, "r", encoding="utf-8-sig"))
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ importTextFileBuffer(self, inFile): XXX
|
|
|
|
|
def importTextFileBuffer(self, inFile):
|
|
|
|
|
self.inSize, self.outMap = None, None
|
|
|
|
|
inCurColourSpec, inCurRow, inMaxCols, inSize, outMap = "", -1, 0, [0, 0], []
|
|
|
|
|
inLine = inFile.readline()
|
2018-01-07 01:08:35 +00:00
|
|
|
|
while inLine:
|
|
|
|
|
inCellState = self._CellState.CS_NONE
|
|
|
|
|
inParseState = self._ParseState.PS_CHAR
|
|
|
|
|
inCurCol = 0; inMaxCol = len(inLine);
|
|
|
|
|
inCurColourDigits = 0; inCurColours = (15, 1); inCurColourSpec = "";
|
|
|
|
|
inCurRow += 1; outMap.append([]); inRowCols = 0; inSize[1] += 1;
|
|
|
|
|
while inCurCol < inMaxCol:
|
|
|
|
|
inChar = inLine[inCurCol]
|
|
|
|
|
if inChar in set("\r\n"): \
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
elif inParseState == self._ParseState.PS_CHAR:
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
if inChar == "":
|
|
|
|
|
inCellState = self._flipCellStateBit( \
|
|
|
|
|
inCellState, self._CellState.CS_BOLD)
|
|
|
|
|
elif inChar == "":
|
|
|
|
|
inParseState = self._ParseState.PS_COLOUR_DIGIT0
|
|
|
|
|
elif inChar == "":
|
|
|
|
|
inCellState = self._flipCellStateBit( \
|
|
|
|
|
inCellState, self._CellState.CS_ITALIC)
|
|
|
|
|
elif inChar == "":
|
|
|
|
|
inCellState |= self._CellState.CS_NONE
|
|
|
|
|
inCurColours = (15, 1)
|
|
|
|
|
elif inChar == "":
|
|
|
|
|
inCurColours = (inCurColours[1], inCurColours[0])
|
|
|
|
|
elif inChar == "":
|
|
|
|
|
inCellState = self._flipCellStateBit( \
|
|
|
|
|
inCellState, self._CellState.CS_UNDERLINE)
|
|
|
|
|
else:
|
|
|
|
|
inRowCols += 1
|
2018-01-30 10:17:35 +00:00
|
|
|
|
outMap[inCurRow].append([*inCurColours, inCellState, inChar])
|
2018-01-07 01:08:35 +00:00
|
|
|
|
elif inParseState == self._ParseState.PS_COLOUR_DIGIT0 \
|
|
|
|
|
or inParseState == self._ParseState.PS_COLOUR_DIGIT1:
|
|
|
|
|
if inChar == "," \
|
|
|
|
|
and inParseState == self._ParseState.PS_COLOUR_DIGIT0:
|
2018-01-20 08:17:52 +00:00
|
|
|
|
if (inCurCol + 1) < inMaxCol \
|
|
|
|
|
and not inLine[inCurCol + 1] in set("0123456789"):
|
2019-09-04 10:50:21 +00:00
|
|
|
|
inCurColours = self._parseCharAsColourSpec( \
|
2018-01-20 08:17:52 +00:00
|
|
|
|
inCurColourSpec, inCurColours)
|
|
|
|
|
inCurColourDigits = 0; inCurColourSpec = "";
|
|
|
|
|
inParseState = self._ParseState.PS_CHAR
|
|
|
|
|
else:
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
inCurColourDigits = 0; inCurColourSpec += inChar;
|
|
|
|
|
inParseState = self._ParseState.PS_COLOUR_DIGIT1
|
2018-01-07 01:08:35 +00:00
|
|
|
|
elif inChar in set("0123456789") \
|
|
|
|
|
and inCurColourDigits == 0:
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
inCurColourDigits += 1; inCurColourSpec += inChar;
|
|
|
|
|
elif inChar in set("0123456789") \
|
|
|
|
|
and inCurColourDigits == 1 \
|
|
|
|
|
and inCurColourSpec[-1] == "0":
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
inCurColourDigits += 1; inCurColourSpec += inChar;
|
|
|
|
|
elif inChar in set("012345") \
|
|
|
|
|
and inCurColourDigits == 1 \
|
|
|
|
|
and inCurColourSpec[-1] == "1":
|
|
|
|
|
inCurCol += 1
|
|
|
|
|
inCurColourDigits += 1; inCurColourSpec += inChar;
|
|
|
|
|
else:
|
|
|
|
|
inCurColours = self._parseCharAsColourSpec( \
|
|
|
|
|
inCurColourSpec, inCurColours)
|
|
|
|
|
inCurColourDigits = 0; inCurColourSpec = "";
|
|
|
|
|
inParseState = self._ParseState.PS_CHAR
|
|
|
|
|
inMaxCols = max(inMaxCols, inRowCols)
|
2019-09-04 10:50:21 +00:00
|
|
|
|
inLine = inFile.readline()
|
|
|
|
|
inSize[0] = inMaxCols; self.inSize, self.outMap = inSize, outMap;
|
|
|
|
|
inFile.close()
|
2018-01-07 01:08:35 +00:00
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
#
|
2018-01-07 03:19:38 +00:00
|
|
|
|
# __init__(self, inFile=None, parentCanvas=None): initialisation method
|
|
|
|
|
def __init__(self, inFile=None, parentCanvas=None):
|
2019-09-04 10:50:21 +00:00
|
|
|
|
self.inSize, self.outMap, self.parentCanvas = None, None, parentCanvas
|
2018-01-07 03:19:38 +00:00
|
|
|
|
if inFile != None:
|
|
|
|
|
self.importTextFile(inFile)
|
2018-01-07 01:08:35 +00:00
|
|
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|