2021-03-11 00:47:57 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
from argparse import Namespace
|
2022-07-22 17:44:19 +12:00
|
|
|
from pathlib import Path
|
2021-03-11 00:47:57 +13:00
|
|
|
from typing import Optional
|
2022-03-28 07:50:34 +13:00
|
|
|
import logging
|
2021-03-11 00:47:57 +13:00
|
|
|
|
2021-03-11 13:43:26 +13:00
|
|
|
import click
|
2022-03-28 07:29:05 +13:00
|
|
|
import yaml
|
2021-03-11 13:43:26 +13:00
|
|
|
|
2022-03-28 07:50:34 +13:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-03-11 00:47:57 +13:00
|
|
|
|
|
|
|
class Configuration(Namespace):
|
|
|
|
def __init__(self):
|
|
|
|
super(Configuration, self).__init__()
|
2021-03-11 14:20:21 +13:00
|
|
|
self.authenticate = False
|
|
|
|
self.config = None
|
2022-03-28 07:29:05 +13:00
|
|
|
self.opts: Optional[str] = None
|
2021-03-11 00:47:57 +13:00
|
|
|
self.directory: str = '.'
|
2021-06-06 22:47:56 +12:00
|
|
|
self.disable_module: list[str] = []
|
2021-05-21 09:14:35 +12:00
|
|
|
self.exclude_id = []
|
|
|
|
self.exclude_id_file = []
|
2021-06-06 22:47:56 +12:00
|
|
|
self.file_scheme: str = '{REDDITOR}_{TITLE}_{POSTID}'
|
|
|
|
self.folder_scheme: str = '{SUBREDDIT}'
|
2021-10-31 15:19:46 +13:00
|
|
|
self.ignore_user = []
|
2021-07-05 18:58:33 +12:00
|
|
|
self.include_id_file = []
|
2021-03-11 00:47:57 +13:00
|
|
|
self.limit: Optional[int] = None
|
|
|
|
self.link: list[str] = []
|
2021-04-27 14:29:37 +12:00
|
|
|
self.log: Optional[str] = None
|
2021-06-06 22:47:56 +12:00
|
|
|
self.make_hard_links = False
|
2021-04-12 19:12:41 +12:00
|
|
|
self.max_wait_time = None
|
2021-03-11 00:47:57 +13:00
|
|
|
self.multireddit: list[str] = []
|
|
|
|
self.no_dupes: bool = False
|
|
|
|
self.saved: bool = False
|
|
|
|
self.search: Optional[str] = None
|
2021-03-11 17:18:48 +13:00
|
|
|
self.search_existing: bool = False
|
2021-05-21 09:14:35 +12:00
|
|
|
self.skip: list[str] = []
|
2021-03-11 00:47:57 +13:00
|
|
|
self.skip_domain: list[str] = []
|
2021-04-17 21:56:43 +12:00
|
|
|
self.skip_subreddit: list[str] = []
|
2021-03-11 00:47:57 +13:00
|
|
|
self.sort: str = 'hot'
|
|
|
|
self.submitted: bool = False
|
2022-07-06 18:52:01 +12:00
|
|
|
self.subscribed: bool = False
|
2021-03-11 00:47:57 +13:00
|
|
|
self.subreddit: list[str] = []
|
|
|
|
self.time: str = 'all'
|
2021-05-02 15:56:39 +12:00
|
|
|
self.time_format = None
|
2021-03-11 00:47:57 +13:00
|
|
|
self.upvoted: bool = False
|
2021-05-27 17:22:58 +12:00
|
|
|
self.user: list[str] = []
|
2021-03-11 00:47:57 +13:00
|
|
|
self.verbose: int = 0
|
2021-03-11 13:43:26 +13:00
|
|
|
|
2021-03-13 23:18:30 +13:00
|
|
|
# Archiver-specific options
|
2021-04-10 01:15:45 +12:00
|
|
|
self.all_comments = False
|
2021-06-11 17:31:11 +12:00
|
|
|
self.format = 'json'
|
2021-06-12 12:35:31 +12:00
|
|
|
self.comment_context: bool = False
|
2021-03-13 23:18:30 +13:00
|
|
|
|
2021-03-11 13:43:26 +13:00
|
|
|
def process_click_arguments(self, context: click.Context):
|
2022-03-28 08:13:33 +13:00
|
|
|
if context.params.get('opts') is not None:
|
2022-07-22 17:44:19 +12:00
|
|
|
self.parse_yaml_options(context.params['opts'])
|
2021-03-11 13:43:26 +13:00
|
|
|
for arg_key in context.params.keys():
|
2022-03-28 07:50:34 +13:00
|
|
|
if not hasattr(self, arg_key):
|
|
|
|
logger.warning(f'Ignoring an unknown CLI argument: {arg_key}')
|
2022-03-28 07:29:05 +13:00
|
|
|
continue
|
2022-03-28 07:50:34 +13:00
|
|
|
val = context.params[arg_key]
|
|
|
|
if val is None or val == ():
|
|
|
|
# don't overwrite with an empty value
|
2022-03-28 07:29:05 +13:00
|
|
|
continue
|
2022-03-28 07:50:34 +13:00
|
|
|
setattr(self, arg_key, val)
|
2022-07-22 17:44:19 +12:00
|
|
|
|
|
|
|
def parse_yaml_options(self, file_path: str):
|
|
|
|
yaml_file_loc = Path(file_path)
|
|
|
|
if not yaml_file_loc.exists():
|
|
|
|
logger.error(f'No YAML file found at {yaml_file_loc}')
|
|
|
|
return
|
2022-07-22 17:45:09 +12:00
|
|
|
with open(yaml_file_loc) as file:
|
2022-07-22 17:44:19 +12:00
|
|
|
try:
|
2022-07-22 17:45:09 +12:00
|
|
|
opts = yaml.load(file, Loader=yaml.FullLoader)
|
2022-07-22 17:44:19 +12:00
|
|
|
except yaml.YAMLError as e:
|
|
|
|
logger.error(f'Could not parse YAML options file: {e}')
|
|
|
|
return
|
|
|
|
for arg_key, val in opts.items():
|
|
|
|
if not hasattr(self, arg_key):
|
|
|
|
logger.error(f'Ignoring an unknown YAML argument: {arg_key}')
|
|
|
|
continue
|
|
|
|
setattr(self, arg_key, val)
|