def save_data_path(*resource): """Path to save data. Ensure $XDG_DATA_HOME/<resource>/ exists, and return its path. 'resource' should normally be the name of your application. Use this when STORING a resource. Use the xdg_data_dirs variable for loading. """ resource = os.path.join(*resource) assert not resource.startswith('/') path = os.path.join(get_xdg_data_home(), resource.encode('utf-8')) # access the file system always with unicode # to properly behave in some operating systems if not os.path.isdir(unicode_path(path)): os.makedirs(unicode_path(path), 0o700) return path
def get_config_files(): """ return the path to the config files or and empty list. The search path is based on the paths returned by load_config_paths but it's returned in reverse order (e.g: /etc/xdg first). """ config_files = [] for xdg_config_dir in load_config_paths("ubuntuone"): xdg_config_dir = unicode_path(xdg_config_dir) config_file = os.path.join(xdg_config_dir, CONFIG_FILE) if os.path.exists(config_file): config_files.append(config_file) config_logs = os.path.join(xdg_config_dir, CONFIG_LOGS) if os.path.exists(config_logs): config_files.append(config_logs) # reverse the list as load_config_paths returns the user dir first config_files.reverse() # if we are running from a branch, get the config files from it too config_file = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, "data", CONFIG_FILE) if os.path.exists(config_file): config_files.append(config_file) config_logs = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, "data", CONFIG_LOGS) if os.path.exists(config_logs): config_files.append(config_logs) return config_files
def save_config_path(*resource): """Path to save configuration. Ensure $XDG_CONFIG_HOME/<resource>/ exists, and return its path. 'resource' should normally be the name of your application. Use this when SAVING configuration settings. Use the xdg_config_dirs variable for loading. """ resource = os.path.join(*resource) assert not resource.startswith('/') path = os.path.join(get_xdg_config_home(), resource) # access the file system always with unicode # to properly behave in some operating systems if not os.path.isdir(unicode_path(path)): os.makedirs(unicode_path(path), 0700) return path
def load_data_paths(*resource): """Iterator of data paths. Return an iterator which gives each directory named 'resource' in the stored data search path. Information provided by earlier directories should take precedence over later ones. """ resource = os.path.join(*resource) assert not resource.startswith('/') for data_dir in get_xdg_data_dirs(): path = os.path.join(data_dir, resource.encode('utf-8')) # access the file system always with unicode # to properly behave in some operating systems if os.path.exists(unicode_path(path)): yield path
def load_config_paths(*resource): """Iterator of configuration paths. Return an iterator which gives each directory named 'resource' in the configuration search path. Information provided by earlier directories should take precedence over later ones (ie, the user's config dir comes first). """ resource = os.path.join(*resource) assert not resource.startswith('/') for config_dir in get_xdg_config_dirs(): path = os.path.join(config_dir, resource) # access the file system always with unicode # to properly behave in some operating systems if os.path.exists(unicode_path(path)): yield path
# version. If you delete this exception statement from all source # files in the program, then also delete it here. """Miscellaneous logging functions.""" import logging import os import sys from dirspec.basedir import xdg_cache_home from dirspec.utils import unicode_path from functools import wraps from logging.handlers import RotatingFileHandler LOGFOLDER = os.path.join(xdg_cache_home, 'sso') # create log folder if it doesn't exists if not os.path.exists(unicode_path(LOGFOLDER)): os.makedirs(unicode_path(LOGFOLDER)) if os.environ.get('U1_DEBUG'): LOG_LEVEL = logging.DEBUG else: # Only log this level and above LOG_LEVEL = logging.INFO LOG_PATH = os.path.join(LOGFOLDER, 'sso-client.log') FMT = "%(asctime)s:%(msecs)s - %(name)s - %(levelname)s - %(message)s" MAIN_HANDLER = RotatingFileHandler(unicode_path(LOG_PATH), maxBytes=1048576, backupCount=5) MAIN_HANDLER.setLevel(LOG_LEVEL)