Repository: abdelrahman0w/ouro (language: python)

Issue:
create-project-dir-reader



Discussion / hints:

Fix patch:
diff --git a/ouroboros/reader.py b/ouroboros/reader.py
new file mode 100644
index 0000000..0936d3f
--- /dev/null
+++ b/ouroboros/reader.py
@@ -0,0 +1,49 @@
+import os
+import re
+from typing import Generator
+from typing import List
+from typing import Union
+
+
+class Reader:
+    _CHECKABLE: List[str] = [".py", ".pyi"]
+
+    def __init__(
+        self,
+        path: str,
+        ignore: Union[List[str], None] = None,
+    ) -> None:
+        self.path = path
+        self.ignore = set(ignore) if ignore else set()
+
+    def _is_ignored(self, path: str) -> bool:
+        return any(re.search(ignored, path) for ignored in self.ignore)
+
+    def _is_checkable(self, file_name: str) -> bool:
+        _, ext = os.path.splitext(file_name)
+        return ext in Reader._CHECKABLE
+
+    def _read_file(self, file_name: str) -> str:
+        with open(file_name, "r") as readable:
+            return readable.read()
+
+    @property
+    def _valid_files(self) -> Generator:
+        for dir, _, files in os.walk(self.path):
+            if self._is_ignored(dir):
+                continue
+
+            for file in files:
+                if self._is_checkable(file) and not self._is_ignored(file):
+                    yield os.path.join(dir, file)
+
+    @property
+    def read(self) -> Generator:
+        for file in self._valid_files:
+            yield file, self._read_file(file)
+
+    def __enter__(self):
+        return self.read
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        pass


Test patch:
diff --git a/tests/test_pkg/ignore_this_dir/should_be_ignored.py b/tests/test_pkg/ignore_this_dir/should_be_ignored.py
new file mode 100644
index 0000000..7faee20
--- /dev/null
+++ b/tests/test_pkg/ignore_this_dir/should_be_ignored.py
@@ -0,0 +1,1 @@
+VAR = "INVALID: test_pkg/ignore_this_dir/should_be_ignored.py"
diff --git a/tests/test_pkg/module.py b/tests/test_pkg/module.py
new file mode 100644
index 0000000..731764b
--- /dev/null
+++ b/tests/test_pkg/module.py
@@ -0,0 +1,1 @@
+VAR = "VALID: test_pkg/module.py"
diff --git a/tests/test_pkg/module.pyi b/tests/test_pkg/module.pyi
new file mode 100644
index 0000000..9c30db1
--- /dev/null
+++ b/tests/test_pkg/module.pyi
@@ -0,0 +1,1 @@
+VAR = "VALID: test_pkg/module.pyi"
diff --git a/tests/test_pkg/random_c_file.c b/tests/test_pkg/random_c_file.c
new file mode 100644
index 0000000..905869d
--- /dev/null
+++ b/tests/test_pkg/random_c_file.c
@@ -0,0 +1,4 @@
+int main()
+{
+    return 0;
+}
diff --git a/tests/test_pkg/sub_dir/sub_module.py b/tests/test_pkg/sub_dir/sub_module.py
new file mode 100644
index 0000000..a09f0af
--- /dev/null
+++ b/tests/test_pkg/sub_dir/sub_module.py
@@ -0,0 +1,1 @@
+VAR = "VALID: test_pkg/sub_dir/sub_module.py"
diff --git a/tests/test_pkg/sub_dir/sub_sub_dir/sub_sub_module.py b/tests/test_pkg/sub_dir/sub_sub_dir/sub_sub_module.py
new file mode 100644
index 0000000..bf23921
--- /dev/null
+++ b/tests/test_pkg/sub_dir/sub_sub_dir/sub_sub_module.py
@@ -0,0 +1,1 @@
+VAR = "VALID: test_pkg/sub_dir/sub_sub_dir/sub_sub_module.py"
diff --git a/tests/test_reader.py b/tests/test_reader.py
new file mode 100644
index 0000000..5b15e35
--- /dev/null
+++ b/tests/test_reader.py
@@ -0,0 +1,19 @@
+import os
+
+from ouroboros.reader import Reader
+
+
+def test_reader():
+    BASE_DIR = os.path.dirname(
+        os.path.dirname(os.path.abspath(__file__)),
+    )
+
+    with Reader(
+        path=os.path.join(BASE_DIR, "tests", "test_pkg"),
+        ignore=["ignore*"],
+    ) as reader:
+        files_lst, content_lst = zip(*reader)
+
+    assert len(files_lst) == 4
+    assert len(content_lst) == 4
+    assert "INVALID" not in " ".join(content_lst)