Skip to content

Commit

Permalink
Merge pull request #40 from VTimofeenko/confluence_poster-39
Browse files Browse the repository at this point in the history
Confluence poster 39

Closes #39
  • Loading branch information
VTimofeenko committed Jan 11, 2021
2 parents 2125687 + 8324d7d commit 2605e22
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Description

Supplementary script for writing Confluence articles in
local editor. Uses information from the config to post the article content to confluence.
local editor. Uses information from the config to post the article content to Confluence.

May be used either on its own:

Expand Down
13 changes: 9 additions & 4 deletions confluence_poster/config_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from xdg import xdg_config_dirs, xdg_config_home
from confluence_poster.poster_config import Config, PartialConfig
from collections import UserDict
from collections.abc import Mapping
Expand All @@ -7,7 +6,8 @@

def merge_configs(first_config: Mapping, other_config: Mapping):
"""Merges two configs together, like so:
{'auth': {'user': 'a'}} + {'auth': {'password': 'b'}} = {'auth': {'user': 'a', 'password': 'b'}}"""
{'auth': {'user': 'a'}} + {'auth': {'password': 'b'}} = {'auth': {'user': 'a', 'password': 'b'}}.
Other config values overwrite values in first config."""
for key in set(first_config).union(other_config):
if key in first_config and key in other_config:
check_list = [
Expand All @@ -34,9 +34,14 @@ def load_config(local_config: Path) -> Config:
"""Function that goes through the config directories trying to load the config.
Reads configs from XDG_CONFIG_DIRS, then from XDG_CONFIG_HOME, then the local one - either the default one, or
supplied through command line."""
import xdg.BaseDirectory
from importlib import reload

reload(xdg.BaseDirectory)

final_config = UserDict()
for path in xdg_config_dirs()[:-1] + [xdg_config_home()]:
config_path = path / "confluence_poster/config.toml"
for path in list(xdg.BaseDirectory.load_config_paths("confluence_poster"))[::-1]:
config_path = Path(path) / "config.toml"
if config_path.exists():
final_config = dict(
merge_configs(final_config, PartialConfig(file=config_path))
Expand Down
10 changes: 6 additions & 4 deletions confluence_poster/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from confluence_poster.page_creation_helpers import create_page
from confluence_poster.file_upload_helpers import attach_files_to_page

__version__ = "1.4.0"
__version__ = "1.4.1"
default_config_name = "config.toml"


Expand Down Expand Up @@ -328,7 +328,7 @@ def create_config(
),
):
"""Runs configuration wizard. The wizard guides through setting up values for configuration file."""
import xdg
import xdg.BaseDirectory
from confluence_poster.config_wizard import (
config_dialog,
get_filled_attributes_from_file,
Expand All @@ -341,7 +341,9 @@ def create_config(
confirm = state.confirm_function
prompt = state.prompt_function

home_config_location = xdg.xdg_config_home() / "confluence_poster/config.toml"
home_config_location = (
Path(xdg.BaseDirectory.xdg_config_home) / "confluence_poster/config.toml"
)

all_params = (
DialogParameter(
Expand Down Expand Up @@ -529,7 +531,7 @@ def main(
),
):
"""Supplementary script for writing Confluence articles in
local editor. Uses information from the config to post the article content to confluence.
local editor. Uses information from the config to post the article content to Confluence.
"""

Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="confluence_poster",
version="1.4.0",
version="1.4.1",
description="Script that updates Confluence articles from files written locally",
long_description=(here / "README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
Expand All @@ -25,11 +25,10 @@
"typer>=0.3.2",
"toml",
"requests",
"xdg>=5.0.1",
"pyxdg==0.26",
"tomlkit==0.7.0",
"click>=7.1.1, <7.2.0",
"markdown==3.3.3",
"marshmallow==3.10.0",
],
python_requires=">=3.8, <4",
tests_require=[
Expand All @@ -39,6 +38,7 @@
"beautifulsoup4==4.9.3",
"bs4",
"lxml",
"marshmallow==3.10.0",
],
extras_require={"docs": ["jinja2", "typer-cli"]},
classifiers=[
Expand Down
7 changes: 5 additions & 2 deletions tests/command tests/test_config_wizard_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pathlib import Path
import pytest


pytestmark = pytest.mark.offline

runner = CliRunner()
Expand All @@ -21,6 +20,8 @@ def setup_env_dirs(tmp_path, monkeypatch):
cwd.mkdir()

monkeypatch.setenv("XDG_CONFIG_HOME", str(new_home))
monkeypatch.setattr("xdg.BaseDirectory.xdg_config_home", str(new_home))

monkeypatch.chdir(cwd)


Expand Down Expand Up @@ -168,6 +169,7 @@ def test_dialog_home_directory_created(tmp_path, monkeypatch):
new_home_config: Path = tmp_path / "new_home" # new_home to override the fixture
new_home_config.mkdir()
monkeypatch.setenv("XDG_CONFIG_HOME", str(new_home_config))
monkeypatch.setattr("xdg.BaseDirectory.xdg_config_home", str(new_home_config))
assert not (new_home_config / "confluence_poster").exists()
_input = (
"",
Expand All @@ -179,5 +181,6 @@ def test_dialog_home_directory_created(tmp_path, monkeypatch):
"Y", # save the edit
)

default_run_cmd(input="\n".join(_input) + "\n", other_args=["--home-only"])
result = default_run_cmd(input="\n".join(_input) + "\n", other_args=["--home-only"])
assert result.exit_code == 0
assert (new_home_config / "confluence_poster").exists()
34 changes: 25 additions & 9 deletions tests/unit tests/test_config_loader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
from confluence_poster.poster_config import Config
import toml
from confluence_poster.config_loader import load_config, merge_configs
from utils import mk_tmp_file
from utils import mk_tmp_file, repo_config_path
from pathlib import Path
import pytest

from confluence_poster.poster_config import Config
from confluence_poster.config_loader import load_config

pytestmark = pytest.mark.offline


@pytest.fixture(scope="function", autouse=True)
def reload_xdg_module():
"""Needed because monkeypatch may pollute mass runs."""
from importlib import reload
import xdg.BaseDirectory

reload(xdg.BaseDirectory)
yield


@pytest.fixture(scope="function")
def setup_xdg_dirs(tmp_path):
my_xdg_config_dirs = [
Expand All @@ -26,12 +37,13 @@ def setup_xdg_dirs(tmp_path):
@pytest.mark.parametrize("dir_undefined", [None, "home", "dirs"])
def test_config_construct(tmp_path, setup_xdg_dirs, monkeypatch, dir_undefined):
"""Creates all XDG_CONFIG_ dirs for the test and checks that relevant keys are constructed"""

my_xdg_config_dirs, my_xdg_config_home = setup_xdg_dirs
global_config = Path(
f"{my_xdg_config_dirs.split(':')[0]}/confluence_poster/config.toml"
)
home_config = Path(f"{my_xdg_config_home}/confluence_poster/config.toml")
repo_config = toml.load("config.toml")
repo_config = toml.load(repo_config_path)

# Strip repo config into parts
if dir_undefined == "home":
Expand Down Expand Up @@ -64,12 +76,14 @@ def test_config_construct(tmp_path, setup_xdg_dirs, monkeypatch, dir_undefined):
monkeypatch.setenv("XDG_CONFIG_DIRS", str(my_xdg_config_dirs))

_ = load_config(local_config=config_file)
repo_config = Config("config.toml")
repo_config = Config(repo_config_path)
assert repo_config == _


def test_util_merge():
"""Tests the utility function that merges configs"""
from confluence_poster.config_loader import merge_configs

assert dict(merge_configs({"a": "b"}, {"c": "d"})) == {"a": "b", "c": "d"}
assert dict(
merge_configs({"auth": {"user": "a"}}, {"auth": {"password": "b"}})
Expand All @@ -86,20 +100,22 @@ def test_util_merge():

def test_no_configs_except_local(monkeypatch):
"""Checks that the script works if only the local config exists"""

monkeypatch.setenv("XDG_CONFIG_HOME", str(None))
monkeypatch.setenv("XDG_CONFIG_DIRS", str(None))
_ = load_config(Path("config.toml"))
repo_config = Config("config.toml")
_ = load_config(Path(repo_config_path))
repo_config = Config(repo_config_path)
assert repo_config == _


def test_multiple_xdg_config_dirs(tmp_path, setup_xdg_dirs, monkeypatch):
"""Checks that the value from leftmost XGD_CONFIG_DIRS is the applied one"""
"""Checks that the value from leftmost XDG_CONFIG_DIRS is the applied one"""
my_xdg_config_dirs, _ = setup_xdg_dirs
monkeypatch.setenv("XDG_CONFIG_DIRS", my_xdg_config_dirs)

my_xdg_config_dirs = my_xdg_config_dirs.split(":")

repo_config = toml.load("config.toml")
repo_config = toml.load(repo_config_path)
global_config = {
key: repo_config["auth"][key] for key in repo_config["auth"].keys()
}
Expand Down
1 change: 1 addition & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def locate_real_confluence_config_file(config_name="local_config.toml"):
# The config filename for testing against local instance
real_confluence_config = locate_real_confluence_config_file()
other_user_config = locate_real_confluence_config_file("local_config_other_user.toml")
repo_config_path = locate_real_confluence_config_file("config.toml")

if Path(real_confluence_config).exists():
real_config = Config(real_confluence_config)
Expand Down

0 comments on commit 2605e22

Please sign in to comment.