@@ -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
296297Path.cwd(frozen=True) is K:/pathlibutil/examples
297298Path.cwd(frozen=False) is K:/pathlibutil
298299Path.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+ ```
0 commit comments