Wednesday, June 22, 2011

Python, EasyGui y SAP


Post original: Tasting the mix of Python and SAP - Volume 2

Este post continua con mis aventuras aprendiendo Python, y asi como amo los Micro Frameworks Web, tambien amo los frameworks graficos simples...esta vez, vamos a utilizar EasyGui, que es un wrapper para Tkinter que es a su vez una capa para Tk.

Asi que cual el alboroto con EasyGui? Bueno...es una forma facil de hacer GUI's...entienden? Easy-Gui...Gui-Facil.

Como sea, no es tan completo como TKinter pero de hecho es mas facil de aprender y programar. Basta de hablar...veamos el codigo fuente...

Primero, use Yaml nuevamente para esconder la mayoria de los parametros de conexion del usuario.


ashost: localhost
sysnr: "00"
client: "001"
lang: EN
trace: 1
loglevel: warn

SE16_EasyGui.py

from easygui import *
import sapnwrfc

TITLE = "Python (Easy Gui) & SAP - SE16 Emulator"
conn = ""
table = ""


def Login():
global conn
msg = "Python (Easy Gui) & SAP - SE16 Emulator"
title = "Login"
fieldNames = ["User", "Passwd"]
fieldValues = []
fieldValues = multpasswordbox(msg, title, fieldNames)
user, passwd = fieldValues[0], fieldValues[1]
sapnwrfc.base.config_location = "sap.yml"
sapnwrfc.base.load_config()
conn = sapnwrfc.base.rfc_connect({'user': user, 'passwd': passwd})
ChooseTable()


def ChooseTable():
global table
table = enterbox("Show Table")
if table != " ":
ShowTable()


def ShowTable():
global conn, table
fields = []
fields_name = []
fields_length = []
output = []
header = ""
lines = ""
separator = ""
counter = 0
func_disc = conn.discover("RFC_READ_TABLE")
func = func_disc.create_function_call()
func.QUERY_TABLE(table)
func.DELIMITER("|")
func.invoke()
data_fields = func.DATA.value
data_names = func.FIELDS.value
long_fields = len(func.DATA())
long_names = len(func.FIELDS())

for line in range(0, long_fields):
fields.append(data_fields[line]["WA"].strip())
for line in range(0, long_names):
counter = counter + 1
fields_name.append(data_names[line]["FIELDNAME"].strip())
fields_length.append(data_names[line]["LENGTH"].strip())

for line in range(0, long_names):
field_name = fields_name[line]
field_length = fields_length[line]
if len(field_name) > field_length:
field_length = len(field_name)
field_length = int(field_length) - len(field_name)
spaces = " " * int(field_length)
counter = counter + len(field_name) + len(spaces)
header = header + field_name + spaces + "|"

separator = "-" * counter
output.append(header)
output.append(separator)

for line in range(0, long_fields):
lines = ""
field_length = ""
spaces = ""
data_split = fields[line].split("|")
for line in range(0, long_names):
field_name = fields_name[line]
field_length = fields_length[line]
if len(field_name) > int(field_length):
field_length = len(field_name)
field_length = int(field_length) -
len(data_split[line])
else:
field_length = 0
spaces = " " * int(field_length)
lines = lines + data_split[line] + spaces + "|"
output.append(lines)


codebox("", TITLE, "\n".join(output))

if __name__ == "__main__":
Login()

Ahora, unas imagenes por supuesto -:)





Espero que les haya gustado...nos vemos la proxima vez...

Saludos,

Blag.

No comments: