def initcfg(): """ Initialize the configuration. Read in settings if the configuration file exists, else create the settings directory if nonexistent. """ # read pre-v0.2.2b configuration if present if sys.platform == "darwin": oldcfg = os.path.join(expanduseru("~"), "Library", "Preferences", appname + " Preferences") else: oldcfg = os.path.join(expanduseru("~"), "." + appname) makecfgdir() if os.path.exists(confighome) and \ not os.path.exists(os.path.join(confighome, appname + ".ini")): try: if os.path.isfile(oldcfg): oldcfg_file = open(oldcfg, "rb") oldcfg_contents = oldcfg_file.read() oldcfg_file.close() cfg_file = open(os.path.join(confighome, appname + ".ini"), "wb") cfg_file.write("[Default]\n" + oldcfg_contents) cfg_file.close() elif sys.platform == "win32": key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\" + appname) numsubkeys, numvalues, mtime = _winreg.QueryInfoKey(key) cfg_file = open(os.path.join(confighome, appname + ".ini"), "wb") cfg_file.write("[Default]\n") for i in range(numvalues): name, value, type_ = _winreg.EnumValue(key, i) if type_ == 1: cfg_file.write((u"%s = %s\n" % (name, value)).encode("UTF-8")) cfg_file.close() except Exception, exception: # WindowsError 2 means registry key does not exist, do not show # warning in that case if sys.platform != "win32" or not hasattr(exception, "errno") or \ exception.errno != 2: from log import safe_print safe_print("Warning - could not process old configuration:", safe_unicode(exception)) # Set a few defaults which have None as possible value and thus cannot # be set in the 'defaults' collection setcfg("gamap_src_viewcond", "mt") setcfg("gamap_out_viewcond", "mt")
def get_verified_path(cfg_item_name, path=None): """ Verify and return dir and filename for a path from the user cfg, or a given path """ defaultPath = path or getcfg(cfg_item_name) defaultDir = expanduseru("~") defaultFile = "" if defaultPath: if os.path.exists(defaultPath): defaultDir, defaultFile = (os.path.dirname(defaultPath), os.path.basename(defaultPath)) elif (defaults.get(cfg_item_name) and os.path.exists(defaults[cfg_item_name])): defaultDir, defaultFile = (os.path.dirname(defaults[cfg_item_name]), os.path.basename(defaults[cfg_item_name])) elif os.path.exists(os.path.dirname(defaultPath)): defaultDir = os.path.dirname(defaultPath) return defaultDir, defaultFile
def update_defaults(): defaults.update({ "last_3dlut_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_archive_save_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_cal_path": os.path.join(storage, getstr("unnamed")), "last_cal_or_icc_path": os.path.join(storage, getstr("unnamed")), "last_colorimeter_ti3_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_testchart_export_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_filedialog_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_icc_path": os.path.join(storage, getstr("unnamed")), "last_reference_ti3_path": os.path.join(expanduseru("~"), getstr("unnamed")), "last_ti1_path": os.path.join(storage, getstr("unnamed")), "last_ti3_path": os.path.join(storage, getstr("unnamed")), "last_vrml_path": os.path.join(storage, getstr("unnamed")) })
except ImportError: import ctypes (CSIDL_APPDATA, CSIDL_COMMON_APPDATA, CSIDL_COMMON_STARTUP, CSIDL_PROFILE, CSIDL_PROGRAM_FILES_COMMON, CSIDL_STARTUP, CSIDL_SYSTEM) = (26, 35, 24, 40, 43, 7, 37) MAX_PATH = 260 def SHGetSpecialFolderPath(hwndOwner, nFolder, create=0): """ ctypes wrapper around shell32.SHGetSpecialFolderPathW """ buffer = ctypes.create_unicode_buffer(u'\0' * MAX_PATH) ctypes.windll.shell32.SHGetSpecialFolderPathW(0, buffer, nFolder, create) return buffer.value from util_os import expanduseru, expandvarsu, getenvu home = expanduseru("~") if sys.platform == "win32": # Always specify create=1 for SHGetSpecialFolderPath so we don't get an # exception if the folder does not yet exist library_home = appdata = SHGetSpecialFolderPath(0, CSIDL_APPDATA, 1) commonappdata = [SHGetSpecialFolderPath(0, CSIDL_COMMON_APPDATA, 1)] library = commonappdata[0] commonprogramfiles = SHGetSpecialFolderPath(0, CSIDL_PROGRAM_FILES_COMMON, 1) autostart = SHGetSpecialFolderPath(0, CSIDL_COMMON_STARTUP, 1) autostart_home = SHGetSpecialFolderPath(0, CSIDL_STARTUP, 1) iccprofiles = [os.path.join(SHGetSpecialFolderPath(0, CSIDL_SYSTEM), "spool", "drivers", "color")] iccprofiles_home = iccprofiles elif sys.platform == "darwin": library_home = os.path.join(home, "Library") library = os.path.join(os.path.sep, "Library")
}.get(folderid, folderid).upper() if folderid != "DESKTOP" or XDG.UserDirs.enabled: user_dir = XDG.UserDirs.default_dirs.get(folderid) if user: user_dir = XDG.UserDirs.user_dirs.get(folderid, user_dir) if user_dir: folder_path = os.path.join(home, user_dir) if ((folderid != "DESKTOP" and (not user_dir or (not os.path.isdir(folder_path) and not XDG.UserDirs.enabled))) or not waccess(folder_path, os.W_OK)): folder_path = home return folder_path home = expanduseru("~") if sys.platform == "win32": # Always specify create=1 for SHGetSpecialFolderPath so we don't get an # exception if the folder does not yet exist try: library_home = appdata = SHGetSpecialFolderPath(0, CSIDL_APPDATA, 1) except Exception as exception: raise Exception( "FATAL - Could not get/create user application data folder: %s" % exception) try: localappdata = SHGetSpecialFolderPath(0, CSIDL_LOCAL_APPDATA, 1) except Exception as exception: localappdata = os.path.join(appdata, "Local") cache = localappdata # Argyll CMS uses ALLUSERSPROFILE for local system wide app related data
data_dirs += [datahome] data_dirs += [os.path.join(dir_, appname) for dir_ in commonappdata] del dir_ data_dirs += [os.path.join(commonprogramfiles, appname)] exe_ext = ".exe" profile_ext = ".icm" else: btn_width_correction = 10 if sys.platform == "darwin": script_ext = ".command" mac_create_app = True scale_adjustment_factor = 1.0 config_sys = os.path.join(prefs, appname) confighome = os.path.join(prefs_home, appname) datahome = os.path.join(appdata, appname) logdir = os.path.join(expanduseru("~"), "Library", "Logs", appname) data_dirs += [datahome, os.path.join(commonappdata[0], appname)] else: script_ext = ".sh" scale_adjustment_factor = 1.0 config_sys = os.path.join(xdg_config_dir_default, appname) confighome = os.path.join(xdg_config_home, appname) datahome = os.path.join(xdg_data_home, appname) datahome_default = os.path.join(xdg_data_home_default, appname) logdir = os.path.join(datahome, "logs") data_dirs += [datahome] if not datahome_default in data_dirs: data_dirs += [datahome_default] data_dirs += [os.path.join(dir_, appname) for dir_ in xdg_data_dirs] del dir_
def trash(paths): """ Move files and folders to the trash. If a trashcan facility does not exist, do not touch the files. Return a list of successfully processed paths. """ if isinstance(paths, str): paths = [paths] if not isinstance(paths, list): raise TypeError(str(type(paths)) + " is not list") deleted = [] if sys.platform == "win32": for path in paths: path = os.path.abspath(path) if not os.path.exists(path): raise IOError("No such file or directory: " + path) if recycle(path): deleted.append(path) else: # http://freedesktop.org/wiki/Specifications/trash-spec trashroot = os.path.join( getenvu("XDG_DATA_HOME", os.path.join(expanduseru("~"), ".local", "share")), "Trash") trashinfo = os.path.join(trashroot, "info") # Older Linux distros and Mac OS X trashcan = os.path.join(expanduseru("~"), ".Trash") if sys.platform != "darwin" and not os.path.isdir(trashcan): # Modern Linux distros trashcan = os.path.join(trashroot, "files") if not os.path.isdir(trashcan): try: os.makedirs(trashcan) except OSError: raise TrashcanUnavailableError("Not a directory: '%s'" % trashcan) for path in paths: if os.path.isdir(trashcan): n = 1 dst = os.path.join(trashcan, os.path.basename(path)) while os.path.exists(dst): # avoid name clashes n += 1 dst = os.path.join(trashcan, os.path.basename(path) + "." + str(n)) if os.path.isdir(trashinfo): info = open( os.path.join(trashinfo, os.path.basename(dst) + ".trashinfo"), "w") info.write("[Trash Info]\n") info.write("Path=%s\n" % quote(path.encode(sys.getfilesystemencoding()))) info.write("DeletionDate=" + strftime("%Y-%m-%dT%H:%M:%S")) info.close() shutil.move(path, dst) else: # if trashcan does not exist, simply delete file/folder? pass # if os.path.isdir(path) and not os.path.islink(path): # shutil.rmtree(path) # else: # os.remove(path) deleted.append(path) return deleted