def test_get_class_from_module(self): m = TestClassTools.__module__ cls = classtools.get_class_from_module(m, amcattest.AmCATTestCase) self.assertEqual(cls, self.__class__) cls = classtools.get_class_from_module(m, TestClassTools) self.assertEqual(cls, TestClassTools) import unittest cls = classtools.get_class_from_module(m, unittest.TestCase) self.assertEqual(cls, TestClassTools) from amcat.models.article import Article self.assertRaises(ValueError, classtools.get_class_from_module, m, Article.__class__)
def _bind_form(self, *args, **kargs): form = super(AddPlugin, self)._bind_form(*args, **kargs) form.data = form.data.copy() if form.data['module'] is None: if "/" in form.data['class_name']: # filename, so find and import module and class is first defined Script class form.data['module'] = guess_module(form.data['class_name']) cls = classtools.get_class_from_module(form.data['module'], Script) form.data['class_name'] = cls.__name__ elif "." in form.data['class_name']: # qualified class name, so split into module and class mod, cls = form.data['class_name'].rsplit(".", 1) form.data['module'], form.data['class_name'] = mod, cls else: raise forms.ValidationError( "If module is not given," "class name needs to be fully qualified") elif set(form.data['class_name']) & set([".", "/"]): raise forms.ValidationError( "If module name is given, class name %r cannot contain" ". or / characters" % form.data['class_name']) try: plugin_class = classtools.import_attribute(form.data['module'], form.data['class_name']) except ImportError as e: raise forms.ValidationError( "Class %s.%s could not be imported: %s" % (form.data['module'], form.data['class_name'], e)) if not form.data['label']: form.data['label'] = form.data['class_name'] if not form.data['description']: if plugin_class.__doc__ is None: plugin_module = classtools.import_attribute( form.data['module']) form.data['description'] = plugin_module.__doc__.strip() else: form.data['description'] = plugin_class.__doc__.strip() if form.data['type'] is None: superclass = plugin_class.__bases__[0] form.data['type'] = ".".join( (superclass.__module__, superclass.__name__)) lan = form.data['analysis_language'] if lan: try: int(lan) except ValueError: if isinstance(lan, basestring): lan = Language.objects.get(label=lan) form.data['analysis_language'] = lan.id f = self.options_form( form.data) # hack: re-bind form to propagate data print("XXXXXXXXXXX", f.data) return f
def run_cli(cls=None): """Handle command line interface invocation of this script""" if cls is None: module = classtools.get_calling_module() cls = classtools.get_class_from_module(module, Script) parser = argument_parser_from_script(cls) args = parser.parse_args() options = args.__dict__ verbose = options.pop("verbose", None) logging.basicConfig(format='[%(asctime)s] [%(name)s] %(message)s', level=(logging.DEBUG if verbose else logging.INFO)) logging.getLogger('requests').setLevel(logging.WARN) instance = cls(options) return instance.run()
def get_script(depth=2): module = classtools.get_calling_module(depth=depth) return classtools.get_class_from_module(module, Script)