def getAsciiContents(self): """Return the contents of this message, encoded in base64 if they are not already printable.""" if self.type is None: self.decode() if self.type == 'plain' and isPrintingAscii(self.contents, allowISO=1): return self.contents else: return encodeBase64(self.contents)
def _readConfigFile(contents): """Helper function. Given the string contents of a configuration file, returns a list of (SECTION-NAME, SECTION) tuples, where each SECTION is a list of (KEY, VALUE, LINENO) tuples. Throws ConfigError if the file is malformatted. """ # List of (heading, [(key, val, lineno), ...]) sections = [] # [(key, val, lineno)] for the current section. curSection = None # Current line number lineno = 0 # Make sure all characters in the file are ASCII. if not isPrintingAscii(contents): raise ConfigError("Invalid characters in file") fileLines = contents.split("\n") if fileLines[-1] == '': del fileLines[-1] for line in fileLines: lineno += 1 if line == '': continue space = line[0] and line[0] in ' \t' line = line.strip() if line == '' or line[0] == '#': continue elif space: try: lastLine = curSection[-1] curSection[-1] = (lastLine[0], "%s %s" % (lastLine[1], line), lastLine[2]) except (IndexError, TypeError): raise ConfigError("Unexpected indentation at line %s" % lineno) elif line[0] == '[': m = _section_re.match(line) curSection = [] sections.append((m.group(1), curSection)) else: m = _entry_re.match(line) if not m: raise ConfigError("Bad entry at line %s" % lineno) try: curSection.append((m.group(1), m.group(2), lineno)) except AttributeError: raise ConfigError("Unknown section at line %s" % lineno) return sections
def _readConfigFile(contents): """Helper function. Given the string contents of a configuration file, returns a list of (SECTION-NAME, SECTION) tuples, where each SECTION is a list of (KEY, VALUE, LINENO) tuples. Throws ConfigError if the file is malformatted. """ # List of (heading, [(key, val, lineno), ...]) sections = [] # [(key, val, lineno)] for the current section. curSection = None # Current line number lineno = 0 # Make sure all characters in the file are ASCII. if not isPrintingAscii(contents): raise ConfigError("Invalid characters in file") fileLines = contents.split("\n") if fileLines[-1] == '': del fileLines[-1] for line in fileLines: lineno += 1 if line == '': continue space = line[0] and line[0] in ' \t' line = line.strip() if line == '' or line[0] == '#': continue elif space: try: lastLine = curSection[-1] curSection[-1] = (lastLine[0], "%s %s" % (lastLine[1], line),lastLine[2]) except (IndexError, TypeError): raise ConfigError("Unexpected indentation at line %s" %lineno) elif line[0] == '[': m = _section_re.match(line) curSection = [ ] sections.append( (m.group(1), curSection) ) else: m = _entry_re.match(line) if not m: raise ConfigError("Bad entry at line %s"%lineno) try: curSection.append( (m.group(1), m.group(2), lineno) ) except AttributeError: raise ConfigError("Unknown section at line %s" % lineno) return sections
def _readRestrictedConfigFile(contents): """Same interface as _readConfigFile, but only supports the restrictd file format as used by directories and descriptors.""" # List of (heading, [(key, val, lineno), ...]) sections = [] # [(key, val, lineno)] for the current section. curSection = None # Current line number lineno = 0 # Make sure all characters in the file are ASCII. if not isPrintingAscii(contents): raise ConfigError("Invalid characters in file") fileLines = contents.split("\n") if fileLines[-1] == '': del fileLines[-1] if len(fileLines) == 1 and fileLines[0].strip() == '': raise ConfigError("File is empty") for line in fileLines: lineno += 1 line = line.strip() if line == '' or line[0] == '#': raise ConfigError("Empty line not allowed at line %s" % lineno) elif line[0] == '[': m = _section_re.match(line) if not m: raise ConfigError("Bad section declaration at line %s" % lineno) curSection = [] sections.append((m.group(1), curSection)) else: colonIdx = line.find(':') if colonIdx >= 1: try: curSection.append((line[:colonIdx].strip(), line[colonIdx + 1:].strip(), lineno)) except AttributeError: raise ConfigError("Unknown section at line %s" % lineno) else: raise ConfigError("Bad Entry at line %s" % lineno) return sections
def _readRestrictedConfigFile(contents): """Same interface as _readConfigFile, but only supports the restrictd file format as used by directories and descriptors.""" # List of (heading, [(key, val, lineno), ...]) sections = [] # [(key, val, lineno)] for the current section. curSection = None # Current line number lineno = 0 # Make sure all characters in the file are ASCII. if not isPrintingAscii(contents): raise ConfigError("Invalid characters in file") fileLines = contents.split("\n") if fileLines[-1] == '': del fileLines[-1] if len(fileLines) == 1 and fileLines[0].strip() == '': raise ConfigError("File is empty") for line in fileLines: lineno += 1 line = line.strip() if line == '' or line[0] == '#': raise ConfigError("Empty line not allowed at line %s" % lineno) elif line[0] == '[': m = _section_re.match(line) if not m: raise ConfigError("Bad section declaration at line %s" % lineno) curSection = [] sections.append((m.group(1), curSection)) else: colonIdx = line.find(':') if colonIdx >= 1: try: curSection.append((line[:colonIdx].strip(), line[colonIdx+1:].strip(), lineno)) except AttributeError: raise ConfigError("Unknown section at line %s" % lineno) else: raise ConfigError("Bad Entry at line %s" % lineno) return sections
def isPrintingAscii(self): """Return true iff this packets contents are printing characters suitable for inclusion in a text transport medium.""" if self.type is None: self.decode() return isPrintingAscii(self.contents, allowISO=1)