|
1 | | -import os |
2 | 1 | from pathlib import Path |
3 | 2 | from typing import Optional |
4 | | -from urllib.parse import urlparse |
5 | 3 |
|
6 | 4 | import click |
7 | 5 | import pandas as pd |
8 | 6 |
|
9 | | -from ..const import SUPPORTED_PROTOCOLS |
10 | | -from ..registry import Registry |
11 | | -from ..vecorel.util import name_from_uri |
12 | | - |
13 | | - |
14 | | -def is_valid_file_uri(uri, extensions=[]): |
15 | | - """Determine if the input is a file path or a URL and handle it.""" |
16 | | - if not isinstance(uri, str): |
17 | | - raise click.BadParameter("Input must be a string representing a file path or URL") |
18 | | - elif len(extensions) > 0 and not uri.endswith(tuple(extensions)): |
19 | | - raise click.BadParameter( |
20 | | - f"File '{uri}' must have one of the following extensions: {', '.join(extensions)}" |
21 | | - ) |
22 | | - elif os.path.exists(uri): |
23 | | - return uri |
24 | | - elif is_valid_url(uri): |
25 | | - return uri |
26 | | - else: |
27 | | - raise click.BadParameter( |
28 | | - "Input must be an existing local file or a URL with protocol: " |
29 | | - + ",".join(SUPPORTED_PROTOCOLS) |
30 | | - ) |
31 | | - |
32 | | - |
33 | | -def is_valid_url(url): |
34 | | - """Check if a URL is valid.""" |
35 | | - try: |
36 | | - result = urlparse(url) |
37 | | - return all([result.scheme in SUPPORTED_PROTOCOLS, result.netloc]) |
38 | | - except ValueError: |
39 | | - return False |
40 | | - |
41 | | - |
42 | | -def get_files(value, extensions=[]): |
43 | | - files = [] |
44 | | - extensions = tuple(extensions) |
45 | | - for v in value: |
46 | | - v = is_valid_file_uri(v) |
47 | | - if os.path.isdir(v): |
48 | | - for f in os.listdir(v): |
49 | | - if len(extensions) > 0 and not f.endswith(extensions): |
50 | | - continue |
51 | | - if f == "collection.json" or f == "catalog.json": # likely STAC |
52 | | - continue |
53 | | - files.append(os.path.join(v, f)) |
54 | | - else: |
55 | | - files.append(v) |
56 | | - return files |
57 | | - |
58 | | - |
59 | | -def valid_file(ctx, param, value): |
60 | | - return is_valid_file_uri(value) |
61 | | - |
62 | | - |
63 | | -def valid_vecorel_file(ctx, param, value) -> Path: |
64 | | - ext = Registry.get_vecorel_extensions() |
65 | | - return is_valid_file_uri(value, extensions=ext) |
66 | | - |
67 | | - |
68 | | -def valid_vecorel_files(ctx, param, value) -> list[Path]: |
69 | | - ext = Registry.get_vecorel_extensions() |
70 | | - |
71 | | - files = [] |
72 | | - if isinstance(value, str): |
73 | | - files = [value] |
74 | | - elif isinstance(value, tuple): |
75 | | - files = list(value) |
76 | | - elif isinstance(value, list): |
77 | | - files = value |
78 | | - |
79 | | - if len(files) == 0: |
80 | | - raise click.BadParameter("No files provided.") |
81 | | - |
82 | | - actual_files = [] |
83 | | - for file in files: |
84 | | - if is_valid_file_uri(file, extensions=ext): |
85 | | - actual_files.append(Path(file)) |
86 | | - |
87 | | - return actual_files |
| 7 | +from ..vecorel.util import is_url, name_from_uri |
88 | 8 |
|
89 | 9 |
|
90 | 10 | def parse_converter_input_files(ctx, param, value): |
@@ -130,14 +50,14 @@ def valid_schemas_for_cli(value: tuple[str]) -> dict[str, Path]: |
130 | 50 |
|
131 | 51 | if len(part) != 2: |
132 | 52 | raise click.BadParameter( |
133 | | - "Schema must be a URL and a local file path separated by a comma character." |
| 53 | + "Schema must be a URL and a local file path, separated by a comma character." |
134 | 54 | ) |
135 | | - if not is_valid_url(part[0]): |
| 55 | + if not is_url(part[0]): |
136 | 56 | raise click.BadParameter(f"Schema URL '{part[0]}' is not a valid URL.") |
137 | 57 |
|
138 | 58 | p = Path(part[1]) |
139 | 59 | if not p.exists(): |
140 | | - raise click.BadParameter(f"Local schema file '{p}' does not exist.") |
| 60 | + raise click.BadParameter(f"Local schema file '{p.resolve()}' does not exist.") |
141 | 61 |
|
142 | 62 | map_[part[0]] = p |
143 | 63 |
|
|
0 commit comments