]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Tools/bgen/bgen/bgenOutput.py
1 """Output primitives for the binding generator classes.
3 This should really be a class, but then everybody would be passing
4 the output object to each other. I chose for the simpler approach
5 of a module with a global variable. Use SetOutputFile() or
6 SetOutputFileName() to change the output file.
11 def SetOutputFile(file = None, needclose
= 0):
12 """Call this with an open file object to make it the output file.
14 Call it without arguments to close the current file (if necessary)
15 and reset it to sys.stdout.
16 If the second argument is true, the new file will be explicitly closed
17 on a subsequence call.
19 global _File
, _NeedClose
29 _NeedClose
= file and needclose
31 def SetOutputFileName(filename
= None):
32 """Call this with a filename to make it the output file.
34 Call it without arguments to close the current file (if necessary)
35 and reset it to sys.stdout.
39 SetOutputFile(open(filename
, 'w'), 1)
41 SetOutputFile() # Initialize _File
43 _Level
= 0 # Indentation level
46 """Return the current indentation level."""
50 """Set the current indentation level.
52 This does no type or range checking -- use at own risk.
57 def Output(format
= "", *args
):
58 VaOutput(format
, args
)
60 def VaOutput(format
, args
):
61 """Call this with a format string and argument tuple for the format.
63 A newline is always added. Each line in the output is indented
64 to the proper indentation level -- even if the result of the
65 format expansion contains embedded newlines. Exception: lines
66 beginning with '#' are not indented -- these are assumed to be
67 C preprprocessor lines.
71 indent
= '\t' * _Level
72 lines
= text
.split('\n')
73 for i
in range(len(lines
)):
74 if lines
[i
] and lines
[i
][0] != '#':
75 lines
[i
] = indent
+ lines
[i
]
76 text
= '\n'.join(lines
)
77 _File
.write(text
+ '\n')
79 def IndentLevel(by
= 1):
80 """Increment the indentation level by one.
82 When called with an argument, adds it to the indentation level.
86 raise Error
, "indentation underflow (internal error)"
89 def DedentLevel(by
= 1):
90 """Decrement the indentation level by one.
92 When called with an argument, subtracts it from the indentation level.
96 def OutIndent(format
= "", *args
):
97 """Combine Output() followed by IndentLevel().
99 If no text is given, acts like lone IndentLevel().
101 if format
: VaOutput(format
, args
)
104 def OutDedent(format
= "", *args
):
105 """Combine Output() followed by DedentLevel().
107 If no text is given, acts like loneDedentLevel().
109 if format
: VaOutput(format
, args
)
112 def OutLbrace(format
= "", *args
):
113 """Like Output, but add a '{' and increase the indentation level.
115 If no text is given a lone '{' is output.
118 format
= format
+ " {"
121 VaOutput(format
, args
)
125 """Decrease the indentation level and output a '}' on a line by itself."""
129 def OutHeader(text
, dash
):
130 """Output a header comment using a given dash character."""
133 Output("/* %s %s %s */", dash
* (n
/2), text
, dash
* (n
- n
/2))
136 def OutHeader1(text
):
137 """Output a level 1 header comment (uses '=' dashes)."""
140 def OutHeader2(text
):
141 """Output a level 2 header comment (uses '-' dashes)."""
145 """Output multiline text that's internally indented.
147 Pass this a multiline character string. The whitespace before the
148 first nonblank line of the string will be subtracted from all lines.
149 The lines are then output using Output(), but without interpretation
150 of formatting (if you need formatting you can do it before the call).
158 printf("Hello, world\\n");
163 Caveat: the indentation must be consistent -- if you use three tabs
164 in the first line, (up to) three tabs are removed from following lines,
165 but a line beginning with 24 spaces is not trimmed at all. Don't use
168 # (Don't you love using triple quotes *inside* triple quotes? :-)
170 lines
= text
.split('\n')
181 if line
[:n
] == indent
:
185 if line
[:1] <> c
: break
191 """Test program. Run when the module is run as a script."""
192 OutHeader1("test bgenOutput")
205 /* Here are a few comment lines.
206 Just to test indenting multiple lines.
208 End of the comment lines. */
210 Output("for (i = 0; i < argc; i++)")
212 Output('printf("argv[%%d] = %%s\\n", i, argv[i]);')
216 OutHeader2("end test")
218 if __name__
== '__main__':