<commit_before># -*- coding: utf-8 -*-
##j## BOF

"""
direct PAS
Python Application Services
----------------------------------------------------------------------------
(C) direct Netware Group - All rights reserved
https://www.direct-netware.de/redirect?pas;http;table

This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------------------------
https://www.direct-netware.de/redirect?licenses;mpl2
----------------------------------------------------------------------------
#echo(pasHttpTableVersion)#
#echo(__FILEPATH__)#
"""

from binascii import hexlify
from math import ceil, floor
from os import urandom

from dNG.data.xml_parser import XmlParser
from dNG.pas.data.binary import Binary
from dNG.pas.data.http.translatable_exception import TranslatableException
from dNG.pas.data.xhtml.formatting import Formatting as XHtmlFormatting
from dNG.pas.data.xhtml.page_links_renderer import PageLinksRenderer
from dNG.pas.data.xhtml.link import Link
from dNG.pas.data.xhtml.oset.file_parser import FileParser
from dNG.pas.data.xhtml.table.abstract import Abstract
from dNG.pas.runtime.type_exception import TypeException
from .module import Module

class Table(Module):
#
	"""
"Table" is used to render a table of rows of columns.

:author:     direct Netware Group
:copyright:  (C) direct Netware Group - All rights reserved
:package:    pas.http
:subpackage: table
:since:      v0.1.00
:license:    https://www.direct-netware.de/redirect?licenses;mpl2
             Mozilla Public License, v. 2.0
	"""

	def __init__(self):
	#
		"""
Constructor __init__(Table)

:since: v0.1.00
		"""

		Module.__init__(self)

		self.dsd_page_key = "page"
		"""
DSD key used for pagination
		"""
		self.dsd_sort_key = "sort"
		"""
DSD key used for sorting
		"""
		self.page = 1
		"""
Page currently rendered
		"""
		self.page_link_cell = None
		"""
Rendered page link cell
		"""
		self.pages = 1
		"""
Total number of pages available
		"""
		self.sort_direction = None
		"""
Direction of the current sort value
		"""
		self.sort_column_key = ""
		"""
Column key of the current sort value
		"""
		self.table = None
		"""
Table instance
		"""
	#

	def _execute_cell_oset_renderer(self, row, column_definition):
	#
		"""
Renders a cell using the defined OSet template.
		"""

		row_attributes = column_definition['renderer'].get("oset_row_attributes")
		template_name = column_definition['renderer'].get("oset_template_name")

		if (not isinstance(row_attributes, list)): row_attributes = [ column_definition['key'] ]

		content = { }
		for row_attribute in row_attributes: content[row_attribute] = row[row_attribute]

		parser = FileParser()
		parser.set_oset(self.response.get_oset())
		return parser.render(template_name, content)
	#

	def execute_render(self):
	#
		"""
Action for "render_subs"

:since: v0.1.00
		"""

		table = self.context.get("table")
		if (not isinstance(table, Abstract)): raise TranslatableException("core_unknown_error", value = "Missing table instance to render")

		if ("dsd_page_key" in self.context): self.dsd_page_key = self.context['dsd_page_key']
		if ("dsd_sort_key" in self.context): self.dsd_sort_key = self.context['dsd_sort_key']
		self.table = table

		table_id = self.context.get("id")
		self.table_id = ("pas_table_{0}".format(Binary.str(hexlify(urandom(10)))) if (table_id is None) else table_id)

		limit = self.table.get_limit()
		row_count = self.table.get_row_count()

		self.page = (self.context['page'] if ("page" in self.context) else 1)
		self.pages = (1 if (row_count == 0) else ceil(float(row_count) / limit))

		self.table.set_offset(0 if (self.page < 1 or self.page > self.pages) else (self.page - 1) * limit)

		sort_value = self.context.get("sort_value", "")

		if (sort_value != ""):
		#
			self.sort_direction = sort_value[-1:]
			self.sort_column_key = sort_value[:-1]

			self.table.add_sort_definition(self.sort_column_key, self.sort_direction)
		#

		rendered_content = self._render_table_header()
		rendered_content += self._render_table_rows()
		rendered_content += self._render_table_footer()

		self.set_action_result(rendered_content)
	#

	def _get_sort_value(self, column_key):
	#
		"""
Returns the value used to sort the column based on the current one.

:return: (str) Sort value
:since:  v0.1.02
		"""

		_return = ""
<commit_msg>Enhancement: Added sprite for table sort direction indication<commit_after># -*- coding: utf-8 -*-
##j## BOF

