<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# MIT License. See license.txt 
"""
globals attached to webnotes module
+ some utility functions that should probably be moved
"""

from __future__ import unicode_literals

class _dict(dict):
	"""dict like object that exposes keys as attributes"""
	def __getattr__(self, key):
		return self.get(key)
	def __setattr__(self, key, value):
		self[key] = value
	def __getstate__(self): 
		return self
	def __setstate__(self, d): 
		self.update(d)
	def update(self, d):
		"""update and return self -- the missing dict feature in python"""
		super(_dict, self).update(d)
		return self
	def copy(self):
		return _dict(super(_dict, self).copy())
		
def _(msg):
	"""translate object in current lang, if exists"""
	from webnotes.translate import messages
	return messages.get(lang, {}).get(msg, msg)

def set_user_lang(user, user_language=None):
	global lang, user_lang
	try:
		from startup import lang_list, lang_names
	except ImportError:
		return
	
	if not user_language:
		user_language = conn.get_value("Profile", user, "language")
	if user_language and (user_language.lower() in lang_names):
		lang = lang_names[user_language.lower()]
		user_lang = True

def load_translations(module, doctype, name):
	from webnotes.translate import load_doc_messages
	load_doc_messages(module, doctype, name)

request = form_dict = _dict()
conn = None
_memc = None
form = None
session = None
user = None
incoming_cookies = {}
add_cookies = {} # append these to outgoing request
cookies = {}
response = _dict({'message':'', 'exc':''})
error_log = []
debug_log = []
message_log = []
mute_emails = False
mute_messages = False
test_objects = {}
request_method = None
print_messages = False
user_lang = False
lang = 'en'
in_import = False
in_test = False

# memcache

def cache():
	global _memc
	if not _memc:
		from webnotes.memc import MClient
		_memc = MClient(['localhost:11211'])
	return _memc
		
class DuplicateEntryError(Exception): pass
class ValidationError(Exception): pass
class AuthenticationError(Exception): pass
class PermissionError(Exception): pass
class OutgoingEmailError(ValidationError): pass
class UnknownDomainError(Exception): pass
class SessionStopped(Exception): pass
class MappingMismatchError(ValidationError): pass
class InvalidStatusError(ValidationError): pass
class DoesNotExistError(ValidationError): pass
class MandatoryError(ValidationError): pass
		
def getTraceback():
	import utils
	return utils.getTraceback()

def errprint(msg):
	from utils import cstr
	if not request_method:
		print cstr(msg)

	error_log.append(cstr(msg))

def log(msg):
	if not request_method:
		import conf
		if getattr(conf, "logging", False):
			print repr(msg)
	
	from utils import cstr
	debug_log.append(cstr(msg))

def msgprint(msg, small=0, raise_exception=0, as_table=False):
	def _raise_exception():
		if raise_exception:
			import inspect
			if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
				raise raise_exception, msg
			else:
				raise ValidationError, msg
			conn.rollback()

	if mute_messages:
		_raise_exception()
		return

	from utils import cstr
	if as_table and type(msg) in (list, tuple):<commit_msg>[minor] rollback in msgprint with exception<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# MIT License. See license.txt 
"""
globals attached to webnotes module
+ some utility functions that should probably be moved
"""

from __future__ import unicode_literals

class _dict(dict):
	"""dict like object that exposes keys as attributes"""
	def __getattr__(self, key):
		return self.get(key)
	def __setattr__(self, key, value):
		self[key] = value
	def __getstate__(self): 
		return self
	def __setstate__(self, d): 
		self.update(d)
	def update(self, d):
		"""update and return self -- the missing dict feature in python"""
		super(_dict, self).update(d)
		return self
	def copy(self):
		return _dict(super(_dict, self).copy())
		
def _(msg):
	"""translate object in current lang, if exists"""
	from webnotes.translate import messages
	return messages.get(lang, {}).get(msg, msg)

def set_user_lang(user, user_language=None):
	global lang, user_lang
	try:
		from startup import lang_list, lang_names
	except ImportError:
		return
	
	if not user_language:
		user_language = conn.get_value("Profile", user, "language")
	if user_language and (user_language.lower() in lang_names):
		lang = lang_names[user_language.lower()]
		user_lang = True

def load_translations(module, doctype, name):
	from webnotes.translate import load_doc_messages
	load_doc_messages(module, doctype, name)

request = form_dict = _dict()
conn = None
_memc = None
form = None
session = None
user = None
incoming_cookies = {}
add_cookies = {} # append these to outgoing request
cookies = {}
response = _dict({'message':'', 'exc':''})
error_log = []
debug_log = []
message_log = []
mute_emails = False
mute_messages = False
test_objects = {}
request_method = None
print_messages = False
user_lang = False
lang = 'en'
in_import = False
in_test = False

# memcache

def cache():
	global _memc
	if not _memc:
		from webnotes.memc import MClient
		_memc = MClient(['localhost:11211'])
	return _memc
		
class DuplicateEntryError(Exception): pass
class ValidationError(Exception): pass
class AuthenticationError(Exception): pass
class PermissionError(Exception): pass
class OutgoingEmailError(ValidationError): pass
class UnknownDomainError(Exception): pass
class SessionStopped(Exception): pass
class MappingMismatchError(ValidationError): pass
class InvalidStatusError(ValidationError): pass
class DoesNotExistError(ValidationError): pass
class MandatoryError(ValidationError): pass
		
def getTraceback():
	import utils
	return utils.getTraceback()

def errprint(msg):
	from utils import cstr
	if not request_method:
		print cstr(msg)

	error_log.append(cstr(msg))

def log(msg):
	if not request_method:
		import conf
		if getattr(conf, "logging", False):
			print repr(msg)
	
	from utils import cstr
	debug_log.append(cstr(msg))

def msgprint(msg, small=0, raise_exception=0, as_table=False):
	def _raise_exception():
		if raise_exception:
			conn.rollback()
			import inspect
			if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
				raise raise_exception, msg
			else:
				raise ValidationError, msg

	if mute_messages:
		_raise_exception()
		return

	from utils import cstr
	if as_table and type(msg) in (list, tuple):
{'fraction_non_alphanumeric': 0.09962756052141528, 'fraction_numerical': 0.003414028553693358, 'mean_word_length': 5.419322709163347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5285043', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2008, 'n_words': 622}