Skip to content

Commit 819de53

Browse files
authored
Merge pull request #20 from d-chris/develop
Update URL normalization functions and enhance documentation
2 parents d46814a + 863cefc commit 819de53

9 files changed

Lines changed: 696 additions & 87 deletions

File tree

.pre-commit-config.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ repos:
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
7-
exclude: \.md$
87
- id: trailing-whitespace
98
- id: check-toml
109
- repo: https://github.com/tox-dev/pyproject-fmt
11-
rev: 2.4.3
10+
rev: v2.4.3
1211
hooks:
1312
- id: pyproject-fmt
1413
- repo: https://github.com/tox-dev/tox-ini-fmt
@@ -24,7 +23,7 @@ repos:
2423
hooks:
2524
- id: black
2625
- repo: https://github.com/adamchainz/blacken-docs
27-
rev: "1.19.0"
26+
rev: "1.19.1"
2827
hooks:
2928
- id: blacken-docs
3029
files: pathlibutil/
@@ -48,7 +47,9 @@ repos:
4847
- id: flake8
4948
args: ["--max-line-length", "88", "--exclude=examples/**"]
5049
- repo: https://github.com/d-chris/jinja2_pdoc
51-
rev: v1.1.0
50+
rev: v1.2.0
5251
hooks:
5352
- id: jinja2pdoc
5453
files: docs/README\.md\.jinja2$
54+
additional_dependencies:
55+
- pyperclip

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Parse and modify URLs with `pathlibutil.urlpath`.
4949

5050
- `pathlibutil.urlpath.UrlPath()` modify URL and easy access the `path` of the url like a `pathlib.PurePosixPath` object.
5151
- `pathlibutil.urlpath.UrlNetloc()` to parse and modify the `netloc` part of a URL.
52-
- `pathlibutil.urlpath.normalize_url()` to normalize a URL string.
52+
- `pathlibutil.urlpath.normalize()` to normalize a URL string.
53+
- `pathlibutil.urlpath.url_from()` to create a URL from an UNC path object.
5354

5455

5556
## Installation
@@ -296,4 +297,92 @@ os.getcwd is K:/pathlibutil
296297
Path.cwd(frozen=True) is K:/pathlibutil/examples
297298
Path.cwd(frozen=False) is K:/pathlibutil
298299
Path.cwd(frozen=_MEIPASS) is C:/Users/CHRIST~1.DOE/AppData/Local/Temp/_MEI106042
299-
```
300+
```
301+
302+
## Example 7
303+
304+
Console application to convert UNC paths to intranet URLs.
305+
306+
By default, it checks if the filename and URL are available and copies the
307+
normalized URL to the clipboard.
308+
309+
> `pathlibutil.urlpath.url_from()`
310+
311+
```python
312+
import argparse
313+
import sys
314+
315+
try:
316+
import pyperclip
317+
318+
import pathlibutil.urlpath as up
319+
except ModuleNotFoundError as e:
320+
raise ModuleNotFoundError(f"pip install {e.name.split('.')[0]}") from e
321+
322+
323+
def intranet_from(uncpath: str, check: bool = True) -> str:
324+
"""
325+
Return the intranet URL for the given UNC path.
326+
"""
327+
328+
url = up.url_from(
329+
uncpath,
330+
hostname="http://intranet.example.de",
331+
strict=check,
332+
)
333+
334+
return url.normalize()
335+
336+
337+
def cli():
338+
339+
parser = argparse.ArgumentParser(
340+
description=intranet_from.__doc__,
341+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
342+
)
343+
344+
parser.add_argument(
345+
"filename",
346+
nargs="*",
347+
help="The UNC path to the file.",
348+
)
349+
parser.add_argument(
350+
"-c",
351+
"--no-check",
352+
action="store_false",
353+
dest="check",
354+
help="Don't check if filename and url is available.",
355+
)
356+
parser.add_argument(
357+
"-s",
358+
"--silent",
359+
action="store_true",
360+
help="Do not print the url to stdout.",
361+
)
362+
parser.add_argument(
363+
"-n",
364+
"--no-clip",
365+
action="store_false",
366+
dest="clip",
367+
help="Don't copy the url to the clipboard.",
368+
)
369+
370+
args = parser.parse_args()
371+
filename = " ".join(args.filename)
372+
373+
url = intranet_from(filename, check=args.check)
374+
375+
if not args.silent:
376+
print(url)
377+
378+
if args.clip:
379+
pyperclip.copy(url)
380+
381+
382+
if __name__ == "__main__":
383+
try:
384+
cli()
385+
except Exception as e:
386+
print(e, file=sys.stderr)
387+
sys.exit(1)
388+
```

