un peu plus simple a utiliser
darcs-hash:20080207221338-af139-51a04967da887735ec416a52ad2d61590c8ea60f.gz
This commit is contained in:
parent
60a86b726c
commit
2b9c65a1de
1 changed files with 16 additions and 1 deletions
17
bcfg2/pygen.py
Normal file → Executable file
17
bcfg2/pygen.py
Normal file → Executable file
|
@ -1,3 +1,4 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- mode: python; coding: utf-8 -*-
|
||||
#
|
||||
# pygen.py
|
||||
|
@ -96,7 +97,7 @@ class Environment(dict):
|
|||
|
||||
# Cette fonction pourrait être écrite directement dans les scripts
|
||||
# mais elle est pratique donc on la met pour tout le monde
|
||||
def export(variable):
|
||||
def export(self, variable):
|
||||
'''Exporte une variable'''
|
||||
self["exports"].add(variable)
|
||||
|
||||
|
@ -152,10 +153,16 @@ def generate(code, environment=None):
|
|||
if not environment:
|
||||
environment = Environment()
|
||||
environment.stream = StringIO()
|
||||
save_stdout = sys.stdout
|
||||
sys.stdout = environment.stream
|
||||
try:
|
||||
exec(code, environment)
|
||||
except Done, _:
|
||||
pass
|
||||
except Exception, exn:
|
||||
sys.stdout = save_stdout
|
||||
raise exn
|
||||
sys.stdout = save_stdout
|
||||
return environment.stream.getvalue()
|
||||
|
||||
def load(fname, cfname=None):
|
||||
|
@ -171,3 +178,11 @@ def load(fname, cfname=None):
|
|||
marshal.dump(code, cfile)
|
||||
cfile.close()
|
||||
return code
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
result = generate(load(sys.argv[1]))
|
||||
|
||||
print "resultat:"
|
||||
sys.stdout.write(result)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue