Example #1
0
    def __call__(self, model, output_filename, solver_capability, io_options):
        if output_filename is None:
            output_filename = model.name + ".bar"

        # If the user provides a file name, we will use the opened file
        # as a context manager to ensure it gets closed.  If the user
        # provided something else (e.g., a file-like object), we will
        # use a nullcontext manager to prevent closing it.
        if isinstance(output_filename, str):
            output_file = open(output_filename, "w")
        else:
            output_file = nullcontext(output_filename)

        with output_file as FILE:
            symbol_map = self._write_bar_file(model, FILE, solver_capability,
                                              io_options)

        return output_filename, symbol_map
Example #2
0
 def available(self, exception_flag=False):
     """ True if the solver is available """
     if self._assert_available:
         return True
     if not OptSolver.available(self, exception_flag):
         return False
     try:
         # HACK: Suppress logged warnings about the executable not
         # being found
         cm = nullcontext() if exception_flag else LoggingIntercept()
         with cm:
             ans = self.executable()
     except NotImplementedError:
         ans = None
     if ans is None:
         if exception_flag:
             msg = "No executable found for solver '%s'"
             raise ApplicationError(msg % self.name)
         return False
     return True