files
File pattern normalization utilities for S3 and MFT path analysis.
FileDetection
dataclass
A custom detection rule for :func:detect_file_pattern.
Attributes:
| Name | Type | Description |
|---|---|---|
pattern |
str
|
Regex applied to the working string (after prior substitutions). |
replacement |
str
|
Replacement string, e.g. |
substring |
str | None
|
If given, the rule is skipped when this literal is absent from the original basename — cheap fast-path for context-specific rules. |
Source code in src/pinky_core/files.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
detect_file_pattern(filepath, custom_detections=None)
Normalize a filename to a canonical pattern by replacing variable segments.
Replaces dates, timestamps, UUIDs, ISO country codes, structured business identifiers (SIRET, SIREN, IBAN, BIC, VAT), and generic numeric IDs with named placeholders — enabling grouping of structurally similar filenames regardless of the specific value they carry.
Detection is purely structural: no business-context hint is required. Substitutions run most-specific first to avoid double-replacement.
Placeholder conventions:
{...}— temporal or generic numeric variable (date, timestamp, UUID, ID)<...>— typed structured identifier (country code, SIRET, IBAN…)
Substitution order:
- Timestamps (most digits, most specific)
- UUIDs
- Dates (delimited, then bare YYYYMMDD)
- Year-month combinations
- ISO country codes (alpha-3 before alpha-2)
- Structured business identifiers (IBAN, SIRET, SIREN, VAT_FR)
- Generic numeric segments (least specific)
Note: BIC/SWIFT codes (e.g. BNPAFRPP) are structurally indistinguishable
from 8-letter uppercase words and cannot be detected reliably without a bank
directory. BIC detection is intentionally excluded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Full file path or bare filename to normalize. The basename
(last |
required |
custom_detections
|
list[FileDetection] | None
|
Optional list of :class: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Pattern string for the basename, with variable segments replaced by |
str
|
placeholders. |
Examples:
>>> detect_file_pattern("EMPLOYEES_20240115.csv")
'EMPLOYEES_{DATE}.csv'
>>> detect_file_pattern("s3://bucket/feed/EMPLOYEES_20240115.csv")
'EMPLOYEES_{DATE}.csv'
>>> detect_file_pattern("PAYROLL_55208131700027.csv")
'PAYROLL_{SIRET}.csv'
>>> detect_file_pattern("TRANSFER_FR7630004000031234567890143.csv")
'TRANSFER_{IBAN}.csv'
Source code in src/pinky_core/files.py
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |