|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
18 | | -#$Id: back_anydbm.py,v 1.65 2002-08-30 08:35:45 richard Exp $ |
| 18 | +#$Id: back_anydbm.py,v 1.66 2002-09-01 04:32:30 richard Exp $ |
19 | 19 | ''' |
20 | 20 | This module defines a backend that saves the hyperdatabase in a database |
21 | 21 | chosen by anydbm. It is guaranteed to always be available in python |
@@ -1471,8 +1471,8 @@ def filter(self, search_matches, filterspec, sort, group, |
1471 | 1471 | sort spec. |
1472 | 1472 |
|
1473 | 1473 | "filterspec" is {propname: value(s)} |
1474 | | - "sort" is ['+propname', '-propname', 'propname', ...] |
1475 | | - "group is ['+propname', '-propname', 'propname', ...] |
| 1474 | + "sort" and "group" are (dir, prop) where dir is '+', '-' or None |
| 1475 | + and prop is a prop name or None |
1476 | 1476 | "search_matches" is {nodeid: marker} |
1477 | 1477 | ''' |
1478 | 1478 | cn = self.classname |
@@ -1591,126 +1591,109 @@ def filter(self, search_matches, filterspec, sort, group, |
1591 | 1591 | k.append(v) |
1592 | 1592 | l = k |
1593 | 1593 |
|
1594 | | - # optimise sort |
1595 | | - m = [] |
1596 | | - for entry in sort: |
1597 | | - if entry[0] != '-': |
1598 | | - m.append(('+', entry)) |
1599 | | - else: |
1600 | | - m.append((entry[0], entry[1:])) |
1601 | | - sort = m |
1602 | | - |
1603 | | - # optimise group |
1604 | | - m = [] |
1605 | | - for entry in group: |
1606 | | - if entry[0] != '-': |
1607 | | - m.append(('+', entry)) |
1608 | | - else: |
1609 | | - m.append((entry[0], entry[1:])) |
1610 | | - group = m |
1611 | 1594 | # now, sort the result |
1612 | 1595 | def sortfun(a, b, sort=sort, group=group, properties=self.getprops(), |
1613 | 1596 | db = self.db, cl=self): |
1614 | 1597 | a_id, an = a |
1615 | 1598 | b_id, bn = b |
1616 | 1599 | # sort by group and then sort |
1617 | | - for list in group, sort: |
1618 | | - for dir, prop in list: |
1619 | | - # sorting is class-specific |
1620 | | - propclass = properties[prop] |
| 1600 | + for dir, prop in group, sort: |
| 1601 | + if dir is None: continue |
1621 | 1602 |
|
1622 | | - # handle the properties that might be "faked" |
1623 | | - # also, handle possible missing properties |
1624 | | - try: |
1625 | | - if not an.has_key(prop): |
1626 | | - an[prop] = cl.get(a_id, prop) |
1627 | | - av = an[prop] |
1628 | | - except KeyError: |
1629 | | - # the node doesn't have a value for this property |
1630 | | - if isinstance(propclass, Multilink): av = [] |
1631 | | - else: av = '' |
| 1603 | + # sorting is class-specific |
| 1604 | + propclass = properties[prop] |
| 1605 | + |
| 1606 | + # handle the properties that might be "faked" |
| 1607 | + # also, handle possible missing properties |
| 1608 | + try: |
| 1609 | + if not an.has_key(prop): |
| 1610 | + an[prop] = cl.get(a_id, prop) |
| 1611 | + av = an[prop] |
| 1612 | + except KeyError: |
| 1613 | + # the node doesn't have a value for this property |
| 1614 | + if isinstance(propclass, Multilink): av = [] |
| 1615 | + else: av = '' |
| 1616 | + try: |
| 1617 | + if not bn.has_key(prop): |
| 1618 | + bn[prop] = cl.get(b_id, prop) |
| 1619 | + bv = bn[prop] |
| 1620 | + except KeyError: |
| 1621 | + # the node doesn't have a value for this property |
| 1622 | + if isinstance(propclass, Multilink): bv = [] |
| 1623 | + else: bv = '' |
| 1624 | + |
| 1625 | + # String and Date values are sorted in the natural way |
| 1626 | + if isinstance(propclass, String): |
| 1627 | + # clean up the strings |
| 1628 | + if av and av[0] in string.uppercase: |
| 1629 | + av = an[prop] = av.lower() |
| 1630 | + if bv and bv[0] in string.uppercase: |
| 1631 | + bv = bn[prop] = bv.lower() |
| 1632 | + if (isinstance(propclass, String) or |
| 1633 | + isinstance(propclass, Date)): |
| 1634 | + # it might be a string that's really an integer |
1632 | 1635 | try: |
1633 | | - if not bn.has_key(prop): |
1634 | | - bn[prop] = cl.get(b_id, prop) |
1635 | | - bv = bn[prop] |
1636 | | - except KeyError: |
1637 | | - # the node doesn't have a value for this property |
1638 | | - if isinstance(propclass, Multilink): bv = [] |
1639 | | - else: bv = '' |
1640 | | - |
1641 | | - # String and Date values are sorted in the natural way |
1642 | | - if isinstance(propclass, String): |
1643 | | - # clean up the strings |
1644 | | - if av and av[0] in string.uppercase: |
1645 | | - av = an[prop] = av.lower() |
1646 | | - if bv and bv[0] in string.uppercase: |
1647 | | - bv = bn[prop] = bv.lower() |
1648 | | - if (isinstance(propclass, String) or |
1649 | | - isinstance(propclass, Date)): |
1650 | | - # it might be a string that's really an integer |
1651 | | - try: |
1652 | | - av = int(av) |
1653 | | - bv = int(bv) |
1654 | | - except: |
1655 | | - pass |
| 1636 | + av = int(av) |
| 1637 | + bv = int(bv) |
| 1638 | + except: |
| 1639 | + pass |
| 1640 | + if dir == '+': |
| 1641 | + r = cmp(av, bv) |
| 1642 | + if r != 0: return r |
| 1643 | + elif dir == '-': |
| 1644 | + r = cmp(bv, av) |
| 1645 | + if r != 0: return r |
| 1646 | + |
| 1647 | + # Link properties are sorted according to the value of |
| 1648 | + # the "order" property on the linked nodes if it is |
| 1649 | + # present; or otherwise on the key string of the linked |
| 1650 | + # nodes; or finally on the node ids. |
| 1651 | + elif isinstance(propclass, Link): |
| 1652 | + link = db.classes[propclass.classname] |
| 1653 | + if av is None and bv is not None: return -1 |
| 1654 | + if av is not None and bv is None: return 1 |
| 1655 | + if av is None and bv is None: continue |
| 1656 | + if link.getprops().has_key('order'): |
1656 | 1657 | if dir == '+': |
1657 | | - r = cmp(av, bv) |
| 1658 | + r = cmp(link.get(av, 'order'), |
| 1659 | + link.get(bv, 'order')) |
1658 | 1660 | if r != 0: return r |
1659 | 1661 | elif dir == '-': |
1660 | | - r = cmp(bv, av) |
| 1662 | + r = cmp(link.get(bv, 'order'), |
| 1663 | + link.get(av, 'order')) |
1661 | 1664 | if r != 0: return r |
1662 | | - |
1663 | | - # Link properties are sorted according to the value of |
1664 | | - # the "order" property on the linked nodes if it is |
1665 | | - # present; or otherwise on the key string of the linked |
1666 | | - # nodes; or finally on the node ids. |
1667 | | - elif isinstance(propclass, Link): |
1668 | | - link = db.classes[propclass.classname] |
1669 | | - if av is None and bv is not None: return -1 |
1670 | | - if av is not None and bv is None: return 1 |
1671 | | - if av is None and bv is None: continue |
1672 | | - if link.getprops().has_key('order'): |
1673 | | - if dir == '+': |
1674 | | - r = cmp(link.get(av, 'order'), |
1675 | | - link.get(bv, 'order')) |
1676 | | - if r != 0: return r |
1677 | | - elif dir == '-': |
1678 | | - r = cmp(link.get(bv, 'order'), |
1679 | | - link.get(av, 'order')) |
1680 | | - if r != 0: return r |
1681 | | - elif link.getkey(): |
1682 | | - key = link.getkey() |
1683 | | - if dir == '+': |
1684 | | - r = cmp(link.get(av, key), link.get(bv, key)) |
1685 | | - if r != 0: return r |
1686 | | - elif dir == '-': |
1687 | | - r = cmp(link.get(bv, key), link.get(av, key)) |
1688 | | - if r != 0: return r |
1689 | | - else: |
1690 | | - if dir == '+': |
1691 | | - r = cmp(av, bv) |
1692 | | - if r != 0: return r |
1693 | | - elif dir == '-': |
1694 | | - r = cmp(bv, av) |
1695 | | - if r != 0: return r |
1696 | | - |
1697 | | - # Multilink properties are sorted according to how many |
1698 | | - # links are present. |
1699 | | - elif isinstance(propclass, Multilink): |
| 1665 | + elif link.getkey(): |
| 1666 | + key = link.getkey() |
1700 | 1667 | if dir == '+': |
1701 | | - r = cmp(len(av), len(bv)) |
| 1668 | + r = cmp(link.get(av, key), link.get(bv, key)) |
1702 | 1669 | if r != 0: return r |
1703 | 1670 | elif dir == '-': |
1704 | | - r = cmp(len(bv), len(av)) |
| 1671 | + r = cmp(link.get(bv, key), link.get(av, key)) |
1705 | 1672 | if r != 0: return r |
1706 | | - elif isinstance(propclass, Number) or isinstance(propclass, Boolean): |
| 1673 | + else: |
1707 | 1674 | if dir == '+': |
1708 | 1675 | r = cmp(av, bv) |
| 1676 | + if r != 0: return r |
1709 | 1677 | elif dir == '-': |
1710 | 1678 | r = cmp(bv, av) |
1711 | | - |
1712 | | - # end for dir, prop in list: |
1713 | | - # end for list in sort, group: |
| 1679 | + if r != 0: return r |
| 1680 | + |
| 1681 | + # Multilink properties are sorted according to how many |
| 1682 | + # links are present. |
| 1683 | + elif isinstance(propclass, Multilink): |
| 1684 | + if dir == '+': |
| 1685 | + r = cmp(len(av), len(bv)) |
| 1686 | + if r != 0: return r |
| 1687 | + elif dir == '-': |
| 1688 | + r = cmp(len(bv), len(av)) |
| 1689 | + if r != 0: return r |
| 1690 | + elif isinstance(propclass, Number) or isinstance(propclass, Boolean): |
| 1691 | + if dir == '+': |
| 1692 | + r = cmp(av, bv) |
| 1693 | + elif dir == '-': |
| 1694 | + r = cmp(bv, av) |
| 1695 | + |
| 1696 | + # end for dir, prop in sort, group: |
1714 | 1697 | # if all else fails, compare the ids |
1715 | 1698 | return cmp(a[0], b[0]) |
1716 | 1699 |
|
@@ -1909,13 +1892,18 @@ def __init__(self, db, classname, **properties): |
1909 | 1892 | if not properties.has_key('files'): |
1910 | 1893 | properties['files'] = hyperdb.Multilink("file") |
1911 | 1894 | if not properties.has_key('nosy'): |
1912 | | - properties['nosy'] = hyperdb.Multilink("user") |
| 1895 | + # note: journalling is turned off as it really just wastes |
| 1896 | + # space. this behaviour may be overridden in an instance |
| 1897 | + properties['nosy'] = hyperdb.Multilink("user", do_journal="no") |
1913 | 1898 | if not properties.has_key('superseder'): |
1914 | 1899 | properties['superseder'] = hyperdb.Multilink(classname) |
1915 | 1900 | Class.__init__(self, db, classname, **properties) |
1916 | 1901 |
|
1917 | 1902 | # |
1918 | 1903 | #$Log: not supported by cvs2svn $ |
| 1904 | +#Revision 1.65 2002/08/30 08:35:45 richard |
| 1905 | +#minor edits |
| 1906 | +# |
1919 | 1907 | #Revision 1.64 2002/08/22 07:57:11 richard |
1920 | 1908 | #Consistent quoting |
1921 | 1909 | # |
|
0 commit comments