Commit b572ffec authored by Romain Courteaud's avatar Romain Courteaud

Add configuration file

parent 1c4c8adf
...@@ -8,14 +8,20 @@ def runUrlChecker(): ...@@ -8,14 +8,20 @@ def runUrlChecker():
@runUrlChecker.command("bot", short_help="Runs url checker bot.") @runUrlChecker.command("bot", short_help="Runs url checker bot.")
@click.argument("url") @click.option("--url", "-u", help="The url to check.")
@click.argument("sqlite_path") @click.option("--sqlite", "-s", help="The path of the sqlite DB.")
def runWebBot(url, sqlite_path): @click.argument("configuration")
from urlchecker_http import WebBot def runWebBot(url, sqlite, configuration):
from urlchecker_http import create_bot
click.echo("Running url checker bot") click.echo("Running url checker bot")
bot = WebBot() mapping = {}
return bot.run(url, sqlite_path) if url:
mapping["URL"] = url
if sqlite:
mapping["SQLITE"] = sqlite
bot = create_bot(cfgfile=configuration, mapping=mapping)
return bot.run()
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -9,11 +9,14 @@ import dns.resolver ...@@ -9,11 +9,14 @@ import dns.resolver
import miniupnpc import miniupnpc
import platform import platform
from urlchecker_db import LogDB from urlchecker_db import LogDB
import configparser
import os
__version__ = "0.0.3" __version__ = "0.0.3"
PREFERRED_TYPE = "text/html" PREFERRED_TYPE = "text/html"
TIMEOUT = 2 TIMEOUT = 2
CONFIG_SECTION = "URLCHECKER"
class BotError(Exception): class BotError(Exception):
...@@ -21,6 +24,10 @@ class BotError(Exception): ...@@ -21,6 +24,10 @@ class BotError(Exception):
class WebBot: class WebBot:
def __init__(self):
self.config = configparser.ConfigParser(empty_lines_in_values=False)
self.config[CONFIG_SECTION] = {}
def initDB(self, sqlite_path): def initDB(self, sqlite_path):
self._db = LogDB(sqlite_path) self._db = LogDB(sqlite_path)
self._db.createTables() self._db.createTables()
...@@ -110,9 +117,9 @@ class WebBot: ...@@ -110,9 +117,9 @@ class WebBot:
print(ip, hostname, response.status_code) print(ip, hostname, response.status_code)
def run(self, url, sqlite_path): def run(self):
print(time.strftime("%Y-%m-%d %H:%M:%S")) print(time.strftime("%Y-%m-%d %H:%M:%S"))
self.initDB(sqlite_path) self.initDB(self.config[CONFIG_SECTION]["SQLITE"])
self._db.storeEntry(platform=platform.platform()) self._db.storeEntry(platform=platform.platform())
print("Platform", platform.platform()) print("Platform", platform.platform())
...@@ -138,10 +145,29 @@ class WebBot: ...@@ -138,10 +145,29 @@ class WebBot:
except Exception: except Exception:
pass pass
try: for url in self.config[CONFIG_SECTION]["URL"].split():
self.check(url) try:
except KeyboardInterrupt: self.check(url)
self.stop() except KeyboardInterrupt:
except: self.stop()
print("Oups, error") except:
raise print("Oups, error")
raise
def create_bot(envvar="URLCHECKER_SETTINGS", cfgfile=None, mapping=None):
bot = WebBot()
if (envvar is not None) and (envvar in os.environ):
bot.config.read([os.environ.get(envvar)])
if cfgfile is not None:
print(cfgfile)
bot.config.read([cfgfile])
if mapping is not None:
bot.config.read_dict({CONFIG_SECTION: mapping})
for parameter in ["URL", "SQLITE"]:
if parameter not in bot.config[CONFIG_SECTION]:
raise AttributeError("Config %s not defined" % parameter)
return bot
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment