Move config dir init logic to config.init
This commit is contained in:
parent
d60340e9e9
commit
34d3a08416
@ -9,57 +9,9 @@ def command_init(args):
|
||||
pidfile, a lexicon config directory, and a user config
|
||||
directory.
|
||||
"""
|
||||
from collections import OrderedDict
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import config.init
|
||||
|
||||
import resources
|
||||
|
||||
cfd = args.config_dir
|
||||
# Create the directory if it doesn't exist.
|
||||
if not os.path.isdir(cfd):
|
||||
os.mkdir(cfd)
|
||||
# The directory should be empty if we're not updating an existing one.
|
||||
if len(os.listdir(cfd)) > 0 and not args.update:
|
||||
print("Directory {} is not empty".format(cfd))
|
||||
return -1
|
||||
|
||||
# Update or create global config.
|
||||
def_cfg = resources.get_stream("global.json")
|
||||
if args.update and os.path.isfile(os.path.join(cfd, "config.json")):
|
||||
with open(os.path.join(cfd, "config.json"), 'r+', encoding='utf8') as cfg_file:
|
||||
fcntl.lockf(cfg_file, fcntl.LOCK_EX)
|
||||
old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict)
|
||||
new_cfg = json.load(def_cfg, object_pairs_hook=OrderedDict)
|
||||
merged = {}
|
||||
for key in new_cfg:
|
||||
merged[key] = old_cfg[key] if key in old_cfg else new_cfg[key]
|
||||
if key not in old_cfg:
|
||||
print("Added key '{}' to config".format(key))
|
||||
for key in old_cfg:
|
||||
if key not in new_cfg:
|
||||
print("Config contains unknown key '{}'".format(key))
|
||||
merged[key] = old_cfg[key]
|
||||
cfg_file.seek(0)
|
||||
json.dump(merged, cfg_file, allow_nan=False, indent='\t')
|
||||
cfg_file.truncate()
|
||||
fcntl.lockf(cfg_file, fcntl.LOCK_UN)
|
||||
else:
|
||||
with open(os.path.join(cfd, "config.json"), 'wb') as f:
|
||||
f.write(def_cfg.read())
|
||||
# Ensure pidfile exists.
|
||||
if not os.path.isfile(os.path.join(cfd, "pid")):
|
||||
with open(os.path.join(cfd, "pid"), 'w') as f:
|
||||
f.write(str(os.getpid()))
|
||||
# Ensure subdirs exist.
|
||||
if not os.path.isdir(os.path.join(cfd, "lexicon")):
|
||||
os.mkdir(os.path.join(cfd, "lexicon"))
|
||||
if not os.path.isdir(os.path.join(cfd, "user")):
|
||||
os.mkdir(os.path.join(cfd, "user"))
|
||||
if not os.path.isfile(os.path.join(cfd, 'user', 'index.json')):
|
||||
with open(os.path.join(cfd, 'user', 'index.json'), 'w') as f:
|
||||
json.dump({}, f)
|
||||
config.init.create_config_dir(args.config_dir, args.update)
|
||||
|
||||
@no_argument
|
||||
def command_generate_secret(args):
|
||||
|
@ -6,9 +6,76 @@ import os
|
||||
|
||||
# Module imports
|
||||
from errors import MissingConfigError, MalformedConfigError
|
||||
import config
|
||||
from config.loader import json_ro
|
||||
import resources
|
||||
|
||||
def create_config_dir(config_dir, update=False):
|
||||
"""
|
||||
Create or update a config directory
|
||||
"""
|
||||
from collections import OrderedDict
|
||||
import fcntl
|
||||
|
||||
path = config.prepend
|
||||
|
||||
# Create the directory if it doesn't exist.
|
||||
if not os.path.isdir(config_dir):
|
||||
os.mkdir(config_dir)
|
||||
|
||||
# Initialize the config dir without verification
|
||||
config.CONFIG_DIR = config_dir
|
||||
|
||||
# The directory should be empty if we're not updating an existing one.
|
||||
if len(os.listdir(config_dir)) > 0 and not update:
|
||||
print("Directory {} is not empty".format(config_dir))
|
||||
return -1
|
||||
|
||||
# Update or create global config.
|
||||
def_cfg = resources.get_stream("global.json")
|
||||
global_config_path = path("config.json")
|
||||
if update and os.path.isfile(global_config_path):
|
||||
# We need to write an entirely different ordereddict to the config
|
||||
# file, so we mimic the config.loader functionality manually.
|
||||
with open(global_config_path, 'r+', encoding='utf8') as cfg_file:
|
||||
fcntl.lockf(cfg_file, fcntl.LOCK_EX)
|
||||
old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict)
|
||||
new_cfg = json.load(def_cfg, object_pairs_hook=OrderedDict)
|
||||
merged = {}
|
||||
for key in new_cfg:
|
||||
merged[key] = old_cfg[key] if key in old_cfg else new_cfg[key]
|
||||
if key not in old_cfg:
|
||||
print("Added key '{}' to config".format(key))
|
||||
for key in old_cfg:
|
||||
if key not in new_cfg:
|
||||
print("Config contains unknown key '{}'".format(key))
|
||||
merged[key] = old_cfg[key]
|
||||
cfg_file.seek(0)
|
||||
json.dump(merged, cfg_file, allow_nan=False, indent='\t')
|
||||
cfg_file.truncate()
|
||||
fcntl.lockf(cfg_file, fcntl.LOCK_UN)
|
||||
else:
|
||||
with open(path("config.json"), 'wb') as f:
|
||||
f.write(def_cfg.read())
|
||||
|
||||
# Ensure pidfile exists.
|
||||
if not os.path.isfile(path("pid")):
|
||||
with open(path("pid"), 'w') as f:
|
||||
f.write(str(os.getpid()))
|
||||
|
||||
# Ensure lexicon subdir exists.
|
||||
if not os.path.isdir(path("lexicon")):
|
||||
os.mkdir(path("lexicon"))
|
||||
if not os.path.isfile(path("lexicon", "index.json")):
|
||||
with open(path("lexicon", "index.json"), 'w') as f:
|
||||
json.dump({}, f)
|
||||
|
||||
# Ensure user subdir exists.
|
||||
if not os.path.isdir(path("user")):
|
||||
os.mkdir(path("user"))
|
||||
if not os.path.isfile(path('user', 'index.json')):
|
||||
with open(path('user', 'index.json'), 'w') as f:
|
||||
json.dump({}, f)
|
||||
|
||||
def verify_config_dir(config_dir):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user