daredare

Module daredare |
[hide private]
[frames] | no frames]

# Source Code for Module daredare

      1  import sys,getopt,commands
      2
      3  from model.symbolic import *
      4  from model.model import *
      5  from solver.solver import *
      6  from misc.calculus import *
      7  from compilator.compilator import *
      8
      9  if __name__ == "__main__":
     10      main(sys.argv[1:])
     11
     12 -    def main(argv):
     13          if len(argv)<1:
     14              print("not enough argument")
     15              sys.exit(2)
     16
     17          filetype = ""
     18
     19          filename = argv[-1] # last argument is the filename
     20          regex_mod = re.compile("(.*)\.mod")
     21          regex_mod_match = re.match(regex_mod,filename)
     22          if regex_mod_match:
     23              filetype = "mod"
     24              filename_trunc = regex_mod_match.groups()[0]
     25
     26          regex_xml = re.compile("(.*)\.xml")
     27          regex_xml_match = re.match(regex_xml,filename)
     28          if regex_xml_match:
     29              filetype = "xml"
     30              filename_trunc = regex_xml_match.groups()[0]
     31
     32          if filetype == "":
     33              print("Unknown filetype")
     34              sys.exit(2)
     35
     36          # Read options
     37          options = {"ramsey":False,"portfolio":False}
     38          short_arg_dict = "hr"
     39          long_arg_dict = ["help","ramsey","portfolio"]
     40          try:
     41              opts, args = getopt.getopt(argv, short_arg_dict, long_arg_dict)
     42          except getopt.GetoptError:
     43              usage()
     44              sys.exit(2)
     45          for opt, arg in opts:
     46              if opt in ("-h","--help"):
     47                  usage()
     48                  sys.exit()
     49              if opt in ("-r","--ramsey"):
     50                  options["ramsey"] = True
     51              if opt in ("--portfolio"):
     52                  options["portfolio"] = True
     53
     54          # Start the actual work
     55          if filetype == "mod":
     56              process_mod_file(filename_trunc)
     57          elif filetype == "xml":
     58              model = read_xml_file(filename_trunc)
     59              if options["ramsey"]:
     60                  write_ramsey_policy(filename_trunc, options, model)
     61              if options["portfolio"]:
     62                  write_portfolio_version(filename_trunc, options, model)
     63
     64 -    def process_mod_file(filename_trunc,options={}):
     65          '''read mod file ; write xml file'''
     66          file_path = filename_trunc
     67          print("Processing modfile : " + file_path + '.mod')
     68          output_file_path = filename_trunc + '_dd'
     69          command = "ruby DareDare.rb convert " + file_path + " " + output_file_path
     70          output = commands.getstatusoutput(command)
     71          if not output[0]:
     72              print("Conversion successfull. Written to : " + output_file_path + ".xml")
     73              sys.exit(2)
     74
     75 -    def read_xml_file(filename_trunc,options={}):
     76          '''process xml file ; returns parsed model with symbolic equations'''
     77          print("Processing xmlfile : " + filename_trunc + ".xml")
     78          f = file(filename_trunc + ".xml")
     79          txt = f.read()
     80          f.close()
     81          model = Model(filename_trunc)
     82          model.import_from_xml(ET.XML(txt))
     83          model.sympify_model()
     84          return(model)
     85
     86 -    def write_ramsey_policy(filename_trunc,options,model):
     87          #model.export_to_modfile()
     88          ramsey_model = model.process_ramsey_policy_model()
     89          f = open(filename_trunc + "_ramsey.mod","w")
     90          f.write(ramsey_model.export_to_modfile())
     91          f.close()
     92          print("Ramsey model written to : " + filename_trunc + "_ramsey.mod")
     93
     94 -    def write_portfolio_version(filename_trunc,options,model):
     95          print("Processing portfolios")
     96          portfolio_model = model.process_portfolio_model()
     97          f = open(filename_trunc + "_pf.mod","w")
     98          f.write(portfolio_model.export_to_modfile())
     99          f.close()
    100          print("Portfolio model written to : " + filename_trunc + "_pf.mod")
    101
    102 -    def usage():
    103          print("Bad arguments")
    104