config can be placed in standard config dirs & passed as command line argument
This commit is contained in:
parent
ce01d4d665
commit
ac3d51ea36
14
README.md
14
README.md
@ -51,7 +51,7 @@ You can then run `spip2md` as a Python module with command `python -m spip2md`.
|
||||
Make sure to replace `spip2md` with a path to directory `spip2md` if you
|
||||
didn’t `cd` into this repository’s directory.
|
||||
|
||||
## Usage
|
||||
## Configuration and Usage
|
||||
|
||||
Make sure you have access to the SPIP database you want to export on a
|
||||
MySQL/MariaDB server. By default, `spip2md` expects a database named `spip` hosted on
|
||||
@ -62,10 +62,13 @@ If you want to copy over attached files like images, you also need access to
|
||||
the data directory of your SPIP website, usually named `IMG`, and either rename it
|
||||
`data` in your current working directory, or set `data_dir` setting to its path.
|
||||
|
||||
Currently, the config file you want to use can be given as the only CLI parameter,
|
||||
or if no parameter is given, the program searches a `spip2md.yml` file in the current
|
||||
working directory. Here’s the *default configuration options* with commentaries
|
||||
explaining their meaning :
|
||||
### YAML configuration file
|
||||
|
||||
To configure `spip2md` you can place a file named `spip2md.yml` in standard \*nix
|
||||
configuration locations, set it with the command line argument, or run the
|
||||
program with a `spip2md.yml` file in your working directory.
|
||||
|
||||
Here’s the *default configuration options* with comments explaining their meaning :
|
||||
|
||||
```yaml
|
||||
db: spip # Name of the database
|
||||
@ -97,7 +100,6 @@ clear_output: true # Clear output dir between runs instead of merging into
|
||||
|
||||
logfile: log-spip2md.log # Name of the logs file
|
||||
loglevel: WARNING # Refer to Python’s loglevels
|
||||
logname: spip2md # Beginning of log lines
|
||||
|
||||
export_filetype: md # Filetype of exported text files
|
||||
```
|
||||
|
@ -14,20 +14,48 @@ You should have received a copy of the GNU General Public License along with spi
|
||||
If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
# pyright: strict
|
||||
from os import environ
|
||||
from os.path import expanduser, isfile
|
||||
from typing import Optional
|
||||
|
||||
from yaml import Loader, load
|
||||
|
||||
CONFIG_PATHS = ("spip2md.yml", "spip2md.yaml")
|
||||
NAME: str = "spip2md" # Name of program, notably used in logs
|
||||
|
||||
|
||||
def config_file() -> Optional[str]:
|
||||
for path in CONFIG_PATHS:
|
||||
# Searches for a configuration file from all CLI args and in standard locations
|
||||
# & return his path if found
|
||||
def config(*start_locations: str) -> Optional[str]:
|
||||
# Search for config files in CLI arguments and function params first
|
||||
argv = __import__("sys").argv
|
||||
config_locations: list[str] = argv[1:] + list(start_locations)
|
||||
|
||||
if "XDG_CONFIG_HOME" in environ:
|
||||
config_locations += [
|
||||
environ["XDG_CONFIG_HOME"] + "/spip2md.yml",
|
||||
environ["XDG_CONFIG_HOME"] + "/spip2md.yaml",
|
||||
]
|
||||
|
||||
if "HOME" in environ:
|
||||
config_locations += [
|
||||
environ["HOME"] + "/.config/spip2md.yml",
|
||||
environ["HOME"] + "/.config/spip2md.yaml",
|
||||
environ["HOME"] + "/spip2md.yml",
|
||||
environ["HOME"] + "/spip2md.yaml",
|
||||
]
|
||||
|
||||
# Search in working directory in last resort
|
||||
config_locations += [
|
||||
"/spip2md.yml",
|
||||
"/spip2md.yaml",
|
||||
]
|
||||
|
||||
for path in config_locations:
|
||||
if isfile(path):
|
||||
return path
|
||||
|
||||
|
||||
# Global configuration object
|
||||
class Configuration:
|
||||
db: str = "spip" # DB name
|
||||
db_host: str = "localhost" # Where is the DB
|
||||
@ -49,11 +77,8 @@ class Configuration:
|
||||
ignore_pattern: list[str] = [] # Ignore objects of which title match
|
||||
logfile: str = "log-spip2md.log" # File where logs will be written, relative to wd
|
||||
loglevel: str = "WARNING" # Minimum criticity of logs written in logfile
|
||||
logname: str = "spip2md" # Labelling of logs
|
||||
export_filetype: str = "md" # Extension of exported text files
|
||||
debug_meta: bool = False # Include more metadata from SPIP DB in frontmatters
|
||||
# max_articles_export: int = 1000 # TODO reimplement
|
||||
# max_sections_export: int = 500 # TODO reimplement
|
||||
|
||||
def __init__(self, config_file: Optional[str] = None):
|
||||
if config_file is not None:
|
||||
@ -72,4 +97,4 @@ class Configuration:
|
||||
setattr(self, attr, config[attr])
|
||||
|
||||
|
||||
CFG = Configuration(config_file=config_file())
|
||||
CFG = Configuration(config())
|
||||
|
@ -29,7 +29,7 @@ from peewee import (
|
||||
from slugify import slugify
|
||||
from yaml import dump
|
||||
|
||||
from spip2md.config import CFG
|
||||
from spip2md.config import CFG, NAME
|
||||
from spip2md.regexmaps import (
|
||||
ARTICLE_LINK,
|
||||
BLOAT,
|
||||
@ -60,7 +60,7 @@ from spip2md.style import BOLD, CYAN, GREEN, WARNING_STYLE, YELLOW, esc
|
||||
DeepDict = dict[str, "list[DeepDict] | list[str] | str"]
|
||||
|
||||
# Define logger for this file’s logs
|
||||
LOG = logging.getLogger(CFG.logname + ".models")
|
||||
LOG = logging.getLogger(NAME + ".models")
|
||||
|
||||
|
||||
class SpipWritable:
|
||||
|
@ -19,7 +19,7 @@ from os.path import isfile
|
||||
from shutil import rmtree
|
||||
from typing import Optional
|
||||
|
||||
from spip2md.config import CFG
|
||||
from spip2md.config import CFG, NAME
|
||||
from spip2md.extended_models import (
|
||||
DeepDict,
|
||||
DontExportDraftError,
|
||||
@ -31,8 +31,8 @@ from spip2md.spip_models import DB
|
||||
from spip2md.style import BOLD, esc
|
||||
|
||||
# Define loggers for this file
|
||||
ROOTLOG = logging.getLogger(CFG.logname + ".root")
|
||||
TREELOG = logging.getLogger(CFG.logname + ".tree")
|
||||
ROOTLOG = logging.getLogger(NAME + ".root")
|
||||
TREELOG = logging.getLogger(NAME + ".tree")
|
||||
# Initialize the database with settings from CFG
|
||||
DB.init(CFG.db, host=CFG.db_host, user=CFG.db_user, password=CFG.db_pass)
|
||||
|
||||
@ -137,17 +137,6 @@ def clear_output() -> None:
|
||||
makedirs(CFG.output_dir, exist_ok=True)
|
||||
|
||||
|
||||
# def main(*addargv: str):
|
||||
# import sys
|
||||
# argv: list[str] = sys.argv + list(addargv)
|
||||
|
||||
# TODO Define max nb of sections/articles to export based on first CLI argument
|
||||
# if len(argv) >= 2:
|
||||
# sections_export = int(argv[1])
|
||||
# else:
|
||||
# sections_export = CFG.max_sections_export
|
||||
|
||||
|
||||
# When directly executed as a script
|
||||
def cli():
|
||||
init_logging() # Initialize logging and logfile
|
||||
|
Loading…
Reference in New Issue
Block a user