def config_remove(name: str): """Removes a Maestral configuration.""" if name not in list_configs(): click.echo("Configuration '{}' could not be found.".format(name)) else: from maestral.config.base import get_conf_path for file in os.listdir(get_conf_path("maestral")): if file.startswith(name): os.unlink(os.path.join(get_conf_path("maestral"), file)) click.echo("Deleted configuration '{}'.".format(name))
def list_configs(): from maestral.config.base import get_conf_path configs = [] for file in os.listdir(get_conf_path("maestral")): if file.endswith(".ini"): configs.append(os.path.splitext(os.path.basename(file))[0]) return configs
def delete_pid(config_name): """ Reads the PID of the current process to the appropriate file for the given config name. """ from maestral.config.base import get_conf_path pid_file = get_conf_path("maestral", config_name + ".pid") os.unlink(pid_file)
def write_pid(config_name, socket_address="gui"): """ Write the PID of the current process to the appropriate file for the given config name. If a socket_address is given, it will be appended after a '|'. """ from maestral.config.base import get_conf_path pid_file = get_conf_path("maestral", config_name + ".pid") with open(pid_file, "w") as f: f.write(str(os.getpid()) + "|" + socket_address)
def read_pid(config_name): """ Reads the PID of the current process to the appropriate file for the given config name. """ from maestral.config.base import get_conf_path pid_file = get_conf_path("maestral", config_name + ".pid") with open(pid_file, "r") as f: pid, socket = f.read().split("|") pid = int(pid) return pid, socket
def filename(self): """Create a .ini filename. This .ini files stores the global preferences. """ if self.subfolder is None: config_file = osp.join(get_home_dir(), '.%s.ini' % self.name) return config_file else: folder = get_conf_path(self.subfolder) # Save defaults in a "defaults" dir of .spyder2 to not pollute it if 'defaults' in self.name: folder = osp.join(folder, 'defaults') if not osp.isdir(folder): os.mkdir(folder) config_file = osp.join(folder, '%s.ini' % self.name) return config_file
def get_cache_path(subfolder=None, filename=None, create=True): """ Returns the default cache path for the platform. This will be: - macOS: '~/Library/Application Support/SUBFOLDER/FILENAME' - Linux: 'XDG_CACHE_HOME/SUBFOLDER/FILENAME' - other: '~/.cache/SUBFOLDER/FILENAME' :param str subfolder: The subfolder for the app. :param str filename: The filename to append for the app. :param bool create: If ``True``, the folder '<subfolder>' will be created on-demand. """ if platform.system() == 'Darwin': return get_conf_path(subfolder, filename, create) else: return get_log_path(subfolder, filename, create)
def get_cache_path(subfolder=None, filename=None, create=True): """ Returns the default cache path for the platform. This will be: - macOS: "~/Library/Application Support/SUBFOLDER/FILENAME" - Linux: "$XDG_CACHE_HOME/SUBFOLDER/FILENAME" - fallback: "$HOME/.cache/SUBFOLDER/FILENAME" :param str subfolder: The subfolder for the app. :param str filename: The filename to append for the app. :param bool create: If ``True``, the folder "<subfolder>" will be created on-demand. """ if platform.system() == "Darwin": cache_path = get_conf_path(create=False) else: fallback = osp.join(_home_dir, ".cache") cache_path = os.environ.get("XDG_CACHE_HOME", fallback) return _to_full_path(cache_path, subfolder, filename, create)
def get_autostart_path(filename=None, create=True): """ Returns the default path for login items for the platform. This will be: - macOS: "~/Library/LaunchAgents/FILENAME" - Linux: "$XDG_CONFIG_HOME/autostart/FILENAME" - fallback: "$HOME/.config/autostart/FILENAME" :param str filename: The filename to append for the app. :param bool create: If ``True``, the folder "<subfolder>" will be created on-demand. """ if platform.system() == "Darwin": autostart_path = osp.join(_home_dir, "Library", "LaunchAgents") else: autostart_path = get_conf_path("autostart", create=create) if filename: autostart_path = osp.join(autostart_path, filename) return autostart_path
def get_runtime_path(subfolder=None, filename=None, create=True): """ Returns the default runtime path for the platform. This will be: - macOS: '~/Library/Application Support/SUBFOLDER/FILENAME' - Linux: '$XDG_RUNTIME_DIR/SUBFOLDER/FILENAME' - fallback: '$HOME/.cache/SUBFOLDER/FILENAME' :param str subfolder: The subfolder for the app. :param str filename: The filename to append for the app. :param bool create: If ``True``, the folder '<subfolder>' will be created on-demand. """ if platform.system() == 'Darwin': runtime_path = get_conf_path(create=False) else: fallback = get_cache_path(create=False) runtime_path = os.environ.get('XDG_RUNTIME_DIR', fallback) return _to_full_path(runtime_path, subfolder, filename, create)
def get_autostart_path(filename=None, create=True): """ Returns the default path for login items for the platform. This will be: - macOS: '~/Library/LaunchAgents/FILENAME' - Linux: '$XDG_CONFIG_HOME/autostart/FILENAME' - fallback: '$HOME/.config/autostart/FILENAME' :param str filename: The filename to append for the app. :param bool create: If ``True``, the folder '<subfolder>' will be created on-demand. """ if platform.system() == 'Darwin': autostart_path = osp.join(_home_dir, 'Library', 'LaunchAgents') else: autostart_path = get_conf_path('autostart', create=create) if filename: autostart_path = osp.join(autostart_path, filename) return autostart_path
def get_autostart_path(filename: Optional[str] = None, create: bool = True) -> str: """ Returns the default path for login items for the platform. This will be: - macOS: '~/Library/LaunchAgents/FILENAME' - Linux: '$XDG_CONFIG_HOME/autostart/FILENAME' - fallback: '$HOME/.config/autostart/FILENAME' :param filename: The filename to append for the app. :param create: If ``True``, the folder '<subfolder>' will be created on-demand. """ if platform.system() == "Darwin": autostart_path = osp.join(home_dir, "Library", "LaunchAgents") elif platform.system() == "Linux": autostart_path = get_conf_path("autostart", create=create) else: raise RuntimeError("Platform not supported") if filename: autostart_path = osp.join(autostart_path, filename) return autostart_path
def get_cache_path(subfolder: Optional[str] = None, filename: Optional[str] = None, create: bool = True) -> str: """ Returns the default cache path for the platform. This will be: - macOS: '~/Library/Application Support/SUBFOLDER/FILENAME' - Linux: '$XDG_CACHE_HOME/SUBFOLDER/FILENAME' - fallback: '$HOME/.cache/SUBFOLDER/FILENAME' :param subfolder: The subfolder for the app. :param filename: The filename to append for the app. :param create: If ``True``, the folder '<subfolder>' will be created on-demand. """ if platform.system() == "Darwin": cache_path = get_conf_path(create=False) elif platform.system() == "Linux": fallback = osp.join(home_dir, ".cache") cache_path = os.environ.get("XDG_CACHE_HOME", fallback) else: raise RuntimeError("Platform not supported") return to_full_path(cache_path, subfolder, filename, create)
def migrate_user_config(config_name): config_path = get_conf_path(CONFIG_DIR_NAME, create=False) config_fname = osp.join(config_path, config_name + '.ini') # load old config non-destructively try: old_conf = DefaultsConfig(config_path, config_name, '.ini') old_conf.read(config_fname, encoding='utf-8') old_version = old_conf.get(UserConfig.DEFAULT_SECTION_NAME, 'version') except OSError: return if Version(old_version) < Version('11.0.0'): # get values for moved settings excluded_folders = old_conf.get('main', 'excluded_folders') email = old_conf.get('account', 'email') display_name = old_conf.get('account', 'display_name') abbreviated_name = old_conf.get('account', 'abbreviated_name') acc_type = old_conf.get('account', 'type') usage = old_conf.get('account', 'usage') usage_type = old_conf.get('account', 'usage_type') update_notification_last = old_conf.get('app', 'update_notification_last') latest_release = old_conf.get('app', 'latest_release') cursor = old_conf.get('internal', 'cursor') lastsync = old_conf.get('internal', 'lastsync') recent_changes = old_conf.get('internal', 'recent_changes') # convert non-string types update_notification_last = float(update_notification_last) lastsync = float(lastsync) recent_changes = ast.literal_eval(recent_changes) excluded_folders = ast.literal_eval(excluded_folders) # set state values state = MaestralState(config_name) state.set('account', 'email', email) state.set('account', 'display_name', display_name) state.set('account', 'abbreviated_name', abbreviated_name) state.set('account', 'type', acc_type) state.set('account', 'usage', usage) state.set('account', 'usage_type', usage_type) state.set('app', 'update_notification_last', update_notification_last) state.set('app', 'latest_release', latest_release) state.set('sync', 'cursor', cursor) state.set('sync', 'lastsync', lastsync) state.set('sync', 'recent_changes', recent_changes) # load actual config to remove obsolete options and add moved ones conf = MaestralConfig(config_name) conf.set('main', 'excluded_items', excluded_folders) # clean up backup and defaults files from previous version of maestral for file in os.scandir(old_conf._path): if file.is_file(): if (conf._backup_suffix in file.name or conf._defaults_name_prefix in file.name): os.remove(file.path) logger.info(f'Migrated user config "{config_name}"') elif Version(old_version) < Version('12.0.0'): excluded_folders = old_conf.get('main', 'excluded_folders') excluded_folders = ast.literal_eval(excluded_folders) conf = MaestralConfig(config_name) conf.set('main', 'excluded_items', excluded_folders)