Skip to content

Commit

Permalink
Merge pull request #71 from the-geekery/feature/multiline-file-conten…
Browse files Browse the repository at this point in the history
…t-filter

Add support for multiline content filters
  • Loading branch information
tfeldmann authored Mar 28, 2020
2 parents 4d1ff8b + 00e1c2f commit ddf7841
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion organize/filters/file_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FileContent(Filter):
"""

def __init__(self, expr) -> None:
self.expr = re.compile(expr)
self.expr = re.compile(expr, re.MULTILINE | re.DOTALL)

def matches(self, path: Path) -> Any:
if path.suffix.lower() not in SUPPORTED_EXTENSIONS:
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/test_file_content.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from conftest import create_filesystem, assertdir
from organize.cli import main

Expand All @@ -7,17 +9,17 @@ def test_file_content(tmp_path):
create_filesystem(
tmp_path,
files=[
("Test1.txt", "Invoice 12345"),
("Test1.txt", "Lorem MegaCorp Ltd. ipsum\nInvoice 12345\nMore text\nID: 98765"),
("Test2.txt", "Tests"),
("Test3.txt", "My Homework ...")
],
config=r"""
rules:
- folders: files
filters:
- filecontent: '.*Invoice (?P<number>\w+).*'
- filecontent: 'MegaCorp Ltd.+^Invoice (?P<number>\w+)$.+^ID: 98765$'
actions:
- rename: "Invoice_{filecontent.number}.txt"
- rename: "MegaCorp_Invoice_{filecontent.number}.txt"
- folders: files
filters:
- filecontent: '.*Homework.*'
Expand All @@ -26,4 +28,4 @@ def test_file_content(tmp_path):
""",
)
main(["run", "--config-file=%s" % (tmp_path / "config.yaml")])
assertdir(tmp_path, "Homework.txt", "Invoice_12345.txt", "Test2.txt")
assertdir(tmp_path, "Homework.txt", "MegaCorp_Invoice_12345.txt", "Test2.txt")

0 comments on commit ddf7841

Please sign in to comment.