Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update common.py #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions zotero_cli/common.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,62 @@
import os
try:
import configparser
except ImportError:
import ConfigParser as configparser
try:
UNICODE_EXISTS = bool(type(unicode))
except NameError:
unicode = lambda s: str(s)
from collections import namedtuple

import click
import configparser

# Define application name and a namedtuple for items
APP_NAME = "zotcli"

Item = namedtuple("Item", ("key", "creator", "title", "abstract", "date", "citekey"))


def _get_config_path():
return os.path.join(click.get_app_dir(APP_NAME), 'config.ini')
"""
Get the path to the configuration file in the application directory.

:return: Path to the configuration file.
:rtype: str
"""
return os.path.join(click.get_app_dir(APP_NAME), 'config.ini')

def load_config():
""" Load configuration from application directory.
"""
Load configuration from the application directory.

:returns: Configuration
:rtype: (flat) dict
:return: Configuration data as a flat dictionary.
:rtype: dict
:raises ValueError: If the configuration file is not found.
"""
cfg_path = _get_config_path()
if not os.path.exists(cfg_path):
raise ValueError("Could not find configuration file. Please run "
"`zotcli configure` to perform the first-time "
"setup.")
"`zotcli configure` to perform the first-time setup.")

# Initialize parser and read configuration file
parser = configparser.RawConfigParser()
parser.read([cfg_path])
rv = {}
for section in parser.sections():
for key, value in parser.items(section):
rv['%s.%s' % (section, key)] = value
return rv

parser.read(cfg_path)

# Convert configuration to a flat dictionary
return {f'{section}.{key}': value
for section in parser.sections()
for key, value in parser.items(section)}

def save_config(cfgdata):
""" Save configuration to application directory.
"""
Save configuration to the application directory.

:param cfgdata: Configuration
:type cfgdata: (flat) dict
:param cfgdata: Configuration data as a flat dictionary.
:type cfgdata: dict
"""
cfg_path = _get_config_path()
cfg_dir = os.path.dirname(cfg_path)
if not os.path.exists(cfg_dir):
os.makedirs(cfg_dir)
cfg = configparser.SafeConfigParser()

# Create directory if it does not exist
os.makedirs(cfg_dir, exist_ok=True)

# Initialize configuration parser and add data
cfg = configparser.ConfigParser()
cfg.add_section("zotcli")
for key, value in cfgdata.items():
cfg.set("zotcli", key, unicode(value))
cfg.set("zotcli", key, str(value))

# Write configuration to file
with open(cfg_path, "w") as fp:
cfg.write(fp)