daredare.daredare

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

# Source Code for Module daredare.daredare

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