def _validate_namespace(self, namespace): """ Validate the given namespace. This method is idempotent! """ # Update config values (so other code can access them in the bootstrap phase) self._update_config(namespace) # Validate announce URLs for key, val in namespace["announce"].items(): if isinstance(val, basestring): namespace["announce"][key] = val.split() # Re-escape output formats self._interpolation_escape(namespace["formats"]) # Create objects from module specs for factory in ("engine",): if isinstance(namespace[factory], basestring): namespace[factory] = pymagic.import_name(namespace[factory])() if namespace[factory] else None # Do some standard type conversions for key in namespace: # Split lists if key.endswith("_list") and isinstance(namespace[key], basestring): namespace[key] = [i.strip() for i in namespace[key].replace(',', ' ').split()] # Resolve factory and callback handler lists elif any(key.endswith(i) for i in ("_factories", "_callbacks")) and isinstance(namespace[key], basestring): namespace[key] = [pymagic.import_name(i.strip()) for i in namespace[key].replace(',', ' ').split()] # Update config values again self._update_config(namespace)
def _validate_namespace(self, namespace): """ Validate the given namespace. This method is idempotent! """ # Update config values (so other code can access them in the bootstrap phase) self._update_config(namespace) # Validate announce URLs for key, val in namespace["announce"].items(): if isinstance(val, basestring): namespace["announce"][key] = val.split() # Re-escape output formats self._interpolation_escape(namespace["formats"]) # Create objects from module specs for factory in ("engine", ): if isinstance(namespace[factory], basestring): namespace[factory] = pymagic.import_name( namespace[factory])() if namespace[factory] else None # Do some standard type conversions for key in namespace: # Split lists if key.endswith("_list") and isinstance(namespace[key], basestring): namespace[key] = [ i.strip() for i in namespace[key].replace(',', ' ').split() ] # Resolve factory and callback handler lists elif any(key.endswith(i) for i in ("_factories", "_callbacks")) and isinstance( namespace[key], basestring): namespace[key] = [ pymagic.import_name(i.strip()) for i in namespace[key].replace(',', ' ').split() ] # Update config values again self._update_config(namespace)
def add_route(self, template, controller, **kwargs): """ Add a route definition `controller` can be either a controller instance, or the name of a callable that will be imported. """ if isinstance(controller, basestring): controller = pymagic.import_name(controller) self.routes.append((self.parse_route(template), controller, kwargs)) return self
def load_plugin_classes(): """ From all modules in this package, load a dict of the plugin classes. """ plugins = {} modules = set(os.path.splitext(module_file)[0] for module_file in pymagic.resource_listdir(__name__, '') if not module_file.startswith('_') and module_file.endswith(".py") ) for module in modules: for name, obj in vars(pymagic.import_name(__name__, module)).items(): if isinstance(obj, type) and issubclass(obj, plugin.Plugin) and not name.endswith("PluginBase"): plugins[name] = obj return plugins
def test_import_missing_colon(self): try: pymagic.import_name("pyrocore") except ValueError, exc: assert "pyrocore" in str(exc), str(exc)
def test_import_colon(self): docstr = pymagic.import_name("pyrocore:__doc__") assert "Core Package" in docstr
def test_import_fail(self): try: pymagic.import_name("pyrocore.does_not_exit", "__doc__") except ImportError, exc: assert "pyrocore.does_not_exit" in str(exc), str(exc)
def test_import_name(self): docstr = pymagic.import_name("pyrocore", "__doc__") assert "Core Package" in docstr docstr = pymagic.import_name("pyrocore.util", "__doc__") assert "Utility Modules" in docstr
def _validate_config(self): """ Handle and check configuration. """ groups = dict( job=defaultdict(Bunch), httpd=defaultdict(Bunch), ) for key, val in config.torque.items(): # Auto-convert numbers and bools if val.isdigit(): config.torque[key] = val = int(val) elif val.lower() in (matching.TRUE | matching.FALSE): val = matching.truth(str(val), key) # Assemble grouped parameters stem = key.split('.', 1)[0] if key == "httpd.active": groups[stem]["active"] = val elif stem in groups: try: stem, name, param = key.split('.', 2) except (TypeError, ValueError): self.fatal( "Bad %s configuration key %r (expecting %s.NAME.PARAM)" % (stem, key, stem)) else: groups[stem][name][param] = val for key, val in groups.iteritems(): setattr(self, key.replace("job", "jobs"), Bunch(val)) # Validate httpd config if self.httpd.active: if self.httpd.waitress.url_scheme not in ("http", "https"): self.fatal("HTTP URL scheme must be either 'http' or 'https'") if not isinstance(self.httpd.waitress.port, int) or not ( 1024 <= self.httpd.waitress.port < 65536): self.fatal("HTTP port must be a 16 bit number >= 1024") # Validate jobs for name, params in self.jobs.items(): for key in ("handler", "schedule"): if key not in params: self.fatal( "Job '%s' is missing the required 'job.%s.%s' parameter" % (name, name, key)) bool_param = lambda k, default, p=params: matching.truth( p.get(k, default), "job.%s.%s" % (name, k)) params.job_name = name params.dry_run = bool_param("dry_run", False) or self.options.dry_run params.active = bool_param("active", True) params.schedule = self._parse_schedule(params.schedule) if params.active: try: params.handler = pymagic.import_name(params.handler) except ImportError as exc: self.fatal("Bad handler name '%s' for job '%s':\n %s" % (params.handler, name, exc))