def _LoadAllowedFileStoragePaths(filename): """Loads file containing allowed file storage paths. @rtype: list @return: List of allowed paths (can be an empty list) """ try: contents = utils.ReadFile(filename) except EnvironmentError: return [] else: return utils.FilterEmptyLinesAndComments(contents)
def ParsePasswordFile(contents): """Parses the contents of a password file. Lines in the password file are of the following format:: <username> <password> [options] Fields are separated by whitespace. Username and password are mandatory, options are optional and separated by comma (','). Empty lines and comments ('#') are ignored. @type contents: str @param contents: Contents of password file @rtype: dict @return: Dictionary containing L{PasswordFileUser} instances """ users = {} for line in utils.FilterEmptyLinesAndComments(contents): parts = line.split(None, 2) if len(parts) < 2: # Invalid line # TODO: Return line number from FilterEmptyLinesAndComments logging.warning( "Ignoring non-comment line with less than two fields") continue name = parts[0] password = parts[1] # Extract options options = [] if len(parts) >= 3: for part in parts[2].split(","): options.append(part.strip()) else: logging.warning("Ignoring values for user '%s': %s", name, parts[3:]) users[name] = PasswordFileUser(name, password, options) return users
def test(self): text = """ This is # with comments a test # in # saying ...#... # multiple places Hello World! """ self.assertEqual(utils.FilterEmptyLinesAndComments(text), [ "This", "is", "a", "test", "saying", "...#...", "Hello World!", ])
def testEmpty(self): self.assertEqual(utils.FilterEmptyLinesAndComments(""), []) self.assertEqual(utils.FilterEmptyLinesAndComments("\n"), []) self.assertEqual(utils.FilterEmptyLinesAndComments("\n" * 100), []) self.assertEqual(utils.FilterEmptyLinesAndComments("\n \n\t \n"), [])