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 mainloop(self): """ The main loop. """ if self.options.create_config: # Create configuration config_loader = load_config.ConfigLoader(self.options.config_dir) config_loader.create() # Create directories for dirname in self.CONFIG_DIRS: dirpath = os.path.join(config_loader.config_dir, dirname) if not os.path.isdir(dirpath): self.LOG.info("Creating %r..." % (dirpath, )) os.mkdir(dirpath) # Initialize webserver stuff if matching.truth( getattr(config, "torque", {}).get("httpd.active", "False"), "httpd.active"): self.download_resource( config.torque["httpd.download_url.foundation"], "htdocs/f4", "css/foundation.css") self.download_resource( config.torque["httpd.download_url.smoothie"], "htdocs/js", "smoothie.js") self.download_resource( config.torque["httpd.download_url.d3js"], "htdocs/d3", "d3.v3.js") self.download_resource( config.torque["httpd.download_url.nvd3js"], "htdocs/d3", "nv.d3.min.js") self.download_resource( config.torque["httpd.download_url.cubism"], "htdocs/d3", "cubism.v1.min.js") elif self.options.dump_config or self.options.output: # Get public config attributes public = dict( (key, val) for key, val in vars(config).items() if not key.startswith('_') and (self.options.reveal or not ( callable(val) or key in config._PREDEFINED))) if self.options.dump_config: # Dump configuration pprinter = (pprint.PrettyPrinter if self.options.reveal else metafile.MaskingPrettyPrinter)() pprinter.pprint(public) else: def splitter(fields): "Yield single names for a list of comma-separated strings." for flist in fields: for field in flist.split(','): yield field.strip() values = [] for field in splitter(self.options.output): default = None if '=' in field: field, default = field.split('=', 1) try: val = public for key in field.split('.'): if key in val: val = val[key] elif isinstance(val, list) and key.isdigit(): val = val[int(key, 10)] else: matches = [ i for i in val.keys() if i.lower() == key.lower() ] if matches: val = val[matches[0]] else: raise KeyError(key) except (IndexError, KeyError), exc: if default is None: self.LOG.error("Field %r not found (%s)" % (field, exc)) break values.append(default) else: values.append(str(val)) else: print '\t'.join(values)
def mainloop(self): """ The main loop. """ if self.options.create_config: # Create configuration config_loader = load_config.ConfigLoader(self.options.config_dir) config_loader.create() # Create directories for dirname in self.CONFIG_DIRS: dirpath = os.path.join(config_loader.config_dir, dirname) if not os.path.isdir(dirpath): self.LOG.info("Creating %r..." % (dirpath,)) os.mkdir(dirpath) # Initialize webserver stuff if matching.truth(getattr(config, "torque", {}).get("httpd.active", "False"), "httpd.active"): self.download_resource(config.torque["httpd.download_url.smoothie"], "htdocs/js", "smoothie.js") elif self.options.dump_config or self.options.output: # Get public config attributes public = dict( (key, val) for key, val in vars(config).items() if not key.startswith("_") and (self.options.reveal or not (callable(val) or key in config._PREDEFINED)) ) if self.options.dump_config: # Dump configuration pprinter = (pprint.PrettyPrinter if self.options.reveal else metafile.MaskingPrettyPrinter)() pprinter.pprint(public) else: def splitter(fields): "Yield single names for a list of comma-separated strings." for flist in fields: for field in flist.split(","): yield field.strip() values = [] for field in splitter(self.options.output): default = None if "=" in field: field, default = field.split("=", 1) try: val = public for key in field.split("."): if key in val: val = val[key] elif isinstance(val, list) and key.isdigit(): val = val[int(key, 10)] else: matches = [i for i in val.keys() if i.lower() == key.lower()] if matches: val = val[matches[0]] else: raise KeyError(key) except (IndexError, KeyError), exc: if default is None: self.LOG.error("Field %r not found (%s)" % (field, exc)) break values.append(default) else: values.append(str(val)) else: print "\t".join(values)