Пример #1
0
	def getValue(self, attribute, section, encode=False):
		"""
		This method returns requested attribute value.

		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.getValue("Attribute 1", "Section A")
			Value A

		:param attribute: Attribute name. ( String )
		:param section: Section containing the searched attribute. ( String )
		:param encode: Encode value to unicode. ( Boolean )
		:return: Attribute value. ( String )
		"""

		if not self.__sections:
			return

		if self.attributeExists(attribute, section):
			if attribute in self.__sections[section]:
				value = self.__sections[section][attribute]
			elif namespace.setNamespace(section, attribute) in self.__sections[section]:
				value = self.__sections[section][namespace.setNamespace(section, attribute)]
			LOGGER.debug("> Attribute: '{0}', value: '{1}'.".format(attribute, value))
			value = encode and strings.encode(value) or value
			return value
Пример #2
0
	def testSetNamespace(self):
		"""
		This method tests :func:`foundations.namespace.setNamespace` definition.
		"""

		self.assertIsInstance(namespace.setNamespace("Namespace", "Attribute"), str)
		self.assertEqual(namespace.setNamespace("Namespace", "Attribute"), "Namespace|Attribute")
		self.assertEqual(namespace.setNamespace("Namespace", "Attribute", ":"), "Namespace:Attribute")
Пример #3
0
	def walk(self, filtersIn=None, filtersOut=None, flags=0, shorterHashKey=True, visitor=None):
		"""
		This method gets root directory files list as a dictionary using given filters.

		:param filtersIn: Regex filters in list. ( Tuple / List )
		:param filtersIn: Regex filters out list. ( Tuple / List )
		:param flags: Regex flags. ( Integer )
		:param visitor: Visitor object. ( Object )
		:return: Files list. ( Dictionary or None )
		"""

		if filtersIn:
			LOGGER.debug("> Current filters in: '{0}'.".format(filtersIn))

		if filtersOut:
			LOGGER.debug("> Current filters out: '{0}'.".format(filtersOut))

		if not self.__root:
			return

		self.__files = {}
		for parentDirectory, directories, files in os.walk(self.__root, topdown=False, followlinks=True):
			for item in files:
				LOGGER.debug("> Current file: '{0}' in '{1}'.".format(item, self.__root))
				path = strings.toForwardSlashes(os.path.join(parentDirectory, item))
				if os.path.isfile(path):
					if not strings.filterWords((path,), filtersIn, filtersOut, flags):
						continue

					LOGGER.debug("> '{0}' file filtered in!".format(path))

					hashKey = hashlib.md5(path).hexdigest()
					name = namespace.setNamespace(foundations.common.getFirstItem(os.path.splitext(item)),
													hashKey[:self.__hashSize] if shorterHashKey else hashKey)
					LOGGER.debug("> Adding '{0}' with path: '{1}' to files list.".format(name, path))
					self.__files[name] = path

					visitor and visitor(self.__files, name)

		return self.__files