#RobotDB prefinal

dev-linux
Ivan Maslov 5 years ago
parent 029a5ce67d
commit fb3e122783

@ -0,0 +1,11 @@
import win32com.client as win32
def OpenWorkbook(xlapp, xlfile):
try:
xlwb = xlapp.Workbooks(xlfile)
except Exception as e:
try:
xlwb = xlapp.Workbooks.Open(xlfile)
except Exception as e:
print(e)
xlwb = None
return(xlwb)

@ -14,10 +14,6 @@ $(document).ready(function() {
success: null, success: null,
dataType: "html" dataType: "html"
}); });
SQLInsert
data: [{"TableName":"",RowDict:{"Key1":Value1, "Key2":Value2}}] data: [{"TableName":"",RowDict:{"Key1":Value1, "Key2":Value2}}]
$(document).ready(function() { $(document).ready(function() {
@ -25,9 +21,42 @@ $(document).ready(function() {
$("#myid").addClass("highlight"); $("#myid").addClass("highlight");
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "http://localhost:81/", url: "http://localhost:8081/",
data: '[{"TableName":"Test", "RowDict":{"Name":"Name1","Description":"DescTest", "Money":100, "Date":"01.01.2020"}}]', data: '[{"TableName":"Test", "RowDict":{"Name":"Name1","Description":"DescTest", "Money":100, "Date":"01.01.2020"}}]',
success: null, success: null,
dataType: "html" dataType: "html"
}); });
}); });
/SQLInsert
POST
Result: json
{
"Status":"OK"|"ERROR", #General status (OK or Error in general alg)
"ErrorMessage":"", #Error message if Status = ERROR
"Result":[
{
"Status":"OK"|"ERROR", #Row status (OK or Error in current insert)
"ErrorMessage":"", #Error message if Status = ERROR
},
{}
]
}
/SQLExportXLS.xlsx
POST
Args: {
"XLSTemplatePath":"C:\\Path\\To\\XLS template.xlsx",
"XLSSheetName":"SheetName",
"OffsetRow": 2,
"OffsetCol":1,
"XLSResultPath": "C:\\Path\\To\\XLS Result.xlsx",
"XLSResultFlagSendInResponse": true,
"XLSResultFlagDeleteAfterSend": true
}
Result: xls file

