[Zope] Sharing this class

Nicolas Évrard nicoe@altern.org
Mon, 14 Apr 2003 11:58:31 +0200


--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=iso-8859-15; format=flowed
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by sarek.skynet.be id h3E9wkkp015455

Hello,

I've done this little class to add comments to files, news items,
photos, ...

I'm wondering how can I do it in zope. Should I put this class in the
python hierarchy or put it in a Product (will it be visible from other
products ?)

Btw, the functions genKey and isChild seems a little too simplistic, do
you have any idea for a better implemantation ?

--=20
(=B0>  Nicolas =C9vrard
/ )  Li=E8ge - Belgique
^^

--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=iso-8859-15
Content-Disposition: attachment; filename="Comment.py"
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by sarek.skynet.be id h3E9wkkp015455

def genKey(fkey, lkey) :
    return fkey + chr(lkey + 65)

def isChild(father, key) :
    return key.startswith(father)

class Comment :
    """
    This class stores comments and allow the nesting of comment.
    """

    def __init__(self, txt =3D '', key =3D '') :
        self.childs =3D []
        self.content =3D txt
        self.key =3D key

    def emptyMe(self) :
        """
        Empty the list of comments
        """
        self.childs =3D []
        self.content =3D ''
        self._p_changed =3D 1

    def addComment(self, comment, father =3D None) :
        """
        Add a comment to the list.
        If father is omitted (or None), the comment is added to the root =
of the
        tree.
        If father is present, the comment is added to the comment with th=
is Id.
        """
        if not father or father =3D=3D self.key :
            key =3D genKey(self.key, len(self.childs))
            self.childs.append(Comment(comment, key))
            return key
        else :
            for i in self.childs :
                if isChild(i.key, father) :=20
                    return i.addComment(comment, father)
            raise 'FatherKeyError', father

    def flatten(self, level=3D0) :
        """
        Just a little function to flatten this comment (for use in zpt).
        Return a liste of tuples.=20
         * First element is the nesting level
         * Second element is the comment.
        """
        val =3D []
        if self.childs :
            for i in self.childs :
                val +=3D i.flatten(level + 1)
        return [(level, self)] + val
       =20
    def __str__(self) :
        return_val =3D ""
        flat =3D self.flatten()
        for i in flat :
            return_val +=3D '%s-> CommentId : %s\n' % (i[0] * '    ', i[1=
].key)
            return_val +=3D '%s   Comment   : %s\n' % (i[0] * '    ', i[1=
].content)
        return return_val
   =20
    def __nonzero__(self) :
        return self.content !=3D ''

if __name__ =3D=3D '__main__' :
    test =3D Comment()
    sId =3D test.addComment('Salut')
    oId =3D test.addComment('Ola')
    test.addComment('Au revoir', sId)
    hId =3D test.addComment('Hasta Luego', oId)
    test.addComment('Adieu', sId)
    eId =3D test.addComment('So long', oId)
    test.addComment('Anglais', eId)
    test.addComment('Espagnol', hId)
   =20
    print test
    try :
        test.addComment('test', 'C')
    except 'FatherKeyError', cle :
        print 'Pas de p=E8re appel=E9 %s' % cle


--3V7upXqbjpZ4EhLz--