Esempio n. 1
0
	def getPlugins(self, dir, recursive = False):
		"""
		retrieve .js files from directory

		if there is a whitelist file, only those items will be retrieved
		if there is a blacklist file, those items will be excluded
		whitelist and blacklist files contain one file or directory name per line
		whitelist takes precedence over blacklist

		@param dir (str): directory (relative path)
		@param recursive (bool): process subdirectories
		@return (list): plugin tiddlers
		"""
		plugins = []
		uri = posixpath.join(self.host, dir)
		content = self._get(uri)
		soup = BeautifulSoup(content)
		list = soup.find("ul")
		try:
			items = list.findChildren("li")
		except AttributeError: # means 404 -- XXX: ugly hack; check for HTTP status code directly
			raise IOError("404 Not Found") # XXX: IOError not appropriate
		uris = [item.findChild("a")["href"] for item in items]
		if self.whitelist in uris: # whitelisting
			uri = posixpath.join(self.host, dir, self.whitelist)
			patterns = self._get(uri).split("\n")
			meta = [uri.strip() for uri in uris if uri.endswith(".meta")]
			uris = [uri.strip() for uri in uris if matchPatterns(uri, patterns)]
			uris.extend(meta)
		elif self.blacklist in uris: # blacklisting
			uri = posixpath.join(self.host, dir, self.blacklist)
			patterns = self._get(uri).split("\n")
			patterns.append(self.blacklist)
			uris = [uri.strip() for uri in uris if not matchPatterns(uri, patterns)]
		for uri in uris:
			if uri == "../":
				continue
			if uri.endswith(".js"): # plugin -- XXX: also excludes whitelisted items missing .js extension
				plugin = Tiddler()
				plugin.title = posixpath.basename(uri[:-3])
				plugin.tags = ["systemConfig"]
				fullURI = posixpath.join(self.host, dir, uri)
				plugin.text = self._get(fullURI)
				if uri + ".meta" in uris: # retrieve metadata
					metaURI = posixpath.join(self.host, dir, uri + ".meta")
					self.retrieveMetadata(plugin, metaURI)
				plugins.append(plugin)
			elif uri.endswith("/") and recursive: # directory -- XXX: potential for infinite loop?
				subDir = posixpath.join(dir, uri)
				plugins.extend(self.getPlugins(subDir, recursive))
		return plugins
Esempio n. 2
0
	def testReturnsTrueForAnyMatch(self):
		"""returns True if URI matches any of the given patterns"""
		term = "lorem.js"
		patterns = ["foo", "bar", "*.js"]
		self.assertTrue(utils.matchPatterns(term, patterns))
Esempio n. 3
0
	def testReturnsFalseForNoMatch(self):
		"""returns False if URI matches none of the given patterns"""
		term = "lorem.js"
		patterns = ["foo", "bar", "baz"]
		self.assertFalse(utils.matchPatterns(term, patterns))