<commit_before># vim: tabstop=4 noexpandtab shiftwidth=4 softtabstop=4
from django.core.exceptions import ObjectDoesNotExist
import api.models as models
from django.http import HttpResponse
from django.db   import IntegrityError
from giscanner.girparser import GIRParser
import giscanner.ast as ast

GIR_PATH="/usr/share/gir-1.0/%s.gir"

"""
AST_TYPE_MAPPINGS = {
}
"""

def _store_props(db_obj, ast_obj, ast_classes):
	""" Utility to map storage and ast properties """
	for ast_class in ast_classes:
		for prop in models.PROPERTY_MAPPINS[ast_class]:
			if getattr(ast_obj, prop) == None:
				continue
			setattr(db_obj, prop, getattr(ast_obj, prop))


def _store_member(node, parent):
	is_bitfield = isinstance(parent, models.Bitfield)
	try:
		if is_bitfield:
			db_member = models.Member.objects.get(name=node.name, bitfield=parent)
		else:
			db_member = models.Member.objects.get(name=node.name, enum=parent)
		return db_member
	except ObjectDoesNotExist:
		pass

	db_member = models.Member()
	_store_props (db_member, node, (ast.Annotated,ast.Member))

	if is_bitfield:
		db_member.bitfield = parent
	else:
		db_member.enum = parent

	db_member.save()

	return db_member

def _store_type(node):
	if isinstance(node, ast.TypeUnknown):
		model = models.TypeUnknown
		props = (ast.Type,)
	if isinstance(node, ast.Varargs):
		model = models.Varargs
		props = (ast.Type,)
	if isinstance(node, ast.Array):
		model = models.Array
		props = (ast.Type, ast.Array)
	if isinstance(node, ast.List):
		model = models.List
		props = (ast.Type, ast.List)
	if isinstance(node, ast.Map):
		model = models.Map
		props = (ast.Type,)
	else:
		model = models.Type
		props = (ast.Type,)
	
	try:
		return model.objects.get (ctype=node.ctype)
	except ObjectDoesNotExist:
		pass

	db_type = model()
	_store_props (db_type, node, props)

	if isinstance (node, ast.Array):
		db_type.element_type = _store_type (node.element_type)
	if isinstance(node, ast.List):
		db_type.element_type = _store_type (node.element_type)
	if isinstance(node, ast.Map):
		db_type.key_type   = _store_type (node.key_type)
		db_type.value_type = _store_type (node.value_type)

	db_type.save()
	return db_type

def _store_enum_generic (node, is_bitfield=False):
	db_ns = _store_namespace(node.namespace)

	if is_bitfield:
		model = models.Bitfield
	else:
		model = models.Enum

	try:
		db_enum = model.objects.get(namespace = db_ns, name = node.name)
		return db_enum
	except ObjectDoesNotExist:
		pass

	db_enum = model()
	db_enum.namespace = db_ns
	_store_props (db_enum, node, (ast.Annotated, ast.Node, ast.Registered))
	if is_bitfield:
		_store_props(db_enum, node, (ast.Bitfield,))
	else:
		_store_props(db_enum, node, (ast.Enum,))
	db_enum.save()

	for member in node.members:
		_store_member(member, db_enum)

	#TODO: static_methods

def _store_enum (node):
	return _store_enum_generic (node, False)

def _store_bitfield (node):
	return _store_enum_generic (node, True)

def _store_alias (node):
	db_ns = _store_namespace(node.namespace)
	try:
		db_alias = models.Alias.objects.get(namespace = db_ns, name = node.name)
		return db_alias
	except ObjectDoesNotExist:
		pass

	db_alias = models.Alias()
	db_alias.namespace = db_ns
	_store_props (db_alias, node, (ast.Annotated, ast.Node, ast.Alias))
	db_alias.target = _store_type (node.target)
	db_alias.save()
	return db_alias

def _store_retval(node):
	db_ret = models.Return()
	_store_props (db_ret, node, (ast.TypeContainer, ast.Annotated))
	db_ret.type = _store_type(node.type)
	db_ret.save()
	return db_ret

def _store_param (node):
	db_param = models.Parameter()
	db_param.type = _store_type (node.type)
	print node.scope.__class__
	print node.caller_allocates.__class__
	_store_props (db_param, node, (ast.Parameter, ast.TypeContainer, ast.Annotated))
	db_param.save()
	return db_param

def _store_function(node, parent=None):
	db_ns = _store_namespace(node.namespace)
	try:
		return models.Function.objects.get(namespace = db_ns, name = node.name)
	except ObjectDoesNotExist:
		pass

	db_func = models.Function()
	_store_props (db_func, node, (ast.Node, ast.Callable, ast.Function))
	db_func.namespace = db_ns
	db_func.retval = _store_retval (node.retval)
	db_func.method_of=parent
	db_func.instance_parameter = -1
	db_func.save()

	i = 0
	for param in node.parameters:
		#TODO: Instance parameter<commit_msg>Add instance parameter value if found<commit_after># vim: tabstop=4 noexpandtab shiftwidth=4 softtabstop=4
