class_docstr: Global configuration settings
class_docstr_tok: ["Global","configuration","settings"]
docstr: Load passed configuration file, if it exists; create a default
  otherwise. If this method finds an incorrect config file, it
  backs the file up with a human-readable timestamp suffix and
  creates a default one.
  
  :param config_file: no sanity checks are performed, as this
      method is for internal use only
  :type config_file: `os.path`
  :returns: a dictionary of parsed configuration options
  :rtype: `dict`
docstr_tok: ["Load","passed","configuration","file",",","if","it","exists",";","create","a","default","otherwise",".","If","this","method","finds","an","incorrect","config","file",",","it","backs","the","file","up","with","a","human-readable","timestamp","suffix","and","creates","a","default","one",".",":","param","config_file",":","no","sanity","checks","are","performed",",","as","this","method","is","for","internal","use","only",":","type","config_file",":","`","os.path","`",":","returns",":","a","dictionary","of","parsed","configuration","options",":","rtype",":","`","dict","`"]
code: def __load_or_create(config_file):
      """Load passed configuration file, if it exists; create a default
      otherwise. If this method finds an incorrect config file, it
      backs the file up with a human-readable timestamp suffix and
      creates a default one.
  
      :param config_file: no sanity checks are performed, as this
          method is for internal use only
      :type config_file: `os.path`
      :returns: a dictionary of parsed configuration options
      :rtype: `dict`
      """
      required_sections = [NiftyNetGlobalConfig.global_section]
      required_keys = {
          required_sections[0]: [NiftyNetGlobalConfig.home_key]
      }
      default_values = {
          required_sections[0]: {
              NiftyNetGlobalConfig.home_key: "~/niftynet"
          }
      }
  
      backup = False
      if isfile(config_file):
          try:
              config = ConfigParser()
              config.read(config_file)
  
              # check all required sections and keys present
              for required_section in required_sections:
                  if required_section not in config:
                      backup = True
                      break
  
                  for required_key in required_keys[required_section]:
                      if required_key not in config[required_section]:
                          backup = True
                          break
  
                  if backup:
                      break
  
          except Error:
              backup = True
  
          if not backup:  # loaded file contains all required
              # config options: so return
              return dict(config)
  
      config_dir, config_filename = split(config_file)
      if not isdir(config_dir):
          os.makedirs(config_dir)
  
      if (
          backup
      ):  # config file exists, but does not contain all required
          # config opts: so backup not to override
          timestamp = strftime("%Y-%m-%d-%H-%M-%S")
          random_str = "".join(
              choice(ascii_lowercase) for _ in range(3)
          )
          backup_suffix = "-".join(["backup", timestamp, random_str])
  
          filename, extension = splitext(config_filename)
          backup_filename = "".join(
              [filename, "-", backup_suffix, extension]
          )
          backup_file = join(config_dir, backup_filename)
          os.rename(config_file, backup_file)
  
      # create a new default global config file
      config = ConfigParser(default_values)
      for required_section in required_sections:
          for required_key in required_keys[required_section]:
              config.add_section(required_section)
              config[required_section][required_key] = default_values[
                  required_section
              ][required_key]
      with open(config_file, "w") as new_config_file:
          config.write(new_config_file)
      return dict(config)
  
file_path: NiftyNet/niftynet/utilities/niftynet_global_config.py
imports: import sys
  from random import choice
  from string import ascii_lowercase
  from time import strftime
  import os
  from os.path import expanduser, join, split, isdir, isfile, splitext
  from niftynet.utilities.decorators import singleton
label_desc: Use image node_id 4 for calling the NiftyNetGlobalConfig obj's underlying member method code with example usage: obj.__load_or_create(config_file) and returns: dict, dict