reposnap 0.3.0__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,74 @@
1
+ # src/reposnap/controllers/project_controller.py
2
+
3
+ import logging
4
+ from pathlib import Path
5
+ from reposnap.core.git_repo import GitRepo
6
+ from reposnap.core.file_system import FileSystem
7
+ from reposnap.core.markdown_generator import MarkdownGenerator
8
+ from reposnap.models.file_tree import FileTree
9
+ import pathspec
10
+ from typing import List, Optional
11
+
12
+
13
+ class ProjectController:
14
+ def __init__(self, args: object):
15
+ self.logger = logging.getLogger(__name__)
16
+ self.root_dir: Path = Path(args.path).resolve()
17
+ self.output_file: Path = Path(args.output).resolve()
18
+ self.structure_only: bool = args.structure_only
19
+ self.args: object = args
20
+ self.file_tree: Optional[FileTree] = None
21
+ self.gitignore_patterns: List[str] = self._load_gitignore_patterns()
22
+
23
+ def run(self) -> None:
24
+ self.collect_file_tree()
25
+ self.apply_filters()
26
+ self.generate_output()
27
+
28
+ def collect_file_tree(self) -> None:
29
+ self.logger.info("Collecting git files.")
30
+ git_repo: GitRepo = GitRepo(self.root_dir)
31
+ git_files: List[Path] = git_repo.get_git_files()
32
+ self.logger.debug(f"Git files before filtering: {git_files}")
33
+
34
+ self.logger.info("Building tree structure.")
35
+ file_system: FileSystem = FileSystem(self.root_dir)
36
+ tree_structure: dict = file_system.build_tree_structure(git_files)
37
+
38
+ self.file_tree = FileTree(tree_structure)
39
+ self.logger.debug(f"Tree structure: {self.file_tree.structure}")
40
+
41
+ def apply_filters(self) -> None:
42
+ self.logger.info("Applying filters to the file tree.")
43
+ spec: pathspec.PathSpec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, self.gitignore_patterns)
44
+ self.logger.debug(f"Filter patterns: {self.gitignore_patterns}")
45
+ self.file_tree.filter_files(spec)
46
+
47
+ def generate_output(self) -> None:
48
+ self.logger.info("Starting markdown generation.")
49
+ markdown_generator: MarkdownGenerator = MarkdownGenerator(
50
+ root_dir=self.root_dir,
51
+ output_file=self.output_file,
52
+ structure_only=self.structure_only
53
+ )
54
+ markdown_generator.generate_markdown(self.file_tree.structure, self.file_tree.get_all_files())
55
+ self.logger.info("Markdown generation completed.")
56
+
57
+ def _load_gitignore_patterns(self) -> List[str]:
58
+ gitignore_path: Path = self.root_dir / '.gitignore'
59
+ if not gitignore_path.exists():
60
+ for parent in self.root_dir.parents:
61
+ gitignore_path = parent / '.gitignore'
62
+ if gitignore_path.exists():
63
+ break
64
+ else:
65
+ gitignore_path = None
66
+
67
+ if gitignore_path and gitignore_path.exists():
68
+ with gitignore_path.open('r') as gitignore:
69
+ patterns: List[str] = gitignore.readlines()
70
+ self.logger.debug(f"Patterns from .gitignore in {gitignore_path.parent}: {patterns}")
71
+ return patterns
72
+ else:
73
+ self.logger.debug(f"No .gitignore found starting from {self.root_dir}. Proceeding without patterns.")
74
+ return []
@@ -2,29 +2,31 @@
2
2
 
3
3
  import logging
4
4
  from pathlib import Path
5
+ from typing import List, Dict, Any
6
+
5
7
 
6
8
  class FileSystem:
7
9
  def __init__(self, root_dir: Path):
8
- self.root_dir = root_dir.resolve()
10
+ self.root_dir: Path = root_dir.resolve()
9
11
  self.logger = logging.getLogger(__name__)
10
12
 
11
- def build_tree_structure(self, files):
13
+ def build_tree_structure(self, files: List[Path]) -> Dict[str, Any]:
12
14
  """
13
15
  Builds a hierarchical tree structure from the list of files.
14
16
 
15
17
  Args:
16
- files (list of Path): List of file paths relative to root_dir.
18
+ files (List[Path]): List of file paths relative to root_dir.
17
19
 
18
20
  Returns:
19
- dict: Nested dictionary representing the directory structure.
21
+ Dict[str, Any]: Nested dictionary representing the directory structure.
20
22
  """
