def create(self): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir, )) else: os.mkdir(self.config_dir) # Create default configuration files for filepath in walk_resources("pyrocore", "data/config"): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file, )) else: config_trail.append('') for i in config_trail: with closing(open(config_file + i, "w")) as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i, ))
def create(self): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir,)) else: os.mkdir(self.config_dir) # Create default configuration files for filepath in walk_resources("pyrocore", "data/config"): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file,)) else: config_trail.append('') for i in config_trail: with closing(open(config_file + i, "w")) as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i,))
def create(self, remove_all_rc_files=False): """ Create default configuration files at either the default location or the given directory. """ # Check and create configuration directory if os.path.exists(self.config_dir): self.LOG.debug("Configuration directory %r already exists!" % (self.config_dir, )) else: os.mkdir(self.config_dir) if remove_all_rc_files: for subdir in ('.', 'rtorrent.d'): config_files = list( glob.glob( os.path.join(os.path.abspath(self.config_dir), subdir, '*.rc'))) config_files += list( glob.glob( os.path.join(os.path.abspath(self.config_dir), subdir, '*.rc.default'))) for config_file in config_files: self.LOG.info("Removing %r!" % (config_file, )) os.remove(config_file) # Create default configuration files for filepath in sorted(walk_resources("pyrocore", "data/config")): # Load from package data text = pymagic.resource_string("pyrocore", "data/config" + filepath) # Create missing subdirs config_file = self.config_dir + filepath if not os.path.exists(os.path.dirname(config_file)): os.makedirs(os.path.dirname(config_file)) # Write configuration files config_trail = [".default"] if os.path.exists(config_file): self.LOG.debug("Configuration file %r already exists!" % (config_file, )) else: config_trail.append('') for i in config_trail: with open(config_file + i, "w") as handle: handle.write(text) self.LOG.info("Configuration file %r written!" % (config_file + i, ))
def _set_defaults(self, namespace, optional_cfg_files): """ Set default values in the given dict. """ # Add current configuration directory namespace["config_dir"] = self.config_dir # Load defaults for idx, cfg_file in enumerate([self.CONFIG_INI] + optional_cfg_files): if any(i in cfg_file for i in set('/' + os.sep)): continue # skip any non-plain filenames try: defaults = pymagic.resource_string("pyrocore", "data/config/" + cfg_file) #@UndefinedVariable except IOError, exc: if idx and exc.errno == errno.ENOENT: continue raise ini_file = ConfigParser.SafeConfigParser() ini_file.optionxform = str # case-sensitive option names ini_file.readfp(StringIO.StringIO(defaults), "<defaults>") self._set_from_ini(namespace, ini_file)
def _set_defaults(self, namespace, optional_cfg_files): """ Set default values in the given dict. """ # Add current configuration directory namespace["config_dir"] = self.config_dir # Load defaults for idx, cfg_file in enumerate([self.CONFIG_INI] + optional_cfg_files): if any(i in cfg_file for i in set('/' + os.sep)): continue # skip any non-plain filenames try: defaults = pymagic.resource_string( "pyrocore", "data/config/" + cfg_file) #@UndefinedVariable except IOError, exc: if idx and exc.errno == errno.ENOENT: continue raise ini_file = ConfigParser.SafeConfigParser() ini_file.optionxform = str # case-sensitive option names ini_file.readfp(StringIO.StringIO(defaults), "<defaults>") self._set_from_ini(namespace, ini_file)