# Path: tracer/resources/rules.py
# class Rules(object):
# 
# 	DEFINITIONS = map(lambda x: x + "/rules.xml", [DATA_DIR] + USER_CONFIG_DIRS)
# 
# 	ACTIONS = {
# 		"CALL-PARENT"  :  "call-parent",
# 		"RETURN"        :  "return",
# 	}
# 	_DEFAULT_ACTION = ACTIONS["CALL-PARENT"]
# 	_rules = None
# 
# 	@staticmethod
# 	def find(app_name):
# 		if not Rules._rules:
# 			Rules._load_definitions()
# 
# 		for rule in Rules._rules:
# 			if rule.name == app_name:
# 				return rule
# 
# 	@staticmethod
# 	def all():
# 		if not Rules._rules:
# 			Rules._load_definitions()
# 
# 		return Rules._rules
# 
# 	@staticmethod
# 	def _load_definitions():
# 		Rules._rules = []
# 		for file in Rules.DEFINITIONS:
# 			try: Rules._load(file);
# 			except PathNotFound as ex:
# 				if not dirname(file) in USER_CONFIG_DIRS:
# 					raise ex
# 
# 	@staticmethod
# 	def _load(file):
# 		try:
# 			with open(file, "r") as f:
# 				xmldoc = minidom.parseString(f.read())
# 		except IOError:
# 			raise PathNotFound('DATA_DIR')
# 		except ExpatError as ex:
# 			msg = "Unable to parse {0}\nHint: {1}".format(file, ex)
# 			raise TracerError(msg)
# 
# 		for rules in xmldoc.getElementsByTagName("rules"):
# 			for rule in rules.getElementsByTagName("rule"):
# 				attrs = dict(rule.attributes.items())
# 				r = Rule(attrs)
# 				if r in Rules._rules:
# 					i = Rules._rules.index(r)
# 					Rules._rules[i].update(r)
# 				else:
# 					r.setdefault('action', Rules._DEFAULT_ACTION)
# 					Rules._rules.append(r)
# 
# class Rule(object):
# 
# 	"""
# 	Represent the rule defined in `rules.xml`
# 
# 	Attributes
# 	----------
# 	name : str
# 	action : str
# 		See `Rules.ACTIONS` for possible values
# 	"""
# 
# 	_attributes = None
# 
# 	def __init__(self, attributes_dict):
# 		self._attributes = attributes_dict
# 
# 	def __eq__(self, other):
# 		return isinstance(other, Rule) and self.name == other.name
# 
# 	def __ne__(self, other):
# 		return not self.__eq__(other)
# 
# 	def __getattr__(self, item):
# 		return self._attributes[item]
# 
# 	def __len__(self):
# 		return len(self._attributes)
# 
# 	def __contains__(self, item):
# 		return item in self._attributes
# 
# 	def __str__(self):
# 		return "<Rule: " + self._attributes["name"] + ">"
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# 	def setdefault(self, key, value):
# 		self._attributes.setdefault(key, value)
# 
# 	def update(self, values):
# 		if isinstance(values, Rule):
# 			values = values._attributes
# 		self._attributes.update(values)


from .__meta__ import *
from tracer.paths import DATA_DIR
from tracer.resources.rules import Rules, Rule
	from unittest.mock import patch, mock_open
	from mock import patch, mock_open

try:
	builtins_open = "builtins.open"
except:
	builtins_open = "__builtin__.open"