21
- tree = {}
23
+ tree: Dict[str, Any] = {}
22
24
  self.logger.debug("Building tree structure.")
23
25
  for relative_path in files:
24
26
  parts = relative_path.parts
25
27
  current_level = tree
26
28
  for part in parts[:-1]:
27
29
  current_level = current_level.setdefault(part, {})
28
- current_level[parts[-1]] = relative_path.as_posix()
30
+ current_level[parts[-1]] = None # Indicate a file node
29
31
  self.logger.debug(f"Tree structure built: {tree}")
30
32
  return tree
reposnap/core/git_repo.py CHANGED
@@ -3,23 +3,25 @@
3
3
  import logging
4
4
  from pathlib import Path
5
5
  from git import Repo, InvalidGitRepositoryError
6
+ from typing import List
7
+
6
8
 
7
9
  class GitRepo:
8
10
  def __init__(self, repo_path: Path):
9
- self.repo_path = repo_path.resolve()
11
+ self.repo_path: Path = repo_path.resolve()
10
12
  self.logger = logging.getLogger(__name__)
11
13
 
12
- def get_git_files(self):
14
+ def get_git_files(self) -> List[Path]:
13
15
  try:
14
- repo = Repo(self.repo_path, search_parent_directories=True)
15
- repo_root = Path(repo.working_tree_dir).resolve()
16
- git_files = repo.git.ls_files().splitlines()
16
+ repo: Repo = Repo(self.repo_path, search_parent_directories=True)
17
+ repo_root: Path = Path(repo.working_tree_dir).resolve()
18
+ git_files: List[str] = repo.git.ls_files().splitlines()
17
19
  self.logger.debug(f"Git files from {repo_root}: {git_files}")
18
- git_files_relative = []
20
+ git_files_relative: List[Path] = []
19
21
  for f in git_files:
20
- absolute_path = (repo_root / f).resolve()
22
+ absolute_path: Path = (repo_root / f).resolve()
21
23
  try:
22
- relative_path = absolute_path.relative_to(self.repo_path)
24
+ relative_path: Path = absolute_path.relative_to(self.repo_path)
23
25
  git_files_relative.append(relative_path)
24
26
  except ValueError:
25
27
  # Skip files not under root_dir
@@ -28,3 +30,4 @@ class GitRepo:
28
30
  except InvalidGitRepositoryError:
29
31
  self.logger.error(f"Invalid Git repository at: {self.repo_path}")
30
32
  return []
33
+
@@ -2,28 +2,30 @@
2
2
 
3
3
  import logging
4
4
  from pathlib import Path
5
- from ..utils.path_utils import format_tree
5
+ from reposnap.utils.path_utils import format_tree
6
+ from typing import List, Dict, Any
7
+
6
8
 
7
9
  class MarkdownGenerator:
8
10
  def __init__(self, root_dir: Path, output_file: Path, structure_only: bool = False):
9
- self.root_dir = root_dir.resolve()
10
- self.output_file = output_file.resolve()
11
- self.structure_only = structure_only
11
+ self.root_dir: Path = root_dir.resolve()
12
+ self.output_file: Path = output_file.resolve()
13
+ self.structure_only: bool = structure_only
12
14
  self.logger = logging.getLogger(__name__)
13
15
 
14
- def generate_markdown(self, tree_structure: dict, files: list):
16
+ def generate_markdown(self, tree_structure: Dict[str, Any], files: List[Path]) -> None:
15
17
  """
16
18
  Generates the Markdown file based on the provided tree structure and files.
17
19
 
18
20
  Args:
19
- tree_structure (dict): The hierarchical structure of the project files.
20
- files (list of Path): List of file paths to include in the markdown.
21
+ tree_structure (Dict[str, Any]): The hierarchical structure of the project files.
22
+ files (List[Path]): List of file paths to include in the markdown.
21
23
  """
22
24
  self._write_header(tree_structure)
23
25
  if not self.structure_only:
24
26
  self._write_file_contents(files)
25
27
 
26
- def _write_header(self, tree_structure: dict):
28
+ def _write_header(self, tree_structure: Dict[str, Any]) -> None:
27
29
  """
28
30
  Writes the header and project structure to the Markdown file.
29
31
  """
@@ -40,16 +42,16 @@ class MarkdownGenerator:
40
42
  self.logger.error(f"Failed to write header to {self.output_file}: {e}")
41
43
  raise
