From 3f42814544ba97d4b50a8fbb96fef6b5c3117987 Mon Sep 17 00:00:00 2001 From: metalgamer Date: Mon, 7 Dec 2020 20:46:11 +0100 Subject: Add prss --- patternutils/commands/prss.py | 91 +++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 + 2 files changed, 93 insertions(+) create mode 100644 patternutils/commands/prss.py diff --git a/patternutils/commands/prss.py b/patternutils/commands/prss.py new file mode 100644 index 0000000..814672e --- /dev/null +++ b/patternutils/commands/prss.py @@ -0,0 +1,91 @@ +import os +import os.path +from typing import Generator + +import click +import feedparser + +from .. import utils + + +def walk(dir: str) -> Generator[os.DirEntry, None, None]: + for entry in os.scandir(dir): + if entry.is_file(): + yield entry + else: + continue + + +@click.command(context_settings={"help_option_names": ("-h", "--help", "-?")}) +@click.option("-d", "--directory", default="./") +@click.option( + "--stream", + is_flag=True, + default=False, + help="Output a stream of objects instead a list of objects", +) +@click.option( + "--human-readable", + is_flag=True, + default=False, + help="output the JSON in a human readable format", +) +@click.option( + "-v", "--verbose", is_flag=True, default=False, help="explain what is being done" +) +@click.version_option() +@click.argument("url") +def prss( + url: str, directory: str, stream: bool, human_readable: bool, verbose: bool +) -> None: + + parsed_feed = feedparser.parse(url) + + default_values = { + "feed_title": parsed_feed.feed.title, + "feed_link": parsed_feed.feed.link, + "feed_description": parsed_feed.feed.description, + } + + files = [f for f in walk(directory)] + + matches = [] + + for entry in parsed_feed.entries: + + href = entry.enclosures[0]["href"] + + splitted_href = href.split("/") + + file_to_search = splitted_href[-1] + + try: + file_index = files.index(file_to_search) + except ValueError: + click.secho(f"{file_to_search} not found!", fg="red", err=True) + else: + filename = file_to_search[file_index] + entry_values = { + "title": entry.title, + "link": entry.link, + "description": entry.description, + "id": entry.id, + "enclosure_href": entry.enclosures[0]["href"], + "_path": filename.path, + "_name": filename.name, + "_abspath": os.path.abspath(f.path), + "_realpath": os.path.realpath(f.path), + "_relpath": os.path.relpath(f.path), + } + entry_values.update(default_values) + matches.append(entry_values) + + if stream: + for m in matches: + click.echo(utils.json_dumps(m, human_readable)) + else: + click.echo(utils.json_dumps(matches, human_readable)) + + +if __name__ == "__main__": + prss() diff --git a/setup.py b/setup.py index 95a892f..1cb4252 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,7 @@ ENTRY_POINTS = { "tmv=patternutils.commands.tmv:tmv", "tln=patternutils.commands.tln:tln", "patterncfg=patternutils.commands.patterncfg:patterncfg", + "prss=patternutils.commands.prss:prss", ], } @@ -28,6 +29,7 @@ REQUIRED = [ OPTIONAL = { "jinja2": ["Jinja2==2.10.1"], "pygments": ["Pygments==2.4.2"], + "prss": ["feedparser==6.0.2"], } setup( -- cgit v1.3.1 From 95f47606d64db938901074fb4c011004c391e7e4 Mon Sep 17 00:00:00 2001 From: metalgamer Date: Mon, 7 Dec 2020 20:50:10 +0100 Subject: Black and Isort --- patternutils/commands/patterncfg.py | 2 +- patternutils/commands/pmatch.py | 4 +--- patternutils/commands/targs.py | 15 +++------------ patternutils/template.py | 1 - patternutils/utils.py | 4 ++-- 5 files changed, 7 insertions(+), 19 deletions(-) diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py index 83168cf..fa30e01 100644 --- a/patternutils/commands/patterncfg.py +++ b/patternutils/commands/patterncfg.py @@ -9,7 +9,7 @@ from .. import config try: from pygments import highlight from pygments.formatters import TerminalFormatter - from pygments.lexers import Python3Lexer, DjangoLexer + from pygments.lexers import DjangoLexer, Python3Lexer except ImportError: has_pygments = False else: diff --git a/patternutils/commands/pmatch.py b/patternutils/commands/pmatch.py index 6bbd349..521aa9d 100644 --- a/patternutils/commands/pmatch.py +++ b/patternutils/commands/pmatch.py @@ -27,9 +27,7 @@ def walk( def apply_regex( - walk_function: Iterator[Any], - regex_pattern: Pattern[str], - match_full_path: bool, + walk_function: Iterator[Any], regex_pattern: Pattern[str], match_full_path: bool, ) -> Tuple[List[Dict[str, str]], bool]: matches = [] diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py index 04c286d..9fd0831 100644 --- a/patternutils/commands/targs.py +++ b/patternutils/commands/targs.py @@ -11,11 +11,7 @@ import click from .. import config, template, utils -def run_command( - data: Dict[str, str], - run: Callable[..., None], - shell: bool, -) -> None: +def run_command(data: Dict[str, str], run: Callable[..., None], shell: bool,) -> None: if not shell: prepared_command = shlex.split(c) # type: Union[str, List[str]] else: @@ -124,15 +120,10 @@ def targs( stdout = subprocess.DEVNULL if redirect_stdout else None stderr = subprocess.DEVNULL if redirect_stderr else None patched_subprocess = functools.partial( - subprocess.run, - shell=shell, - stdout=stdout, - stderr=stderr, + subprocess.run, shell=shell, stdout=stdout, stderr=stderr, ) patched_run_command = functools.partial( - run_command, - run=patched_subprocess, - shell=shell, + run_command, run=patched_subprocess, shell=shell, ) if max_procs == 1: for r in commands: diff --git a/patternutils/template.py b/patternutils/template.py index 53746a2..56bb0be 100644 --- a/patternutils/template.py +++ b/patternutils/template.py @@ -6,7 +6,6 @@ except ImportError: jinja_feature = False else: import shlex - from datetime import datetime jinja_feature = True diff --git a/patternutils/utils.py b/patternutils/utils.py index 266f3b9..a2decea 100644 --- a/patternutils/utils.py +++ b/patternutils/utils.py @@ -1,6 +1,6 @@ -import os import json -from typing import Dict, Any +import os +from typing import Any, Dict import click -- cgit v1.3.1 From 998231e994cf58c9bd5851eb4d8f39581a60f65b Mon Sep 17 00:00:00 2001 From: metalgamer Date: Mon, 7 Dec 2020 21:53:53 +0100 Subject: Fail with error message, if feedparser not found. --- patternutils/commands/prss.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/patternutils/commands/prss.py b/patternutils/commands/prss.py index 814672e..cc81ee1 100644 --- a/patternutils/commands/prss.py +++ b/patternutils/commands/prss.py @@ -3,7 +3,11 @@ import os.path from typing import Generator import click -import feedparser + +try: + import feedparser +except ImportError: + click.secho("feedparser not installed!", fg="red", err=True) from .. import utils -- cgit v1.3.1