Exemple #1
0
	def code(self):
		if self._code is not None:
			return self._code
		if self.parent['language'] != 'python':
			raise RuntimeError("Wrong language for %s: %s" % ('/'.join(self.path), self.parent['language']))
		self._code = import_string(self.value)
		return self._code
Exemple #2
0
    async def do(self, args):
        tree = await self.root._get_tree()
        t = await self.setup(meta=True)
        if args:
            objs = []
            for a in args:
                m = import_string(a)
                if isinstance(m, py_types.ModuleType):
                    from moat.script.util import objects
                    n = 0
                    for c in objects(m,
                                     Task,
                                     filter=lambda x: getattr(
                                         x, 'taskdef', None) is not None):
                        await t.add_task(c, force=self.force)
                        n += 1
                    if self.root.verbose > (1 if n else 0):
                        print("%s: %s command%s found." %
                              (a, n if n else "no", "" if n == 1 else "s"),
                              file=self.stdout)
                else:
                    if not isinstance(m, Task):
                        raise CommandError("%s is not a task" % a)
                    await t.add_task(m, force=self.force)
        else:
            for c in task_types():
                await t.add_task(c, force=self.force)

            r = await tree.subdir(*TASKSCAN_DIR)
            from moat.task.collect import Collector
            await r.add_task(path=(),
                             taskdef=Collector.taskdef,
                             force=self.force)

        await t.wait()
Exemple #3
0
	async def do(self,args):
		tree = await self.root._get_tree()
		t = await self.setup(meta=True)
		if args:
			objs = []
			for a in args:
				m = import_string(a)
				if isinstance(m,py_types.ModuleType):
					from moat.script.util import objects
					n = 0
					for c in objects(m, Task, filter=lambda x:getattr(x,'taskdef',None) is not None):
						await t.add_task(c, force=self.force)
						n += 1
					if self.root.verbose > (1 if n else 0):
						print("%s: %s command%s found." % (a,n if n else "no", "" if n==1 else "s"), file=self.stdout)
				else:
					if not isinstance(m,Task):
						raise CommandError("%s is not a task"%a)
					await t.add_task(m, force=self.force)
		else:
			for c in task_types():
				await t.add_task(c, force=self.force)

			r = await tree.subdir(*TASKSCAN_DIR)
			from moat.task.collect import Collector
			await r.add_task(path=(),taskdef=Collector.taskdef, force=self.force)

		await t.wait()
Exemple #4
0
 def code(self):
     if self._code is not None:
         return self._code
     if self.parent['language'] != 'python':
         raise RuntimeError("Wrong language for %s: %s" %
                            ('/'.join(self.path), self.parent['language']))
     self._code = import_string(self.value)
     return self._code
Exemple #5
0
	def _update_cls(self):
		if self.get('language','') == 'python':
			try:
				self.cls = import_string(self['code'])
			except (ImportError,AttributeError):
				logger.error("%s: Unable to import %s", '/'.join(self.path[:-1]),self['code'])
				self.cls = None

		else:
			self.cls = None
Exemple #6
0
    async def do(self, args):
        t = await self.setup()
        tree = await self.root._get_tree()
        t2 = await tree.subdir(TASKDEF_DIR)

        if args:
            objs = []
            for a in args:
                m = import_string(a)
                if isinstance(m, py_types.ModuleType):
                    try:
                        syms = m.__all__
                    except AttributeError:
                        syms = dir(m)
                    n = 0
                    for c in syms:
                        c = getattr(m, c, None)
                        if isinstance(c, type) and issubclass(
                                c, BaseModule) and c.prefix is not None:
                            objs.append(c)
                            n += 1
                    if self.root.verbose > (1 if n else 0):
                        print("%s: %s module%s found." %
                              (a, n if n else "no", "" if n == 1 else "s"),
                              file=self.stdout)

                    if self.tasks:
                        n = 0
                        for c in objects(m,
                                         Task,
                                         filter=lambda x: getattr(
                                             x, 'taskdef', None) is not None):
                            await t2.add_task(c, force=self.force)
                            n += 1
                        if self.root.verbose > (1 if n else 0):
                            print("%s: %s command%s found." %
                                  (a, n if n else "no", "" if n == 1 else "s"),
                                  file=self.stdout)

                else:
                    if not isinstance(m, BaseModule):
                        raise CommandError("%s is not a task" % a)
                    objs.append(m)
        else:
            objs = modules()

        tasks = await t.root.subdir(TASKDEF_DIR)
        for obj in objs:
            await t.add_module(obj, force=self.force)
            for ta in obj.task_types():
                await tasks.add_task(ta, force=self.force)
        await t.wait()
Exemple #7
0
	def _update_cls(self):
		self.cls = None
		try:
			if self['language'] != 'python':
				return
			code = self['code']
		except KeyError:
			return
		else:
			try:
				self.cls = import_string(code)
			except (ImportError,AttributeError):
				logger.error("%s: Unable to import %s", '/'.join(self.path[:-1]),self['code'])
