-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
106 lines (92 loc) · 4.15 KB
/
config.py
File metadata and controls
106 lines (92 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Configuration management for FormFiller application."""
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional
@dataclass
class TemplateConfig:
"""Configuration for PDF template and field mapping."""
name: str
template_path: str
mapping_path: str
output_prefix: str
def validate(self) -> bool:
"""Validate that all required files exist."""
return os.path.exists(self.template_path) and os.path.exists(self.mapping_path)
class FormFillerConfig:
"""Main configuration class for FormFiller application."""
# Built-in template configurations
BUILT_IN_TEMPLATES = {
"misc": TemplateConfig(
name="1099-MISC",
template_path="templates/misc_template.pdf",
mapping_path="misc_field_number_mapping.yml",
output_prefix="misc_big",
),
"nec": TemplateConfig(
name="1099-NEC",
template_path="templates/nec_template.pdf",
mapping_path="nec_fnm.yml",
output_prefix="nec_big",
),
}
def __init__(self, base_path: Optional[str] = None):
"""Initialize configuration with optional base path."""
self.base_path = Path(base_path) if base_path else Path.cwd()
self.inputs_folder = self.base_path / "inputs"
self.outputs_folder = self.base_path / "outputs"
self.templates_folder = self.base_path / "templates"
def get_template_config(self, template_name: str) -> TemplateConfig:
"""Get template configuration by name or path."""
if template_name in self.BUILT_IN_TEMPLATES:
config = self.BUILT_IN_TEMPLATES[template_name]
# Make paths absolute based on base_path
config.template_path = str(self.base_path / config.template_path)
config.mapping_path = str(self.base_path / config.mapping_path)
return config
else:
# Treat as custom template path
return TemplateConfig(
name="custom",
template_path=template_name,
mapping_path=str(self.base_path / "field_number_mapping_custom.yml"),
output_prefix="custom_big",
)
def validate_input_file(self, filename: str) -> str:
"""Validate and return full path to input CSV file."""
input_path = self.inputs_folder / filename
if not input_path.exists():
available_files = [
f.name for f in self.inputs_folder.glob("*.csv") if f.is_file()
]
if not available_files:
raise FileNotFoundError(
f"Input file '{input_path}' does not exist and no CSV files found in '{self.inputs_folder}'."
)
print(f"Input file '{input_path}' does not exist.")
print("Available CSV files:")
for idx, fname in enumerate(available_files, 1):
print(f"{idx}: {fname}")
choice = input("Select a file by number: ")
try:
selected_idx = int(choice) - 1
if selected_idx < 0 or selected_idx >= len(available_files):
raise ValueError
input_path = self.inputs_folder / available_files[selected_idx]
except Exception:
raise FileNotFoundError("Invalid selection. Aborting.")
return str(input_path)
def get_output_path(self, filename: str) -> str:
"""Get full output path, creating directory if needed."""
output_path = self.outputs_folder / "individual" / filename
output_path.parent.mkdir(parents=True, exist_ok=True)
return str(output_path)
def get_big_output_path(self, output_prefix: str) -> str:
"""Get path for combined output file."""
big_output_path = self.outputs_folder / "big" / f"{output_prefix}.pdf"
big_output_path.parent.mkdir(parents=True, exist_ok=True)
return str(big_output_path)
@classmethod
def list_available_templates(cls) -> Dict[str, str]:
"""List all available built-in templates."""
return {name: config.name for name, config in cls.BUILT_IN_TEMPLATES.items()}