Only write to stdout when asked, to facilitate use in scripts instead of as a command line tool.

This commit is contained in:
Solderpunk 2020-03-17 18:50:21 +01:00
parent 333cc83282
commit acbee0af9e
1 changed files with 11 additions and 5 deletions

View File

@ -90,7 +90,8 @@ def populate_entry_from_file(filename, base_url, entry):
title = extract_first_heading(filename, filename)
entry.title(title)
def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="", author="", email=""):
def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="",
author="", email="", verbose=False):
"""
Build an Atom feed for all world readable Gemini files in the current
directory, and write it to atom.xml.
@ -118,18 +119,21 @@ def build_feed(base_url, output="atom.xml", n=10, title="", subtitle="", author=
# Add one entry per .gmi file
files = find_files(n)
if not files:
print("No world-readable Gemini content found! :(")
if verbose:
print("No world-readable Gemini content found! :(")
return
for n, filename in enumerate(files):
entry = feed.add_entry()
populate_entry_from_file(filename, base_url, entry)
print("Adding {} with title '{}'...".format(filename, entry.title()))
if n == 0:
feed.updated(entry.updated())
if verbose:
print("Adding {} with title '{}'...".format(filename, entry.title()))
# Write file
feed.atom_file(output, pretty=True)
print("Wrote Atom feed to {}.".format(output))
if verbose:
print("Wrote Atom feed to {}.".format(output))
def main():
"""
@ -148,6 +152,8 @@ def main():
help='include N most recently created files in feed (default 10)')
parser.add_argument('-o', '--output', dest='output', type=str,
default="atom.xml", help='output filename')
parser.add_argument('-q', '--quiet', dest='verbose', action="store_false",
help='Write nothing to stdout under non-error conditions')
parser.add_argument('-s', '--subtitle', dest='subtitle', type=str,
help='feed subtitle')
parser.add_argument('-t', '--title', dest='title', type=str,
@ -164,7 +170,7 @@ def main():
# Build the feed
build_feed(args.base_url, args.output, args.n, args.title, args.subtitle,
args.author, args.email)
args.author, args.email, args.verbose)
if __name__ == "__main__":
main()