Exemplo n.º 1
0
def load_metadata(ob, filename):
    if not IPropertyManager.providedBy(ob):
        return
    filename = metadata_filename(filename)
    if not os.path.exists(filename):
        return
    f = file(filename, 'r')
    for line in f:
        line = line.rstrip('\n')
        if line == '[properties]':
            break
    for line in f:
        line = line.rstrip('\n')
        if line.startswith('['):
            break
        if line.startswith('#'):
            continue
        if not line.strip():
            continue
        if ' = ' not in line:
            print "%s: bad metadata line: %s" % (filename, line)
            continue
        key, value = line.split(' = ', 1)
        if ':' not in key:
            print "%s: bad metadata key: %s" % (filename, key)
            continue
        name, type = key.split(':')
        value = value.decode('string-escape')
        # TODO: saner type conversion

        if type == 'boolean':
            if value in ('True', 'False'):
                value = (value == 'True')
            else:
                value = int(value)
        elif type == 'int':
            value = int(value)
        elif type == 'string':
            pass
        elif type == 'ustring':
            value = unicode(value, 'UTF-8')
        else:
            print "%s: unsupported type: %s" % (filename, type)
            continue

        # TODO: handle selection/multiple selection
        if not ob.hasProperty(name):
            ob.manage_addProperty(name, value, type)
            # XXX: apparently type == 'tokens' also needs special handling
        else:
            setattr(ob, name, value)
    f.close()
    def __iter__(self):
        helper = self.helper
        doc = self.doc

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]

            if not pathkey:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:  # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = tuple(self.exclude)
                if excludekey:
                    excluded_props = tuple(
                        set(item[excludekey]) | set(excluded_props))

                helper.context = obj
                node = doc.createElement('properties')
                for elem in helper._extractProperties().childNodes:
                    if elem.nodeName != 'property':
                        continue
                    if elem.getAttribute('name') not in excluded_props:
                        node.appendChild(deepcopy(elem))
                if node.hasChildNodes():
                    doc.appendChild(node)
                    data = doc.toprettyxml(indent='  ', encoding='utf-8')
                    doc.unlink()

                if data:
                    item.setdefault(self.fileskey, {})
                    item[self.fileskey]['propertymanager'] = {
                        'name': '.properties.xml',
                        'data': data,
                    }

            yield item
Exemplo n.º 3
0
 def _getInfo(self, obj):
     href = obj.absolute_url()
     path = '/'.join(obj.getPhysicalPath())
     info = {
         'path': path,
         'href': href,
         'left_slots': None,
         'right_slots': None,
     }
     if IPropertyManager.providedBy(obj):
         obj = aq_base(obj)
         if obj.hasProperty('left_slots'):
             info['left_slots'] = obj.getProperty('left_slots')
             self.expressions = self.expressions.union(set(info['left_slots']))
         if obj.hasProperty('right_slots'):
             info['right_slots'] = obj.getProperty('right_slots')
             self.expressions = self.expressions.union(set(info['right_slots']))
     return info
Exemplo n.º 4
0
    def __iter__(self):
        helper = self.helper
        doc = self.doc

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]

            if not pathkey:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:  # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = tuple(self.exclude)
                if excludekey:
                    excluded_props = tuple(
                        set(item[excludekey]) | set(excluded_props))

                helper.context = obj
                for elem in helper._extractProperties():
                    if elem.attrib['name'] not in excluded_props:
                        doc.append(deepcopy(elem))
                if len(doc):
                    data = etree.tostring(doc,
                                          xml_declaration=True,
                                          encoding='utf-8',
                                          pretty_print=True)
                    doc.clear()

                if data:
                    item.setdefault(self.fileskey, {})
                    item[self.fileskey]['propertymanager'] = {
                        'name': '.properties.xml',
                        'data': data,
                    }

            yield item
