Beispiel #1
0
def get_writable_path(location, config=None):
    # TODO: Make sure these fit Qt's implementation.
    if location == Location.home:
        return pathlib.Path(os.path.expanduser('~'))
    if location == Location.temp:
        return pathlib.Path(tempfile.gettempdir())

    if location == Location.download:
        _load('Foundation')  # Needed by Rubicon.
        manager = ObjCClass('NSFileManager').defaultManager()
        err_ptr = ctypes.c_void_p(0)
        url = manager.URLForDirectory_inDomain_appropriateForURL_create_error_(
            NSDownloadsDirectory,
            NSUserDomainMask,
            None,
            False,
            ctypes.byref(err_ptr),
        )
        if not url:
            err_msg = 'Could not resolve {}'.format(location.name)
            if err_ptr:
                error = ObjCClass('NSError')(err_ptr)
                description = error.localizedDescription
                if description:
                    err_msg = '{}: {}'.format(err_msg, description)
            raise LocationError(err_msg)
        return pathlib.Path(url.path)

    if location == Location.log:
        path = pathlib.Path(os.path.expanduser('~/Library/Logs'))
        return _append_org_and_app(path, config)

    domain = {
        Location.generic_data: kUserDomain,
        Location.app_data: kUserDomain,
        Location.app_local_data: kUserDomain,
        Location.generic_cache: kUserDomain,
        Location.cache: kUserDomain,
        Location.runtime: kUserDomain,
    }.get(location, kOnAppropriateDisk)
    return _get_path(location, domain, config)