summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Fink2020-12-07 22:01:59 +0100
committerDennis Fink2020-12-07 22:01:59 +0100
commit513025857397403deae6cf6fa28554b35dc951dc (patch)
treea7441688eb419fd543daa1ae2d14220f1f6d38f7
parentaea0f7c6f80d1bd1bb7af5c36f6de707527b39b4 (diff)
parent998231e994cf58c9bd5851eb4d8f39581a60f65b (diff)
downloadpatternutils-513025857397403deae6cf6fa28554b35dc951dc.tar.gz
patternutils-513025857397403deae6cf6fa28554b35dc951dc.zip
Merge
Diffstat (limited to '')
-rw-r--r--patternutils/commands/pmatch.py4
-rw-r--r--patternutils/commands/prss.py95
-rw-r--r--patternutils/commands/targs.py15
-rw-r--r--setup.py2
4 files changed, 101 insertions, 15 deletions
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/prss.py b/patternutils/commands/prss.py
new file mode 100644
index 0000000..cc81ee1
--- /dev/null
+++ b/patternutils/commands/prss.py
@@ -0,0 +1,95 @@
+import os
+import os.path
+from typing import Generator
+
+import click
+
+try:
+ import feedparser
+except ImportError:
+ click.secho("feedparser not installed!", fg="red", err=True)
+
+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/patternutils/commands/targs.py b/patternutils/commands/targs.py
index 6478377..bcd2e8e 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(
- command: str,
- run: Callable[..., None],
- shell: bool,
-) -> None:
+def run_command(data: Dict[str, str], run: Callable[..., None], shell: bool,) -> None:
run(shlex.split(command) if not shell else command)
@@ -117,15 +113,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,
)
max_procs = utils.max_procs() if max_procs == 0 else max_procs
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(