예제 #1
0
파일: sqla.py 프로젝트: pombredanne/tw2.jit
        def make_node_from_property(prefix, parent, key, value, depth):
            node_id = safe_id(SEP.join([prefix, key, _unicode(value or '')]))
            children = []
            if type(value) in cls.entities:
                result = make_node_from_object(value, depth, node_id)
                if cls.auto_labels:
                    result['name'] = "%s:<br/>%s" % (
                        name2label(key), result['name'])
                return result
            elif type(value) != sa.orm.collections.InstrumentedList:
                name = "%s:<br/>%s" % (name2label(key), unicode(value))
            else:
                salt = SEP.join(prefix.split(SEP)[-2:] + [key])
                digest = md5(salt).hexdigest()
                node_id = SEP.join([prefix, key, digest])
                name = "%s (%i)" % (name2label(key), len(value))
                if depth < cls.depth:
                    if not cls._alphabetize(value):
                        children = [make_node_from_object(o, depth+1, node_id)
                                    for o in value]
                    else:
                        children = cls._do_alphabetize(value, depth+1, node_id)

                        for child, obj in product(children, value):
                            if not child['id'].endswith(_unicode(obj)[0].upper()):
                                continue
                            # This would mess with all the depth checking if it
                            # weren't for the removal code three blocks below.
                            n = make_node_from_object(obj,depth+2,child['id'])
                            child['children'].append(n)

                        for child in children:
                            child['name'] += " (%i)" % len(child['children'])

                        if cls.alphabetize_minimal:
                            children = [c for c in children
                                        if len(c['children']) > 0]

                        if not depth + 1 < cls.depth:
                            # Just delete 'em'!  We had to query them anyways
                            # to get the length counts for each alphabet letter,
                            # but if we keep them, we disobey the cls.depth
                            # parameter!  Oh wellz.
                            for child in children:
                                child['children'] = []


            node_id = safe_id(node_id)

            return {
                'id' : node_id, 'name' : name, 'children' : children, 'data' : {},
            }
예제 #2
0
    def nested_fetch(cls):
        cls.sanity()

        if type(cls.rrd_directories) != list:
            raise ValueError, "rrd_directories must be a list"

        if not cls.rrd_directories:
            raise ValueError, "rrd_directories is empty"

        types = [type(item) for item in cls.rrd_directories]
        if len(list(set(types))) != 1:
            raise ValueError, "rrd_directories must be of homogeneous form"

        _type = types[0]
        if not _type in [str]:
            raise ValueError, "rrd_directories items must be 'str'"

        rrd_directories = cls.rrd_directories
        if _type == str:
            rrd_directories = [
                (util.name2label(cls.directory2name(d)), d)
                for d in rrd_directories
            ]

        lens = [len(item) for item in rrd_directories]
        if len(list(set(lens))) != 1:
            raise ValueError, "rrd_directories items must be of the same length"

        _len = lens[0]
        if _len != 2:
            raise ValueError, "rrd_directories items must be of length 2"

        for item in rrd_directories:
            if not os.path.exists(item[1]):
                raise ValueError, "rrd_directory %s does not exist." % item[1]
            if not os.path.isdir(item[1]):
                raise ValueError, "rrd_directory %s is not a file." % item[1]

        ###################################
        # Done error checking.
        # Now we can actually get the data.
        ###################################

        labels = [item[0] for item in rrd_directories]
        data = []
        for name, directory in rrd_directories:
            rrd_filenames = [
                (cls.file2name(directory+fname), directory+fname)
                for fname in os.listdir(directory) if fname.endswith('.rrd')
            ]
            data.append(cls._do_flat_fetch(rrd_filenames))

        # Wrap up the output into a list of dicts
        return [
            {
                'data' : data[i],
                'label' : labels[i],
            } for i in range(len(data))
        ]
예제 #3
0
    def nested_fetch(cls):
        cls.sanity()

        if type(cls.rrd_directories) != list:
            raise ValueError, "rrd_directories must be a list"

        if not cls.rrd_directories:
            raise ValueError, "rrd_directories is empty"

        types = [type(item) for item in cls.rrd_directories]
        if len(list(set(types))) != 1:
            raise ValueError, "rrd_directories must be of homogeneous form"

        _type = types[0]
        if not _type in [str]:
            raise ValueError, "rrd_directories items must be 'str'"

        rrd_directories = cls.rrd_directories
        if _type == str:
            rrd_directories = [(util.name2label(cls.directory2name(d)), d)
                               for d in rrd_directories]

        lens = [len(item) for item in rrd_directories]
        if len(list(set(lens))) != 1:
            raise ValueError, "rrd_directories items must be of the same length"

        _len = lens[0]
        if _len != 2:
            raise ValueError, "rrd_directories items must be of length 2"

        for item in rrd_directories:
            if not os.path.exists(item[1]):
                raise ValueError, "rrd_directory %s does not exist." % item[1]
            if not os.path.isdir(item[1]):
                raise ValueError, "rrd_directory %s is not a file." % item[1]

        ###################################
        # Done error checking.
        # Now we can actually get the data.
        ###################################

        labels = [item[0] for item in rrd_directories]
        data = []
        for name, directory in rrd_directories:
            rrd_filenames = [
                (cls.file2name(directory + fname), directory + fname)
                for fname in os.listdir(directory) if fname.endswith('.rrd')
            ]
            data.append(cls._do_flat_fetch(rrd_filenames))

        # Wrap up the output into a list of dicts
        return [{
            'data': data[i],
            'label': labels[i],
        } for i in range(len(data))]
