# Path: jasper_erpnext_report/jasper_reports/compile_reports.py
# def jasper_compile(jrxml, destFileName):
# 	try:
# 		compiler = jr.ReportCompiler()
# 		compiler.compile(jrxml,destFileName)
# 	except Exception as e:
# 		import jasper_erpnext_report as jer
# 		if jer.pyjnius == False:
# 			frappe.throw(_("Please install pyjnius python module."))
# 			return
# 		frappe.throw(_("Error while compiling report %s, error is: %s." % (jrxml, e)))
# 
# Path: jasper_erpnext_report/utils/file.py
# def get_image_name(iname):
# def get_images_path(compiled_path):
# def get_jasper_path(for_all_sites = False):
# def write_StringIO_to_file(file_path, output):
# def write_file(content, file_path, modes="w+"):
# def check_extension(fname):
# def get_extension(fname):
# def remove_from_doc(dt, dn, field, where_field = "name"):
# def delete_from_doc(dt, dn, field, value, where_field):
# def delete_from_FileData(dt, dn, file_url):
# def remove_directory(path, ignore_errors=True):
# def remove_compiled_report(root_path):
# def get_file(path, modes="r", raise_not_found=False):
# def read_file(path, modes="r", raise_not_found=False):
# def get_html_reports_path(report_name, where="reports", hash=None, localsite=None):
# def get_html_reports_images_path(report_path, where="images"):


import frappe
import os
import StringIO
			import shutil
from frappe import _
from io import BytesIO
from jasper_erpnext_report.jasper_reports.compile_reports import jasper_compile
from jasper_erpnext_report.utils.file import check_extension, get_jasper_path, get_extension, JasperXmlReport,\
	write_file
from frappe.utils.file_manager import check_max_file_size, get_content_hash
from PIL import Image, ImageOps
					from .file import remove_directory
				s = sub.rsplit("/",1)
				if len(s) > 1:
					if not (s[1][:-7] == self.fname[:-6]):
						frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)
				elif not (sub[:-7] == self.fname[:-6]):
					frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)

			if not xmldoc.subreports:
				frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)

			#self.subreport = True
			#self.file_path = self.path_join(jrxml_path, docs[0].file_name)

	def make_content_jrxml(self, name):
		#import re
		xmldoc = JasperXmlReport(BytesIO(self.content))
		xmldoc.change_subreport_expression_path()
		self.scriptlet = xmldoc.get_attrib("scriptletClass")
		#TODO
		if not self.scriptlet:
			pass

		xmldoc.change_path_images()
		xmldoc.setProperty("parent", self.parent)
		xmldoc.setProperty("jasperId", name)

		self.content = xmldoc.toString()
		#self.content = re.sub("<queryString>(.*?)</queryString>", "<queryString>%s</queryString>" % self.queryString, self.content, count=1, flags=re.S|re.M)

	def save(self):

# Path: jasper_erpnext_report/jasper_reports/compile_reports.py
# def jasper_compile(jrxml, destFileName):
# 	try:
# 		compiler = jr.ReportCompiler()
# 		compiler.compile(jrxml,destFileName)
# 	except Exception as e:
# 		import jasper_erpnext_report as jer
# 		if jer.pyjnius == False:
# 			frappe.throw(_("Please install pyjnius python module."))
# 			return
# 		frappe.throw(_("Error while compiling report %s, error is: %s." % (jrxml, e)))
# 
# Path: jasper_erpnext_report/utils/file.py
# def get_image_name(iname):
# def get_images_path(compiled_path):
# def get_jasper_path(for_all_sites = False):
# def write_StringIO_to_file(file_path, output):
# def write_file(content, file_path, modes="w+"):
# def check_extension(fname):
# def get_extension(fname):
# def remove_from_doc(dt, dn, field, where_field = "name"):
# def delete_from_doc(dt, dn, field, value, where_field):
# def delete_from_FileData(dt, dn, file_url):
# def remove_directory(path, ignore_errors=True):
# def remove_compiled_report(root_path):
# def get_file(path, modes="r", raise_not_found=False):
# def read_file(path, modes="r", raise_not_found=False):
# def get_html_reports_path(report_name, where="reports", hash=None, localsite=None):
# def get_html_reports_images_path(report_path, where="images"):


# Path: jasper_erpnext_report/utils/jasper_file_jrxml.py
import frappe
import os
import StringIO
			import shutil
from frappe import _
from io import BytesIO
from jasper_erpnext_report.jasper_reports.compile_reports import jasper_compile
from jasper_erpnext_report.utils.file import check_extension, get_jasper_path, get_extension, JasperXmlReport,\
	write_file
from frappe.utils.file_manager import check_max_file_size, get_content_hash
from PIL import Image, ImageOps
					from .file import remove_directory

				s = sub.rsplit("/",1)
				if len(s) > 1:
					if not (s[1][:-7] == self.fname[:-6]):
						frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)
				elif not (sub[:-7] == self.fname[:-6]):
					frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)

			if not xmldoc.subreports:
				frappe.msgprint(_("The report %s is not a subreport of %s."  % (self.fname[:-6], docs[0].file_name[:-6])),raise_exception=True)

			#self.subreport = True
			#self.file_path = self.path_join(jrxml_path, docs[0].file_name)

	def make_content_jrxml(self, name):
		#import re
		xmldoc = JasperXmlReport(BytesIO(self.content))
		xmldoc.change_subreport_expression_path()
		self.scriptlet = xmldoc.get_attrib("scriptletClass")
		#TODO
		if not self.scriptlet:
			pass

		xmldoc.change_path_images()
		xmldoc.setProperty("parent", self.parent)
		xmldoc.setProperty("jasperId", name)

		self.content = xmldoc.toString()
		#self.content = re.sub("<queryString>(.*?)</queryString>", "<queryString>%s</queryString>" % self.queryString, self.content, count=1, flags=re.S|re.M)

	def save(self):

		self.save_path = write_file(self.content, self.file_path)