# Path: jasper_erpnext_report/utils/file.py
# def get_file(path, modes="r", raise_not_found=False):
# 	if modes != "r":
# 		content = read_file(path, modes=modes, raise_not_found=raise_not_found)
# 	else:
# 		content = frappe.read_file(path, raise_not_found=raise_not_found)
# 
# 	return content
# 
# def get_html_reports_images_path(report_path, where="images"):
# 	path = os.path.join(report_path, where)
# 	frappe.create_folder(path)
# 	return path
# 
# 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"):


from frappe.model.document import Document
from frappe import _
from io import BytesIO
from jasper_erpnext_report.utils.file import get_file, get_html_reports_images_path
from jasper_erpnext_report.utils.file import JasperXmlReport, get_html_reports_path
		from distutils.dir_util import copy_tree
		from jasper_erpnext_report.utils.utils import getFrappeVersion
import frappe
import uuid, os
import jasper_erpnext_report.utils.utils as utils
							import re
		import hashlib
			import frappe.utils.email_lib
			import frappe.email
	def get_html_path(self, report_name, localsite=None, content=None):
		site = localsite or frappe.local.site
		if not self.html_hash:
			hash_obj = hashlib.md5(frappe.utils.encode(content))
			self.html_hash = hash_obj.hexdigest()
		self.report_html_path = get_html_reports_path(report_name, localsite=site, hash=self.html_hash)
		return self.report_html_path

	def save_html_cache(self, report_name, reportPath):
		name = "client_html_" + report_name.replace(" ", "_")
		new_data = frappe._dict({'data': {}})
		data = utils.get_jasper_data(name, get_from_db=self.get_session_from_db, tab="tabJasperClientHtmlDocs")
		if data:
			rp = data['data']['reportPath']
			if rp == reportPath:
				data['data']['hash'] = self.html_hash
				utils.update_list_all_memcache_db(data, cachename=name)
				return
		new_data['data']['reportPath'] = reportPath
		new_data['data']['hash'] = self.html_hash
		utils.insert_list_all_memcache_db(new_data['data'], cachename=name)

	def copy_images(self, content, outputPath, fileName, report_name, localsite):

		src = fileName + "." + "html_files"
		html_files = os.path.join(outputPath, src)
		#this is a report without folder html_files
		if not os.path.exists(html_files):
			return
		report_path = self.get_html_path(report_name, localsite=localsite, content=content)

# Path: jasper_erpnext_report/utils/file.py
# def get_file(path, modes="r", raise_not_found=False):
# 	if modes != "r":
# 		content = read_file(path, modes=modes, raise_not_found=raise_not_found)
# 	else:
# 		content = frappe.read_file(path, raise_not_found=raise_not_found)
# 
# 	return content
# 
# def get_html_reports_images_path(report_path, where="images"):
# 	path = os.path.join(report_path, where)
# 	frappe.create_folder(path)
# 	return path
# 
# 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/core/JasperBase.py
from frappe.model.document import Document
from frappe import _
from io import BytesIO
from jasper_erpnext_report.utils.file import get_file, get_html_reports_images_path
from jasper_erpnext_report.utils.file import JasperXmlReport, get_html_reports_path
		from distutils.dir_util import copy_tree
		from jasper_erpnext_report.utils.utils import getFrappeVersion
import frappe
import uuid, os
import jasper_erpnext_report.utils.utils as utils
							import re
		import hashlib
			import frappe.utils.email_lib
			import frappe.email

	def get_html_path(self, report_name, localsite=None, content=None):
		site = localsite or frappe.local.site
		if not self.html_hash:
			hash_obj = hashlib.md5(frappe.utils.encode(content))
			self.html_hash = hash_obj.hexdigest()
		self.report_html_path = get_html_reports_path(report_name, localsite=site, hash=self.html_hash)
		return self.report_html_path

	def save_html_cache(self, report_name, reportPath):
		name = "client_html_" + report_name.replace(" ", "_")
		new_data = frappe._dict({'data': {}})
		data = utils.get_jasper_data(name, get_from_db=self.get_session_from_db, tab="tabJasperClientHtmlDocs")
		if data:
			rp = data['data']['reportPath']
			if rp == reportPath:
				data['data']['hash'] = self.html_hash
				utils.update_list_all_memcache_db(data, cachename=name)
				return
		new_data['data']['reportPath'] = reportPath
		new_data['data']['hash'] = self.html_hash
		utils.insert_list_all_memcache_db(new_data['data'], cachename=name)

	def copy_images(self, content, outputPath, fileName, report_name, localsite):

		src = fileName + "." + "html_files"
		html_files = os.path.join(outputPath, src)
		#this is a report without folder html_files
		if not os.path.exists(html_files):
			return
		report_path = self.get_html_path(report_name, localsite=localsite, content=content)

		dst = get_html_reports_images_path(report_path, where=src)