Example #1
0
	def testToForwardSlashes(self):
		"""
		This method tests :func:`foundations.strings.toForwardSlashes` definition.
		"""

		self.assertIsInstance(strings.toForwardSlashes("To\Forward\Slashes\Test\Case"), str)
		self.assertEqual(strings.toForwardSlashes("To\Forward\Slashes\Test\Case"), "To/Forward/Slashes/Test/Case")
		self.assertEqual(strings.toForwardSlashes("\Users/JohnDoe\Documents"), "/Users/JohnDoe/Documents")
Example #2
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