import sys
import subprocess
from basic_modeling_interface import Bmi


class BmiIlamb(Bmi):
    _command = 'ilamb-run'
    _args = None
    _env = None

    def __init__(self):
        self._time = self.get_start_time()

    @property
    def args(self):
        return [self._command] + (self._args or [])

    def get_component_name(self):
        return 'ILAMB'

    def initialize(self, filename):
        self._args = [filename or 'ilamb.cfg']

    def update(self):
        subprocess.check_call(self.args, shell=False, env=self._env)
        self._time = self.get_end_time()

    def update_until(self, time):
        self.update(time)

    def finalize(self):
        pass

    def get_input_var_names(self):
        return ()

    def get_output_var_names(self):
        return ()

    def get_start_time(self):
        return 0.0

    def get_end_time(self):
        return 1.0

    def get_current_time(self):
        return self._time

    def get_time_step(self):
        return 1.0

    def get_time_units(self):
        return 's'

"""Basic Model Interface (BMI) for the ILAMB benchmarking system."""

import os
import subprocess
from basic_modeling_interface import Bmi
from .config import Configuration


class BmiIlamb(Bmi):
    _component_name = 'ILAMB'
    _command = 'ilamb-run'
    _args = None

    def __init__(self):
        self._time = self.get_start_time()
        self.config = Configuration()

    @property
    def args(self):
        return [self._command] + (self._args or [])

    def get_component_name(self):
        return self._component_name

    def initialize(self, filename):
        self.config.load(filename)
        os.environ['ILAMB_ROOT'] = self.config.get_ilamb_root()
        os.environ['MPLBACKEND'] = 'Agg'
        self._args = self.config.get_arguments()

    def update(self):
        with open('stdout', 'w') as fp:
            subprocess.check_call(self.args, stdout=fp)
        self._time = self.get_end_time()

    def update_until(self, time):
        self.update()

    def finalize(self):
        pass

    def get_input_var_names(self):
        return ()

    def get_output_var_names(self):
        return ()

    def get_start_time(self):
        return 0.0

    def get_end_time(self):
        return 1.0

    def get_current_time(self):
        return self._time

    def get_time_step(self):
        return 1.0

    def get_time_units(self):
        return 's'

Update ILAMB BMI to use Configuration
Update ILAMB BMI to use Configuration

I also set ILAMB_ROOT and MPLBACKEND at `initialize`. Not sure if
there's a better location.

+ """Basic Model Interface (BMI) for the ILAMB benchmarking system."""
+ 
- import sys
+ import os
  import subprocess
  from basic_modeling_interface import Bmi
+ from .config import Configuration
  
  
  class BmiIlamb(Bmi):
+     _component_name = 'ILAMB'
      _command = 'ilamb-run'
      _args = None
-     _env = None
  
      def __init__(self):
          self._time = self.get_start_time()
+         self.config = Configuration()
  
      @property
      def args(self):
          return [self._command] + (self._args or [])
  
      def get_component_name(self):
-         return 'ILAMB'
+         return self._component_name
  
      def initialize(self, filename):
-         self._args = [filename or 'ilamb.cfg']
+         self.config.load(filename)
+         os.environ['ILAMB_ROOT'] = self.config.get_ilamb_root()
+         os.environ['MPLBACKEND'] = 'Agg'
+         self._args = self.config.get_arguments()
  
      def update(self):
-         subprocess.check_call(self.args, shell=False, env=self._env)
+         with open('stdout', 'w') as fp:
+             subprocess.check_call(self.args, stdout=fp)
          self._time = self.get_end_time()
  
      def update_until(self, time):
-         self.update(time)
+         self.update()
  
      def finalize(self):
          pass
  
      def get_input_var_names(self):
          return ()
  
      def get_output_var_names(self):
          return ()
  
      def get_start_time(self):
          return 0.0
  
      def get_end_time(self):
          return 1.0
  
      def get_current_time(self):
          return self._time
  
      def get_time_step(self):
          return 1.0
  
      def get_time_units(self):
          return 's'
  
Update ILAMB BMI to use Configuration
## Code Before:
import sys
import subprocess
from basic_modeling_interface import Bmi


class BmiIlamb(Bmi):
    _command = 'ilamb-run'
    _args = None
    _env = None

    def __init__(self):
        self._time = self.get_start_time()

    @property
    def args(self):
        return [self._command] + (self._args or [])

    def get_component_name(self):
        return 'ILAMB'

    def initialize(self, filename):
        self._args = [filename or 'ilamb.cfg']

    def update(self):
        subprocess.check_call(self.args, shell=False, env=self._env)
        self._time = self.get_end_time()

    def update_until(self, time):
        self.update(time)

    def finalize(self):
        pass

    def get_input_var_names(self):
        return ()

    def get_output_var_names(self):
        return ()

    def get_start_time(self):
        return 0.0

    def get_end_time(self):
        return 1.0

    def get_current_time(self):
        return self._time

    def get_time_step(self):
        return 1.0

    def get_time_units(self):
        return 's'

## Instruction:
Update ILAMB BMI to use Configuration
## Code After:
"""Basic Model Interface (BMI) for the ILAMB benchmarking system."""

import os
import subprocess
from basic_modeling_interface import Bmi
from .config import Configuration


class BmiIlamb(Bmi):
    _component_name = 'ILAMB'
    _command = 'ilamb-run'
    _args = None

    def __init__(self):
        self._time = self.get_start_time()
        self.config = Configuration()

    @property
    def args(self):
        return [self._command] + (self._args or [])

    def get_component_name(self):
        return self._component_name

    def initialize(self, filename):
        self.config.load(filename)
        os.environ['ILAMB_ROOT'] = self.config.get_ilamb_root()
        os.environ['MPLBACKEND'] = 'Agg'
        self._args = self.config.get_arguments()

    def update(self):
        with open('stdout', 'w') as fp:
            subprocess.check_call(self.args, stdout=fp)
        self._time = self.get_end_time()

    def update_until(self, time):
        self.update()

    def finalize(self):
        pass

    def get_input_var_names(self):
        return ()

    def get_output_var_names(self):
        return ()

    def get_start_time(self):
        return 0.0

    def get_end_time(self):
        return 1.0

    def get_current_time(self):
        return self._time

    def get_time_step(self):
        return 1.0

    def get_time_units(self):
        return 's'
