Skip to content

Commit

Permalink
Configuration file (#56)
Browse files Browse the repository at this point in the history
* Added a simple config parser and updated argument defaults

* Fixed import sorting

* Update changelog

* Added configuration instructions to the readme

* Added logging level to configuration file

* Fixed import sorting
  • Loading branch information
milivojevicu authored Sep 17, 2023
1 parent 0a63360 commit 9991906
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 12 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## Version 0.2.0

- [ ] Handle no album art being available when the file is missing.
- [ ] Keybinds for forcing album art refresh.
- [ ] Add ability to change settings through a config file.
- [ ] Better logging.
- [x] Handle no album art being available when the file is missing.
- [x] Keybinds for forcing album art refresh.
- [x] Add ability to change settings through a config file.

## Version 0.1.0

Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@
[![Code QC](https://github.com/milivojevicu/mpcover/actions/workflows/check.yml/badge.svg)](https://github.com/milivojevicu/mpcover/actions/workflows/check.yml)

Python program for displaying album covers of music currently playing through MPD using tkinter.

## Configuration

The configuration file should be located in the user home directory with the name ".mpcover.init".

For more information on where the user home directory is,
reference [`os.path.expanduser`](https://docs.python.org/3/library/os.path.html#os.path.expanduser).

Example configuration file:

```ini
[connection]
# Connection settings. The password is optional, to leave it unset simply remove
# the "password = ..." line from the configuration file.
port = 6600
host = localhost
password = example_password

[logging]
level = info

[binds]
# The values should be `tkinter` key bind strings.
refresh = r
```
5 changes: 4 additions & 1 deletion mpcover/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging

from .config import get_config

logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(levelname)7s : %(name)s : %(message)s"
level=get_config().get("logging", "level").upper(),
format="%(asctime)s %(levelname)8s : %(name)s -> %(message)s",
)
35 changes: 29 additions & 6 deletions mpcover/__main__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
#!python

import argparse
import configparser
import logging

from .config import get_config
from .gui import init

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


def parse_arguments():
parser = argparse.ArgumentParser(description="Album cover viewer for MPD.")
def parse_arguments(config: configparser.ConfigParser) -> argparse.Namespace:
"""
Parse arguments from the command line.
:arg config: Configuration object read from a file. Used to set default values.
Values from the command line override the values from the config file.
:return: `argparse.Namespace` object with the parsed arguments.
"""

parser = argparse.ArgumentParser(
description="Album cover viewer for MPD.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

parser.add_argument(
"-a",
"--address",
metavar="ADDRESS",
type=str,
default="127.0.0.1",
default=config.get("connection", "host"),
help="MPD server IP address",
)

parser.add_argument(
"-p", "--port", metavar="PORT", type=int, default=6600, help="MPD server port"
"-p",
"--port",
metavar="PORT",
type=int,
default=config.get("connection", "port"),
help="MPD server port",
)

parser.add_argument(
"-s",
"--pass",
metavar="PASSWORD",
dest="password",
type=str,
default=None,
default=config.get("connection", "password", fallback=None),
help="password for auth with the MPD server",
)

Expand All @@ -40,7 +62,8 @@ def run():
Entry point. Called when the `mpcover` command is called.
"""

arguments = parse_arguments()
config = get_config()
arguments = parse_arguments(config)
address = arguments.address, arguments.port
init(address, arguments.password)

Expand Down
45 changes: 45 additions & 0 deletions mpcover/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import configparser
import os.path

__DEFAULTS_CONNECTION = {
"host": "localhost",
"port": 6600,
}

__DEFAULTS_LOGGING = {
"level": "info",
}

__DEFAULTS_BINDS = {
"refresh": "r",
}

__CONFIG = None


def get_config():
"""
Read configuration from a file.
"""

global __CONFIG

if __CONFIG is not None:
return __CONFIG

config = configparser.ConfigParser()

# Load default settings.
config.read_dict({"connection": __DEFAULTS_CONNECTION})
config.read_dict({"logging": __DEFAULTS_LOGGING})
config.read_dict({"binds": __DEFAULTS_BINDS})

# Read user settings from a file.
config.read(os.path.expanduser(os.path.join("~", ".mpcover.ini")))

__CONFIG = config

return config


__all__ = "get_config"
6 changes: 5 additions & 1 deletion mpcover/gui/root.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import configparser
import io
import tkinter as tk
from logging import getLogger
Expand All @@ -6,6 +7,7 @@

from PIL import Image, ImageTk

from ..config import get_config
from ..connection import Connection
from ..controler import Controler

Expand Down Expand Up @@ -41,6 +43,8 @@ class Root(tk.Tk):
def __init__(self, address: Tuple[str, int], password: Optional[str]):
super().__init__()

config: configparser.ConfigParser = get_config()

# Connect to MPD.
self.__connection: Connection = Connection(*address)
self.__controler: Controler = Controler(self.__connection, password)
Expand Down Expand Up @@ -105,7 +109,7 @@ def __init__(self, address: Tuple[str, int], password: Optional[str]):
self.__get_album_art()

# Handle whole window keybinds.
self.bind("r", self.__get_album_art)
self.bind(config.get("binds", "refresh"), self.__get_album_art)

def on_close(self):
"""
Expand Down

0 comments on commit 9991906

Please sign in to comment.