def patch(self, out_filename=None): """Apply patches and write changes to specified file. Args: out_filename: Output filename. If not set then input one is used. """ in_ = FileInput(source_path=self._filename, encoding=self._encoding) content = in_.read() content = self._patcher.patch(content) content = '\n'.join(content) out = FileOutput(destination_path=out_filename or self._filename, encoding=self._encoding) out.write(content)
def build_finished(app, exception): if exception is not None: return builder = app.builder output_full_name = os.path.join( builder.outdir, 'schedule.ics', ) output_dir_name = os.path.dirname(output_full_name) if not os.path.exists(output_dir_name): os.makedirs(output_dir_name) destination = FileOutput( destination_path=output_full_name, encoding='utf-8', ) LOG.info('generating {}'.format(output_full_name)) destination.write(_global_calendar.to_ical())
def write(self, data): # type: (unicode) -> unicode if (self.destination_path and self.autoclose and 'b' not in self.mode and self.overwrite_if_changed and os.path.exists(self.destination_path)): with codecs.open(self.destination_path, encoding=self.encoding) as f: # skip writing: content not changed if f.read() == data: return data return FileOutput.write(self, data)
def write(self, data): # type: (unicode) -> unicode if (self.destination_path and self.autoclose and 'b' not in self.mode and self.overwrite_if_changed and os.path.exists(self.destination_path)): with open(self.destination_path, encoding=self.encoding) as f: # type: ignore # skip writing: content not changed if f.read() == data: return data return FileOutput.write(self, data)
class DependencyList(object): """ List of dependencies, with file recording support. Note that the output file is not automatically closed. You have to explicitly call the close() method. """ def __init__(self, output_file=None, dependencies=[]): """ Initialize the dependency list, automatically setting the output file to `output_file` (see `set_output()`) and adding all supplied dependencies. """ self.set_output(output_file) for i in dependencies: self.add(i) def set_output(self, output_file): """ Set the output file and clear the list of already added dependencies. `output_file` must be a string. The specified file is immediately overwritten. If output_file is '-', the output will be written to stdout. If it is None, no file output is done when calling add(). """ self.list = [] if output_file: if output_file == '-': of = None else: of = output_file self.file = FileOutput(destination_path=of, encoding='utf8', autoclose=False) else: self.file = None def add(self, *filenames): """ If the dependency `filename` has not already been added, append it to self.list and print it to self.file if self.file is not None. """ for filename in filenames: if not filename in self.list: self.list.append(filename) if self.file is not None: self.file.write(filename+'\n') def close(self): """ Close the output file. """ self.file.close() self.file = None def __repr__(self): try: output_file = self.file.name except AttributeError: output_file = None return '%s(%r, %s)' % (self.__class__.__name__, output_file, self.list)
def doctree_resolved(app, doctree, docname): builder = app.builder for node in doctree.traverse(PendingICS): series_name = node._series_name data = node._data LOG.info('building {} calendar'.format(series_name)) cal = icalendar.Calendar() cal.add('prodid', '-//releases.openstack.org//EN') cal.add('X-WR-CALNAME', '{} schedule'.format(series_name)) for week in data['cycle']: if not week.get('name'): continue event = icalendar.Event() summary = [] for item in week.get('x-project', []): try: # Look up the cross-reference name to get the # section, then get the title from the first child # node. title = doctree.ids[item].children[0].astext() except Exception as e: # NOTE(dhellmann): Catching "Exception" is a bit # ugly, but given the complexity of the expression # above there are a bunch of ways things might # fail. LOG.info('could not get title for {}: {}'.format(item, e)) title = item summary.append(title) if summary: summary_text = ' (' + '; '.join(summary) + ')' else: summary_text = '' event.add( 'summary', '{} {}{}'.format(series_name.title(), week['name'], summary_text), ) event.add('dtstamp', node.last_update) event.add('uid', '%s-%s' % (series_name, week.get('name'))) start = datetime.datetime.strptime(week['start'], '%Y-%m-%d') event.add('dtstart', icalendar.vDate(start.date())) # NOTE(dhellmann): ical assumes a time of midnight, so in # order to have the event span the final day of the week # we have to add an extra day. raw_end = datetime.datetime.strptime(week['end'], '%Y-%m-%d') end = raw_end + datetime.timedelta(days=1) event.add('dtend', icalendar.vDate(end.date())) # Look up the cross-reference name to get the # section, then add the full description to the # text. description = [ _format_description(doctree.ids[item]) for item in week.get('x-project', []) if item in doctree.ids ] if description: event.add('description', '\n\n'.join(description)) cal.add_component(event) _global_calendar.add_component(event) output_full_name = os.path.join( builder.outdir, docname + '.ics', ) output_dir_name = os.path.dirname(output_full_name) if not os.path.exists(output_dir_name): os.makedirs(output_dir_name) destination = FileOutput( destination_path=output_full_name, encoding='utf-8', ) LOG.info('generating {}'.format(output_full_name)) destination.write(cal.to_ical()) # Remove the node that the writer won't understand. node.parent.replace(node, [])
class DependencyList(object): """ List of dependencies, with file recording support. Note that the output file is not automatically closed. You have to explicitly call the close() method. """ def __init__(self, output_file=None, dependencies=[]): """ Initialize the dependency list, automatically setting the output file to `output_file` (see `set_output()`) and adding all supplied dependencies. """ self.set_output(output_file) for i in dependencies: self.add(i) def set_output(self, output_file): """ Set the output file and clear the list of already added dependencies. `output_file` must be a string. The specified file is immediately overwritten. If output_file is '-', the output will be written to stdout. If it is None, no file output is done when calling add(). """ self.list = [] if output_file: if output_file == '-': of = None else: of = output_file self.file = FileOutput(destination_path=of, encoding='utf8', autoclose=False) else: self.file = None def add(self, *filenames): """ If the dependency `filename` has not already been added, append it to self.list and print it to self.file if self.file is not None. """ for filename in filenames: if not filename in self.list: self.list.append(filename) if self.file is not None: self.file.write(filename + '\n') def close(self): """ Close the output file. """ self.file.close() self.file = None def __repr__(self): try: output_file = self.file.name except AttributeError: output_file = None return '%s(%r, %s)' % (self.__class__.__name__, output_file, self.list)