def test_relative_uri(self): """Test relative URI. """ self.assertEqual( relative_uri('directory/file.txt', 'somefile.txt'), 'directory/somefile.txt') self.assertEqual( relative_uri('file.txt', 'somefile.txt'), 'somefile.txt') self.assertEqual( relative_uri('directory/file.txt', '/root/somefile.txt'), '/root/somefile.txt') self.assertEqual( relative_uri('directory/file.txt', 'http://localhost/extends.txt'), 'http://localhost/extends.txt') self.assertEqual( relative_uri('directory/file.txt', 'https://localhost/extends.txt'), 'https://localhost/extends.txt') self.assertEqual( relative_uri('http://localhost/file.txt', 'versions.txt'), 'http://localhost/versions.txt') self.assertEqual( relative_uri('https://localhost/file.txt', 'versions.txt'), 'https://localhost/versions.txt') self.assertEqual( relative_uri('', 'somefile.txt'), 'somefile.txt')
def parse_line(line): options = shlex.split(line) mapping = [] for info in options[1:]: directories = info.split(':') if len(directories) != 2: raise ConfigurationError( value.location, u"Invalid directory definition", info) mapping.append(directories) return relative_uri(origin, options[0], True), mapping
def read_lines(cls, lines, origin): """Read a configuration from the given string, that would be refered by origin. """ configuration = cls(origin) line_number = 0 section = None for text in lines(): line_number += 1 # Comment if not text.strip() or text[0] in '#;': continue # Some sources gives '\n' at the end of the lines, someother don't text = text.rstrip() # New section new_section = SectionParser.new_section( configuration, text, origin, line_number) if new_section is not None: if section is not None: section.done(line_number - 1) section = new_section else: if section is not None: section.add(line_number, text) else: raise ConfigurationError( origin, u'Garbage text before section at line %d' % line_number) if section is not None: section.done(line_number) else: raise ConfigurationError(origin, u'No section defined') # Support online include to extend configuration if cls.default_section in configuration: section = configuration[cls.default_section] if 'extends' in section: for uri in section['extends'].as_list(): configuration += Configuration.read( relative_uri(origin, uri)) del configuration[cls.default_section]['extends'] return configuration
def as_files(self): """Return the value as a list of files, relative to the configuration one where the option is defined. """ origin = self.get_cfg_directory() return map(lambda uri: relative_uri(origin, uri, True), self.as_list())
def as_file(self): """Return the value as a file, relative to the configuration one where the option is defined. """ origin = self.get_cfg_directory() return relative_uri(origin, self.as_text(), True)