예제 #4
0
파일: sqla.py 프로젝트: pombredanne/tw2.jit
        def make_node_from_object(obj, depth=0, prefix=''):
            node_id = safe_id(
                SEP.join([prefix, type(obj).__name__,
                          _unicode(getattr(obj, get_pkey(type(obj))))]))
            prefix = node_id
            children = []
            data = getattr(obj, '__jit_data__', lambda : {})()
            cost = lambda k : data.get('traversal_costs', {}).get(k, 1)
            if depth < cls.depth:
                props = dict([(p.key, getattr(obj, p.key))
                              for p in obj.__mapper__.iterate_properties
                              if not exclude_property(p) ])

                # If 'imply_relations' is set to False
                # Wipe out each relation in the dict of props and replace it
                # with the actual objects from the relation.
                children = []
                for k, v in list(props.iteritems()):
                    if isinstance(v, sa.orm.collections.InstrumentedList):
                        del props[k]
                        if depth+cost(k) > cls.depth:
                            continue
                        if not cls.show_empty_relations and len(v) == 0:
                            continue
                        if not cls.imply_relations:
                            children.extend([
                                make_node_from_object(
                                    item, depth+cost(k),
                                    "%s%s%s%s%i" % (prefix, SEP, k, SEP, i))
                                for i, item in enumerate(v)
                            ])
                        else:
                            children.append(
                                make_node_from_property(
                                    prefix, obj, k, v, depth+cost(k))
                            )

                for k, v in props.iteritems():
                    if depth+cost(k) > cls.depth:
                        continue
                    children.append(
                        make_node_from_property(prefix, obj, k, v,
                                                depth+cost(k))
                    )

            name = unicode(obj)
            if cls.auto_labels:
                name = "%s:  %s" % (name2label(type(obj).__name__), name)
            return {
                'id' : node_id,
                'name' : name,
                'children' : children,
                'data' : data,
            }
예제 #5
0
파일: flat.py 프로젝트: pombredanne/tw2.rrd
    def flat_fetch(cls):
        cls.sanity()

        if type(cls.rrd_filenames) != list:
            raise ValueError, "rrd_filenames must be a list"

        if not cls.rrd_filenames:
            raise ValueError, "rrd_filenames is empty"

        types = [type(item) for item in cls.rrd_filenames]
        if len(list(set(types))) != 1:
            raise ValueError, "rrd_filenames must be of homogeneous form"

        _type = types[0]
        if _type not in [str, tuple]:
            raise ValueError, "rrd_filenames items must be 'str' or 'tuple'"

        rrd_filenames = cls.rrd_filenames
        if _type == str:
            rrd_filenames = [
                (util.name2label(cls.file2name(f)), f) for f in rrd_filenames
            ]

        lens = [len(item) for item in rrd_filenames]
        if len(list(set(lens))) != 1:
            raise ValueError, "rrd_filenames items must be of the same length"

        _len = lens[0]
        if _len != 2:
            raise ValueError, "rrd_filenames items must be of length 2"

        for item in rrd_filenames:
            if not os.path.exists(item[1]):
                raise ValueError, "rrd_filename %s does not exist." % item[1]
            if not os.path.isfile(item[1]):
                raise ValueError, "rrd_filename %s is not a file." % item[1]

        ###################################
        # Done error checking.
        # Now we can actually get the data.
        ###################################

        return cls._do_flat_fetch(rrd_filenames)
예제 #6
0
    def flat_fetch(cls):
        cls.sanity()

        if type(cls.rrd_filenames) != list:
            raise ValueError, "rrd_filenames must be a list"

        if not cls.rrd_filenames:
            raise ValueError, "rrd_filenames is empty"

        types = [type(item) for item in cls.rrd_filenames]
        if len(list(set(types))) != 1:
            raise ValueError, "rrd_filenames must be of homogeneous form"

        _type = types[0]
        if _type not in [str, tuple]:
            raise ValueError, "rrd_filenames items must be 'str' or 'tuple'"

        rrd_filenames = cls.rrd_filenames
        if _type == str:
            rrd_filenames = [(util.name2label(cls.file2name(f)), f)
                             for f in rrd_filenames]

        lens = [len(item) for item in rrd_filenames]
        if len(list(set(lens))) != 1:
            raise ValueError, "rrd_filenames items must be of the same length"

        _len = lens[0]
        if _len != 2:
            raise ValueError, "rrd_filenames items must be of length 2"

        for item in rrd_filenames:
            if not os.path.exists(item[1]):
                raise ValueError, "rrd_filename %s does not exist." % item[1]
            if not os.path.isfile(item[1]):
                raise ValueError, "rrd_filename %s is not a file." % item[1]

        ###################################
        # Done error checking.
        # Now we can actually get the data.
        ###################################

        return cls._do_flat_fetch(rrd_filenames)