maphew / winsf

Get Windows user "special folders" info (Desktop, Start Menu, Documents, …​) in a locale agnostic way
MIT License
1 stars 0 forks source link

Switch to Eryk Sun's ctype implementation, knownfolders.py #2

Open maphew opened 5 years ago

maphew commented 5 years ago

https://stackoverflow.com/a/33181421/14420

It's more complete and closer in line with how Windows handles special folders internally.

maphew commented 5 years ago

b995c9220f79d60c31d5320c81b045664d13d111 has first working integration. A bit wordy/repetive but short and clean at same time.

I don't like how "flat" knownfolders.py is, meaning after import all it's internal vars clutter up the namespace. It would be nice to narrow down to just the 2 we'd be using: get_known_folder_path() and FOLDERID master name list.

maphew commented 5 years ago

Trying to decide which is better:

print(kf.get(kf.FOLDERID.StartMenu)) or print(kf.table['StartMenu'])

maphew commented 4 years ago

Also see ActiveState's appdirs module: https://github.com/ActiveState/appdirs/blob/master/appdirs.py. It uses 3 access methods in cascade, attempting each in order: pywin32, ctypes, registry. The ctypes method does not use Eryk's newer approach with GUID instead of CLSIS. Also note the special handling of unicode in path name (without handling the path is not returned at all).

maphew commented 4 years ago

Found clean way! Create a new namespace from the existing FOLDERID namespace. Thank you @wim at https://stackoverflow.com/questions/43921240/pythonic-way-to-convert-dictionary-to-namedtuple-or-another-hashable-dict-like

First create a dictionary of the folder names and paths, then turn the dict into a namespace:

table = {}
for fid in dir(FOLDERID):
    try:
        path = get_known_folder_path(getattr(FOLDERID, fid))
        table[fid] = path
    except OSError:
        table[fid] = None

folders = SimpleNamespace(**table)

And usage:

from knownfolders import folders as kf
print('Desktop: ', kf.Desktop)
print('Programs: ', kf.Programs)
print('Profile: ', kf.Profile)

emits:

Desktop:  C:\Users\mattw\Desktop
Programs:  C:\Users\mattw\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Profile:  C:\Users\mattw