<commit_before>import json
import sys
from datetime import date
from xmlrpc import client
from ..utility import catch
import copy
	
class DateEncoder(json.JSONEncoder):
	def default (self, obj):
		if isinstance (obj, date):
			return str (obj)			
		return json.JSONEncoder.default (self, obj)

class API:
	def __init__ (self, request, data = None):
		self.request = request # for response typing 
		self.data = data or {}
		self.content_type = self.set_content_type ()
		
	def __setitem__ (self, k, v):
		self.set (k, v)
	
	def __getitem__ (self, k, v = None):
		return self.get (k, v)
	
	def set (self, k, v):
		self.data [k] = v
	
	def get (self, k, v = None):
		return self.data.get (k, )
	
	def get_content_type (self):
		return self.content_type
		
	def set_content_type (self):
		content_type = self.request.get_header ("accept", 'application/json')
		if not content_type.startswith ("text/xml"):
			content_type = 'application/json'
		return content_type	
		
	def encode (self, charset):
		return self.to_string ().encode (charset)
	
	def __str__ (self):
		return self.to_string ()
		
	def to_string (self):
		if self.content_type.startswith ("text/xml"):
			return client.dumps ((self.data,))
		return json.dumps (self.data, cls = DateEncoder, ensure_ascii = False, indent = 2)
			
	def traceback (self, message = 'exception occured', code = 20000, debug = 'see traceback', more_info = None):
		self.error (message, code, debug, more_info, sys.exc_info ())
		
	def error (self, message, code = 20000, debug = None, more_info = None, exc_info = None):
		self.data = {}
		self.data ['code'] = code
		self.data ['message'] = message
		if debug:
			self.data ['debug'] = debug
		if more_info:
			self.data ['more_info'] = more_info
		if exc_info:
			self.data ["traceback"] = type (exc_info) is tuple and catch (2, exc_info) or exc_info

	def set_spec (self, app):		
		module_id, mount_option, resource_id, route, parameter_requirements = app.get_resource_spec (self.request.routed)	
		routable = copy.deepcopy (self.request.routable)
		routable ["methods"] = list (routable ["methods"])
		self.data ["__spec__"] = {
			"endpoint": self.request.uri,
			"route": route,
			"routable": routable,
			"resource_id": resource_id,
			"module_id": module_id,
			"parameter_requirements": parameter_requirements,
			"doc": self.request.routed.__doc__,
			"mount_option": mount_option,			
		}
		#print (self.data ["__spec__"])<commit_msg>chg API.set_spec<commit_after>import json
import sys
from datetime import date
from xmlrpc import client
from ..utility import catch
import copy
	
class DateEncoder(json.JSONEncoder):
	def default (self, obj):
		if isinstance (obj, date):
			return str (obj)			
		return json.JSONEncoder.default (self, obj)

class API:
	def __init__ (self, request, data = None):
		self.request = request # for response typing 
		self.data = data or {}
		self.content_type = self.set_content_type ()
		
	def __setitem__ (self, k, v):
		self.set (k, v)
	
	def __getitem__ (self, k, v = None):
		return self.get (k, v)
	
	def set (self, k, v):
		self.data [k] = v
	
	def get (self, k, v = None):
		return self.data.get (k, )
	
	def get_content_type (self):
		return self.content_type
		
	def set_content_type (self):
		content_type = self.request.get_header ("accept", 'application/json')
		if not content_type.startswith ("text/xml"):
			content_type = 'application/json'
		return content_type	
		
	def encode (self, charset):
		return self.to_string ().encode (charset)
	
	def __str__ (self):
		return self.to_string ()
		
	def to_string (self):
		if self.content_type.startswith ("text/xml"):
			return client.dumps ((self.data,))
		return json.dumps (self.data, cls = DateEncoder, ensure_ascii = False, indent = 2)
			
	def traceback (self, message = 'exception occured', code = 20000, debug = 'see traceback', more_info = None):
		self.error (message, code, debug, more_info, sys.exc_info ())
		
	def error (self, message, code = 20000, debug = None, more_info = None, exc_info = None):
		self.data = {}
		self.data ['code'] = code
		self.data ['message'] = message
		if debug:
			self.data ['debug'] = debug
		if more_info:
			self.data ['more_info'] = more_info
		if exc_info:
			self.data ["traceback"] = type (exc_info) is tuple and catch (2, exc_info) or exc_info

	def set_spec (self, app):		
		resource_id = self.request.routable ["func_id"]
		routable = copy.deepcopy (self.request.routable)
		routable ["methods"] = list (routable ["methods"])		
		self.data ["__spec__"] = {
			"http_method": self.request.method,
			"http_version": self.request.version,
			"uri": self.request.uri,			
			"resource_id": resource_id,
			"routable": routable,			
			"parameter_requirements": app.get_parameter_requirements (resource_id),
			'auth_requirements': app.get_auth_flags (resource_id),
			"doc": self.request.routed.__doc__
		}
		#print (self.data ["__spec__"])
{'fraction_non_alphanumeric': 0.1317083162638263, 'fraction_numerical': 0.004916018025399427, 'mean_word_length': 3.9223790322580645, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1838163', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1657, 'n_words': 480}