| 149 | |
| 150 | |
| 151 | def _read_csv(path: str) -> List[Dict[str, str]]: |
| 152 | full_path = os.path.abspath(os.path.expanduser(path)) |
| 153 | if not os.path.isfile(full_path): |
| 154 | print(f"CSV file not found: {path}", ) |
| 155 | sys.exit(2) |
| 156 | fieldnames = ["hostname", "username", "password"] |
| 157 | out: List[Dict[str, str]] = [] |
| 158 | with open(full_path, encoding="utf-8") as fp: |
| 159 | reader = csv.DictReader(fp, fieldnames=fieldnames) |
| 160 | header_row = next(reader) # skip header row if present |
| 161 | if list(header_row.values()) != fieldnames: |
| 162 | fp.seek(0) |
| 163 | reader = csv.DictReader(fp, fieldnames=fieldnames) |
| 164 | for line_no, row in enumerate(reader, 0): |
| 165 | h, u, p = (row.get("hostname", "").strip(), |
| 166 | row.get("username", "").strip(), |
| 167 | row.get("password", "").strip()) |
| 168 | if not all((h, u, p)): |
| 169 | print(f"Row {line_no} incomplete - skipped") |
| 170 | continue |
| 171 | out.append({"hostname": h, "username": u, "password": p}) |
| 172 | return out |
| 173 | |
| 174 | |
| 175 | def _gen_data(csv_data: List[Dict[str, str]], |