configuration.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. from argparse import Namespace
  4. from pathlib import Path
  5. from typing import Optional
  6. import logging
  7. import click
  8. import yaml
  9. logger = logging.getLogger(__name__)
  10. class Configuration(Namespace):
  11. def __init__(self):
  12. super(Configuration, self).__init__()
  13. self.authenticate = False
  14. self.config = None
  15. self.opts: Optional[str] = None
  16. self.directory: str = '.'
  17. self.disable_module: list[str] = []
  18. self.exclude_id = []
  19. self.exclude_id_file = []
  20. self.file_scheme: str = '{REDDITOR}_{TITLE}_{POSTID}'
  21. self.folder_scheme: str = '{SUBREDDIT}'
  22. self.ignore_user = []
  23. self.include_id_file = []
  24. self.limit: Optional[int] = None
  25. self.link: list[str] = []
  26. self.log: Optional[str] = None
  27. self.make_hard_links = False
  28. self.max_wait_time = None
  29. self.multireddit: list[str] = []
  30. self.no_dupes: bool = False
  31. self.saved: bool = False
  32. self.search: Optional[str] = None
  33. self.search_existing: bool = False
  34. self.skip: list[str] = []
  35. self.skip_domain: list[str] = []
  36. self.skip_subreddit: list[str] = []
  37. self.min_score = None
  38. self.max_score = None
  39. self.min_score_ratio = None
  40. self.max_score_ratio = None
  41. self.sort: str = 'hot'
  42. self.submitted: bool = False
  43. self.subscribed: bool = False
  44. self.subreddit: list[str] = []
  45. self.time: str = 'all'
  46. self.time_format = None
  47. self.upvoted: bool = False
  48. self.user: list[str] = []
  49. self.verbose: int = 0
  50. # Archiver-specific options
  51. self.all_comments = False
  52. self.format = 'json'
  53. self.comment_context: bool = False
  54. def process_click_arguments(self, context: click.Context):
  55. if context.params.get('opts') is not None:
  56. self.parse_yaml_options(context.params['opts'])
  57. for arg_key in context.params.keys():
  58. if not hasattr(self, arg_key):
  59. logger.warning(f'Ignoring an unknown CLI argument: {arg_key}')
  60. continue
  61. val = context.params[arg_key]
  62. if val is None or val == ():
  63. # don't overwrite with an empty value
  64. continue
  65. setattr(self, arg_key, val)
  66. def parse_yaml_options(self, file_path: str):
  67. yaml_file_loc = Path(file_path)
  68. if not yaml_file_loc.exists():
  69. logger.error(f'No YAML file found at {yaml_file_loc}')
  70. return
  71. with open(yaml_file_loc) as file:
  72. try:
  73. opts = yaml.load(file, Loader=yaml.FullLoader)
  74. except yaml.YAMLError as e:
  75. logger.error(f'Could not parse YAML options file: {e}')
  76. return
  77. for arg_key, val in opts.items():
  78. if not hasattr(self, arg_key):
  79. logger.warning(f'Ignoring an unknown YAML argument: {arg_key}')
  80. continue
  81. setattr(self, arg_key, val)