Skip to content

Commit

Permalink
Code adjustments and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jadsonbr committed Dec 20, 2023
1 parent 582e79c commit b59c436
Show file tree
Hide file tree
Showing 40 changed files with 181 additions and 127 deletions.
26 changes: 13 additions & 13 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@



**/test/resources/reports/*.pdf
**/test/resources/reports/*.odt
**/test/resources/reports/*.rtf
**/test/resources/reports/*.xls
**/test/resources/reports/*.xlsx
**/test/resources/reports/*.html
**/test/resources/reports/*.ods
**/test/resources/reports/*.docx
**/test/resources/reports/*.csv
**/test/resources/reports/*.xml
**/test/resources/reports/*.pptx
**/test/resources/reports/*.jasper
**/test/resources/reports/*.jrprint
**/tests/resources/reports/*.pdf
**/tests/resources/reports/*.odt
**/tests/resources/reports/*.rtf
**/tests/resources/reports/*.xls
**/tests/resources/reports/*.xlsx
**/tests/resources/reports/*.html
**/tests/resources/reports/*.ods
**/tests/resources/reports/*.docx
**/tests/resources/reports/*.csv
**/tests/resources/reports/*.xml
**/tests/resources/reports/*.pptx
**/tests/resources/reports/*.jasper
**/tests/resources/reports/*.jrprint



Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
python -m pip install jpype1
- name: Run test targets for ${{ matrix.python-version }}
run: python -m unittest discover ./test -p '*.py'
run: python -m unittest discover ./tests -p '*.py'
windows_tests:
name: Win - Python ${{ matrix.python-version }} with Java ${{ matrix.java-version }}
needs: linux_tests
Expand All @@ -63,7 +63,7 @@ jobs:
run: |
python -m pip install jpype1
- name: Run test targets for ${{ matrix.python-version }}
run: python -m unittest discover ./test -p '*.py'
run: python -m unittest discover ./tests -p '*.py'
mac_tests:
name: MacOS - Python ${{ matrix.python-version }} with Java ${{ matrix.java-version }}
needs: [linux_tests, windows_tests]
Expand All @@ -90,4 +90,4 @@ jobs:
run: |
python -m pip install jpype1
- name: Run test targets for ${{ matrix.python-version }}
run: python -m unittest discover ./test -p '*.py'
run: python -m unittest discover ./tests -p '*.py'
26 changes: 13 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ __pycache__/
*.py[cod]
*$py.class

/test/resources/reports/*.pdf
/test/resources/reports/*.odt
/test/resources/reports/*.rtf
/test/resources/reports/*.xls
/test/resources/reports/*.xlsx
/test/resources/reports/*.html
/test/resources/reports/*.ods
/test/resources/reports/*.docx
/test/resources/reports/*.csv
/test/resources/reports/*.xml
/test/resources/reports/*.pptx
/test/resources/reports/*.jasper
/test/resources/reports/*.jrprint
/tests/resources/reports/*.pdf
/tests/resources/reports/*.odt
/tests/resources/reports/*.rtf
/tests/resources/reports/*.xls
/tests/resources/reports/*.xlsx
/tests/resources/reports/*.html
/tests/resources/reports/*.ods
/tests/resources/reports/*.docx
/tests/resources/reports/*.csv
/tests/resources/reports/*.xml
/tests/resources/reports/*.pptx
/tests/resources/reports/*.jasper
/tests/resources/reports/*.jrprint

*.coverage
htmlcov/
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ PyReportJasper Changelog

Here you can see the full list of changes between each PyReportJasper release.

Version 2.1.3
-------------


Version 2.1.0
-------------
- Removed the jpy dependency.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ ENV PYTHONUNBUFFERED=1

COPY . .

CMD ["python", "-m", "unittest", "discover", "./test", "-p", "*.py"]
CMD ["python", "-m", "unittest", "discover", "./tests", "-p", "*.py"]
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ reporting.
.. |pyversions| image:: https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-blue.svg
.. |javaversions| image:: https://img.shields.io/badge/java-%3E=9%20and%20%3C=20-blue.svg
.. |jvm| image:: https://img.shields.io/badge/jvm-Open%20%7C%20Oracle%20%7C%20Corretto-blue.svg
.. |platform| image:: https://img.shields.io/badge/platform-Linux%20%7C%20Windows%20%7C%20Mac-lightgrey
.. |platform| image:: https://img.shields.io/badge/platform-Linux%20%7C%20Windows%20%7C%20Mac-blue
.. _GNU GENERAL PUBLIC LICENSE: https://github.com/acesseonline/pyreportjasper/blob/master/LICENSE
.. _Python Docs: https://pyreportjasper.readthedocs.io/en/latest/
.. |license| image:: https://img.shields.io/badge/License-GPLv3-blue.svg
Expand Down
2 changes: 1 addition & 1 deletion docs/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Compile PyReportJasper using the included ``setup.py`` script: ::

**3. Test PyReportJasper with (optional):** ::

python -m unittest discover ./test -p '*.py'
python -m unittest discover ./tests -p '*.py'

**4. Install PyReportJasper with:** ::

Expand Down
2 changes: 2 additions & 0 deletions pyreportjasper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Config:
input = None
jdbcDir = None
dataFile = None
dataURL = None
csvFirstRow = None
csvColumns = None
csvRecordDel = None
Expand All @@ -42,6 +43,7 @@ class Config:
outFieldDel = None
outCharset = None
useJaxen = True
subreports = {}

jvm_maxmem = '512M'
jvm_classpath = None
Expand Down
25 changes: 23 additions & 2 deletions pyreportjasper/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# 2023 Jadson Bonfim Ribeiro <[email protected]>
#

import os
import jpype
import pathlib
import jpype.imports

from pyreportjasper.config import Config
Expand All @@ -29,6 +31,8 @@ def __init__(self):
self.JRLoader = jpype.JPackage('net').sf.jasperreports.engine.util.JRLoader
self.StringEscapeUtils = jpype.JPackage('org').apache.commons.lang.StringEscapeUtils
self.File = jpype.JPackage('java').io.File
self.URL = jpype.JPackage('java').net.URL
self.ByteArrayInputStream = jpype.JPackage('java').io.ByteArrayInputStream

def get_csv_datasource(self, config: Config):
ds = self.JRCsvDataSource(self.get_data_file_input_stream(config), config.csvCharset)
Expand All @@ -44,15 +48,32 @@ def get_xml_datasource(self, config: Config):
return jpype.JObject(ds, self.JRXmlDataSource)

def get_json_datasource(self, config: Config):
ds = self.JsonDataSource(self.get_data_file_input_stream(config), config.jsonQuery)
if config.dataURL:
ds = self.JsonDataSource(self.get_data_url_input_stream(config), config.jsonQuery)
else:
ds = self.JsonDataSource(self.get_data_file_input_stream(config), config.jsonQuery)
return jpype.JObject(ds, self.JsonDataSource)

def get_jsonql_datasource(self, config: Config):
ds = self.JsonQLDataSource(self.get_data_file_input_stream(config), config.jsonQLQuery)
return jpype.JObject(ds, self.JsonQLDataSource)

def get_data_file_input_stream(self, config: Config):
return self.JRLoader.getInputStream(self.File(config.dataFile))
data_file = config.dataFile
bytes_data_file = None
if isinstance(data_file, str) or isinstance(data_file, pathlib.PurePath):
if not os.path.isfile(data_file):
raise NameError('dataFile is not file')
with open(data_file, 'rb') as file:
bytes_data_file = file.read()
elif isinstance(data_file, bytes):
bytes_data_file = data_file
else:
raise NameError('dataFile does not have a valid type. Please enter the file path or its bytes')
return self.ByteArrayInputStream(bytes_data_file)

def get_data_url_input_stream(self, config: Config):
return self.JRLoader.getInputStream(self.URL(config.dataURL))

def get_connection(self, config: Config):
dbtype = config.dbType
Expand Down
133 changes: 58 additions & 75 deletions pyreportjasper/pyreportjasper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PyReportJasper:
METHODS = ('GET', 'POST', 'PUT')

def config(self, input_file, output_file=False, output_formats=['pdf'], parameters={}, db_connection={},
locale='pt_BR', resource=None):
locale='pt_BR', resource=None, subreports=None):
if not input_file:
raise NameError('No input file!')
if isinstance(output_formats, list):
Expand All @@ -53,6 +53,7 @@ def config(self, input_file, output_file=False, output_formats=['pdf'], paramete
raise NameError("'output_formats' value is not list!")
self.config = Config()
self.config.input = input_file
self.config.subreports = subreports if subreports else {}
self.config.locale = locale
self.config.resource = resource
self.config.outputFormats = output_formats
Expand All @@ -62,48 +63,35 @@ def config(self, input_file, output_file=False, output_formats=['pdf'], paramete
self.config.output = input_file
self.config.params = parameters
if len(db_connection) > 0:
if 'driver' in db_connection:
self.config.dbType = db_connection['driver']
if 'username' in db_connection:
self.config.dbUser = db_connection['username']
if 'password' in db_connection:
self.config.dbPasswd = db_connection['password']
if 'host' in db_connection:
self.config.dbHost = db_connection['host']
if 'database' in db_connection:
self.config.dbName = db_connection['database']
if 'port' in db_connection:
self.config.dbPort = db_connection['port']
if 'jdbc_driver' in db_connection:
self.config.dbDriver = db_connection['jdbc_driver']
if 'jdbc_url' in db_connection:
self.config.dbUrl = db_connection['jdbc_url']
if 'jdbc_dir' in db_connection:
self.config.jdbcDir = db_connection['jdbc_dir']
if 'db_sid' in db_connection:
self.config.dbSid = db_connection['db_sid']
if 'xml_xpath' in db_connection:
self.config.xmlXpath = db_connection['xml_xpath']
if 'data_file' in db_connection:
self.config.dataFile = db_connection['data_file']
if 'json_query' in db_connection:
self.config.jsonQuery = db_connection['json_query']
if 'jsonql_query' in db_connection:
self.config.jsonQLQuery = db_connection['jsonql_query']
if 'csv_first_row' in db_connection:
self.config.csvFirstRow = True
if 'csv_columns' in db_connection:
self.config.csvColumns = db_connection['csv_columns']
if 'csv_record_del' in db_connection:
self.config.csvRecordDel = db_connection['csv_record_del']
if 'csv_field_del' in db_connection:
self.config.csvFieldDel = db_connection['csv_field_del']
if 'csv_out_field_del' in db_connection:
self.config.outFieldDel = db_connection['csv_out_field_del']
if 'csv_charset' in db_connection:
self.config.csvCharset = db_connection['csv_charset']
if 'csv_out_charset' in db_connection:
self.config.outCharset = db_connection['csv_out_charset']
mapping = {
'driver': 'dbType',
'username': 'dbUser',
'password': 'dbPasswd',
'host': 'dbHost',
'database': 'dbName',
'port': 'dbPort',
'jdbc_driver': 'dbDriver',
'jdbc_url': 'dbUrl',
'jdbc_dir': 'jdbcDir',
'db_sid': 'dbSid',
'xml_xpath': 'xmlXpath',
'data_file': 'dataFile',
'data_url': 'data_url',
'json_query': 'jsonQuery',
'jsonql_query': 'jsonQLQuery',
'csv_columns': 'csvColumns',
'csv_record_del': 'csvRecordDel',
'csv_field_del': 'csvFieldDel',
'csv_out_field_del': 'outFieldDel',
'csv_charset': 'csvCharset',
'csv_out_charset': 'outCharset'
}

for key, value in db_connection.items():
if key in mapping:
setattr(self.config, mapping[key], value)
elif key == 'csv_first_row':
self.config.csvFirstRow = True

def compile(self, write_jasper=False):
error = None
Expand Down Expand Up @@ -132,6 +120,11 @@ def compile(self, write_jasper=False):
else:
return True

def instantiate_report(self):
report = Report(self.config, self.config.input)
report.fill()
return report

def process_report(self):
error = None
base_input = os.path.splitext(self.config.input)
Expand All @@ -142,37 +135,28 @@ def process_report(self):

if os.path.isfile(self.config.input):
try:
report = Report(self.config, self.config.input)
report.fill()
report = self.instantiate_report()
try:
formats = self.config.outputFormats
for f in formats:
if f == 'pdf':
report.export_pdf()
elif f == 'html':
report.export_html()
elif f == 'rtf':
report.export_rtf()
elif f == 'docx':
report.export_docx()
elif f == 'odt':
report.export_odt()
elif f == 'xml':
report.export_xml()
elif f == 'xls':
report.export_xls()
elif f == 'xlsx':
report.export_xlsx()
elif f == 'csv':
report.export_csv()
elif f == 'csv_meta':
report.export_csv_meta()
elif f == 'ods':
report.export_ods()
elif f == 'pptx':
report.export_pptx()
elif f == 'jrprint':
report.export_jrprint()
formats_functions = {
'pdf': report.export_pdf,
'html': report.export_html,
'rtf': report.export_rtf,
'docx': report.export_docx,
'odt': report.export_odt,
'xml': report.export_xml,
'xls': report.export_xls,
'xlsx': report.export_xlsx,
'csv': report.export_csv,
'csv_meta': report.export_csv_meta,
'ods': report.export_ods,
'pptx': report.export_pptx,
'jrprint': report.export_jrprint
}

for f in self.config.outputFormats:
export_function = formats_functions.get(f)
if export_function:
export_function()
else:
raise NameError("Error output format {} not implemented!".format(f))
except Exception as ex:
Expand All @@ -183,8 +167,7 @@ def process_report(self):
error = NameError('Error: not a file: {}'.format(self.config.input))
if error:
raise error
else:
return True
return True

def list_report_params(self):
report = Report(self.config, self.config.input)
Expand Down
Loading

0 comments on commit b59c436

Please sign in to comment.