import configparser
import os

def ephemeralWrite():
    with open(ephemeralFile, 'w') as configfile:
        ephemeral.write(configfile)

# prime ephemeral storage
ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
try:
    file = open(ephemeralFile, 'r')
except IOError:
    file = open(ephemeralFile, 'w+')
    os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens

ephemeral = configparser.ConfigParser()
ephemeral.read(ephemeralFile)

# make sure config is in roughly a valid state
for key in [ 'Cloud', 'Hubs' ]:
    if key not in ephemeral:
        ephemeral[key] = {}
ephemeralWrite()

import configparser
import os

ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
ephemeral = None


def ephemeralWrite():
    with open(ephemeralFile, 'w') as configfile:
        ephemeral.write(configfile)

# allow setting the ephemeral storage location.
# Useful especially for testing without affecting your normal state
def setStatePath(filepath):
    global ephemeralFile
    ephemeralFile = filepath
    _initState()



def _initState():
    global ephemeral

    # prime ephemeral storage
    try:
        file = open(ephemeralFile, 'r')
    except IOError:
        file = open(ephemeralFile, 'w+')
        os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens

    ephemeral = configparser.ConfigParser()
    ephemeral.read(ephemeralFile)

    # make sure config is in roughly a valid state
    for key in [ 'Cloud', 'Hubs' ]:
        if key not in ephemeral:
            ephemeral[key] = {}
    ephemeralWrite()

_initState()

Support for changing ephemeral state storage mid-run. Mostly useful for debugging and testing without hosing your main state
Support for changing ephemeral state storage mid-run.
Mostly useful for debugging and testing without hosing your main state

  import configparser
  import os
+ 
+ ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
+ ephemeral = None
+ 
  
  def ephemeralWrite():
      with open(ephemeralFile, 'w') as configfile:
          ephemeral.write(configfile)
  
+ # allow setting the ephemeral storage location.
+ # Useful especially for testing without affecting your normal state
+ def setStatePath(filepath):
+     global ephemeralFile
+     ephemeralFile = filepath
+     _initState()
- # prime ephemeral storage
- ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
- try:
-     file = open(ephemeralFile, 'r')
- except IOError:
-     file = open(ephemeralFile, 'w+')
-     os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens
  
- ephemeral = configparser.ConfigParser()
- ephemeral.read(ephemeralFile)
  
- # make sure config is in roughly a valid state
- for key in [ 'Cloud', 'Hubs' ]:
-     if key not in ephemeral:
-         ephemeral[key] = {}
- ephemeralWrite()
  
+ def _initState():
+     global ephemeral
+ 
+     # prime ephemeral storage
+     try:
+         file = open(ephemeralFile, 'r')
+     except IOError:
+         file = open(ephemeralFile, 'w+')
+         os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens
+ 
+     ephemeral = configparser.ConfigParser()
+     ephemeral.read(ephemeralFile)
+ 
+     # make sure config is in roughly a valid state
+     for key in [ 'Cloud', 'Hubs' ]:
+         if key not in ephemeral:
+             ephemeral[key] = {}
+     ephemeralWrite()
+ 
+ _initState()
+ 
Support for changing ephemeral state storage mid-run. Mostly useful for debugging and testing without hosing your main state
## Code Before:
import configparser
import os

def ephemeralWrite():
    with open(ephemeralFile, 'w') as configfile:
        ephemeral.write(configfile)

# prime ephemeral storage
ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
try:
    file = open(ephemeralFile, 'r')
except IOError:
    file = open(ephemeralFile, 'w+')
    os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens

ephemeral = configparser.ConfigParser()
ephemeral.read(ephemeralFile)

# make sure config is in roughly a valid state
for key in [ 'Cloud', 'Hubs' ]:
    if key not in ephemeral:
        ephemeral[key] = {}
ephemeralWrite()

## Instruction:
Support for changing ephemeral state storage mid-run. Mostly useful for debugging and testing without hosing your main state
## Code After:
import configparser
import os

ephemeralFile = "%s/.config/python-cozify.cfg" % os.path.expanduser('~')
ephemeral = None


def ephemeralWrite():
    with open(ephemeralFile, 'w') as configfile:
        ephemeral.write(configfile)

# allow setting the ephemeral storage location.
# Useful especially for testing without affecting your normal state
def setStatePath(filepath):
    global ephemeralFile
    ephemeralFile = filepath
    _initState()



def _initState():
    global ephemeral

    # prime ephemeral storage
    try:
        file = open(ephemeralFile, 'r')
    except IOError:
        file = open(ephemeralFile, 'w+')
        os.chmod(ephemeralFile, 0o600) # set to user readwrite only to protect tokens

    ephemeral = configparser.ConfigParser()
    ephemeral.read(ephemeralFile)

    # make sure config is in roughly a valid state
    for key in [ 'Cloud', 'Hubs' ]:
        if key not in ephemeral:
            ephemeral[key] = {}
    ephemeralWrite()

_initState()