42
44
 
43
- def _write_file_contents(self, files: list):
45
+ def _write_file_contents(self, files: List[Path]) -> None:
44
46
  """
45
47
  Writes the contents of each file to the Markdown file.
46
48
 
47
49
  Args:
48
- files (list of Path): List of file paths relative to root_dir.
50
+ files (List[Path]): List of file paths relative to root_dir.
49
51
  """
50
52
  self.logger.debug("Writing file contents to Markdown.")
51
53
  for relative_path in files:
52
- file_path = self.root_dir / relative_path
54
+ file_path: Path = self.root_dir / relative_path
53
55
 
54
56
  if not file_path.exists():
55
57
  self.logger.debug(f"File not found: {file_path}. Skipping.")
@@ -57,16 +59,17 @@ class MarkdownGenerator:
57
59
 
58
60
  self._write_file_content(file_path, relative_path.as_posix())
59
61
 
60
- def _write_file_content(self, file_path: Path, relative_path: str):
62
+ def _write_file_content(self, file_path: Path, relative_path: str) -> None:
61
63
  """
62
64
  Writes the content of a single file to the Markdown file with syntax highlighting.
63
65
  """
64
66
  try:
65
67
  with file_path.open('r', encoding='utf-8') as f:
66
- content = f.read()
68
+ content: str = f.read()
67
69
  with self.output_file.open('a', encoding='utf-8') as f:
68
70
  f.write(f"## {relative_path}\n\n")
69
71
  f.write("```python\n" if file_path.suffix == '.py' else "```\n")
70
72
  f.write(f"{content}\n```\n\n")
71
73
  except IOError as e:
72
74
  self.logger.error(f"Error reading or writing file {file_path}: {e}")
75
+
@@ -2,9 +2,7 @@
2
2
 
3
3
  import argparse
4
4
  import logging
5
- import os
6
- from reposnap.core.collector import ProjectContentCollector
7
- from pathlib import Path
5
+ from reposnap.controllers.project_controller import ProjectController
8
6
 
9
7
 
10
8
  def main():
@@ -20,27 +18,8 @@ def main():
20
18
  log_level = logging.DEBUG if args.debug else logging.INFO
21
19
  logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')
22
20
 
23
- path = Path(args.path).resolve()
24
- gitignore_path = path / '.gitignore'
25
- if not gitignore_path.exists():
26
- # Search for .gitignore in parent directories
27
- for parent in path.parents:
28
- gitignore_path = parent / '.gitignore'
29
- if gitignore_path.exists():
30
- break
31
- else:
32
- gitignore_path = None
33
-
34
- if gitignore_path and gitignore_path.exists():
35
- with gitignore_path.open('r') as gitignore:
36
- patterns = gitignore.readlines()
37
- logging.debug(f"Patterns from .gitignore in {gitignore_path.parent}: {patterns}")
38
- else:
39
- patterns = []
40
- logging.debug(f"No .gitignore found starting from {args.path}. Proceeding without patterns.")
41
-
42
- collector = ProjectContentCollector(str(path), args.output, args.structure_only, patterns)
43
- collector.collect_and_generate()
21
+ controller = ProjectController(args)
22
+ controller.run()
44
23
 
45
24
  if __name__ == "__main__":
46
25
  main()
