| | 858 | |
|---|
| | 859 | class LinkIndex(BaseIndex): |
|---|
| | 860 | def __init__(self, name, container=None, index_href=True, index_rel=False): |
|---|
| | 861 | """ |
|---|
| | 862 | Simple atom category indexing. |
|---|
| | 863 | |
|---|
| | 864 | @type name: string |
|---|
| | 865 | @param name: an internal identifier used to reference |
|---|
| | 866 | the indexer. |
|---|
| | 867 | |
|---|
| | 868 | @type container: object |
|---|
| | 869 | @param container: instance of a class |
|---|
| | 870 | implementing the dictionnary interface. |
|---|
| | 871 | |
|---|
| | 872 | @type index_href: bool |
|---|
| | 873 | @param: when C{True} this leads to the href attribute to be used as |
|---|
| | 874 | an indexing key for a member. |
|---|
| | 875 | |
|---|
| | 876 | |
|---|
| | 877 | @type index_rel: bool |
|---|
| | 878 | @param: when C{True} this leads to the rel attribute to be used as |
|---|
| | 879 | an indexing key for a member. |
|---|
| | 880 | """ |
|---|
| | 881 | BaseIndex.__init__(self, name, container) |
|---|
| | 882 | self.index_href = index_href |
|---|
| | 883 | self.index_rel = index_rel |
|---|
| | 884 | |
|---|
| | 885 | def update(self, member): |
|---|
| | 886 | """ |
|---|
| | 887 | Updates the index based on the link elements of the provided member. |
|---|
| | 888 | |
|---|
| | 889 | @type member: L{MemberResource} |
|---|
| | 890 | @param member: Member object to index. |
|---|
| | 891 | """ |
|---|
| | 892 | entry = member.atom.entry |
|---|
| | 893 | links = entry.xml_xpath('atom:link') |
|---|
| | 894 | if links: |
|---|
| | 895 | for link in links: |
|---|
| | 896 | if self.index_href: |
|---|
| | 897 | if link.is_attribute('href'): |
|---|
| | 898 | hashed_key = sha.new('%s' % str(link.href)).hexdigest() |
|---|
| | 899 | self.store(hashed_key, (member.collection.name_or_id, member.member_id)) |
|---|
| | 900 | scheme, host = urlparse(str(link.href))[:2] |
|---|
| | 901 | base = '://'.join((scheme, host)) |
|---|
| | 902 | hashed_key = sha.new('%s' % base).hexdigest() |
|---|
| | 903 | self.store(hashed_key, (member.collection.name_or_id, member.member_id)) |
|---|
| | 904 | if self.index_rel: |
|---|
| | 905 | if link.is_attribute('rel'): |
|---|
| | 906 | hashed_key = sha.new('%s' % str(link.rel)).hexdigest() |
|---|
| | 907 | self.store(hashed_key, (member.collection.name_or_id, member.member_id)) |
|---|
| | 908 | |
|---|
| | 909 | def lookup(self, href=None, rel=None): |
|---|
| | 910 | """ |
|---|
| | 911 | Returns a set of member identifiers matching the href and rel provided. |
|---|
| | 912 | |
|---|
| | 913 | @type href: string |
|---|
| | 914 | @param href: value to be matched against indexed href attributes |
|---|
| | 915 | |
|---|
| | 916 | @type rel: string |
|---|
| | 917 | @param rel: value to be matched against indexed rel attributes |
|---|
| | 918 | |
|---|
| | 919 | @rtype: L{set} |
|---|
| | 920 | @return: joint result set of the query |
|---|
| | 921 | """ |
|---|
| | 922 | result = set() |
|---|
| | 923 | |
|---|
| | 924 | if href: |
|---|
| | 925 | hashed_key = sha.new('%s' % href).hexdigest() |
|---|
| | 926 | result |= self.load(hashed_key) |
|---|
| | 927 | |
|---|
| | 928 | if rel: |
|---|
| | 929 | hashed_key = sha.new('%s' % rel).hexdigest() |
|---|
| | 930 | result |= self.load(hashed_key) |
|---|
| | 931 | |
|---|
| | 932 | return result |
|---|