docs/README.md.jinja2

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Parse and modify URLs with `pathlibutil.urlpath`.
4949

5050
- `pathlibutil.urlpath.UrlPath()` modify URL and easy access the `path` of the url like a `pathlib.PurePosixPath` object.
5151
- `pathlibutil.urlpath.UrlNetloc()` to parse and modify the `netloc` part of a URL.
52-
- `pathlibutil.urlpath.normalize_url()` to normalize a URL string.
52+
- `pathlibutil.urlpath.normalize()` to normalize a URL string.
53+
- `pathlibutil.urlpath.url_from()` to create a URL from an UNC path object.
5354

5455

5556
## Installation
@@ -126,3 +127,11 @@ print(f'File size: {readme.size()} Bytes')
126127
```cmd
127128
{% pdoc examples/example6.py::docstring %}
128129
```
130+
131+
## Example 7
132+
133+
{% pdoc examples/example7::docstring %}
134+
135+
```python
136+
{% pdoc examples/example7::source.nodoc %}
137+
```

docs/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def main() -> int:
5757
"template_directory": cwd / "dark-mode",
5858
"show_source": False,
5959
"search": False,
60+
"docformat": "google",
6061
}
6162

6263
modules = [

examples/example7.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#! .venv\Scripts\python.exe
2+
3+
"""
4+
Console application to convert UNC paths to intranet URLs.
5+
6+
By default, it checks if the filename and URL are available and copies the
7+
normalized URL to the clipboard.
8+
9+
> `pathlibutil.urlpath.url_from()`
10+
"""
11+
12+
import argparse
13+
import sys
14+
15+
try:
16+
import pyperclip
17+
18+
import pathlibutil.urlpath as up
19+
except ModuleNotFoundError as e:
20+
raise ModuleNotFoundError(f"pip install {e.name.split('.')[0]}") from e
21+
22+
23+
def intranet_from(uncpath: str, check: bool = True) -> str:
24+
"""
25+
Return the intranet URL for the given UNC path.
26+
"""
27+
28+
url = up.url_from(
29+
uncpath,
30+
hostname="http://intranet.example.de",
31+
strict=check,
32+
)
33+
34+
return url.normalize()
35+
36+
37+
def cli():
38+
39+
parser = argparse.ArgumentParser(
40+
description=intranet_from.__doc__,
41+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
42+
)
43+
44+
parser.add_argument(
45+
"filename",
46+
nargs="*",
47+
help="The UNC path to the file.",
48+
)
49+
parser.add_argument(
50+
"-c",
51+
"--no-check",
52+
action="store_false",
53+
dest="check",
54+
help="Don't check if filename and url is available.",
55+
)
56+
parser.add_argument(
57+
"-s",
58+
"--silent",
59+
action="store_true",
60+
help="Do not print the url to stdout.",
61+
)
62+
parser.add_argument(
63+
"-n",
64+
"--no-clip",
65+
action="store_false",
66+
dest="clip",
67+
help="Don't copy the url to the clipboard.",
68+
)
69+
70+
args = parser.parse_args()
71+
filename = " ".join(args.filename)
72+
73+
url = intranet_from(filename, check=args.check)
74+
75+
if not args.silent:
76+
print(url)
77+
78+
if args.clip:
79+
pyperclip.copy(url)
80+
81+
82+
if __name__ == "__main__":
83+
try:
84+
cli()
85+
except Exception as e:
86+
print(e, file=sys.stderr)
87+
sys.exit(1)

0 commit comments

Comments
 (0)