# 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
				print "new folder is 3 %s thumbnail %s file_name %s" % (folder, thumbnail_url, file_name)
				file = frappe.db.sql("""select name from `tabFile`
				where name=%s""", (folder,))
				if not file:
					file = frappe.new_doc("File")
					file.file_name = file_name
					file.is_folder = 1
					file.folder = under
					file.attached_to_doctype = self.dt
					file.attached_to_name =  dn
					file.insert()
				under = under + "/" + file_name
			except:
				pass


def get_compiled_path(dir_path, dn):
	jrxml_path = get_jrxml_path(dir_path, dn)
	compiled_path = frappe.utils.get_path("compiled", base=jrxml_path)
	frappe.create_folder(compiled_path)
	return compiled_path


def get_jrxml_path(dir_path, dn):
	jrxml_path = frappe.utils.get_path(dn, base=dir_path)
	return jrxml_path


def jasper_compile_jrxml(fname, file_path, compiled_path):
	c = len(fname) - 6

# 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

				print "new folder is 3 %s thumbnail %s file_name %s" % (folder, thumbnail_url, file_name)
				file = frappe.db.sql("""select name from `tabFile`
				where name=%s""", (folder,))
				if not file:
					file = frappe.new_doc("File")
					file.file_name = file_name
					file.is_folder = 1
					file.folder = under
					file.attached_to_doctype = self.dt
					file.attached_to_name =  dn
					file.insert()
				under = under + "/" + file_name
			except:
				pass


def get_compiled_path(dir_path, dn):
	jrxml_path = get_jrxml_path(dir_path, dn)
	compiled_path = frappe.utils.get_path("compiled", base=jrxml_path)
	frappe.create_folder(compiled_path)
	return compiled_path


def get_jrxml_path(dir_path, dn):
	jrxml_path = frappe.utils.get_path(dn, base=dir_path)
	return jrxml_path


def jasper_compile_jrxml(fname, file_path, compiled_path):
	c = len(fname) - 6

	jasper_compile(file_path, os.path.join(compiled_path, fname[:c] + ".jasper"))