# -*- coding: utf-8 -*- {{{
# vim: set fenc=utf-8 ft=python sw=4 ts=4 sts=4 et:
#
# Copyright 2019, Battelle Memorial Institute.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This material was prepared as an account of work sponsored by an agency of
# the United States Government. Neither the United States Government nor the
# United States Department of Energy, nor Battelle, nor any of their
# employees, nor any jurisdiction or organization that has cooperated in the
# development of these materials, makes any warranty, express or
# implied, or assumes any legal liability or responsibility for the accuracy,
# completeness, or usefulness or any information, apparatus, product,
# software, or process disclosed, or represents that its use would not infringe
# privately owned rights. Reference herein to any specific commercial product,
# process, or service by trade name, trademark, manufacturer, or otherwise
# does not necessarily constitute or imply its endorsement, recommendation, or
# favoring by the United States Government or any agency thereof, or
# Battelle Memorial Institute. The views and opinions of authors expressed
# herein do not necessarily state or reflect those of the
# United States Government or any agency thereof.
#
# PACIFIC NORTHWEST NATIONAL LABORATORY operated by
# BATTELLE for the UNITED STATES DEPARTMENT OF ENERGY
# under Contract DE-AC05-76RL01830
# }}}
import os
import re
from watchdog.events import PatternMatchingEventHandler
import logging
_log = logging.getLogger(__name__)
[docs]def get_random_key(length: int = 65) -> str:
"""
Returns a hex random key of specified length. The length must be > 0 in order for
the key to be valid. Raises a ValueError if the length is invalid.
The default length is 65, which is 130 in length when hexlify is run.
:param length:
:return:
"""
if length <= 0:
raise ValueError("Invalid length specified for random key must be > 0")
import binascii
random_key = binascii.hexlify(os.urandom(length)).decode('utf-8')
return random_key
[docs]def is_ip_private(vip_address):
""" Determines if the passed vip_address is a private ip address or not.
:param vip_address: A valid ip address.
:return: True if an internal ip address.
"""
ip = vip_address.strip().lower().split("tcp://")[1]
# https://en.wikipedia.org/wiki/Private_network
priv_lo = re.compile("^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
priv_24 = re.compile("^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
priv_20 = re.compile("^192\.168\.\d{1,3}.\d{1,3}$")
priv_16 = re.compile("^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$")
return priv_lo.match(ip) is not None or priv_24.match(
ip) is not None or priv_20.match(ip) is not None or priv_16.match(
ip) is not None
[docs]def get_hostname():
with open('/etc/hostname') as fp:
hostname = fp.read().strip()
assert hostname
return hostname
[docs]class VolttronHomeFileReloader(PatternMatchingEventHandler):
"""
Extends PatternMatchingEvent handler to watch changes to a singlefile/file pattern within volttron home.
filetowatch should be path relative to volttron home.
For example filetowatch auth.json with watch file <volttron_home>/auth.json.
filetowatch *.json will watch all json files in <volttron_home>
"""
def __init__(self, filetowatch, callback):
# Protect from circular reference for file
from volttron.platform import get_home
super(VolttronHomeFileReloader, self).__init__([get_home() + '/' + filetowatch])
_log.debug("patterns is {}".format([get_home() + '/' + filetowatch]))
self._callback = callback
[docs] def on_any_event(self, event):
_log.debug("Calling callback on event {}. Calling {}".format(event, self._callback))
try:
self._callback()
except BaseException as e:
_log.error("Exception in callback: {}".format(e))
_log.debug("After callback on event {}".format(event))
[docs]class AbsolutePathFileReloader(PatternMatchingEventHandler):
"""
Extends PatternMatchingEvent handler to watch changes to a singlefile/file pattern within volttron home.
filetowatch should be path relative to volttron home.
For example filetowatch auth.json with watch file <volttron_home>/auth.json.
filetowatch *.json will watch all json files in <volttron_home>
"""
def __init__(self, filetowatch, callback):
super(AbsolutePathFileReloader, self).__init__([filetowatch])
self._callback = callback
self._filetowatch = filetowatch
@property
def watchfile(self):
return self._filetowatch
[docs] def on_any_event(self, event):
_log.debug("Calling callback on event {}. Calling {}".format(event, self._callback))
try:
self._callback(self._filetowatch)
except BaseException as e:
_log.error("Exception in callback: {}".format(e))
_log.debug("After callback on event {}".format(event))