Exemplo n.º 5
0
def write_metadata(ob, filename):
    if not IPropertyManager.providedBy(ob):
        return
    filename = metadata_filename(filename)
    dirname = os.path.dirname(filename)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    f = file(filename, 'w')
    print >> f, '[properties]'
    for prop in sorted(ob.propertyMap(), key=lambda prop: prop['id']):
        key = '%s:%s' % (prop['id'], prop['type'])
        value = getattr(ob, prop['id'])
        # TODO: selection and multiple selection need some more love
        # (saving prop['select_variable'] too)
        if isinstance(value, unicode):
            value = value.encode('UTF-8')
        value = str(value).encode('string-escape')
        print >> f, '%s = %s' % (key, value)
    f.close()
    def __iter__(self):
        helper = self.helper
        doc = self.doc

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]

            if not pathkey:
                yield item; continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:         # path doesn't exist
                yield item; continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = tuple(self.exclude)
                if excludekey:
                    excluded_props = tuple(set(item[excludekey]) | set(excluded_props))

                helper.context = obj
                node = doc.createElement('properties')
                for elem in helper._extractProperties().childNodes:
                    if elem.nodeName != 'property':
                        continue
                    if elem.getAttribute('name') not in excluded_props:
                        node.appendChild(deepcopy(elem))
                if node.hasChildNodes():
                    doc.appendChild(node)
                    data = doc.toprettyxml(indent='  ', encoding='utf-8')
                    doc.unlink()

                if data:
                    item.setdefault(self.fileskey, {})
                    item[self.fileskey]['propertymanager'] = {
                        'name': '.properties.xml',
                        'data': data,
                    }

            yield item
    def __iter__(self):
        helper = self.helper

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]
            fileskey = self.fileskey(*item.keys())[0]

            if not (pathkey and fileskey):
                yield item
                continue
            if 'propertymanager' not in item[fileskey]:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:  # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = self.exclude
                if excludekey:
                    excluded_props = tuple(
                        set(item[excludekey]) | set(excluded_props))

                data = item[fileskey]['propertymanager']['data']
                doc = minidom.parseString(data)
                root = doc.documentElement
                children = [k for k in root.childNodes]
                for child in children:
                    if child.nodeName != 'property':
                        continue
                    if child.getAttribute('name') in excluded_props:
                        root.removeChild(child)

                helper.context = obj
                helper._initProperties(root)

            yield item
    def __iter__(self):
        helper = self.helper
        doc = self.doc

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]

            if not pathkey:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:         # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = tuple(self.exclude)
                if excludekey:
                    excluded_props = tuple(set(item[excludekey]) | set(excluded_props))

                helper.context = obj
                for elem in helper._extractProperties():
                    if elem.attrib['name'] not in excluded_props:
                        doc.append(deepcopy(elem))
                if len(doc):
                    data = etree.tostring(doc, xml_declaration=True,
                                          encoding='utf-8', pretty_print=True)
                    doc.clear()

                if data:
                    item.setdefault(self.fileskey, {})
                    item[self.fileskey]['propertymanager'] = {
                        'name': '.properties.xml',
                        'data': data,
                    }

            yield item
Exemplo n.º 9
0
    def _getInfo(self, obj):

        href = obj.absolute_url()
        path = '/'.join(obj.getPhysicalPath())
        info = {
            'path': path,
            'href': href,
            'slots': None,
        }
        if IPropertyManager.providedBy(obj):
            obj = aq_base(obj)
            self.proplist.extend([i for i in obj.propertyIds() if i not in self.proplist])
            if obj.hasProperty(self.propname):
                info['slots'] = obj.getProperty(self.propname)
                if isinstance(info['slots'], int):
                    info['slots'] = str(info['slots'])
                if not isinstance(info['slots'], basestring):
                    self.expressions = self.expressions.union(set(info['slots']))
                else:
                    self.expressions = self.expressions.union(set([info['slots']]))
        return info
Exemplo n.º 10
0
    def __iter__(self):
        helper = self.helper

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]
            fileskey = self.fileskey(*item.keys())[0]

            if not (pathkey and fileskey):
                yield item
                continue
            if 'propertymanager' not in item[fileskey]:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:  # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = self.exclude
                if excludekey:
                    excluded_props = tuple(
                        set(item[excludekey]) | set(excluded_props))

                data = item[fileskey]['propertymanager']['data']
                doc = etree.fromstring(data)
                for child in doc:
                    if child.tag != 'property':
                        continue
                    if child.attrib['name'] in excluded_props:
                        doc.remove(child)

                helper.context = obj
                helper._initProperties(doc)

            yield item
    def __iter__(self):
        helper = self.helper

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]
            fileskey = self.fileskey(*item.keys())[0]

            if not (pathkey and fileskey):
                yield item
                continue
            if 'propertymanager' not in item[fileskey]:
                yield item
                continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None:         # path doesn't exist
                yield item
                continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = self.exclude
                if excludekey:
                    excluded_props = tuple(set(item[excludekey]) | set(excluded_props))

                data = item[fileskey]['propertymanager']['data']
                doc = etree.fromstring(data)
                for child in doc:
                    if child.tag != 'property':
                        continue
                    if child.attrib['name'] in excluded_props:
                        doc.remove(child)

                helper.context = obj
                helper._initProperties(doc)

            yield item
    def __iter__(self):
        helper = self.helper

        for item in self.previous:
            pathkey = self.pathkey(*item.keys())[0]
            fileskey = self.fileskey(*item.keys())[0]

            if not (pathkey and fileskey):
                yield item; continue
            if 'propertymanager' not in item[fileskey]:
                yield item; continue

            path = item[pathkey]
            obj = self.context.unrestrictedTraverse(path, None)
            if obj is None or path=="":         # path doesn't exist
                                                # or we are at import context root
                yield item; continue

            if IPropertyManager.providedBy(obj):
                data = None
                excludekey = self.excludekey(*item.keys())[0]
                excluded_props = self.exclude
                if excludekey:
                    excluded_props = tuple(set(item[excludekey]) | set(excluded_props))

                data = item[fileskey]['propertymanager']['data']
                doc = minidom.parseString(data)
                root = doc.documentElement
                children = [k for k in root.childNodes]
                for child in children:
                    if child.nodeName != 'property':
                        continue
                    if child.getAttribute('name') in excluded_props:
                        root.removeChild(child)
                helper.context = obj
                helper._initProperties(root)

            yield item