mirror of
https://github.com/lalbornoz/roar.git
synced 2024-11-22 15:26:37 +00:00
MiRCARTCanvas{Export,Import}Store.py: split from MiRCARTCanvasStore.py.
IrcMiRCARTBot.py, MiRCART{Canvas,Frame,ToPngFile}.py: updated.
This commit is contained in:
parent
cd5ea1fab5
commit
398ee3af3d
@ -27,7 +27,7 @@ import os, sys, time
|
||||
import json
|
||||
import IrcClient
|
||||
import requests, urllib.request
|
||||
from MiRCARTCanvasStore import MiRCARTCanvasStore
|
||||
from MiRCARTCanvasImportStore import MiRCARTCanvasImportStore
|
||||
from MiRCARTToPngFile import MiRCARTToPngFile
|
||||
|
||||
class IrcMiRCARTBot(IrcClient.IrcClient):
|
||||
@ -141,7 +141,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
|
||||
self.queue("PRIVMSG", message[2], "4/!\\ Unknown URL type specified!")
|
||||
return
|
||||
|
||||
canvasStore = MiRCARTCanvasStore(inFile=asciiTmpFilePath)
|
||||
canvasStore = MiRCARTCanvasImportStore(inFile=asciiTmpFilePath)
|
||||
MiRCARTToPngFile(canvasStore.outMap, "DejaVuSansMono.ttf", 11).export(imgTmpFilePath)
|
||||
imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
|
||||
if imgurResponse[0] == 200:
|
||||
|
@ -24,7 +24,8 @@
|
||||
|
||||
from MiRCARTCanvasBackend import MiRCARTCanvasBackend
|
||||
from MiRCARTCanvasJournal import MiRCARTCanvasJournal
|
||||
from MiRCARTCanvasStore import MiRCARTCanvasStore, haveMiRCARTToPngFile, haveUrllib
|
||||
from MiRCARTCanvasExportStore import MiRCARTCanvasExportStore, haveMiRCARTToPngFile, haveUrllib
|
||||
from MiRCARTCanvasImportStore import MiRCARTCanvasImportStore
|
||||
from MiRCARTColours import MiRCARTColours
|
||||
import wx
|
||||
|
||||
@ -33,7 +34,8 @@ class MiRCARTCanvas(wx.Panel):
|
||||
parentFrame = None
|
||||
canvasMap = canvasPos = canvasSize = None
|
||||
brushColours = brushPos = brushSize = None
|
||||
canvasBackend = canvasCurTool = canvasJournal = canvasStore = None
|
||||
canvasBackend = canvasCurTool = canvasJournal = None
|
||||
canvasExportStore = canvasImportStore = None
|
||||
|
||||
# {{{ _commitPatch(self, patch): XXX
|
||||
def _commitPatch(self, patch):
|
||||
@ -165,7 +167,8 @@ class MiRCARTCanvas(wx.Panel):
|
||||
self.canvasBackend = MiRCARTCanvasBackend(canvasSize, cellSize)
|
||||
self.canvasCurTool = None
|
||||
self.canvasJournal = MiRCARTCanvasJournal()
|
||||
self.canvasStore = MiRCARTCanvasStore(parentCanvas=self)
|
||||
self.canvasExportStore = MiRCARTCanvasExportStore(parentCanvas=self)
|
||||
self.canvasImportStore = MiRCARTCanvasImportStore(parentCanvas=self)
|
||||
|
||||
# Bind event handlers
|
||||
self.Bind(wx.EVT_CLOSE, self.onPanelClose)
|
||||
|
128
MiRCARTCanvasExportStore.py
Normal file
128
MiRCARTCanvasExportStore.py
Normal file
@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# MiRCARTCanvasExportStore.py -- XXX
|
||||
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
#
|
||||
|
||||
import io, os, tempfile
|
||||
|
||||
try:
|
||||
from MiRCARTToPngFile import MiRCARTToPngFile
|
||||
haveMiRCARTToPngFile = True
|
||||
except ImportError:
|
||||
haveMiRCARTToPngFile = False
|
||||
|
||||
try:
|
||||
import base64, json, requests, urllib.request
|
||||
haveUrllib = True
|
||||
except ImportError:
|
||||
haveUrllib = False
|
||||
|
||||
class MiRCARTCanvasExportStore():
|
||||
"""XXX"""
|
||||
parentCanvas = None
|
||||
|
||||
# {{{ _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName): upload single PNG file to Imgur
|
||||
def _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName):
|
||||
requestImageData = open(pathName, "rb").read()
|
||||
requestData = { \
|
||||
"image": base64.b64encode(requestImageData), \
|
||||
"key": apiKey, \
|
||||
"name": imgName, \
|
||||
"title": imgTitle, \
|
||||
"type": "base64"}
|
||||
requestHeaders = {"Authorization": "Client-ID " + apiKey}
|
||||
responseHttp = requests.post( \
|
||||
"https://api.imgur.com/3/upload.json", \
|
||||
data=requestData, headers=requestHeaders)
|
||||
responseDict = json.loads(responseHttp.text)
|
||||
if responseHttp.status_code == 200:
|
||||
return [200, responseDict.get("data").get("link")]
|
||||
else:
|
||||
return [responseHttp.status_code, ""]
|
||||
# }}}
|
||||
|
||||
# {{{ exportBitmapToPngFile(self, canvasBitmap, outPathName, outType): XXX
|
||||
def exportBitmapToPngFile(self, canvasBitmap, outPathName, outType):
|
||||
return canvasBitmap.ConvertToImage().SaveFile(outPathName, outType)
|
||||
# }}}
|
||||
# {{{ exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType): XXX
|
||||
def exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType):
|
||||
tmpPathName = tempfile.mkstemp()
|
||||
os.close(tmpPathName[0])
|
||||
canvasBitmap.ConvertToImage().SaveFile(tmpPathName[1], imgType)
|
||||
imgurResult = self._exportFileToImgur(apiKey, imgName, imgTitle, tmpPathName[1])
|
||||
os.remove(tmpPathName[1])
|
||||
return imgurResult
|
||||
# }}}
|
||||
# {{{ exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0): XXX
|
||||
def exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0):
|
||||
if haveUrllib:
|
||||
outFile = io.StringIO()
|
||||
self.exportTextFile(canvasMap, canvasSize, outFile)
|
||||
requestData = { \
|
||||
"api_dev_key": apiDevKey, \
|
||||
"api_option": "paste", \
|
||||
"api_paste_code": outFile.getvalue().encode(), \
|
||||
"api_paste_name": pasteName, \
|
||||
"api_paste_private": pastePrivate}
|
||||
responseHttp = requests.post("https://pastebin.com/api/api_post.php", \
|
||||
data=requestData)
|
||||
if responseHttp.status_code == 200:
|
||||
if responseHttp.text.startswith("http"):
|
||||
return (True, responseHttp.text)
|
||||
else:
|
||||
return (False, responseHttp.text)
|
||||
else:
|
||||
return (False, str(responseHttp.status_code))
|
||||
else:
|
||||
return (False, "missing requests and/or urllib3 module(s)")
|
||||
# }}}
|
||||
# {{{ exportPngFile(self, canvasMap, outPathName): XXX
|
||||
def exportPngFile(self, canvasMap, outPathName):
|
||||
if haveMiRCARTToPngFile:
|
||||
MiRCARTToPngFile(canvasMap).export(outPathName)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
# }}}
|
||||
# {{{ exportTextFile(self, canvasMap, canvasSize, outFile): XXX
|
||||
def exportTextFile(self, canvasMap, canvasSize, outFile):
|
||||
for canvasRow in range(canvasSize[1]):
|
||||
canvasLastColours = []
|
||||
for canvasCol in range(canvasSize[0]):
|
||||
canvasColColours = canvasMap[canvasRow][canvasCol][0]
|
||||
canvasColText = canvasMap[canvasRow][canvasCol][2]
|
||||
if canvasColColours != canvasLastColours:
|
||||
canvasLastColours = canvasColColours
|
||||
outFile.write("\x03" + \
|
||||
str(canvasColColours[0]) + \
|
||||
"," + str(canvasColColours[1]))
|
||||
outFile.write(canvasColText)
|
||||
outFile.write("\n")
|
||||
# }}}
|
||||
|
||||
#
|
||||
# __init__(self, parentCanvas): initialisation method
|
||||
def __init__(self, parentCanvas):
|
||||
self.parentCanvas = parentCanvas
|
||||
|
||||
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# MiRCARTCanvasStore.py -- XXX
|
||||
# MiRCARTCanvasImportStore.py -- XXX
|
||||
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@ -22,27 +22,7 @@
|
||||
# SOFTWARE.
|
||||
#
|
||||
|
||||
import io, os, tempfile
|
||||
|
||||
try:
|
||||
import wx
|
||||
haveWx = True
|
||||
except ImportError:
|
||||
haveWx = False
|
||||
|
||||
try:
|
||||
from MiRCARTToPngFile import MiRCARTToPngFile
|
||||
haveMiRCARTToPngFile = True
|
||||
except ImportError:
|
||||
haveMiRCARTToPngFile = False
|
||||
|
||||
try:
|
||||
import base64, json, requests, urllib.request
|
||||
haveUrllib = True
|
||||
except ImportError:
|
||||
haveUrllib = False
|
||||
|
||||
class MiRCARTCanvasStore():
|
||||
class MiRCARTCanvasImportStore():
|
||||
"""XXX"""
|
||||
inFile = inSize = outMap = None
|
||||
parentCanvas = None
|
||||
@ -62,25 +42,6 @@ class MiRCARTCanvasStore():
|
||||
PS_COLOUR_DIGIT0 = 2
|
||||
PS_COLOUR_DIGIT1 = 3
|
||||
|
||||
# {{{ _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName): upload single PNG file to Imgur
|
||||
def _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName):
|
||||
requestImageData = open(pathName, "rb").read()
|
||||
requestData = { \
|
||||
"image": base64.b64encode(requestImageData), \
|
||||
"key": apiKey, \
|
||||
"name": imgName, \
|
||||
"title": imgTitle, \
|
||||
"type": "base64"}
|
||||
requestHeaders = {"Authorization": "Client-ID " + apiKey}
|
||||
responseHttp = requests.post( \
|
||||
"https://api.imgur.com/3/upload.json", \
|
||||
data=requestData, headers=requestHeaders)
|
||||
responseDict = json.loads(responseHttp.text)
|
||||
if responseHttp.status_code == 200:
|
||||
return [200, responseDict.get("data").get("link")]
|
||||
else:
|
||||
return [responseHttp.status_code, ""]
|
||||
# }}}
|
||||
# {{{ _flipCellStateBit(self, cellState, bit): XXX
|
||||
def _flipCellStateBit(self, cellState, bit):
|
||||
if cellState & bit:
|
||||
@ -101,65 +62,6 @@ class MiRCARTCanvasStore():
|
||||
return (15, 1)
|
||||
# }}}
|
||||
|
||||
# {{{ exportBitmapToPngFile(self, canvasBitmap, outPathName, outType): XXX
|
||||
def exportBitmapToPngFile(self, canvasBitmap, outPathName, outType):
|
||||
return canvasBitmap.ConvertToImage().SaveFile(outPathName, outType)
|
||||
# }}}
|
||||
# {{{ exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType): XXX
|
||||
def exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType):
|
||||
tmpPathName = tempfile.mkstemp()
|
||||
os.close(tmpPathName[0])
|
||||
canvasBitmap.ConvertToImage().SaveFile(tmpPathName[1], imgType)
|
||||
imgurResult = self._exportFileToImgur(apiKey, imgName, imgTitle, tmpPathName[1])
|
||||
os.remove(tmpPathName[1])
|
||||
return imgurResult
|
||||
# }}}
|
||||
# {{{ exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0): XXX
|
||||
def exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0):
|
||||
if haveUrllib:
|
||||
outFile = io.StringIO()
|
||||
self.exportTextFile(canvasMap, canvasSize, outFile)
|
||||
requestData = { \
|
||||
"api_dev_key": apiDevKey, \
|
||||
"api_option": "paste", \
|
||||
"api_paste_code": outFile.getvalue().encode(), \
|
||||
"api_paste_name": pasteName, \
|
||||
"api_paste_private": pastePrivate}
|
||||
responseHttp = requests.post("https://pastebin.com/api/api_post.php", \
|
||||
data=requestData)
|
||||
if responseHttp.status_code == 200:
|
||||
if responseHttp.text.startswith("http"):
|
||||
return (True, responseHttp.text)
|
||||
else:
|
||||
return (False, responseHttp.text)
|
||||
else:
|
||||
return (False, str(responseHttp.status_code))
|
||||
else:
|
||||
return (False, "missing requests and/or urllib3 module(s)")
|
||||
# }}}
|
||||
# {{{ exportPngFile(self, canvasMap, outPathName): XXX
|
||||
def exportPngFile(self, canvasMap, outPathName):
|
||||
if haveMiRCARTToPngFile:
|
||||
MiRCARTToPngFile(canvasMap).export(outPathName)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
# }}}
|
||||
# {{{ exportTextFile(self, canvasMap, canvasSize, outFile): XXX
|
||||
def exportTextFile(self, canvasMap, canvasSize, outFile):
|
||||
for canvasRow in range(canvasSize[1]):
|
||||
canvasLastColours = []
|
||||
for canvasCol in range(canvasSize[0]):
|
||||
canvasColColours = canvasMap[canvasRow][canvasCol][0]
|
||||
canvasColText = canvasMap[canvasRow][canvasCol][2]
|
||||
if canvasColColours != canvasLastColours:
|
||||
canvasLastColours = canvasColColours
|
||||
outFile.write("\x03" + \
|
||||
str(canvasColColours[0]) + \
|
||||
"," + str(canvasColColours[1]))
|
||||
outFile.write(canvasColText)
|
||||
outFile.write("\n")
|
||||
# }}}
|
||||
# {{{ importIntoPanel(self): XXX
|
||||
def importIntoPanel(self):
|
||||
self.parentCanvas.onStoreUpdate(self.inSize, self.outMap)
|
@ -189,7 +189,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
else:
|
||||
outPathName = dialog.GetPath()
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
self.panelCanvas.canvasStore.exportBitmapToPngFile( \
|
||||
self.panelCanvas.canvasExportStore.exportBitmapToPngFile( \
|
||||
self.panelCanvas.canvasBackend.canvasBitmap, outPathName, \
|
||||
wx.BITMAP_TYPE_PNG)
|
||||
self.SetCursor(wx.Cursor(wx.NullCursor))
|
||||
@ -198,8 +198,8 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
# {{{ canvasExportImgur(self): XXX
|
||||
def canvasExportImgur(self):
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
imgurResult = self.panelCanvas.canvasStore.exportBitmapToImgur( \
|
||||
"c9a6efb3d7932fd", self.panelCanvas.canvasBackend.canvasBitmap, \
|
||||
imgurResult = self.panelCanvas.canvasExportStore.exportBitmapToImgur( \
|
||||
"c9a6efb3d7932fd", self.panelCanvas.canvasBackend.canvasBitmap, \
|
||||
"", "", wx.BITMAP_TYPE_PNG)
|
||||
self.SetCursor(wx.Cursor(wx.NullCursor))
|
||||
if imgurResult[0] == 200:
|
||||
@ -217,7 +217,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
def canvasExportPastebin(self):
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
pasteStatus, pasteResult = \
|
||||
self.panelCanvas.canvasStore.exportPastebin( \
|
||||
self.panelCanvas.canvasExportStore.exportPastebin( \
|
||||
"", \
|
||||
self.panelCanvas.canvasMap, \
|
||||
self.panelCanvas.canvasSize)
|
||||
@ -246,7 +246,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
if newCanvasSize == None:
|
||||
newCanvasSize = (100, 30)
|
||||
self.panelCanvas.canvasStore.importNew(newCanvasSize)
|
||||
self.panelCanvas.canvasImportStore.importNew(newCanvasSize)
|
||||
self.canvasPathName = None
|
||||
self.SetCursor(wx.Cursor(wx.NullCursor))
|
||||
self._updateStatusBar(); self.onCanvasUpdate();
|
||||
@ -268,8 +268,8 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
else:
|
||||
self.canvasPathName = dialog.GetPath()
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
self.panelCanvas.canvasStore.importTextFile(self.canvasPathName)
|
||||
self.panelCanvas.canvasStore.importIntoPanel()
|
||||
self.panelCanvas.canvasImportStore.importTextFile(self.canvasPathName)
|
||||
self.panelCanvas.canvasImportStore.importIntoPanel()
|
||||
self.SetCursor(wx.Cursor(wx.NullCursor))
|
||||
self._updateStatusBar(); self.onCanvasUpdate();
|
||||
return True
|
||||
@ -282,7 +282,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
|
||||
try:
|
||||
with open(self.canvasPathName, "w") as outFile:
|
||||
self.SetCursor(wx.Cursor(wx.CURSOR_WAIT))
|
||||
self.panelCanvas.canvasStore.exportTextFile( \
|
||||
self.panelCanvas.canvasExportStore.exportTextFile( \
|
||||
self.panelCanvas.canvasMap, \
|
||||
self.panelCanvas.canvasSize, outFile)
|
||||
self.SetCursor(wx.Cursor(wx.NullCursor))
|
||||
|
@ -22,7 +22,7 @@
|
||||
# SOFTWARE.
|
||||
#
|
||||
|
||||
import MiRCARTCanvasStore
|
||||
import MiRCARTCanvasImportStore
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import sys
|
||||
|
||||
@ -128,7 +128,7 @@ class MiRCARTToPngFile:
|
||||
#
|
||||
# Entry point
|
||||
def main(*argv):
|
||||
canvasStore = MiRCARTCanvasStore.MiRCARTCanvasStore(inFile=argv[1])
|
||||
canvasStore = MiRCARTCanvasImportStore.MiRCARTCanvasImportStore(inFile=argv[1])
|
||||
MiRCARTToPngFile(canvasStore.outMap, *argv[3:]).export(argv[2])
|
||||
if __name__ == "__main__":
|
||||
if ((len(sys.argv) - 1) < 2)\
|
||||
|
Loading…
Reference in New Issue
Block a user