-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularize config module. No user visible changes
- Loading branch information
Showing
1 changed file
with
45 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,64 @@ | ||
import configparser | ||
import os | ||
|
||
# per the XDG basedir-spec we adhere to $XDG_CONFIG_HOME if it's set, otherwise assume $HOME/.config | ||
xdg-config-home = '' | ||
if 'XDG_CONFIG_HOME' in os.environ: | ||
xdg-config-home = os.environ['XDG_CONFIG_HOME'] | ||
else: | ||
xdg-config-home = "%s/.config" % os.path.expanduser('~') | ||
|
||
# XDG base-dir: "If, when attempting to write a file, the destination directory is non-existant an attempt should be made to create it with permission 0700. If the destination directory exists already the permissions should not be changed." | ||
if not os.path.isdir(xdg-config-home): | ||
os.mkdir(xdg-config-home, 0700) | ||
|
||
# finally create our own config dir | ||
config-dir = "%s/%s" % (xdg-config-home, 'python-cozify') | ||
if not os.path.isdir(config-dir): | ||
os.mkdir(config-dir, 0o0700) | ||
|
||
stateFile = "%s/python-cozify.cfg" % config-dir | ||
state = None | ||
|
||
|
||
def stateWrite(): | ||
with open(stateFile, 'w') as configfile: | ||
state.write(configfile) | ||
# write current state to file storage | ||
# if tmpstate is defined, write that specific state object | ||
def stateWrite(tmpstate=None): | ||
global state_file | ||
if tmpstate is None: | ||
global state | ||
tmpstate = state | ||
with open(state_file, 'w') as cf: | ||
tmpstate.write(cf) | ||
|
||
# allow setting the state storage location. | ||
# Useful especially for testing without affecting your normal state | ||
def setStatePath(filepath): | ||
global stateFile | ||
stateFile = filepath | ||
_initState() | ||
|
||
global state_file | ||
global state | ||
state_file = filepath | ||
state = _initState(state_file) | ||
|
||
|
||
def _initState(): | ||
global state | ||
|
||
def _initState(state_file): | ||
# prime state storage | ||
# if we can read it, read it in, otherwise create empty file | ||
state = configparser.ConfigParser() | ||
try: | ||
file = open(stateFile, 'r') | ||
cf = open(state_file, 'r') | ||
except IOError: | ||
file = open(stateFile, 'w+') | ||
os.chmod(stateFile, 0o600) # set to user readwrite only to protect tokens | ||
|
||
state = configparser.ConfigParser() | ||
state.read(stateFile) | ||
cf = open(state_file, 'w+') | ||
os.chmod(state_file, 0o600) # set to user readwrite only to protect tokens | ||
else: | ||
state.read_file(cf) | ||
|
||
# make sure config is in roughly a valid state | ||
for key in [ 'Cloud', 'Hubs' ]: | ||
if key not in state: | ||
state[key] = {} | ||
stateWrite() | ||
stateWrite(state) | ||
return state | ||
|
||
def _initXDG(): | ||
# per the XDG basedir-spec we adhere to $XDG_CONFIG_HOME if it's set, otherwise assume $HOME/.config | ||
xdg_config_home = '' | ||
if 'XDG_CONFIG_HOME' in os.environ: | ||
xdg_config_home = os.environ['XDG_CONFIG_HOME'] | ||
else: | ||
xdg_config_home = "%s/.config" % os.path.expanduser('~') | ||
|
||
# XDG base-dir: "If, when attempting to write a file, the destination directory is non-existant an attempt should be made to create it with permission 0700. If the destination directory exists already the permissions should not be changed." | ||
if not os.path.isdir(xdg_config_home): | ||
os.mkdir(xdg_config_home, 0o0700) | ||
|
||
# finally create our own config dir | ||
config_dir = "%s/%s" % (xdg_config_home, 'python-cozify') | ||
if not os.path.isdir(config_dir): | ||
os.mkdir(config_dir, 0o0700) | ||
|
||
state_file = "%s/python-cozify.cfg" % config_dir | ||
return state_file | ||
|
||
_initState() | ||
state_file = _initXDG() | ||
state = _initState(state_file) |