@ -1,59 +1,150 @@
import json import json
from . import ExcelCom
import os
import sqlite3
import win32com.client
import time
import pythoncom
#Insert in DB #Insert in DB
def SQLInsert(inRequest,inGlobalDict): def SQLInsert(inRequest,inGlobalDict):
inResponseDict = inRequest.OpenRPAResponseDict inResponseDict = inRequest.OpenRPAResponseDict
# Create result JSON # Create result JSON
lResultJSON = {"FlagSQLInsert": False} lResultJSON = {"Status": "OK", "ErrorMessage":"", "Result":[]}
#Set status code 200
inResponseDict["StatusCode"] = 200
try:
#Read the body #Read the body
#ReadRequest #ReadRequest
lInputJSON={} lInputJSON={}
if inRequest.headers.get('Content-Length') is not None: if inRequest.headers.get('Content-Length') is not None:
lInputByteArrayLength = int(inRequest.headers.get('Content-Length')) lInputByteArrayLength = int(inRequest.headers.get('Content-Length'))
lInputByteArray=inRequest.rfile.read(lInputByteArrayLength) lInputByteArray=inRequest.rfile.read(lInputByteArrayLength)
print(lInputByteArray.decode('utf8')) #print(lInputByteArray.decode('utf8'))
#Превращение массива байт в объект #Превращение массива байт в объект
lInputJSON=json.loads(lInputByteArray.decode('utf8')) lInputJSON=json.loads(lInputByteArray.decode('utf8'))
######################################## ########################################
print(lInputJSON)
import sqlite3
conn = sqlite3.connect(inGlobalDict["SQLite"]["DBPath"]) conn = sqlite3.connect(inGlobalDict["SQLite"]["DBPath"])
c = conn.cursor() c = conn.cursor()
# Loop for rows # Loop for rows
for lRowItem in lInputJSON: for lRowItem in lInputJSON:
lRowResult={"Status": "OK", "ErrorMessage":""}
try:
my_dict = lRowItem["RowDict"] my_dict = lRowItem["RowDict"]
# Insert a row of data # Insert a row of data
columns = ', '.join(my_dict.keys()) columns = ', '.join(my_dict.keys())
placeholders = ':'+', :'.join(my_dict.keys()) placeholders = ':'+', :'.join(my_dict.keys())
query = f'INSERT INTO {lRowItem["TableName"]} (%s) VALUES (%s)' % (columns, placeholders) query = f'INSERT INTO {lRowItem["TableName"]} (%s) VALUES (%s)' % (columns, placeholders)
c.execute(query, my_dict) c.execute(query, my_dict)
except Exception as e:
lRowResult["Status"]="ERROR"
lRowResult["ErrorMessage"]=str(e)
finally:
lResultJSON["Result"].append(lRowResult)
# Save (commit) the changes # Save (commit) the changes
conn.commit() conn.commit()
# We can also close the connection if we are done with it. # We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost. # Just be sure any changes have been committed or they will be lost.
conn.close() conn.close()
except Exception as e:
lResultJSON["Status"]="ERROR"
lResultJSON["ErrorMessage"]=str(e)
finally:
######################################## ########################################
# Send message back to client # Send message back to client
message = json.dumps(lResultJSON) message = json.dumps(lResultJSON)
print(message)
# Write content as utf-8 data # Write content as utf-8 data
inResponseDict["Body"] = bytes(message, "utf8") inResponseDict["Body"] = bytes(message, "utf8")
################################################
def GetScreenshot(inRequest,inGlobalDict): #Export SQLite to Excel
# Get Screenshot def SQLExportXLS(inRequest,inGlobalDict):
def SaveScreenshot(inFilePath): #Step 1 - read SQLite
# grab fullscreen conn = sqlite3.connect(inGlobalDict["SQLite"]["DBPath"])
# Save the entire virtual screen as a PNG c = conn.cursor()
lScreenshot = getScreenAsImage() # Loop for rows
lScreenshot.save('screenshot.png', format='png') # for lRowItem in lInputJSON:
# lScreenshot = ScreenshotSecondScreen.grab_screen() # my_dict = lRowItem["RowDict"]
# save image file # # Insert a row of data
# lScreenshot.save('screenshot.png') # columns = ', '.join(my_dict.keys())
# Сохранить файл на диск # placeholders = ':'+', :'.join(my_dict.keys())
SaveScreenshot("Screenshot.png") query = f'select * from Test'
lFileObject = open("Screenshot.png", "rb") #create data array
#row = range(0,10)
i = 0
data_array = []
for row in c.execute(query):
# use the cursor as an iterable
data_array.append(row)
i += 1
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
#step 2 - insert in XLS
pythoncom.CoInitialize()
#write the array to an excel file
#excel = win32com.client.Dispatch("Excel.Application")
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
excel.DisplayAlerts = False
#excel.ScreenUpdating = False
#book = excel.Workbooks.Add()
#sheet = book.Worksheets(1)
#Read input JSON
lInputJSON={}
if inRequest.headers.get('Content-Length') is not None:
lInputByteArrayLength = int(inRequest.headers.get('Content-Length'))
lInputByteArray=inRequest.rfile.read(lInputByteArrayLength)
#print(lInputByteArray.decode('utf8'))
#Превращение массива байт в объект
lInputJSON=json.loads(lInputByteArray.decode('utf8'))
#Config
lOffsetRow = lInputJSON["OffsetRow"]
lOffsetCol = lInputJSON["OffsetCol"]
lXLSTemplatePath = lInputJSON["XLSTemplatePath"]
lXLSSheetName = lInputJSON["XLSSheetName"]
lXLSResultPath = lInputJSON["XLSResultPath"]
lXLSResultFlagSendInResponse = lInputJSON["XLSResultFlagSendInResponse"]
lXLSResultFlagDeleteAfterSend = lInputJSON["XLSResultFlagDeleteAfterSend"]
try:
#excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
book = ExcelCom.OpenWorkbook(excel, lXLSTemplatePath)
sheet = book.Worksheets(lXLSSheetName)
excel.Visible = True
#single loop, writing a row to a range
#Logic
start = time.time()
row = 0
for line in data_array:
row += 1
sheet.Range(sheet.Cells(row+lOffsetRow,1+lOffsetCol), sheet.Cells(row+lOffsetRow, len(line)+lOffsetCol)).Value = line
if lXLSResultPath:
book.SaveAs(Filename = lXLSResultPath)
#excel.ScreenUpdating = True
except Exception as e:
print(e)
finally:
# RELEASES RESOURCES
sheet = None
book = None
excel.DisplayAlerts = True
excel.Quit()
excel = None
pythoncom.CoUninitialize()
#####################
#Step 3 - Send file content to client
#####################
if lXLSResultFlagSendInResponse and lXLSResultPath:
lFileObject = open(lXLSResultPath, "rb")
# Write content as utf-8 data # Write content as utf-8 data
inRequest.OpenRPAResponseDict["Body"] = lFileObject.read() inRequest.OpenRPAResponseDict["Body"] = lFileObject.read()
# Закрыть файловый объект # Закрыть файловый объект
lFileObject.close() lFileObject.close()
#####################
#Step 4 - Delete after send
#####################
if lXLSResultFlagDeleteAfterSend and lXLSResultPath:
if os.path.exists(lXLSResultPath):
os.remove(lXLSResultPath)
def SettingsUpdate(inGlobalConfiguration): def SettingsUpdate(inGlobalConfiguration):
import os import os
import pyOpenRPA.Orchestrator import pyOpenRPA.Orchestrator
@ -70,8 +161,8 @@ def SettingsUpdate(inGlobalConfiguration):
# "ResponseDefRequestGlobal": None #Function with str result # "ResponseDefRequestGlobal": None #Function with str result
#} #}
#Orchestrator basic dependencies #Orchestrator basic dependencies
{"Method":"POST", "URL": "/", "MatchType": "EqualCase", "ResponseDefRequestGlobal": SQLInsert, "ResponseContentType": "text/html"}, {"Method":"POST", "URL": "/SQLInsert", "MatchType": "EqualCase", "ResponseDefRequestGlobal": SQLInsert, "ResponseContentType": "application/json"},
{"Method":"GET", "URL": "/3rdParty/Semantic-UI-CSS-master/semantic.min.css", "MatchType": "EqualCase", "ResponseFilePath": os.path.join(lOrchestratorFolder, "..\\Resources\\Web\\Semantic-UI-CSS-master\\semantic.min.css"), "ResponseContentType": "text/css"} {"Method":"POST", "URL": "/SQLExportXLS.xlsx", "MatchType": "EqualCase", "ResponseDefRequestGlobal": SQLExportXLS, "ResponseContentType": "application/octet-stream"}
] ]
inGlobalConfiguration["Server"]["URLList"]=inGlobalConfiguration["Server"]["URLList"]+lURLList inGlobalConfiguration["Server"]["URLList"]=inGlobalConfiguration["Server"]["URLList"]+lURLList
return inGlobalConfiguration return inGlobalConfiguration

