Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: client/dom/scripts/multiemitter.py

Issue 9845043: Rename client/{dom,html} to lib/{dom,html} . (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « client/dom/scripts/logging.conf ('k') | client/dom/scripts/multiemitter_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
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
6 """Templating to help generate structured text."""
7
8 import os
9 import re
10 import emitter
11 import logging
12
13 _logger = logging.getLogger('multiemitter')
14
15 class MultiEmitter(object):
16 """A set of Emitters that write to different files.
17
18 Each entry has a key.
19
20 file --> emitter
21 key --> emitter
22
23 """
24
25 def __init__(self):
26 self._key_to_emitter = {} # key -> Emitter
27 self._filename_to_emitter = {} # filename -> Emitter
28
29
30 def FileEmitter(self, filename, key=None):
31 """Creates an emitter for writing to a file.
32
33 When this MultiEmitter is flushed, the contents of the emitter are written
34 to the file.
35
36 Arguments:
37 filename: a string, the path name of the file
38 key: provides an access key to retrieve the emitter.
39
40 Returns: the emitter.
41 """
42 e = emitter.Emitter()
43 self._filename_to_emitter[filename] = e
44 if key:
45 self.Associate(key, e)
46 return e
47
48 def Associate(self, key, emitter):
49 """Associates a key with an emitter."""
50 self._key_to_emitter[key] = emitter
51
52 def Find(self, key):
53 """Returns the emitter associated with |key|."""
54 return self._key_to_emitter[key]
55
56 def Flush(self, writer=None):
57 """Writes all pending files.
58
59 Arguments:
60 writer: a function called for each file and it's lines.
61 """
62 if not writer:
63 writer = _WriteFile
64 for file in sorted(self._filename_to_emitter.keys()):
65 emitter = self._filename_to_emitter[file]
66 writer(file, emitter.Fragments())
67
68
69 def _WriteFile(path, lines):
70 (dir, file) = os.path.split(path)
71
72 # Ensure dir exists.
73 if dir:
74 if not os.path.isdir(dir):
75 os.makedirs(dir)
76
77 # Remove file if pre-existing.
78 if os.path.exists(path):
79 os.remove(path)
80
81 # Write the file.
82 # _logger.info('Flushing - %s' % path)
83 f = open(path, 'w')
84 f.writelines(lines)
85 f.close()
OLDNEW
« no previous file with comments | « client/dom/scripts/logging.conf ('k') | client/dom/scripts/multiemitter_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698