コード例 #1
0
	def testReplace(self):
		"""
		This method tests :func:`foundations.strings.replace` definition.
		"""

		self.assertIsInstance(strings.replace("To@Forward|Slashes@Test|Case", {}), str)
		self.assertEqual(strings.replace("To@Forward|Slashes@Test|Case", {"@":"|", "|":":"}),
						"To:Forward:Slashes:Test:Case")
		self.assertEqual(strings.replace("To@Forward@Slashes@Test@Case", {"@":"|", "|":"@"}),
						"To@Forward@Slashes@Test@Case")
コード例 #2
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, {})
コード例 #3
0
ファイル: parsers.py プロジェクト: elanifegnirf/Foundations
	def write(self,
			namespaces=False,
			splitter="=",
			commentLimiter=(";"),
			spacesAroundSplitter=True,
			spaceAfterCommentLimiter=True):
		"""
		This method writes defined file using :obj:`SectionsFileParser.sections` and
			:obj:`SectionsFileParser.comments` class properties content.

		Usage::

			>>> sections = {"Section A": {"Section A|Attribute 1": "Value A"}, \
"Section B": {"Section B|Attribute 2": "Value B"}}
			>>> sectionsFileParser = SectionsFileParser("SectionsFile.rc")
			>>> sectionsFileParser.sections = sections
			>>> sectionsFileParser.write()
			True
			>>> sectionsFileParser.read()
			True
			>>> print sectionsFileParser.content[0:5]
			['[Section A]\\n', 'Attribute 1 = Value A\\n', '\\n', '[Section B]\\n', 'Attribute 2 = Value B\\n', '\\n']

		:param namespaces: Attributes are namespaced. ( Boolean )
		:param splitter: Splitter character. ( String )
		:param commentLimiter: Comment limiter character. ( String )
		:param spacesAroundSplitter: Spaces around attributes and value splitters. ( Boolean )
		:param spaceAfterCommentLimiter: Space after comments limiter. ( Boolean )
		:return: Method success. ( Boolean )
		"""

		if not self.__sections:
			return

		LOGGER.debug("> Setting '{0}' file content.".format(self.file))
		attributeTemplate = spacesAroundSplitter and "{{0}} {0} {{1}}\n".format(splitter) or \
							"{{0}} {0} {{1}}\n".format(splitter)
		attributeTemplate = strings.replace(attributeTemplate, {"{{" : "{", "}}" : "}"})
		commentTemplate = spaceAfterCommentLimiter and "{0} {{0}}\n".format(commentLimiter) or \
							"{0}{{0}}\n".format(commentLimiter)
		if self.__defaultsSection in self.__sections:
			LOGGER.debug("> Appending '{0}' default section.".format(self.__defaultsSection))
			if self.__comments:
				for comment, value in self.__comments.iteritems():
					if self.__defaultsSection in comment:
						value = value["content"] or ""
						LOGGER.debug("> Appending '{0}' comment with '{1}' value.".format(comment, value))
						self.content.append(commentTemplate.format(value))
			for attribute, value in self.__sections[self.__defaultsSection].iteritems():
				attribute = namespaces and attribute or foundations.namespace.removeNamespace(attribute,
																							self.__namespaceSplitter,
																							rootOnly=True)
				value = value or ""
				LOGGER.debug("> Appending '{0}' attribute with '{1}' value.".format(attribute, value))
				self.content.append(attributeTemplate.format(attribute, value))
			self.content.append("\n")

		for i, section in enumerate(self.__sections):
			LOGGER.debug("> Appending '{0}' section.".format(section))
			self.content.append("[{0}]\n".format(section))
			if self.__comments:
				for comment, value in self.__comments.iteritems():
					if section in comment:
						value = value["content"] or ""
						LOGGER.debug("> Appending '{0}' comment with '{1}' value.".format(comment, value))
						self.content.append(commentTemplate.format(value))
			for attribute, value in self.__sections[section].iteritems():
				if foundations.namespace.removeNamespace(attribute) == self.__rawSectionContentIdentifier:
					LOGGER.debug("> Appending '{0}' raw section content.".format(section))
					for line in value:
						self.content.append(line)
				else:
					LOGGER.debug("> Appending '{0}' section.".format(section))
					attribute = namespaces and attribute or foundations.namespace.removeNamespace(attribute,
																								self.__namespaceSplitter,
																								rootOnly=True)
					value = value or ""
					LOGGER.debug("> Appending '{0}' attribute with '{1}' value.".format(attribute, value))
					self.content.append(attributeTemplate.format(attribute, value))
			if i != len(self.__sections) - 1:
				self.content.append("\n")
		io.File.write(self)
		return True