xattrib.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:gtool 作者: gtoolframework 项目源码 文件源码
def __init__(self, obj, config=str()):

        def process(config):
            pathexpr = p.Literal("'").suppress() + \
                       p.Optional(
                        p.Combine(
                            p.OneOrMore(p.Literal("/") + p.Word(p.alphanums)) + p.Literal("/").suppress()
                        )
                       ).setResultsName('path') + \
                       p.Combine(
                           (p.Literal('@').suppress() | p.Literal('!').suppress()) +
                           p.Word(p.alphanums) +
                           p.Literal("'").suppress()
                       ).setResultsName('attrib')

            expr = p.Group(pathexpr).setResultsName('search')

            match = expr.parseString(config)

            _ret = []

            if 'search' in match:
                if 'path' in match['search']:
                    _ret.append(match['search']['path'])
                if 'attrib' in match['search']:
                   _ret.append(match['search']['attrib'])

            return _ret

        super(Xattrib, self).__init__(obj, config=config, defer=True)

        if self.config is None or len(self.config) < 1 or not isinstance(self.config, str):
            raise ValueError('Xattrib plugin function requires a config string')

        try:
            _result = process("'%s'" % self.config)
            if len(_result) == 2:
                self.targetobject, self.targetattribute = _result
            elif len(_result) == 1:
                _config = getattr(obj, _result[0], None)
                if _config is None:
                    raise ValueError('Xattrib plugin received an attribute name that does not exist')
                # TODO len check only required for attributes, but not method plugins
                if len(_config) > 1:
                    raise ValueError('Xattrib plugin received a attribute name that contains multiple values')
                self.targetobject, self.targetattribute = process("'%s'" % _config[0])
            else:
                raise Exception()
        except:
            raise ValueError('An error occured when processing the search string for the Xattrib plugin function')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号