"""
direct PAS
Python Application Services
----------------------------------------------------------------------------
(C) direct Netware Group - All rights reserved
https://www.direct-netware.de/redirect?pas;http;table

This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------------------------
https://www.direct-netware.de/redirect?licenses;mpl2
----------------------------------------------------------------------------
#echo(pasHttpTableVersion)#
#echo(__FILEPATH__)#
"""

from binascii import hexlify
from math import ceil, floor
from os import urandom

from dNG.data.xml_parser import XmlParser
from dNG.pas.data.binary import Binary
from dNG.pas.data.http.translatable_exception import TranslatableException
from dNG.pas.data.xhtml.formatting import Formatting as XHtmlFormatting
from dNG.pas.data.xhtml.page_links_renderer import PageLinksRenderer
from dNG.pas.data.xhtml.link import Link
from dNG.pas.data.xhtml.oset.file_parser import FileParser
from dNG.pas.data.xhtml.table.abstract import Abstract
from dNG.pas.runtime.type_exception import TypeException
from .module import Module

class Table(Module):
#
	"""
"Table" is used to render a table of rows of columns.

:author:     direct Netware Group
:copyright:  (C) direct Netware Group - All rights reserved
:package:    pas.http
:subpackage: table
:since:      v0.1.00
:license:    https://www.direct-netware.de/redirect?licenses;mpl2
             Mozilla Public License, v. 2.0
	"""

	def __init__(self):
	#
		"""
Constructor __init__(Table)

:since: v0.1.00
		"""

		Module.__init__(self)

		self.dsd_page_key = "page"
		"""
DSD key used for pagination
		"""
		self.dsd_sort_key = "sort"
		"""
DSD key used for sorting
		"""
		self.page = 1
		"""
Page currently rendered
		"""
		self.page_link_cell = None
		"""
Rendered page link cell
		"""
		self.pages = 1
		"""
Total number of pages available
		"""
		self.sort_direction = None
		"""
Direction of the current sort value
		"""
		self.sort_column_key = ""
		"""
Column key of the current sort value
		"""
		self.table = None
		"""
Table instance
		"""
	#

	def _execute_cell_oset_renderer(self, row, column_definition):
	#
		"""
Renders a cell using the defined OSet template.
		"""

		row_attributes = column_definition['renderer'].get("oset_row_attributes")
		template_name = column_definition['renderer'].get("oset_template_name")

		if (not isinstance(row_attributes, list)): row_attributes = [ column_definition['key'] ]

		content = { }
		for row_attribute in row_attributes: content[row_attribute] = row[row_attribute]

		parser = FileParser()
		parser.set_oset(self.response.get_oset())
		return parser.render(template_name, content)
	#

	def execute_render(self):
	#
		"""
Action for "render_subs"

:since: v0.1.00
		"""

		table = self.context.get("table")
		if (not isinstance(table, Abstract)): raise TranslatableException("core_unknown_error", value = "Missing table instance to render")

		if ("dsd_page_key" in self.context): self.dsd_page_key = self.context['dsd_page_key']
		if ("dsd_sort_key" in self.context): self.dsd_sort_key = self.context['dsd_sort_key']
		self.table = table

		table_id = self.context.get("id")
		self.table_id = ("pas_table_{0}".format(Binary.str(hexlify(urandom(10)))) if (table_id is None) else table_id)

		limit = self.table.get_limit()
		row_count = self.table.get_row_count()

		self.page = self.context.get("page", 1)
		self.pages = (1 if (row_count == 0) else ceil(float(row_count) / limit))

		self.table.set_offset(0 if (self.page < 1 or self.page > self.pages) else (self.page - 1) * limit)

		sort_value = self.context.get("sort_value", "")

		if (sort_value != ""):
		#
			self.sort_direction = sort_value[-1:]
			self.sort_column_key = sort_value[:-1]

			self.table.add_sort_definition(self.sort_column_key, self.sort_direction)
		#

		if (self.response.is_supported("html_css_files")): self.response.add_theme_css_file("table_sprite.min.css")

		rendered_content = self._render_table_header()
		rendered_content += self._render_table_rows()
		rendered_content += self._render_table_footer()

		self.set_action_result(rendered_content)
	#

	def _get_sort_value(self, column_key):
	#
		"""
Returns the value used to sort the column based on the current one.

:return: (str) Sort value
:since:  v0.1.02
		"""

		_return = ""

{'fraction_non_alphanumeric': 0.1680052811090329, 'fraction_numerical': 0.008361755968753439, 'mean_word_length': 3.3849493487698985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883265', 'n_tokens_mistral': 3135, 'n_tokens_neox': 2887, 'n_words': 721}