Skip to content

Commit 57facdd

Browse files
committed
Fix absolute path issue in create_datastore function
- Add use_relative_path parameter (default True) to create_datastore - Convert absolute paths to relative paths using data/workspace/filename format - Maintain backward compatibility with existing relative paths and HTTP URLs - Add test script to demonstrate path conversion functionality - Fixes issue #72 where absolute paths made GeoServer non-portable The fix ensures that when use_relative_path=True (default), absolute file paths are converted to relative paths like 'data/workspace/filename.shp', making GeoServer configurations portable across different machines.
1 parent f6a3284 commit 57facdd

2 files changed

Lines changed: 120 additions & 2 deletions

File tree

geo/Geoserver.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,7 @@ def create_datastore(
22152215
path: str,
22162216
workspace: Optional[str] = None,
22172217
overwrite: bool = False,
2218+
use_relative_path: bool = True,
22182219
):
22192220
"""
22202221
Create a datastore within the GeoServer.
@@ -2230,6 +2231,9 @@ def create_datastore(
22302231
The workspace to create the datastore in. Default is "default".
22312232
overwrite : bool
22322233
Whether to overwrite the existing datastore.
2234+
use_relative_path : bool, optional
2235+
Whether to convert absolute paths to relative paths. Default is True.
2236+
This ensures portability across different systems.
22332237
22342238
Returns
22352239
-------
@@ -2251,10 +2255,19 @@ def create_datastore(
22512255
if path is None:
22522256
raise Exception("You must provide a full path to the data")
22532257

2254-
data_url = "<url>file:{}</url>".format(path)
2255-
2258+
# Handle HTTP URLs (WFS endpoints)
22562259
if "http://" in path:
22572260
data_url = "<GET_CAPABILITIES_URL>{}</GET_CAPABILITIES_URL>".format(path)
2261+
else:
2262+
# Handle file paths with inline path conversion
2263+
if use_relative_path and os.path.isabs(path):
2264+
# Convert absolute path to relative path inline
2265+
filename = os.path.basename(path)
2266+
relative_path = f"data/{workspace}/{filename}"
2267+
data_url = "<url>file:{}</url>".format(relative_path)
2268+
else:
2269+
# Use path as-is (could be relative or absolute)
2270+
data_url = "<url>file:{}</url>".format(path)
22582271

22592272
data = "<dataStore><name>{}</name><connectionParameters>{}</connectionParameters></dataStore>".format(
22602273
name, data_url

tests/test_path_fix.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to demonstrate the absolute path fix for create_datastore function.
4+
"""
5+
6+
import os
7+
import sys
8+
import tempfile
9+
from geo.Geoserver import Geoserver
10+
11+
def test_path_conversion():
12+
"""Test the path conversion functionality."""
13+
14+
# Initialize GeoServer connection (adjust URL as needed)
15+
geo = Geoserver(
16+
service_url="http://localhost:8080/geoserver",
17+
username="admin",
18+
password="geoserver"
19+
)
20+
21+
# Create some temporary test paths that will work on any machine
22+
temp_dir = tempfile.gettempdir()
23+
24+
# Test paths using dynamic generation
25+
test_paths = [
26+
# Absolute paths (Windows-style)
27+
os.path.join(temp_dir, "countries.shp"),
28+
os.path.join(temp_dir, "data", "demo", "countries.shp"),
29+
30+
# Absolute paths (Unix/Linux-style)
31+
os.path.join("/tmp", "countries.shp"),
32+
os.path.join("/opt", "geoserver", "data_dir", "data", "demo", "countries.shp"),
33+
34+
# Relative paths (should remain unchanged)
35+
"data/demo/countries.shp",
36+
"countries.shp",
37+
38+
# HTTP URLs (should remain unchanged)
39+
"http://localhost:8080/geoserver/wfs?request=GetCapabilities"
40+
]
41+
42+
workspace = "demo"
43+
44+
print("Testing path conversion functionality:")
45+
print("=" * 50)
46+
47+
for path in test_paths:
48+
print(f"\nOriginal path: {path}")
49+
50+
# Test the path conversion logic
51+
if not path.startswith("http"):
52+
if os.path.isabs(path):
53+
filename = os.path.basename(path)
54+
relative_path = f"data/{workspace}/{filename}"
55+
print(f"Converted to: {relative_path}")
56+
else:
57+
print(f"Already relative: {path}")
58+
else:
59+
print("HTTP URL - no conversion needed")
60+
61+
print("\n" + "=" * 50)
62+
print("Usage examples:")
63+
print("=" * 50)
64+
65+
# Example 1: Using relative paths (default behavior)
66+
print("\n1. Using relative paths (default):")
67+
print("geo.create_datastore(")
68+
print(" name='countries',")
69+
print(" path='path/to/your/countries.shp', # Your actual file path")
70+
print(" workspace='demo',")
71+
print(" use_relative_path=True # This is the default")
72+
print(")")
73+
print("# This will create: file:data/demo/countries.shp")
74+
75+
# Example 2: Using absolute paths (legacy behavior)
76+
print("\n2. Using absolute paths (legacy):")
77+
print("geo.create_datastore(")
78+
print(" name='countries',")
79+
print(" path='path/to/your/countries.shp', # Your actual file path")
80+
print(" workspace='demo',")
81+
print(" use_relative_path=False")
82+
print(")")
83+
print("# This will create: file:path/to/your/countries.shp")
84+
85+
# Example 3: Using HTTP URLs
86+
print("\n3. Using HTTP URLs:")
87+
print("geo.create_datastore(")
88+
print(" name='wfs_countries',")
89+
print(" path='http://localhost:8080/geoserver/wfs?request=GetCapabilities',")
90+
print(" workspace='demo'")
91+
print(")")
92+
print("# This will create: <GET_CAPABILITIES_URL>http://localhost:8080/geoserver/wfs?request=GetCapabilities</GET_CAPABILITIES_URL>")
93+
94+
# Example 4: Cross-platform path handling
95+
print("\n4. Cross-platform path handling:")
96+
print("# The function automatically detects absolute vs relative paths")
97+
print("# Works on Windows, Linux, and macOS")
98+
print("geo.create_datastore(")
99+
print(" name='countries',")
100+
print(" path=os.path.join('/path', 'to', 'your', 'data.shp'), # Cross-platform")
101+
print(" workspace='demo'")
102+
print(")")
103+
104+
if __name__ == "__main__":
105+
test_path_conversion()

0 commit comments

Comments
 (0)