from django.core.exceptions import ObjectDoesNotExist
import api.models as models
from django.http import HttpResponse
from django.db   import IntegrityError
from giscanner.girparser import GIRParser
import giscanner.ast as ast

GIR_PATH="/usr/share/gir-1.0/%s.gir"

"""
AST_TYPE_MAPPINGS = {
}
"""

def _store_props(db_obj, ast_obj, ast_classes):
	""" Utility to map storage and ast properties """
	for ast_class in ast_classes:
		for prop in models.PROPERTY_MAPPINS[ast_class]:
			if getattr(ast_obj, prop) == None:
				continue
			setattr(db_obj, prop, getattr(ast_obj, prop))


def _store_member(node, parent):
	is_bitfield = isinstance(parent, models.Bitfield)
	try:
		if is_bitfield:
			db_member = models.Member.objects.get(name=node.name, bitfield=parent)
		else:
			db_member = models.Member.objects.get(name=node.name, enum=parent)
		return db_member
	except ObjectDoesNotExist:
		pass

	db_member = models.Member()
	_store_props (db_member, node, (ast.Annotated,ast.Member))

	if is_bitfield:
		db_member.bitfield = parent
	else:
		db_member.enum = parent

	db_member.save()

	return db_member

def _store_type(node):
	if isinstance(node, ast.TypeUnknown):
		model = models.TypeUnknown
		props = (ast.Type,)
	if isinstance(node, ast.Varargs):
		model = models.Varargs
		props = (ast.Type,)
	if isinstance(node, ast.Array):
		model = models.Array
		props = (ast.Type, ast.Array)
	if isinstance(node, ast.List):
		model = models.List
		props = (ast.Type, ast.List)
	if isinstance(node, ast.Map):
		model = models.Map
		props = (ast.Type,)
	else:
		model = models.Type
		props = (ast.Type,)
	
	try:
		return model.objects.get (ctype=node.ctype)
	except ObjectDoesNotExist:
		pass

	db_type = model()
	_store_props (db_type, node, props)

	if isinstance (node, ast.Array):
		db_type.element_type = _store_type (node.element_type)
	if isinstance(node, ast.List):
		db_type.element_type = _store_type (node.element_type)
	if isinstance(node, ast.Map):
		db_type.key_type   = _store_type (node.key_type)
		db_type.value_type = _store_type (node.value_type)

	db_type.save()
	return db_type

def _store_enum_generic (node, is_bitfield=False):
	db_ns = _store_namespace(node.namespace)

	if is_bitfield:
		model = models.Bitfield
	else:
		model = models.Enum

	try:
		db_enum = model.objects.get(namespace = db_ns, name = node.name)
		return db_enum
	except ObjectDoesNotExist:
		pass

	db_enum = model()
	db_enum.namespace = db_ns
	_store_props (db_enum, node, (ast.Annotated, ast.Node, ast.Registered))
	if is_bitfield:
		_store_props(db_enum, node, (ast.Bitfield,))
	else:
		_store_props(db_enum, node, (ast.Enum,))
	db_enum.save()

	for member in node.members:
		_store_member(member, db_enum)

	#TODO: static_methods

def _store_enum (node):
	return _store_enum_generic (node, False)

def _store_bitfield (node):
	return _store_enum_generic (node, True)

def _store_alias (node):
	db_ns = _store_namespace(node.namespace)
	try:
		db_alias = models.Alias.objects.get(namespace = db_ns, name = node.name)
		return db_alias
	except ObjectDoesNotExist:
		pass

	db_alias = models.Alias()
	db_alias.namespace = db_ns
	_store_props (db_alias, node, (ast.Annotated, ast.Node, ast.Alias))
	db_alias.target = _store_type (node.target)
	db_alias.save()
	return db_alias

def _store_retval(node):
	db_ret = models.Return()
	_store_props (db_ret, node, (ast.TypeContainer, ast.Annotated))
	db_ret.type = _store_type(node.type)
	db_ret.save()
	return db_ret

def _store_param (node):
	db_param = models.Parameter()
	db_param.type = _store_type (node.type)
	_store_props (db_param, node, (ast.Parameter, ast.TypeContainer, ast.Annotated))
	db_param.save()
	return db_param

def _store_function(node, parent=None):
	db_ns = _store_namespace(node.namespace)
	try:
		return models.Function.objects.get(namespace = db_ns, name = node.name)
	except ObjectDoesNotExist:
		pass

	db_func = models.Function()
	_store_props (db_func, node, (ast.Node, ast.Callable, ast.Function))
	db_func.namespace = db_ns
	db_func.retval = _store_retval (node.retval)
	db_func.method_of=parent
	db_func.instance_parameter = -1
	db_func.save()

	i = 0
	for param in node.parameters:
		if param is node.instance_parameter:
			db_func.instance_parameter = i
{'fraction_non_alphanumeric': 0.11064911064911065, 'fraction_numerical': 0.001617001617001617, 'mean_word_length': 4.862559241706161, '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': '1717142', 'n_tokens_mistral': 3332, 'n_tokens_neox': 3180, 'n_words': 707}