Exemplo n.º 1
0
    def load_file(self, f):
        """
    Reads the configuration parameters from a file and adds them to the inner set of parameters.

    :param string|io.TextIOBase|io.StringIO f:
    """
        if isinstance(f, str):
            import os
            assert os.path.isfile(f), "config file not found: %r" % f
            self.files.append(f)
            filename = f
            content = open(filename).read()
        else:
            # assume stream-like
            filename = "<config string>"
            content = f.read()
        content = content.strip()
        if content.startswith("#!"):  # assume Python
            from Util import custom_exec
            # Operate inplace on ourselves.
            # Also, we want that it's available as the globals() dict, so that defined functions behave well
            # (they would loose the local context otherwise).
            user_ns = self.typed_dict
            # Always overwrite:
            user_ns.update({
                "config": self,
                "__file__": filename,
                "__name__": "__crnn_config__"
            })
            custom_exec(content, filename, user_ns, user_ns)
            return
        if content.startswith("{"):  # assume JSON
            from Util import load_json
            json_content = load_json(content=content)
            assert isinstance(json_content, dict)
            self.update(json_content)
            return
        # old line-based format
        for line in content.splitlines():
            if "#" in line:  # Strip away comment.
                line = line[:line.index("#")]
            line = line.strip()
            if not line:
                continue
            line = line.split(None, 1)
            assert len(line) == 2, "unable to parse config line: %r" % line
            self.add_line(key=line[0], value=line[1])
Exemplo n.º 2
0
  def load_file(self, f):
    """
    Reads the configuration parameters from a file and adds them to the inner set of parameters.

    :param string|io.TextIOBase|io.StringIO f:
    """
    if isinstance(f, str):
      import os
      assert os.path.isfile(f), "config file not found: %r" % f
      self.files.append(f)
      filename = f
      content = open(filename).read()
    else:
      # assume stream-like
      filename = "<config string>"
      content = f.read()
    content = content.strip()
    if content.startswith("#!"):  # assume Python
      from Util import custom_exec
      # Operate inplace on ourselves.
      # Also, we want that it's available as the globals() dict, so that defined functions behave well
      # (they would loose the local context otherwise).
      user_ns = self.typed_dict
      # Always overwrite:
      user_ns.update({"config": self, "__file__": filename, "__name__": "__crnn_config__"})
      custom_exec(content, filename, user_ns, user_ns)
      return
    if content.startswith("{"):  # assume JSON
      from Util import load_json
      json_content = load_json(content=content)
      assert isinstance(json_content, dict)
      self.update(json_content)
      return
    # old line-based format
    for line in content.splitlines():
      if "#" in line:  # Strip away comment.
        line = line[:line.index("#")]
      line = line.strip()
      if not line:
        continue
      line = line.split(None, 1)
      assert len(line) == 2, "unable to parse config line: %r" % line
      self.add_line(key=line[0], value=line[1])