def get_lines_from_file(filename, lineno, context=0): """Return `content` number of lines before and after the specified `lineno` from the file identified by `filename`. Returns a `(lines_before, line, lines_after)` tuple. """ if os.path.isfile(filename): fileobj = open(filename, 'U') try: lines = fileobj.readlines() lbound = max(0, lineno - context) ubound = lineno + 1 + context charset = None rep = re.compile('coding[=:]\s*([-\w.]+)') for linestr in lines[0], lines[1]: match = rep.search(linestr) if match: charset = match.group(1) break before = [to_unicode(l.rstrip('\n'), charset) for l in lines[lbound:lineno]] line = to_unicode(lines[lineno].rstrip('\n'), charset) after = [to_unicode(l.rstrip('\n'), charset) \ for l in lines[lineno + 1:ubound]] return before, line, after finally: fileobj.close() return (), None, ()
def save(self): """Write the configuration options to the primary file.""" if not self.filename: return # Only save options that differ from the defaults sections = [] for section in self.sections(): options = [] for option in self[section]: default = None if self.parent: default = self.parent.get(section, option) current = self.parser.has_option(section, option) and \ to_unicode(self.parser.get(section, option)) if current is not False and current != default: options.append((option, current)) if options: sections.append((section, sorted(options))) fileobj = open(self.filename, 'w') try: fileobj.write('# -*- coding: utf-8 -*-\n\n') for section, options in sections: fileobj.write('[%s]\n' % section) for key, val in options: if key in self[section].overridden: fileobj.write('# %s = <inherited>\n' % key) else: val = val.replace(CRLF, '\n').replace('\n', '\n ') fileobj.write('%s = %s\n' % (key, val.encode('utf-8'))) fileobj.write('\n') finally: fileobj.close()
def get_timezone(tzname): """Fetch timezone instance by name or return `None`""" try: # if given unicode parameter, pytz.timezone fails with: # "type() argument 1 must be string, not unicode" tz = pytz.timezone(to_unicode(tzname).encode('ascii', 'replace')) except (KeyError, IOError): tz = _tzmap.get(tzname) if tz and tzname.startswith('Etc/'): tz = _tzoffsetmap.get(tz.utcoffset(None)) return tz
def set(self, name, value): """Change a configuration value. These changes are not persistent unless saved with `save()`. """ if not self.config.parser.has_section(self.name): self.config.parser.add_section(self.name) if value is None: self.overridden[name] = True value = '' else: value = to_unicode(value).encode('utf-8') return self.config.parser.set(self.name, name, value)
def get(self, name, default=''): """Return the value of the specified option. Valid default input is a string. Returns a string. """ if self.config.parser.has_option(self.name, name): value = self.config.parser.get(self.name, name) elif self.config.parent: value = self.config.parent[self.name].get(name, default) else: option = Option.registry.get((self.name, name)) if option: value = option.default or default else: value = default if not value: return u'' elif isinstance(value, basestring): return to_unicode(value) else: return value
def get_last_traceback(): import traceback from StringIO import StringIO tb = StringIO() traceback.print_exc(file=tb) return to_unicode(tb.getvalue())