Flutter Engine
The Flutter Engine
multiemitter.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5"""Templating to help generate structured text."""
6
7import os
8import sys
9import subprocess
10import time
11import emitter
12import logging
13
14_logger = logging.getLogger('multiemitter')
15
16
17class MultiEmitter(object):
18 """A set of Emitters that write to different files.
19
20 Each entry has a key.
21
22 file --> emitter
23 key --> emitter
24
25 """
26
27 def __init__(self, logging_level=logging.WARNING):
28 self._key_to_emitter = {} # key -> Emitter
29 self._filename_to_emitter = {} # filename -> Emitter
30
31 _logger.setLevel(logging_level)
32
33 def FileEmitter(self, filename, key=None):
34 """Creates an emitter for writing to a file.
35
36 When this MultiEmitter is flushed, the contents of the emitter are written
37 to the file.
38
39 Arguments:
40 filename: a string, the path name of the file
41 key: provides an access key to retrieve the emitter.
42
43 Returns: the emitter.
44 """
45 e = emitter.Emitter()
46 self._filename_to_emitter[filename] = e
47 if key:
48 self.Associate(key, e)
49 return e
50
51 def Associate(self, key, emitter):
52 """Associates a key with an emitter."""
53 self._key_to_emitter[key] = emitter
54
55 def Find(self, key):
56 """Returns the emitter associated with |key|."""
57 return self._key_to_emitter[key]
58
59 def Flush(self, writer=None):
60 """Writes all pending files.
61
62 Arguments:
63 writer: a function called for each file and it's lines.
64 """
65 if not writer:
66 writer = _WriteFile
67 for file in sorted(self._filename_to_emitter.keys()):
68 emitter = self._filename_to_emitter[file]
69 writer(file, emitter.Fragments())
70
71
72def _WriteFile(path, lines):
73 (dir, file) = os.path.split(path)
74
75 # Ensure dir exists.
76 if dir:
77 if not os.path.isdir(dir):
78 _logger.info('Mkdir - %s' % dir)
79 os.makedirs(dir)
80
81 # If file exists and is unchanged, return.
82 new_contents = ''.join(lines)
83 if os.path.exists(path):
84 with open(path) as fd:
85 contents = fd.read()
86 if new_contents == contents:
87 _logger.info('Unchanged file %s' % path)
88 return
89
90 # Write the file.
91 num_attempts = 4
92 for i in range(num_attempts):
93 try:
94 _logger.info('Writing (attempt %d) - %s' % (i + 1, path))
95 with open(path, 'w') as fd:
96 fd.write(new_contents)
97 return
98 except IOError as error:
99 last_attempt = (i == (num_attempts - 1))
100 if not last_attempt:
101 # Sleep for 50 ms and try again
102 time.sleep(0.05)
103 else:
104 _logger.info('Got exception (%s) ' % error)
105 raise error
def Associate(self, key, emitter)
Definition: multiemitter.py:51
def Flush(self, writer=None)
Definition: multiemitter.py:59
def __init__(self, logging_level=logging.WARNING)
Definition: multiemitter.py:27
def FileEmitter(self, filename, key=None)
Definition: multiemitter.py:33
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741