2023-01-12 01:04:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-10-21 11:47:10 +00:00
|
|
|
# BSD 2-Clause License
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Apprise - Push Notification Library.
|
2024-06-07 18:48:09 +00:00
|
|
|
# Copyright (c) 2024, Chris Caron <lead2gold@gmail.com>
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
2023-01-12 01:04:47 +00:00
|
|
|
#
|
2023-04-13 08:41:12 +00:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
2024-06-07 18:48:09 +00:00
|
|
|
from .base import ConfigBase
|
2023-01-12 01:04:47 +00:00
|
|
|
from ..common import ConfigFormat
|
2023-01-14 20:40:05 +00:00
|
|
|
from ..common import ContentIncludeMode
|
2024-06-07 18:48:09 +00:00
|
|
|
from ..locale import gettext_lazy as _
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigFile(ConfigBase):
|
|
|
|
"""
|
|
|
|
A wrapper for File based configuration sources
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The default descriptive name associated with the service
|
|
|
|
service_name = _('Local File')
|
|
|
|
|
|
|
|
# The default protocol
|
|
|
|
protocol = 'file'
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Configuration file inclusion can only be of the same type
|
|
|
|
allow_cross_includes = ContentIncludeMode.STRICT
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
def __init__(self, path, **kwargs):
|
|
|
|
"""
|
|
|
|
Initialize File Object
|
|
|
|
|
|
|
|
headers can be a dictionary of key/value pairs that you want to
|
|
|
|
additionally include as part of the server headers to post with
|
|
|
|
|
|
|
|
"""
|
2023-01-14 20:40:05 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Store our file path as it was set
|
2023-01-14 20:40:05 +00:00
|
|
|
self.path = os.path.abspath(os.path.expanduser(path))
|
|
|
|
|
|
|
|
# Update the config path to be relative to our file we just loaded
|
|
|
|
self.config_path = os.path.dirname(self.path)
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def url(self, privacy=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the URL built dynamically based on specified arguments.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Prepare our cache value
|
|
|
|
if isinstance(self.cache, bool) or not self.cache:
|
|
|
|
cache = 'yes' if self.cache else 'no'
|
|
|
|
|
|
|
|
else:
|
|
|
|
cache = int(self.cache)
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
# Define any URL parameters
|
|
|
|
params = {
|
2023-01-12 01:04:47 +00:00
|
|
|
'encoding': self.encoding,
|
|
|
|
'cache': cache,
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.config_format:
|
|
|
|
# A format was enforced; make sure it's passed back with the url
|
2023-01-14 20:40:05 +00:00
|
|
|
params['format'] = self.config_format
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
return 'file://{path}{params}'.format(
|
2023-01-12 01:04:47 +00:00
|
|
|
path=self.quote(self.path),
|
2023-01-14 20:40:05 +00:00
|
|
|
params='?{}'.format(self.urlencode(params)) if params else '',
|
2023-01-12 01:04:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def read(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Perform retrieval of the configuration based on the specified request
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self.max_buffer_size > 0 and \
|
2023-01-14 20:40:05 +00:00
|
|
|
os.path.getsize(self.path) > self.max_buffer_size:
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
# Content exceeds maximum buffer size
|
|
|
|
self.logger.error(
|
|
|
|
'File size exceeds maximum allowable buffer length'
|
|
|
|
' ({}KB).'.format(int(self.max_buffer_size / 1024)))
|
|
|
|
return None
|
|
|
|
|
|
|
|
except OSError:
|
|
|
|
# getsize() can throw this acception if the file is missing
|
|
|
|
# and or simply isn't accessible
|
|
|
|
self.logger.error(
|
2023-01-14 20:40:05 +00:00
|
|
|
'File is not accessible: {}'.format(self.path))
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Always call throttle before any server i/o is made
|
|
|
|
self.throttle()
|
|
|
|
|
|
|
|
try:
|
2023-01-14 20:40:05 +00:00
|
|
|
with open(self.path, "rt", encoding=self.encoding) as f:
|
2023-01-12 01:04:47 +00:00
|
|
|
# Store our content for parsing
|
|
|
|
response = f.read()
|
|
|
|
|
|
|
|
except (ValueError, UnicodeDecodeError):
|
|
|
|
# A result of our strict encoding check; if we receive this
|
|
|
|
# then the file we're opening is not something we can
|
|
|
|
# understand the encoding of..
|
|
|
|
|
|
|
|
self.logger.error(
|
|
|
|
'File not using expected encoding ({}) : {}'.format(
|
2023-01-14 20:40:05 +00:00
|
|
|
self.encoding, self.path))
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
except (IOError, OSError):
|
|
|
|
# IOError is present for backwards compatibility with Python
|
|
|
|
# versions older then 3.3. >= 3.3 throw OSError now.
|
|
|
|
|
|
|
|
# Could not open and/or read the file; this is not a problem since
|
|
|
|
# we scan a lot of default paths.
|
|
|
|
self.logger.error(
|
2023-01-14 20:40:05 +00:00
|
|
|
'File can not be opened for read: {}'.format(self.path))
|
2023-01-12 01:04:47 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Return our response object
|
|
|
|
return response
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_url(url):
|
|
|
|
"""
|
|
|
|
Parses the URL so that we can handle all different file paths
|
|
|
|
and return it as our path object
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
results = ConfigBase.parse_url(url, verify_host=False)
|
|
|
|
if not results:
|
|
|
|
# We're done early; it's not a good URL
|
|
|
|
return results
|
|
|
|
|
2023-01-14 20:40:05 +00:00
|
|
|
match = re.match(r'[a-z0-9]+://(?P<path>[^?]+)(\?.*)?', url, re.I)
|
2023-01-12 01:04:47 +00:00
|
|
|
if not match:
|
|
|
|
return None
|
|
|
|
|
|
|
|
results['path'] = ConfigFile.unquote(match.group('path'))
|
|
|
|
return results
|