<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2014-2017 <NAME> (<EMAIL>)
#
# This file is part of libSigNetSim.
#
# libSigNetSim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libSigNetSim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libSigNetSim.  If not, see <http://www.gnu.org/licenses/>.

""" HasErrorMessages.py

	This file ...

"""

from libsignetsim import MathFormula

from re import match

class HasErrorMessages(object):

	def __init__(self):
		self.nbErrors = 0
		self.errorMessages = []
		self.errorFields = []


	def get_context_data(self, **kwargs):
		kwargs['hasErrors'] = self.hasErrors()
		kwargs['getErrors'] = self.getErrors()
		kwargs['errorFields'] = self.errorFields
		return kwargs


	def clearErrors(self):
		self.nbErrors = 0
		self.errorMessages = []


	def addError(self, message, reportField=False, field=None):
		self.nbErrors += 1
		self.errorMessages.append(message)
		if reportField and field is not None:
			self.errorFields.append(field)


	def hasErrors(self):
		return self.nbErrors > 0

	def getErrors(self):
		return self.errorMessages

	def printErrors(self):
		for error in self.errorMessages:
			print(error)


	def findMathErrors(self, expression):

		try:
			t_formula = MathFormula(self.parent.model)
			t_formula.setPrettyPrintMathFormula(expression)
			return None

		except Exception as e:
			return e.message


	def existField(self, request, field):

		if request.POST.get(field) is None or str(request.POST[field]) == "":
			return False
		else:
			return True



	def readString(self, request, field, name, required=True, reportField=True):

		try:
			if request.POST.get(field) is None:
				if required:
					self.addError("%s does not exist !" % name, reportField, field)

			elif str(request.POST[field]) == "":
				if required:
					self.addError("%s is required !" % name, reportField, field)

			else:
				return str(request.POST[field])
		except UnicodeEncodeError:
			self.addError("Unauthorized special characters (non-utf8) in %s" % name)


	def readUnicodeString(self, request, field, name, required=True, reportField=True):

		if request.POST.get(field) is None:
			self.addError("%s does not exist !" % name, reportField, field)

		elif unicode(request.POST[field]) == "":
			if required:
				self.addError("%s is required !" % name, reportField, field)

		else:
			return unicode(request.POST[field])

	def readInt(self, request, field, name, max_value=None, required=True, reportField=True):

		if request.POST.get(field) is None:
			if required:
				self.addError("%s does not exist !" % name, reportField, field)

		elif str(request.POST[field]) == "":<commit_msg>readUnicode now has special version for python3<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2014-2017 <NAME> (<EMAIL>)
#
# This file is part of libSigNetSim.
#
# libSigNetSim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libSigNetSim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libSigNetSim.  If not, see <http://www.gnu.org/licenses/>.

""" HasErrorMessages.py

	This file ...

"""

from libsignetsim import MathFormula
from sys import version_info
from re import match

class HasErrorMessages(object):

	def __init__(self):
		self.nbErrors = 0
		self.errorMessages = []
		self.errorFields = []


	def get_context_data(self, **kwargs):
		kwargs['hasErrors'] = self.hasErrors()
		kwargs['getErrors'] = self.getErrors()
		kwargs['errorFields'] = self.errorFields
		return kwargs


	def clearErrors(self):
		self.nbErrors = 0
		self.errorMessages = []


	def addError(self, message, reportField=False, field=None):
		self.nbErrors += 1
		self.errorMessages.append(message)
		if reportField and field is not None:
			self.errorFields.append(field)


	def hasErrors(self):
		return self.nbErrors > 0

	def getErrors(self):
		return self.errorMessages

	def printErrors(self):
		for error in self.errorMessages:
			print(error)


	def findMathErrors(self, expression):

		try:
			t_formula = MathFormula(self.parent.model)
			t_formula.setPrettyPrintMathFormula(expression)
			return None

		except Exception as e:
			return e.message


	def existField(self, request, field):

		if request.POST.get(field) is None or str(request.POST[field]) == "":
			return False
		else:
			return True



	def readString(self, request, field, name, required=True, reportField=True):

		try:
			if request.POST.get(field) is None:
				if required:
					self.addError("%s does not exist !" % name, reportField, field)

			elif str(request.POST[field]) == "":
				if required:
					self.addError("%s is required !" % name, reportField, field)

			else:
				return str(request.POST[field])
		except UnicodeEncodeError:
			self.addError("Unauthorized special characters in %s" % name)


	def readUnicodeString(self, request, field, name, required=True, reportField=True):

		if request.POST.get(field) is None:
			self.addError("%s does not exist !" % name, reportField, field)

		elif (version_info[0] >= 3 and request.POST[field] == "") or unicode(request.POST[field]) == "":
			if required:
				self.addError("%s is required !" % name, reportField, field)

		else:
			if version_info[0] >= 3:
				return request.POST['field']
			else:
				return unicode(request.POST[field])

	def readInt(self, request, field, name, max_value=None, required=True, reportField=True):

		if request.POST.get(field) is None:
			if required:
				self.addError("%s does not exist !" % name, reportField, field)

		elif str(request.POST[field]) == "":
{'fraction_non_alphanumeric': 0.11372854914196567, 'fraction_numerical': 0.005304212168486739, 'mean_word_length': 4.1124401913875595, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '2959169', 'n_tokens_mistral': 2215, 'n_tokens_neox': 1929, 'n_words': 628}