class TestRules(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		cls.DEFINITIONS = [x for x in Rules.DEFINITIONS
						   if x.startswith(DATA_DIR)]

	def setUp(self):
		Rules.DEFINITIONS = self.DEFINITIONS
		Rules._rules = None

	def test_rules_types(self):
		for rule in Rules.all():
			self.assertIsInstance(rule, Rule)

	def test_rules_attributes(self):
		i = 1
		for r in Rules.all():
			if ("name" not in r) or ("action" not in r):
				self.fail("Missing attributes in rule #" + str(i))


# Path: tracer/resources/rules.py
# class Rules(object):
# 
# 	DEFINITIONS = map(lambda x: x + "/rules.xml", [DATA_DIR] + USER_CONFIG_DIRS)
# 
# 	ACTIONS = {
# 		"CALL-PARENT"  :  "call-parent",
# 		"RETURN"        :  "return",
# 	}
# 	_DEFAULT_ACTION = ACTIONS["CALL-PARENT"]
# 	_rules = None
# 
# 	@staticmethod
# 	def find(app_name):
# 		if not Rules._rules:
# 			Rules._load_definitions()
# 
# 		for rule in Rules._rules:
# 			if rule.name == app_name:
# 				return rule
# 
# 	@staticmethod
# 	def all():
# 		if not Rules._rules:
# 			Rules._load_definitions()
# 
# 		return Rules._rules
# 
# 	@staticmethod
# 	def _load_definitions():
# 		Rules._rules = []
# 		for file in Rules.DEFINITIONS:
# 			try: Rules._load(file);
# 			except PathNotFound as ex:
# 				if not dirname(file) in USER_CONFIG_DIRS:
# 					raise ex
# 
# 	@staticmethod
# 	def _load(file):
# 		try:
# 			with open(file, "r") as f:
# 				xmldoc = minidom.parseString(f.read())
# 		except IOError:
# 			raise PathNotFound('DATA_DIR')
# 		except ExpatError as ex:
# 			msg = "Unable to parse {0}\nHint: {1}".format(file, ex)
# 			raise TracerError(msg)
# 
# 		for rules in xmldoc.getElementsByTagName("rules"):
# 			for rule in rules.getElementsByTagName("rule"):
# 				attrs = dict(rule.attributes.items())
# 				r = Rule(attrs)
# 				if r in Rules._rules:
# 					i = Rules._rules.index(r)
# 					Rules._rules[i].update(r)
# 				else:
# 					r.setdefault('action', Rules._DEFAULT_ACTION)
# 					Rules._rules.append(r)
# 
# class Rule(object):
# 
# 	"""
# 	Represent the rule defined in `rules.xml`
# 
# 	Attributes
# 	----------
# 	name : str
# 	action : str
# 		See `Rules.ACTIONS` for possible values
# 	"""
# 
# 	_attributes = None
# 
# 	def __init__(self, attributes_dict):
# 		self._attributes = attributes_dict
# 
# 	def __eq__(self, other):
# 		return isinstance(other, Rule) and self.name == other.name
# 
# 	def __ne__(self, other):
# 		return not self.__eq__(other)
# 
# 	def __getattr__(self, item):
# 		return self._attributes[item]
# 
# 	def __len__(self):
# 		return len(self._attributes)
# 
# 	def __contains__(self, item):
# 		return item in self._attributes
# 
# 	def __str__(self):
# 		return "<Rule: " + self._attributes["name"] + ">"
# 
# 	def __repr__(self):
# 		return self.__str__()
# 
# 	def setdefault(self, key, value):
# 		self._attributes.setdefault(key, value)
# 
# 	def update(self, values):
# 		if isinstance(values, Rule):
# 			values = values._attributes
# 		self._attributes.update(values)


# Path: tests/test_rules.py
from .__meta__ import *
from tracer.paths import DATA_DIR
from tracer.resources.rules import Rules, Rule
	from unittest.mock import patch, mock_open
	from mock import patch, mock_open


try:
	builtins_open = "builtins.open"
except:
	builtins_open = "__builtin__.open"


class TestRules(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		cls.DEFINITIONS = [x for x in Rules.DEFINITIONS
						   if x.startswith(DATA_DIR)]

	def setUp(self):
		Rules.DEFINITIONS = self.DEFINITIONS
		Rules._rules = None

	def test_rules_types(self):
		for rule in Rules.all():
			self.assertIsInstance(rule, Rule)

	def test_rules_attributes(self):
		i = 1
		for r in Rules.all():
			if ("name" not in r) or ("action" not in r):
				self.fail("Missing attributes in rule #" + str(i))


			if r.action not in Rules.ACTIONS.values():