Skip to content

Commit 3f52a07

Browse files
committed
Allow folders to be specified as input files #3, v0.0.7
1 parent 23021cd commit 3f52a07

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2727

2828
- ...
2929

30+
## [v0.0.7] - 2024-02-23
31+
32+
- Allow folders to be specified as input files [#3](https://github.com/fiboa/cli/issues/3)
33+
3034
## [v0.0.6] - 2024-02-23
3135

3236
- Add `-e` option for create command to support extension schema mapping to local files

fiboa_cli/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .validate import validate as validate_
55
from .create import create as create_
6-
from .util import log, check_ext_schema_for_cli, valid_file_for_cli, valid_files_for_cli
6+
from .util import log, check_ext_schema_for_cli, valid_file_for_cli, valid_files_folders_for_cli
77
from .version import __version__
88

99
@click.group()
@@ -15,7 +15,7 @@ def cli():
1515
pass
1616

1717
@click.command()
18-
@click.argument('files', nargs=-1, callback=valid_files_for_cli)
18+
@click.argument('files', nargs=-1, callback=lambda ctx, param, value: valid_files_folders_for_cli(value, ["parquet", "geoparquet"]))
1919
@click.option(
2020
'--schema', '-s',
2121
type=click.Path(exists=True),
@@ -72,7 +72,7 @@ def validate(files, schema, ext_schema, fiboa_version, collection, data):
7272
sys.exit(2)
7373

7474
@click.command()
75-
@click.argument('files', nargs=-1, callback=valid_files_for_cli)
75+
@click.argument('files', nargs=-1, callback=lambda ctx, param, value: valid_files_folders_for_cli(value, ["json", "geojson"]))
7676
@click.option(
7777
'--out', '-o',
7878
type=click.Path(exists=False),

fiboa_cli/create.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ def create(config):
2323

2424
# Load all features from the GeoJSON files
2525
features = []
26-
for file in config.get("files"):
26+
files = config.get("files")
27+
for file in files:
2728
geojson = load_file(file)
2829
if geojson["type"] == "Feature":
2930
features.append(geojson)
3031
elif geojson["type"] == "FeatureCollection":
3132
features += geojson.features
3233
else:
33-
raise Exception("Unsupported GeoJSON type, must be Feature or FeatureCollection")
34+
log(f"{file}: Skipped - Unsupported GeoJSON type, must be Feature or FeatureCollection")
35+
36+
if len(features) == 0:
37+
raise Exception("No valid features provided as input files")
3438

3539
properties = {}
3640

fiboa_cli/util.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,20 @@ def is_valid_url(url):
8080
except:
8181
return False
8282

83-
def valid_files_for_cli(ctx, param, value):
84-
return [is_valid_file_uri(v) for v in value]
83+
def valid_files_folders_for_cli(value, extensions = []):
84+
files = []
85+
for v in value:
86+
v = is_valid_file_uri(v)
87+
if os.path.isdir(v):
88+
for f in os.listdir(v):
89+
if len(extensions) > 0 and not f.endswith(tuple(extensions)):
90+
continue
91+
if f == "collection.json" or f == "catalog.json": # likely STAC
92+
continue
93+
files.append(os.path.join(v, f))
94+
else:
95+
files.append(v)
96+
return files
8597

8698
def valid_file_for_cli(ctx, param, value):
8799
return is_valid_file_uri(value)

fiboa_cli/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.6"
1+
__version__ = "0.0.7"

0 commit comments

Comments
 (0)