bmc/libbmc/config.py

111 lines
3.4 KiB
Python
Raw Normal View History

2014-08-03 12:38:40 +02:00
from __future__ import unicode_literals
2014-06-30 00:19:38 +02:00
import os
import errno
2014-06-30 23:02:30 +02:00
import imp
import inspect
2014-06-30 00:19:38 +02:00
import json
import sys
2014-08-03 00:17:01 +02:00
import libbmc.tools as tools
2014-06-30 00:19:38 +02:00
2014-06-30 23:25:51 +02:00
# List of available options (in ~/.config/bmc/bmc.json file):
2014-06-30 00:19:38 +02:00
# * folder : folder in which papers are stored
# * proxies : list of proxies to use, e.g. ['', "socks5://localhost:4711"]
# * format_articles, format_books : masks to rename files
# * ignore_fields : list of fields to ignore when returning bibtex
# Available masks to rename files are
# %f = last name of first author
# %l = last name of last author
# %j = name of the journal
# %Y = published year
# %t = title
# %a = authors
# %v = arXiv version (e.g. '-v1') or nothing if not an arXiv paper
2014-06-30 23:25:51 +02:00
# You can add your custom masks to rename files by adding functions in
# ~/.config/masks.py.
# E.g. : def f(x): x.replace('test', 'some_expr')
2014-06-30 00:19:38 +02:00
def make_sure_path_exists(path):
try:
os.makedirs(path)
2014-06-30 15:07:00 +02:00
return False
2014-06-30 00:19:38 +02:00
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
else:
2014-06-30 15:07:00 +02:00
return True
2014-06-30 00:19:38 +02:00
class Config():
2014-06-30 23:02:30 +02:00
def __init__(self, base_config_path="~/.config/bmc/"):
self.config_path = os.path.expanduser(base_config_path)
2014-06-30 00:19:38 +02:00
self.config = {}
2014-06-30 23:02:30 +02:00
self.config["format_custom"] = []
2014-06-30 00:19:38 +02:00
self.load()
2014-06-30 23:02:30 +02:00
def as_dict(self):
return self.config
2014-06-30 00:19:38 +02:00
def get(self, param):
2014-06-30 15:07:00 +02:00
return self.config.get(param, False)
2014-06-30 00:19:38 +02:00
def set(self, param, value):
self.config[param] = value
def initialize(self):
self.set("folder", os.path.expanduser("~/Papers/"))
self.set("proxies", [''])
self.set("format_articles", "%f_%l-%j-%Y%v")
self.set("format_books", "%a-%t")
self.set("format_custom", [])
self.set("ignore_fields", ["file", "doi", "tag"])
self.save()
def load(self):
try:
2014-06-30 10:50:33 +02:00
folder_exists = make_sure_path_exists(self.config_path)
2014-06-30 15:07:00 +02:00
if folder_exists and os.path.isfile(self.config_path + "bmc.json"):
2014-06-30 10:50:33 +02:00
initialized = True
else:
initialized = False
2014-06-30 15:07:00 +02:00
except OSError:
2014-06-30 00:19:38 +02:00
tools.warning("Unable to create ~/.config folder.")
sys.exit(1)
if not initialized:
self.initialize()
else:
try:
with open(self.config_path + "bmc.json", 'r') as fh:
self.config = json.load(fh)
except (ValueError, IOError):
tools.warning("Config file could not be read.")
sys.exit(1)
2014-06-30 23:02:30 +02:00
self.load_masks()
2014-06-30 00:19:38 +02:00
def save(self):
try:
2014-06-30 00:38:29 +02:00
with open(self.config_path + "bmc.json", 'w') as fh:
2014-06-30 00:19:38 +02:00
fh.write(json.dumps(self.config))
except IOError:
tools.warning("Could not write config file.")
sys.exit(1)
2014-06-30 23:02:30 +02:00
def load_masks(self):
if os.path.isfile(self.config_path + "masks.py"):
try:
self.info = imp.find_module("masks", [self.config_path])
self.masks = imp.load_module("masks", *self.info)
for mask in inspect.getmembers(self.masks, inspect.isfunction):
self.config["format_custom"].append(mask[1])
except ImportError:
self.clean()
tools.warning("Unable to import masks config file.")
pass
finally:
try:
self.info[0].close()
except AttributeError:
pass