Exemple #8
0
    def run(self, ctx, **k):
        event = self.params(ctx)
        if not event:
            raise SyntaxError("Events need at least one parameter")

        for k, v in self.vars.items():
            if k[0] == '$':
                k = ctx[k[1:]]
            try:
                if v[0] == '$':
                    v = ctx[v[1:]]
            except TypeError:  # not an int
                pass
            if k is not None and v is not None:
                setattr(event.ctx, k, v)

        proc = import_string(event[0])
        proc(Env(self, ctx), *event[1:])
Exemple #9
0
	def run(self,ctx,**k):
		event = self.params(ctx)
		if not event:
			raise SyntaxError("Events need at least one parameter")

		for k,v in self.vars.items():
			if k[0] == '$':
				k = ctx[k[1:]]
			try:
				if v[0] == '$':
					v = ctx[v[1:]]
			except TypeError: # not an int
				pass
			if k is not None and v is not None:
				setattr(event.ctx, k,v)

		proc = import_string(event[0])
		proc(Env(self,ctx), *event[1:])
Exemple #10
0
	async def do(self,args):
		t = await self.setup()
		tree = await self.root._get_tree()
		t2 = await tree.subdir(TASKDEF_DIR)

		if args:
			objs = []
			for a in args:
				m = import_string(a)
				if isinstance(m,py_types.ModuleType):
					try:
						syms = m.__all__
					except AttributeError:
						syms = dir(m)
					n = 0
					for c in syms:
						c = getattr(m,c,None)
						if isinstance(c,type) and issubclass(c,BaseModule) and c.prefix is not None:
							objs.append(c)
							n += 1
					if self.root.verbose > (1 if n else 0):
						print("%s: %s module%s found." % (a,n if n else "no", "" if n==1 else "s"), file=self.stdout)

					if self.tasks:
						n = 0
						for c in objects(m, Task, filter=lambda x:getattr(x,'taskdef',None) is not None):
							await t2.add_task(c, force=self.force)
							n += 1
						if self.root.verbose > (1 if n else 0):
							print("%s: %s command%s found." % (a,n if n else "no", "" if n==1 else "s"), file=self.stdout)

				else:
					if not isinstance(m,BaseModule):
						raise CommandError("%s is not a task"%a)
					objs.append(m)
		else:
			objs = modules()

		tasks = await t.root.subdir(TASKDEF_DIR)
		for obj in objs:
			await t.add_module(obj, force=self.force)
			for ta in obj.task_types():
				await tasks.add_task(ta, force=self.force)
		await t.wait()
Exemple #11
0
def objects(module, cls, immediate=False, direct=False, filter=lambda x: True):
    """\
        List all objects of a given class in a directory.

        If @immediate is set, only direct subclasses are returned.
        If @direct is set, modules in subdirectories are ignored.
        """
    def _check(m):
        try:
            if isinstance(m, str):
                m = import_module(m)
        except ImportError as ex:
            raise  # ImportError(m) from ex # pragma: no cover
            # not going to ship a broken file for testing this
        else:
            try:
                syms = m.__all__
            except AttributeError:
                syms = dir(m)
            for c in syms:
                c = getattr(m, c, None)
                if isinstance(c,type) and \
                        ((c.__base__ is cls) if immediate else (c is not cls and issubclass(c,cls))):
                    if filter(c):
                        yield c

    if isinstance(module, str):
        from qbroker.util import import_string
        module = import_string(module)
    yield from _check(module)
    try:
        for a, b, c in pkgutil.walk_packages(
            (os.path.dirname(module.__file__), ), module.__name__ + '.'):
            if direct and a.path != module.__path__[0]:
                continue
            yield from _check(b)
    except ImportError as err:
        yield from _check(module)
Exemple #12
0
def objects(module, cls, immediate=False,direct=False,filter=lambda x:True):
    """\
        List all objects of a given class in a directory.

        If @immediate is set, only direct subclasses are returned.
        If @direct is set, modules in subdirectories are ignored.
        """
    def _check(m):
        try:
            if isinstance(m,str):
                m = import_module(m)
        except ImportError as ex:
            raise # ImportError(m) from ex # pragma: no cover
            # not going to ship a broken file for testing this
        else:
            try:
                syms = m.__all__
            except AttributeError:
                syms = dir(m)
            for c in syms:
                c = getattr(m,c,None)
                if isinstance(c,type) and \
                        ((c.__base__ is cls) if immediate else (c is not cls and issubclass(c,cls))):
                    if filter(c):
                        yield c
            
    if isinstance(module,str):
        from qbroker.util import import_string
        module = import_string(module)
    yield from _check(module)
    try:
        for a,b,c in pkgutil.walk_packages((os.path.dirname(module.__file__),), module.__name__+'.'):
            if direct and a.path != module.__path__[0]:
                continue
            yield from _check(b)
    except ImportError as err:
        yield from _check(module)