Commit 557155b4 authored by Romain Courteaud's avatar Romain Courteaud

Provide file output for status

This allows to have better atomicity than shell redirection
parent b498c212
......@@ -51,6 +51,8 @@ from .ssl import (
)
import datetime
from email.utils import parsedate_to_datetime
import tempfile
import os
__version__ = "0.6.0"
......@@ -645,24 +647,39 @@ class WebBot:
float(self.config["WARNING_PERIOD"]),
)
if self.config["FORMAT"] == "json":
print(json.dumps(status_dict))
status_output = json.dumps(status_dict)
else:
status_list = []
append_status = status_list.append
for table_key in status_dict:
print("# %s" % table_key)
print("")
append_status("# %s" % table_key)
append_status("")
table = status_dict[table_key]
if table:
# Print the header
table_key_list = [x for x in table[0].keys()]
table_key_list.sort()
print(" | ".join(table_key_list))
append_status(" | ".join(table_key_list))
for line in table:
print(
append_status(
" | ".join(
["%s" % (line[x]) for x in table_key_list]
)
)
print("")
append_status("")
status_output = "\n".join(status_list)
if self.config["STDOUT"] == "":
print(status_output)
else:
# https://blog.gocept.com/2013/07/15/reliable-file-updates-with-python/#write-replace
with tempfile.NamedTemporaryFile(
"w",
dir=os.path.dirname(self.config["STDOUT"]),
delete=False,
) as temp_file:
temp_file.write(status_output)
temp_file_name = temp_file.name
os.rename(temp_file_name, self.config["STDOUT"])
def create_bot(**kw):
......
......@@ -48,6 +48,9 @@ from .bot import create_bot
help="Reload the configuration file between each crawl.",
show_default=True,
)
@click.option(
"--stdout", help="File to store status. (default: stdout)", default=""
)
@click.option(
"--output",
"-o",
......@@ -69,6 +72,7 @@ def runSurykatka(
warning,
configuration,
reload,
stdout,
output,
profile,
):
......@@ -91,6 +95,7 @@ def runSurykatka(
mapping["NAMESERVER"] = nameserver
if reload:
mapping["RELOAD"] = str(reload)
mapping["STDOUT"] = stdout
mapping["FORMAT"] = output
bot = create_bot(cfgfile=configuration, mapping=mapping)
if profile is None:
......
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