Skip to content

Commit

Permalink
Added examples, refactored parameters creation, added fixtures, clean…
Browse files Browse the repository at this point in the history
…ed up.
  • Loading branch information
why-not-try-calmer committed Jun 12, 2023
1 parent 3920673 commit 95b078e
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 79 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ commands:

- The code is under a **git** repository (`git archive` is used to bundle the plugin).
- There is no uncommitted changes when doing a package/release (althought there is an option to bypass this requirement).
- A configuration at the top directory either in `.qgis-plugin-ci` or in `setup.cfg` or `pyproject.toml` with a `[qgis-plugin-ci]` section with the following fields:
- github_organization_slug (unless you're running from Travis CI)
- plugin_path
- project_slug (unless you're running from Travis CI)
- The source files of the plugin are within a sub-directory, with among others, a `metadata.txt` file with the following fields:
- A configuration at the top directory either in `.qgis-plugin-ci` or in `setup.cfg` or `pyproject.toml` with a `[qgis-plugin-ci]` section (see `docs/configuration/options.md` for details).
- The source files of the plugin are within a sub-directory with a `metadata.txt` file with the following fields:
- description
- qgisMinimumVersion
- repository
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ plugin_path = QuickOSM
github_organization_slug = 3liz
project_slug = QuickOSM
```
### Using TOML file `pyproject.toml`

```toml
[qgis-plugin-ci]
plugin_path = "qgis_plugin_ci_testing"
github_organization_slug = "opengisch"
project_slug = "qgis-plugin-ci"
```
4 changes: 2 additions & 2 deletions qgispluginci/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from importlib.metadata import version

from qgispluginci.changelog import ChangelogParser
from qgispluginci.parameters import Parameters
from qgispluginci.release import release
from qgispluginci.translation import Translation
from qgispluginci.utils import make_parameters

__version__ = version("qgis-plugin-ci")
__title__ = "QGISPluginCI"
Expand Down Expand Up @@ -164,7 +164,7 @@ def cli():
exit_val = 0

# Initialize Parameters
parameters = make_parameters(args)
parameters = Parameters.make_from(args=args)
# CHANGELOG
if args.command == "changelog":
try:
Expand Down
71 changes: 68 additions & 3 deletions qgispluginci/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
# ##################################

# standard library
import configparser
import datetime
import logging
import os
import re
import sys
from typing import Any, Callable, Iterator, Optional, Tuple
from typing import Any, Callable, Dict, Iterator, Optional, Tuple

import toml
import yaml

# 3rd party
from slugify import slugify

from qgispluginci.exceptions import ConfigurationNotFound

# ############################################################################
# ########## Globals #############
# ################################
Expand Down Expand Up @@ -98,7 +104,67 @@ class Parameters:
"""

def __init__(self, definition: dict):
@classmethod
def make_from(
cls, *, args: Optional[Any] = None, config_file: Optional[str] = None
) -> "Parameters":
"""
Instantiate from a config file or by exploring the filesystem
Accepts an argparse Namespace for backward compatibility.
"""
configuration_not_found = ConfigurationNotFound(
".qgis-plugin-ci or setup.cfg or pyproject.toml with a 'qgis-plugin-ci' section have not been found."
)

def explore_config() -> Dict[str, Any]:
if os.path.isfile(".qgis-plugin-ci"):
# We read the .qgis-plugin-ci file
with open(".qgis-plugin-ci", encoding="utf8") as f:
arg_dict = yaml.safe_load(f)
elif os.path.isfile("pyproject.toml"):
# We read the pyproject.toml file
with open("pyproject.toml", encoding="utf8") as f:
arg_dict = toml.load(f)
else:
config = configparser.ConfigParser()
config.read("setup.cfg")
if "qgis-plugin-ci" in config.sections():
# We read the setup.cfg file
arg_dict = dict(config.items("qgis-plugin-ci"))
else:
# We don't have either a .qgis-plugin-ci or a setup.cfg
if args and args.command == "changelog":
# but for the "changelog" sub command, the config file is not required, we can continue
arg_dict = dict()
else:
raise configuration_not_found
return arg_dict

def load_config(path_to_file: str) -> Dict[str, Any]:
if "setup.cfg" in path_to_file:
with open(path_to_file) as fh:
print(fh.read())
config = configparser.ConfigParser()
config.read(path_to_file)
return dict(config.items("qgis-plugin-ci"))

with open(path_to_file) as f:
if ".qgis-plugin-ci" in path_to_file:
return yaml.safe_load(f)
_, suffix = path_to_file.rsplit(".", 1)
if suffix == "toml":
contents = toml.load(f)
return contents["qgis-plugin-ci"]

raise configuration_not_found

if config_file:
config_dict = load_config(config_file)
else:
config_dict = explore_config()
return cls(config_dict)

def __init__(self, definition: Dict[str, Any]):
self.plugin_path = definition.get("plugin_path")

get_metadata = self.collect_metadata()
Expand Down Expand Up @@ -188,7 +254,6 @@ def collect_metadata(self) -> Callable[[str, Optional[Any]], Any]:
"""
metadata_file = f"{self.plugin_path}/metadata.txt"
metadata = {}

with open(metadata_file) as fh:
for line in fh:
split = line.strip().split("=", 1)
Expand Down
65 changes: 1 addition & 64 deletions qgispluginci/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import configparser
import logging
import os
import re
from math import floor
from math import log as math_log
from math import pow
from typing import Any, Dict, Optional, Union
from typing import Union

import toml
import yaml

from qgispluginci.exceptions import ConfigurationNotFound
from qgispluginci.parameters import Parameters
from qgispluginci.version_note import VersionNote

# GLOBALS
Expand All @@ -35,63 +29,6 @@ def configure_file(source_file: str, dest_file: str, replace: dict):
f.write(content)


def make_parameters(args=None, config_file: Optional[str] = None) -> Parameters:
"""
Make a Dict from a config file or by exploring the filesystem
Accepts an argparse Namespace for backward compatibility.
"""
configuration_not_found = ConfigurationNotFound(
".qgis-plugin-ci or setup.cfg or pyproject.toml with a 'qgis-plugin-ci' section have not been found."
)

def explore_config() -> Dict[str, Any]:
if os.path.isfile(".qgis-plugin-ci"):
# We read the .qgis-plugin-ci file
with open(".qgis-plugin-ci", encoding="utf8") as f:
arg_dict = yaml.safe_load(f)
elif os.path.isfile("pyproject.toml"):
# We read the pyproject.toml file
with open("pyproject.toml", encoding="utf8") as f:
arg_dict = toml.load(f)
else:
config = configparser.ConfigParser()
config.read("setup.cfg")
if "qgis-plugin-ci" in config.sections():
# We read the setup.cfg file
arg_dict = dict(config.items("qgis-plugin-ci"))
else:
# We don't have either a .qgis-plugin-ci or a setup.cfg
if args and args.command == "changelog":
# but for the "changelog" sub command, the config file is not required, we can continue
arg_dict = dict()
else:
raise configuration_not_found
return arg_dict

def load_config(filename: str) -> Dict[str, Any]:
if filename == "setup.cfg":
config = configparser.ConfigParser()
config.read(filename)
return dict(config.items("qgis-plugin-ci"))

_, suffix = filename.rsplit(".", 1)

with open(filename) as f:
if suffix == "toml":
return toml.load(f)
elif suffix in {"yaml", "yml"}:
return yaml.safe_load(f)

raise configuration_not_found

if config_file:
config_dict = load_config(config_file)
else:
config_dict = explore_config()

return Parameters(config_dict)


def convert_octets(octets: int) -> str:
"""Convert a mount of octets in readable size.
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/.qgis-plugin-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugin_path: qgis_plugin_CI_testing
github_organization_slug: opengisch
project_slug: qgis-plugin-ci
transifex_coordinator: geoninja
transifex_organization: pytransifex
translation_languages:
- fr
- it
- de
create_date: 1985-07-21

repository_url: https://github.com/opengisch/qgis-plugin-ci/

#lrelease_path: /usr/local/opt/qt5/bin/lrelease
4 changes: 4 additions & 0 deletions test/fixtures/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[qgis-plugin-ci]
plugin_path = "qgis_plugin_CI_testing"
github_organization_slug = "opengisch"
project_slug = "qgis_plugin_ci_testing"
4 changes: 4 additions & 0 deletions test/fixtures/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[qgis-plugin-ci]
plugin_path = qgis_plugin_CI_testing
github_organization_slug = opengisch
project_slug = qgis_plugin_ci_testing
16 changes: 11 additions & 5 deletions test/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from qgispluginci.parameters import DASH_WARNING, Parameters
from qgispluginci.release import release
from qgispluginci.translation import Translation
from qgispluginci.utils import make_parameters, replace_in_file
from qgispluginci.utils import replace_in_file

# Tests
from .utils import can_skip_test
Expand All @@ -31,10 +31,15 @@

class TestRelease(unittest.TestCase):
def setUp(self):
self.setup_params = make_parameters("setup.cfg")
self.qgis_plugin_config_params = make_parameters(".qgis-plugin-ci")
self.pyproject_params = make_parameters("pyproject.toml")

self.setup_params = Parameters.make_from(
config_file=os.path.relpath("test/fixtures/setup.cfg")
)
self.qgis_plugin_config_params = Parameters.make_from(
config_file=os.path.relpath("test/fixtures/.qgis-plugin-ci.yml")
)
self.pyproject_params = Parameters.make_from(
config_file=os.path.realpath("test/fixtures/pyproject.toml")
)
self.tx_api_token = os.getenv("tx_api_token")
self.github_token = os.getenv("github_token")
self.repo = None
Expand Down Expand Up @@ -72,6 +77,7 @@ def test_release_from_dot_qgis_plugin_ci(self):
release(self.qgis_plugin_config_params, RELEASE_VERSION_TEST)

def test_release_from_pyproject(self):
print(self.pyproject_params)
release(self.pyproject_params, RELEASE_VERSION_TEST)

@unittest.skipIf(can_skip_test(), "Missing tx_api_token")
Expand Down

0 comments on commit 95b078e

Please sign in to comment.