File without changes
@@ -0,0 +1,54 @@
1
+ # src/reposnap/models/file_tree.py
2
+
3
+ import logging
4
+ from pathlib import Path
5
+ from typing import Dict, List, Any
6
+ import pathspec
7
+
8
+
9
+ class FileTree:
10
+ def __init__(self, structure: Dict[str, Any]):
11
+ self.structure: Dict[str, Any] = structure
12
+ self.logger = logging.getLogger(__name__)
13
+
14
+ def get_all_files(self) -> List[Path]:
15
+ """
16
+ Recursively retrieve all file paths from the tree.
17
+ Returns:
18
+ List[Path]: List of file paths relative to root_dir.
19
+ """
20
+ return self._extract_files(self.structure)
21
+
22
+ def _extract_files(self, subtree: Dict[str, Any], path_prefix: str = '') -> List[Path]:
23
+ files: List[Path] = []
24
+ for key, value in subtree.items():
25
+ current_path: str = f"{path_prefix}/{key}".lstrip('/')
26
+ if isinstance(value, dict):
27
+ files.extend(self._extract_files(value, current_path))
28
+ else:
29
+ files.append(Path(current_path))
30
+ return files
31
+
32
+ def filter_files(self, spec: pathspec.PathSpec) -> None:
33
+ """
34
+ Filters files in the tree structure based on the provided pathspec.
35
+
36
+ Args:
37
+ spec (pathspec.PathSpec): The pathspec for filtering files.
38
+ """
39
+ self.logger.debug("Filtering files in the file tree.")
40
+ self.structure = self._filter_tree(self.structure, spec)
41
+
42
+ def _filter_tree(self, subtree: Dict[str, Any], spec: pathspec.PathSpec, path_prefix: str = '') -> Dict[str, Any]:
43
+ filtered_subtree: Dict[str, Any] = {}
44
+ for key, value in subtree.items():
45
+ current_path: str = f"{path_prefix}/{key}".lstrip('/')
46
+ if isinstance(value, dict):
47
+ filtered_value: Dict[str, Any] = self._filter_tree(value, spec, current_path)
48
+ if filtered_value:
49
+ filtered_subtree[key] = filtered_value
50
+ else:
51
+ if not spec.match_file(current_path):
52
+ filtered_subtree[key] = value
53
+ return filtered_subtree
54
+
@@ -1,6 +1,8 @@
1
1
  # src/reposnap/utils/path_utils.py
2
+ from typing import Dict, Generator, Any
2
3
 
3
- def format_tree(tree, indent=''):
4
+
5
+ def format_tree(tree: Dict[str, Any], indent: str = '') -> Generator[str, None, None]:
4
6
  for key, value in tree.items():
5
7
  if isinstance(value, dict):
6
8
  yield f"{indent}{key}/\n"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: reposnap
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Generate a Markdown file with all contents of your project
5
5
  Author: agoloborodko
6
6
  License-File: LICENSE
@@ -44,7 +44,7 @@ To use `reposnap`, run it with the following options:
44
44
  reposnap [-h] [-o OUTPUT] [--structure-only] [--debug] path
