Skip to content

Commit 46cd441

Browse files
committed
feat: add filepath package with dir() function
The filepath package helps work with file paths in KCL. This commit adds: - dir() function to get directory name from path - Comprehensive test cases - Package documentation and metadata Signed-off-by: suin <[email protected]>
1 parent a69de84 commit 46cd441

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

filepath/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# filepath
2+
3+
A KCL package that helps you work with file paths. It provides functions to handle file paths in a safe and consistent way.
4+
5+
## Installation
6+
7+
To install this package, run the following command:
8+
9+
```bash
10+
kcl mod add oci://ghcr.io/appthrust/kcl/filepath
11+
```
12+
13+
## Usage
14+
15+
### dir()
16+
17+
The `dir()` function returns the directory name of a path, similar to Python's `os.path.dirname()`.
18+
19+
```python kcl
20+
import filepath
21+
22+
# Basic usage
23+
dir = filepath.dir("/home/user/file.txt") # Returns "/home/user"
24+
```

filepath/dir.k

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
dir = lambda path: str -> str {
2+
"""Returns the directory name of a path."""
3+
ret = "."
4+
if path == "":
5+
ret = "."
6+
elif path == "/":
7+
ret = "/"
8+
elif path[0] == "/" and path.strip("/") == "":
9+
ret = "/"
10+
else:
11+
parts = path.rstrip("/").rsplit("/", 1)
12+
len = len(parts)
13+
if len > 1:
14+
if parts[0] == "":
15+
ret = "/"
16+
else:
17+
ret = parts[0].rstrip("/")
18+
19+
ret
20+
}

filepath/dir_test.k

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
test_basic_unix_path = lambda {
2+
assert dir("/home/user/file.txt") == "/home/user"
3+
}
4+
5+
test_trailing_slash = lambda {
6+
assert dir("/home/user/") == "/home"
7+
}
8+
9+
test_root_level_file = lambda {
10+
assert dir("/file.txt") == "/"
11+
}
12+
13+
test_filename_only = lambda {
14+
assert dir("file.txt") == "."
15+
}
16+
17+
test_current_directory = lambda {
18+
assert dir(".") == "."
19+
}
20+
21+
test_current_directory_with_slash = lambda {
22+
assert dir("./") == "."
23+
}
24+
25+
test_parent_directory = lambda {
26+
assert dir("..") == "."
27+
}
28+
29+
test_parent_directory_with_slash = lambda {
30+
assert dir("../") == "."
31+
}
32+
33+
test_root_directory = lambda {
34+
assert dir("/") == "/"
35+
}
36+
37+
test_empty_string = lambda {
38+
assert dir("") == "."
39+
}
40+
41+
test_multiple_slashes = lambda {
42+
assert dir("/home//user///file.txt") == "/home//user"
43+
}
44+
45+
test_multiple_current_directories = lambda {
46+
assert dir("./././file") == "././."
47+
}
48+
49+
test_multiple_parent_directories = lambda {
50+
assert dir("../../../file") == "../../.."
51+
}
52+
53+
test_triple_dots = lambda {
54+
assert dir(".../file") == "..."
55+
}
56+
57+
test_path_with_current_directory = lambda {
58+
assert dir("/home/user/./file") == "/home/user/."
59+
}
60+
61+
test_path_with_parent_directory = lambda {
62+
assert dir("/home/user/../file") == "/home/user/.."
63+
}
64+
65+
test_path_with_spaces = lambda {
66+
assert dir("/path/with spaces/file") == "/path/with spaces"
67+
}
68+
69+
test_path_with_tab = lambda {
70+
assert dir("/path/with\tTab/file") == "/path/with\tTab"
71+
}
72+
73+
test_path_with_unicode = lambda {
74+
assert dir("/ユーザー/ファイル") == "/ユーザー"
75+
}
76+
77+
test_path_with_hash = lambda {
78+
assert dir("/path/with#hash/file") == "/path/with#hash"
79+
}
80+
81+
test_path_with_query = lambda {
82+
assert dir("/path/with?query/file") == "/path/with?query"
83+
}
84+
85+
test_multiple_root_slashes = lambda {
86+
assert dir("////////") == "/"
87+
}

filepath/kcl.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "filepath"
3+
edition = "v0.10.0"
4+
version = "0.0.2"

filepath/kcl.mod.lock

Whitespace-only changes.

0 commit comments

Comments
 (0)