Esempio n. 1
0
	def testRemoveNamespace(self):
		"""
		This method tests :func:`foundations.namespace.removeNamespace` definition.
		"""

		self.assertIsInstance(namespace.removeNamespace("Namespace|Attribute"), str)
		self.assertEqual(namespace.removeNamespace("Namespace|Attribute"), "Attribute")
		self.assertEqual(namespace.removeNamespace("Namespace:Attribute", ":"), "Attribute")
		self.assertEqual(namespace.removeNamespace("Namespace|Attribute|Value"), "Value")
		self.assertEqual(namespace.removeNamespace("Namespace|Attribute|Value", rootOnly=True), "Attribute|Value")
Esempio n. 2
0
	def attributeExists(self, attribute, section):
		"""
		This method checks if given attribute exists.

		Usage::

			>>> content = ["[Section A]\\n", "; Comment.\\n", "Attribute 1 = \\"Value A\\"\\n", "\\n", \
"[Section B]\\n", "Attribute 2 = \\"Value B\\"\\n"]
			>>> sectionsFileParser = SectionsFileParser()
			>>> sectionsFileParser.content = content
			>>> sectionsFileParser.parse()
			True
			>>> sectionsFileParser.attributeExists("Attribute 1", "Section A")
			True
			>>> sectionsFileParser.attributeExists("Attribute 2", "Section A")
			False

		:param attribute: Attribute to check existence. ( String )
		:param section: Section to search attribute into. ( String )
		:return: Attribute existence. ( Boolean )
		"""

		if not self.__sections:
			return

		if namespace.removeNamespace(attribute, rootOnly=True) in self.getAttributes(section,
																					orderedDictionary=True,
																					stripNamespaces=True):
			LOGGER.debug("> '{0}' attribute exists in '{1}' section.".format(attribute, section))
			return True
		else:
			LOGGER.debug("> '{0}' attribute doesn't exists in '{1}' section.".format(attribute, section))
			return False
Esempio n. 3
0
	def getAttributes(self, section, orderedDictionary=True, stripNamespaces=False, raiseExceptions=True):
		"""
		This method returns given section attributes.

		Usage::

			>>> content = ["[Section A]\\n", "; Comment.\\n", "Attribute 1 = \\"Value A\\"\\n", "\\n", \
"[Section B]\\n", "Attribute 2 = \\"Value B\\"\\n"]
			>>> sectionsFileParser = SectionsFileParser()
			>>> sectionsFileParser.content = content
			>>> sectionsFileParser.parse()
			True
			>>> sectionsFileParser.getAttributes("Section A")
			OrderedDict([('Section A|Attribute 1', 'Value A')])
			>>> sectionsFileParser.getAttributes("Section A", orderedDictionary=False)
			{'Section A|Attribute 1': 'Value A'}
			>>> sectionsFileParser.getAttributes("Section A", stripNamespaces=True)
			OrderedDict([('Attribute 1', 'Value A')])

		:param section: Section containing the requested attributes. ( String )
		:param orderedDictionary: Use an :class:`collections.OrderedDict` dictionary to store the attributes. ( String )
		:param stripNamespaces: Strip namespaces while retrieving attributes. ( Boolean )
		:param raiseExceptions: Raise if section doesn't exists. ( Boolean )
		:return: Attributes. ( OrderedDict / Dictionary )
		"""

		LOGGER.debug("> Getting section '{0}' attributes.".format(section))
		if self.sectionExists(section):
			dictionary = orderedDictionary and OrderedDict or dict
			attributes = dictionary()
			if stripNamespaces:
				for attribute, value in self.__sections[section].iteritems():
					attributes[namespace.removeNamespace(attribute, rootOnly=True)] = value
			else:
				attributes.update(self.__sections[section])
			LOGGER.debug("> Attributes: '{0}'.".format(attributes))
			return attributes
		else:
			if raiseExceptions:
				raise KeyError("{0} | '{1}' section doesn't exists in '{2}' sections!".format(self.__class__.__name__,
																							section,
																							self.file))
			else:
				LOGGER.warning("!> {0} | '{1}' section doesn't exists in '{2}' sections!".format(self.__class__.__name__,
																								section,
																								self.file))
Esempio n. 4
0
	def testWalk(self):
		"""
		This method tests :meth:`foundations.walkers.FilesWalker.walk` method.
		"""

		filesWalker = FilesWalker()
		filesWalker.root = os.path.join(RESOURCES_DIRECTORY, ROOT_DIRECTORY)
		filesWalker.walk()
		for path in filesWalker.files.itervalues():
			self.assertTrue(os.path.exists(path))

		referencePaths = [strings.replace(os.path.join(RESOURCES_DIRECTORY, ROOT_DIRECTORY, path),
											{"/":"|", "\\":"|"}) for path in FILES_TREE_HIERARCHY]
		walkerFiles = [strings.replace(path, {"/":"|", "\\":"|"}) for path in filesWalker.files.itervalues()]
		for item in referencePaths:
			self.assertIn(item, walkerFiles)

		filesWalker.walk(filtersOut=("\.rc$",))
		walkerFiles = [strings.replace(path, {"/":"|", "\\":"|"}) for path in filesWalker.files.itervalues()]
		for item in walkerFiles:
			self.assertTrue(not re.search(r"\.rc$", item))

		filesWalker.walk(filtersOut=("\.ibl", "\.rc$", "\.sIBLT$", "\.txt$"))
		self.assertTrue(not filesWalker.files)

		referencePaths = [strings.replace(os.path.join(RESOURCES_DIRECTORY, ROOT_DIRECTORY, path),
										{"/":"|", "\\":"|"}) for path in FILES_TREE_HIERARCHY if re.search(r"\.rc$", path)]
		filter = "\.rc$"
		filesWalker.walk(filtersIn=(filter,))
		walkerFiles = [strings.replace(path, {"/":"|", "\\":"|"}) for path in filesWalker.files.itervalues()]
		for item in referencePaths:
			self.assertIn(item, walkerFiles)
		for item in walkerFiles:
			self.assertTrue(re.search(filter, item))

		filesWalker.hashSize = 24
		filesWalker.walk()
		for item in filesWalker.files:
			self.assertEqual(len(namespace.removeNamespace(item)), filesWalker.hashSize)

		filesWalker.walk(visitor=lambda x, y: x.pop(y))
		self.assertDictEqual(filesWalker.files, {})