45
45
  ```
46
46
 
47
- - `path`: Path to the Git repository root folder
47
+ - `path`: Path to the Git repository or subdirectory
48
48
  - `-h, --help`: show help message and exit
49
49
  - `-o, --output`: The name of the output Markdown file. Defaults to `output.md`.
50
50
  - `--structure-only`: Generate a Markdown file that includes only the project structure, without file contents.
@@ -0,0 +1,18 @@
1
+ reposnap/__init__.py,sha256=FGadYKcWDEh_AL8PbUuUrcCZDlF510JZfdGhHdT6XQk,80
2
+ reposnap/controllers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ reposnap/controllers/project_controller.py,sha256=0BPnQ0tJf0lNNgP_TSYWhU8DUUzDyCPzdwtIM6zlQbA,3131
4
+ reposnap/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ reposnap/core/file_system.py,sha256=82gwvmgrsWf63paMrIz-Z0eqIjbqt9_-vujdXlJJoFE,1074
6
+ reposnap/core/git_repo.py,sha256=2u_ILkV-Ur7qr1WHmHM2yg44Ggft61RsdbZLsZaQ5NU,1256
7
+ reposnap/core/markdown_generator.py,sha256=HJSn8ekNiGwFbMwRNZVdA6Y-4InSV3WDVdA-z8s-m0A,3032
8
+ reposnap/interfaces/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ reposnap/interfaces/cli.py,sha256=2ThdHJ0LJTWHaNuBF7PdO2xNdeRP16I-BXHumk_PfuI,961
10
+ reposnap/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ reposnap/models/file_tree.py,sha256=6r7_q-3u03_B9mMUF-2HDL6TMkFkFKhbO191hwUB_gk,2026
12
+ reposnap/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ reposnap/utils/path_utils.py,sha256=NbjZkSScYTISQY4Ww22sSWPf26_mig3bYTKmdSO2U2A,384
14
+ reposnap-0.3.2.dist-info/METADATA,sha256=PFh5yBs306pbvXYoBSZNhmOPLmASoEEJUr97BypZddw,2333
15
+ reposnap-0.3.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
16
+ reposnap-0.3.2.dist-info/entry_points.txt,sha256=aaCuB0RZOLW2--ymW8VCEOm0Qz9KvTZCUjwQFZsGaDU,43
17
+ reposnap-0.3.2.dist-info/licenses/LICENSE,sha256=Aj7WCYBXi98pvi723HPn4GDRyjxToNWb3PC6j1_lnPk,1069
18
+ reposnap-0.3.2.dist-info/RECORD,,
@@ -1,64 +0,0 @@
1
- # src/reposnap/core/collector.py
2
-
3
- import logging
4
- from pathlib import Path
5
- import pathspec
6
- from .git_repo import GitRepo
7
- from .file_system import FileSystem
8
- from .markdown_generator import MarkdownGenerator
9
-
10
-
11
- class ProjectContentCollector:
12
- def __init__(self, root_dir: str, output_file: str, structure_only: bool, gitignore_patterns: list):
13
- self.logger = logging.getLogger(__name__)
14
- self.root_dir = Path(root_dir).resolve()
15
- self.output_file = Path(output_file).resolve()
16
- self.structure_only = structure_only
17
- self.gitignore_patterns = gitignore_patterns
18
- self.spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, gitignore_patterns)
19
-
20
- # Initialize components
21
- self.git_repo = GitRepo(self.root_dir)
22
- self.file_system = FileSystem(self.root_dir)
23
- self.markdown_generator = MarkdownGenerator(
24
- root_dir=self.root_dir,
25
- output_file=self.output_file,
26
- structure_only=self.structure_only
27
- )
28
-
29
- # Collect files and build tree during initialization
30
- self.files = self.collect_files()
31
- self.tree_structure = self.build_tree_structure()
32
-
33
- def collect_files(self):
34
- """
35
- Collects and filters files to be included in the documentation.
36
- """
37
- self.logger.info("Collecting git files.")
38
- git_files = self.git_repo.get_git_files()
39
- self.logger.debug(f"Git files before filtering: {git_files}")
40
-
41
- # Filter files based on .gitignore patterns
42
- filtered_files = [
43
- f for f in git_files if not self.spec.match_file(str(f))
44
- ]
45
- self.logger.debug(f"Git files after filtering: {filtered_files}")
46
-
47
- return filtered_files # Paths relative to root_dir
48
-
49
- def build_tree_structure(self):
50
- """
51
- Builds the tree structure from the collected files.
52
- """
53
- self.logger.info("Building tree structure.")
54
- tree = self.file_system.build_tree_structure(self.files)
55
- self.logger.debug(f"Tree structure: {tree}")
56
- return tree
57
-
58
- def collect_and_generate(self):
59
- """
60
- Initiates the markdown generation process.
61
- """
62
- self.logger.info("Starting markdown generation.")
63
- self.markdown_generator.generate_markdown(self.tree_structure, self.files)
64
- self.logger.info("Markdown generation completed.")
@@ -1,15 +0,0 @@
1
- reposnap/__init__.py,sha256=FGadYKcWDEh_AL8PbUuUrcCZDlF510JZfdGhHdT6XQk,80
2
- reposnap/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- reposnap/core/collector.py,sha256=UQNAaIqgALZmOvtSb7NDYaXmVp4-X4jSWikMMh_B0YY,2401
4
- reposnap/core/file_system.py,sha256=yFIaHtJYqw5t8Lx9Nu7yb3G7XcCP1v1aKP_hGfS_KfQ,974
5
- reposnap/core/git_repo.py,sha256=A8mdgmT9JGHllkFMx5Z_1gGp3Yw1dr-NFOw2-lH_DfA,1163
6
- reposnap/core/markdown_generator.py,sha256=vJl_ac3vDs4CEanjwQazQM9fepGeFtGxo3ozajdkhqA,2889
7
- reposnap/interfaces/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- reposnap/interfaces/cli.py,sha256=bU7QAg2wDzQwyAsRyq4zHLnO8nNiJGrWD24n7yZFEZk,1795
9
- reposnap/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- reposnap/utils/path_utils.py,sha256=lMuZMMIX2lEE1o6V3QqifmyO5dO5fuA5OUXMxSvYcHI,290
11
- reposnap-0.3.0.dist-info/METADATA,sha256=hcDKEinduZiYYDMrl5NsYkv0qz0H3EG-Ls_LfU01WGE,2329
12
- reposnap-0.3.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
13
- reposnap-0.3.0.dist-info/entry_points.txt,sha256=aaCuB0RZOLW2--ymW8VCEOm0Qz9KvTZCUjwQFZsGaDU,43
14
- reposnap-0.3.0.dist-info/licenses/LICENSE,sha256=Aj7WCYBXi98pvi723HPn4GDRyjxToNWb3PC6j1_lnPk,1069
15
- reposnap-0.3.0.dist-info/RECORD,,