@ -3,7 +3,7 @@ r"""
The OpenRPA package (from UnicodeLabs) The OpenRPA package (from UnicodeLabs)
""" """
__version__ = 'v1.0.37' __version__ = 'v1.0.38'
__all__ = [] __all__ = []
__author__ = 'Ivan Maslov <ivan.maslov@unicodelabs.ru>' __author__ = 'Ivan Maslov <ivan.maslov@unicodelabs.ru>'
#from .Core import Robot #from .Core import Robot

Binary file not shown.

@ -14,16 +14,16 @@ def Settings():
}, },
"Server": { "Server": {
"ListenPort_": "Порт, по которому можно подключиться к демону", "ListenPort_": "Порт, по которому можно подключиться к демону",
"ListenPort": 81, "ListenPort": 8081,
"ListenURLList": [ "ListenURLList": [
{ {
"Description": "Local machine test", "Description": "Local machine test",
"URL_": "Сетевое расположение сервера демона", "URL_": "Сетевое расположение сервера демона",
"URL": "http://127.0.0.1:81" "URL": "http://127.0.0.1:8081"
} }
], ],
"AccessUsers": { #Default - all URL is blocked "AccessUsers": { #Default - all URL is blocked
"FlagCredentialsAsk": True, #Turn on Authentication "FlagCredentialsAsk": False, #Turn on Authentication
"RuleDomainUserDict": { "RuleDomainUserDict": {
#("DOMAIN", "USER"): { !!!!!only in upper case!!!! #("DOMAIN", "USER"): { !!!!!only in upper case!!!!
# "MethodMatchURLBeforeList": [ # "MethodMatchURLBeforeList": [

Binary file not shown.
Loading…
Cancel
Save