5"""Templating to help generate structured text."""
14_logger = logging.getLogger(
'multiemitter')
18 """A set of Emitters that write to different files.
27 def __init__(self, logging_level=logging.WARNING):
31 _logger.setLevel(logging_level)
34 """Creates an emitter for writing to a file.
36 When this MultiEmitter is flushed, the contents of the emitter are written
40 filename: a string, the path name of the file
41 key: provides an access key to retrieve the emitter.
52 """Associates a key with an emitter."""
56 """Returns the emitter associated with |key|."""
60 """Writes all pending files.
63 writer: a function called for each file
and it
's lines.
69 writer(file, emitter.Fragments())
72def _WriteFile(path, lines):
73 (dir, file) = os.path.split(path)
77 if not os.path.isdir(dir):
78 _logger.info(
'Mkdir - %s' % dir)
82 new_contents =
''.
join(lines)
83 if os.path.exists(path):
84 with open(path)
as fd:
86 if new_contents == contents:
87 _logger.info(
'Unchanged file %s' % path)
92 for i
in range(num_attempts):
94 _logger.info(
'Writing (attempt %d) - %s' % (i + 1, path))
95 with open(path,
'w')
as fd:
96 fd.write(new_contents)
98 except IOError
as error:
99 last_attempt = (i == (num_attempts - 1))
104 _logger.info(
'Got exception (%s) ' % error)
def Associate(self, key, emitter)
def Flush(self, writer=None)
def __init__(self, logging_level=logging.WARNING)
def FileEmitter(self, filename, key=None)
static SkString join(const CommandLineFlags::StringArray &)