diff --git a/setup.py b/setup.py index aec3953..20e9bc5 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,6 @@ def full_version_bytes(): import subprocess, time try: commit_bytes = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%h']) - t_bytes = subprocess.check_output(['git', 'log', '-1', '--pretty=format:%ct']) t = int(t_bytes.strip().decode('utf-8')) tm = time.gmtime(t) @@ -32,8 +31,8 @@ setuptools.setup(name='umbra', long_description=open('README.md').read(), license='Apache License 2.0', packages=['umbra'], - package_data={'umbra':['behaviors.d/*.js', 'version.txt']}, - install_requires=['kombu', 'websocket-client-py3==0.13.1','argparse'], + package_data={'umbra':['behaviors.d/*.js', 'behaviors.yaml', 'version.txt']}, + install_requires=['kombu', 'websocket-client-py3==0.13.1', 'argparse', 'PyYAML'], scripts=glob.glob('bin/*'), zip_safe=False, classifiers=[ diff --git a/umbra/behaviors.d/default.js b/umbra/behaviors.d/default.js index dc66819..4470cab 100644 --- a/umbra/behaviors.d/default.js +++ b/umbra/behaviors.d/default.js @@ -1,5 +1,3 @@ -// {"request_idle_timeout_sec":10} -// // vim:set sw=8 et: // // Scrolls to the bottom of the page. That's it at the moment. diff --git a/umbra/behaviors.d/facebook.js b/umbra/behaviors.d/facebook.js index e2eb271..a61f484 100644 --- a/umbra/behaviors.d/facebook.js +++ b/umbra/behaviors.d/facebook.js @@ -1,7 +1,4 @@ -// {"url_regex":"^https?://(?:www\\.)?facebook\\.com/.*$", "request_idle_timeout_sec":30} -// // vim:set sw=8 et: -// var umbraAboveBelowOrOnScreen = function(e) { var eTop = e.getBoundingClientRect().top; diff --git a/umbra/behaviors.d/flickr.js b/umbra/behaviors.d/flickr.js index 65787a9..a341480 100644 --- a/umbra/behaviors.d/flickr.js +++ b/umbra/behaviors.d/flickr.js @@ -1,7 +1,4 @@ -// {"url_regex":"^https?://(?:www\\.)?flickr\\.com/.*$", "request_idle_timeout_sec":10} -// // vim:set sw=8 et: -// setInterval(function() { window.scrollBy(0,50); }, 100); diff --git a/umbra/behaviors.d/marquette_edu.js b/umbra/behaviors.d/marquette_edu.js index cdaab00..76cd85c 100644 --- a/umbra/behaviors.d/marquette_edu.js +++ b/umbra/behaviors.d/marquette_edu.js @@ -1,7 +1,4 @@ -// {"url_regex":"^https?://(?:www\\.)?marquette\\.edu/.*$", "request_idle_timeout_sec":10} -// // vim:set sw=8 et: -// var umbraState = {'idleSince':null}; var umbraIntervalID = setInterval(umbraScrollInterval,50); diff --git a/umbra/behaviors.d/vimeo.js b/umbra/behaviors.d/vimeo.js index 53860f2..72a10cb 100644 --- a/umbra/behaviors.d/vimeo.js +++ b/umbra/behaviors.d/vimeo.js @@ -1,7 +1,4 @@ -// {"url_regex":"^https?://(?:www\\.)?vimeo\\.com/.*$", "request_idle_timeout_sec":10} -// // vim:set sw=8 et: -// var umbraState = {'idleSince':null}; var umbraVideoElements = document.getElementsByTagName('video'); diff --git a/umbra/behaviors.py b/umbra/behaviors.py index 41e6055..43cd53b 100644 --- a/umbra/behaviors.py +++ b/umbra/behaviors.py @@ -7,45 +7,28 @@ import re import logging import time import sys +import yaml class Behavior: logger = logging.getLogger(__module__ + "." + __qualname__) _behaviors = None - _default_behavior = None @staticmethod def behaviors(): if Behavior._behaviors is None: - behaviors_directory = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.d']) - behavior_files = itertools.chain(*[[os.path.join(dir, file) for file in files if file.endswith('.js') and file != 'default.js'] for dir, dirs, files in os.walk(behaviors_directory)]) - Behavior._behaviors = [] - for file_name in behavior_files: - Behavior.logger.debug("reading behavior file {}".format(file_name)) - script = open(file_name, encoding='utf-8').read() - first_line = script[:script.find('\n')] - behavior = json.loads(first_line[2:].strip()) - behavior['script'] = script - behavior['file'] = file_name - Behavior._behaviors.append(behavior) - Behavior.logger.info("will run behaviors from {} on urls matching {}".format(file_name, behavior['url_regex'])) + behaviors_yaml = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.yaml']) + with open(behaviors_yaml) as fin: + conf = yaml.load(fin) + Behavior._behaviors = conf['behaviors'] + + for behavior in Behavior._behaviors: + if "behavior_js" in behavior: + behavior_js = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ["behaviors.d"] + [behavior["behavior_js"]]) + behavior["script"] = open(behavior_js, encoding="utf-8").read() return Behavior._behaviors - @staticmethod - def default_behavior(): - if Behavior._default_behavior is None: - behaviors_directory = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.d']) - file_name = os.path.join(behaviors_directory, 'default.js') - Behavior.logger.debug("reading default behavior file {}".format(file_name)) - script = open(file_name, encoding='utf-8').read() - first_line = script[:script.find('\n')] - behavior = json.loads(first_line[2:].strip()) - behavior['script'] = script - behavior['file'] = file_name - Behavior._default_behavior = behavior - return Behavior._default_behavior - def __init__(self, url, umbra_worker): self.url = url self.umbra_worker = umbra_worker @@ -58,14 +41,14 @@ class Behavior: def start(self): for behavior in Behavior.behaviors(): if re.match(behavior['url_regex'], self.url): + if "behavior_js" in behavior: + self.logger.info("using {} behavior for {}".format(behavior["behavior_js"], self.url)) self.active_behavior = behavior - break - - if self.active_behavior is None: - self.active_behavior = Behavior.default_behavior() - - self.umbra_worker.send_to_chrome(method="Runtime.evaluate", params={"expression": self.active_behavior['script']}) - self.notify_of_activity() + self.umbra_worker.send_to_chrome(method="Runtime.evaluate", + suppress_logging=True, params={"expression": behavior["script"]}) + self.notify_of_activity() + return + self.logger.warn("no behavior to run on {}".format(self.url)) def is_finished(self): msg_id = self.umbra_worker.send_to_chrome(method="Runtime.evaluate", diff --git a/umbra/behaviors.yaml b/umbra/behaviors.yaml new file mode 100644 index 0000000..84c98d3 --- /dev/null +++ b/umbra/behaviors.yaml @@ -0,0 +1,22 @@ +# first matched behavior is used, so order matters here +behaviors: + - + url_regex: '^https?://(?:www\.)?facebook\.com/.*$' + behavior_js: facebook.js + request_idle_timeout_sec: 30 + - + url_regex: '^https?://(?:www\.)?flickr\.com/.*$' + behavior_js: flickr.js + request_idle_timeout_sec: 10 + - + url_regex: '^https?://(?:www\.)?marquette\.edu/.*$' + behavior_js: marquette_edu.js + request_idle_timeout_sec: 10 + - + url_regex: '^https?://(?:www\.)?vimeo\.com/.*$' + behavior_js: vimeo.js + request_idle_timeout_sec: 10 + - + url_regex: '^.*$' + request_idle_timeout_sec: 10 + behavior_js: default.js