File: Output.py

package info (click to toggle)
jython 2.5.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 41,624 kB
  • ctags: 101,579
  • sloc: python: 351,444; java: 204,338; xml: 1,316; sh: 330; ansic: 126; perl: 114; makefile: 94
file content (80 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright (c) Corporation for National Research Initiatives
import os
from Modifier import ModifierString



class SourceFile:
    def __init__(self, classname):
	self.text = []
	self.indentation = 0
	self.indent = '    '
	self.startLine = 1
	self.classname = classname
	self.filename = classname+'.java'

    def dump(self, directory="c:\\jython\\test\\comp"):
	fp = open(os.path.join(directory, self.filename), 'w')
	for bit in self.text:
	    fp.write(bit)
	fp.close()

    def __repr__(self):
        EMPTYSTRING = ''
	return EMPTYSTRING.join(self.text)

    def writeModifiers(self, modifiers):
	self.write(ModifierString(modifiers))

    def write(self, text, *args):
	newArgs = []
	for arg in args:
	    if hasattr(arg, 'sourceString'):
		arg = arg.sourceString()
	    newArgs.append(arg)

	if self.startLine:
	    self.text.append(self.indent*self.indentation)
	    self.startLine = 0

	if len(args) > 0:
	    text = text % tuple(newArgs)
	self.text.append(text)

    def writeln(self, text="", *args):
	apply(self.write, (text+'\n',)+args)
	self.startLine = 1

    def writeList(self, values):
	if len(values) == 0:
	    return
	text = "%s, "*(len(values)-1) + "%s "
	print text
	apply(self.write, (text, )+tuple(values))

    def beginBlock(self):
	self.writeln('{')
	self.indentation = self.indentation+1

    def endBlock(self):
	self.indentation = self.indentation-1
	self.writeln('}')



if __name__ == '__main__':
    sf = SourceFile('foo')
    sf.write('public class foo')
    sf.beginBlock()
    sf.writeln()
    sf.writeln("int bar;")
    sf.write("abc")
    sf.write("def")
    sf.beginBlock()
    sf.writeln("hi there")
    sf.endBlock()
    sf.writeList(['a', 'b', 'c'])
    sf.writeln(" extra")
    sf.endBlock()

    print sf