From a1ef68dd22ae5ed8977af4c92c40cd00895ec887 Mon Sep 17 00:00:00 2001 From: Sesostris Vieira Date: Fri, 13 May 2011 16:40:54 +0000 Subject: [PATCH] =?UTF-8?q?M=C3=B3dulo=20Python=20com=20classes=20para=20m?= =?UTF-8?q?anipula=C3=A7=C3=A3o=20de=20relat=C3=B3rios=20produzidos=20no?= =?UTF-8?q?=20Eclipse=20BIRT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/birtReport/__init__.py | 0 sigi/birtReport/birtReport.py | 108 ++++++++++++++++++++++++++++++ sigi/birtReport/birtReportTree.py | 57 ++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 sigi/birtReport/__init__.py create mode 100644 sigi/birtReport/birtReport.py create mode 100644 sigi/birtReport/birtReportTree.py diff --git a/sigi/birtReport/__init__.py b/sigi/birtReport/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/birtReport/birtReport.py b/sigi/birtReport/birtReport.py new file mode 100644 index 0000000..0841ce2 --- /dev/null +++ b/sigi/birtReport/birtReport.py @@ -0,0 +1,108 @@ +''' +Created on 11/05/2011 + +@author: sesostris +''' +import os +import xml.dom.minidom as minidom + +class BirtReport(): + ''' + Handles a birtReport report + ''' + + rptFile = '' + + def __init__(self, rptFile): + ''' + rptFile param is the FQN of birt report file + ''' + self.rptFile = rptFile + + def rptExists(self): + return os.path.isfile(self.rptFile) + + def getReportParams(self): + if not self.rptExists(): + return {} + + formFields = {} + doc = minidom.parse(self.rptFile) + parameterTags = doc.getElementsByTagName("parameters") + + for parameterTag in parameterTags: + for parameter in parameterTag.childNodes: + if parameter.nodeType != minidom.Element.TEXT_NODE: + formField = {} + for property in parameter.childNodes: + if property.nodeType != minidom.Element.TEXT_NODE and property.hasChildNodes(): + fieldName = property.getAttribute('name').encode('ascii') + fieldValue = property.childNodes[0].nodeValue + + if fieldName == 'selectionList': + fieldValue = () + for structure in property.getElementsByTagName('structure'): + s = {} + + for structProp in structure.getElementsByTagName('property'): + s[structProp.getAttribute('name').encode('ascii')] = structProp.childNodes[0].nodeValue + + fieldValue = fieldValue + (s,) + + if fieldName == 'defaultValue': + fieldValue = () + for value in property.getElementsByTagName('value'): + if value.getAttribute('type') == 'constant': + fieldValue = fieldValue + (value.childNodes[0].nodeValue, ) + + formField[fieldName] = fieldValue + + if formField['valueType'] == 'dynamic': + for dataSets in doc.getElementsByTagName("data-sets"): + for dataSet in dataSets.getElementsByTagName("oda-data-set"): + if dataSet.getAttribute('name') == formField['dataSetName']: + for xmlProp in dataSet.getElementsByTagName('xml-property'): + if xmlProp.getAttribute('name') == 'queryText': + formField['queryText'] = xmlProp.childNodes[0].data + + try: + from django.db import connection + cursor = connection.cursor() + cursor.execute(formField['queryText']) + resultSet = cursor.fetchall() + formField['selectionList'] = () + for record in resultSet: + formField['selectionList'] = formField['selectionList'] + ({'value': record[0],'label': record[1]},) + +# formField['selectionList'] = resultSet + except: + formField['selectionList'] = {} + + formFields[parameter.getAttribute('name').encode('ascii')] = formField + + return formFields + + def getName(self): + doc = minidom.parse(self.rptFile) + root = doc.documentElement + + for element in root.childNodes: + if (element.localName == "text-property" and element.hasAttribute("name") and element.getAttribute("name") == 'displayName'): + return element.childNodes[0].data + + def getTitle(self): + doc = minidom.parse(self.rptFile) + root = doc.documentElement + + for element in root.childNodes: + if (element.localName == "text-property" and element.hasAttribute("name") and element.getAttribute("name") == 'title'): + return element.childNodes[0].data + + + +#r = birtReport('/home/sesostris/workspace/sigi/BIRT_Reports/teste_parametros.rptdesign') +#p = r.getReportParams() +#print p['prm_estado'] + +#for q in p: +# print q \ No newline at end of file diff --git a/sigi/birtReport/birtReportTree.py b/sigi/birtReport/birtReportTree.py new file mode 100644 index 0000000..20f3040 --- /dev/null +++ b/sigi/birtReport/birtReportTree.py @@ -0,0 +1,57 @@ +# -*- encoding: utf-8 -*- +''' +Created on 11/05/2011 + +@author: sesostris +''' + +import os +import xml.dom.minidom as minidom + +class BirtReportTree(): + '''Gerencia uma árvore de relatórios birtReports, permitindo montar telas + de navegação (menus) para acessar os relatórios''' + birtReportRoot = '/' + + def __init__(self, root = '/'): + self.birtReportRoot = root + + def getRootItems(self): + return self.getFolderItems('') + + def getFolderItems(self, folder): + folder = self.birtReportRoot + folder + items = {'folders': {}, 'reports': {}} + dirList = os.listdir(folder) + + for fname in dirList: + name = fname + title = "- * -" + + if os.path.isdir(folder + fname) and fname[0] != '.': + # Tratar folders # + if os.path.isfile(folder + fname + '/.birtfolder'): + fd = os.open(folder + fname + '/.birtfolder', os.O_RDONLY) + try: + data = eval(os.read(fd, 4096)) + name = data['name'] + title = data['title'] + finally: + os.close(fd) + items['folders'][fname] = {'name': name, 'title': title} + elif fname[-10:] == '.rptdesign' and fname[0] != '.': + doc = minidom.parse(folder + fname) + root = doc.documentElement + + for element in root.childNodes: + if (element.localName == "text-property" and element.hasAttribute("name")): + name = element.getAttribute("name") + value = element.childNodes[0].data + if name == "title": + title = value + elif name == "displayName": + name = value + + items['reports'][fname] = {'name': name, 'title': title} + + return items \ No newline at end of file