You are not logged in Log in Join
You are here: Home » Members » andym » ~ZDictionary.py » View File

Log in
Name

Password

 

~ZDictionary.py

File details
Size
4 K
File type
text/plain

File contents

__version__ = '0.1'


# needed for inheritance and adding in folders
from OFS.Folder import Folder

# usual cut and paste imports
from Globals import DTMLFile
from Globals import MessageDialog
from Persistence import Persistent

# usual imports
import Globals
import AccessControl.Role
import string

from ts_regex import gsub
from Dictionary import DictOrg
from urllib import quote

# dtml files for adding forms
manage_addZDictionaryForm = DTMLFile('dtml/ZDictionaryAdd', globals())

def manage_addZDictionary(self, id, title='', REQUEST=None):
    ''' Adds a new ZDictionary object '''
    self._setObject(id, ZDictionary(id, title))
    if REQUEST is not None:
        return self.manage_main(self, REQUEST)

class ZDictionary(Folder):
    meta_type = 'ZDictionary'

    defnForm = DTMLFile('dtml/ZDictionaryDefn', globals())
    matchForm = DTMLFile('dtml/ZDictionaryMatch', globals())
    searchForm = DTMLFile('dtml/ZDictionarySearch', globals())

    _properties = (
        {'id':'title', 'type': 'string', 'mode': 'w'},
        {'id':'database', 'type': 'string', 'mode': 'w'},
    )

    __ac_permissions__ = (
        ('View', ('searchForm','defnForm','matchForm',)),
        ('Search ZDictionary', ('manage_search','manage_match',)),
        ('Manage properties', ('manage_addProperty', 'manage_editProperties', 'manage_delProperties', 'manage_changeProperties',)),
    )

    manage_options = (
        ( {'label':'Search', 'action':'searchForm', 'help':'ZDictionary.stx'},
        {'label':'Properties', 'action':'manage_propertiesForm', 'help':'ZDictionary.stx'}, ) +
        (Folder.manage_options[0],) +
        AccessControl.Role.RoleManager.manage_options
    )

    def __init__(self, id, title):
        self.id = id
        self.title = title
        self._set_properties()

    def _set_properties(self):
        self.database = '*'

    def _match_htmlify(self, d):
        i = 0
        out = {}
        for data in d:
            lines = string.split(data, '\r\n')
            if i == 0: del lines[0]
            for line in lines:
                temp = string.split(line, ' "')
                if len(line) > 1:
                    l = out.get(temp[0], [])
                    l.append(temp[1][:-1])
                    out[temp[0]] = l
            i = i + 1
        print out
        return out

    def _std_htmlify(self, d):
        gunk = '\\na-zA-Z0-9@.%%\\-\\/?&+=~ '
        protocol = 'http'
        i = 0
        out = []
        for data in d:
            # urlify urls
            data = gsub('%s:\([%s]+\)' % (protocol, gunk), '<a href="%s:\\1">%s://\\1</a>' % (protocol, protocol), data)
            data = gsub('{\([%s]+\)}' % gunk, '<a href="?searchterm=\\1">\\1</a>', data)

            lines = string.split(data, '\r\n')
            if i == 0: del lines[0]
            temp = string.split(lines[0], '"')
            del lines[0]

            temp = map(lambda x: string.strip(x), temp)
            out.append(
                {'definition':string.join(lines, '<br>'),
                  'term':temp[1],
                  'from':temp[2],
                  'from_detail':temp[3] }
                )
            i = i + 1

        return out

    def _signature(self, terms, options):
        ''' this puts the signature of the query in the cache in a unique form so we can pull it out again later '''
        _s = ''
        for i in terms: _s = '%s%s' % (_s, i)

        # do I have to make sure keys are sorted?
        k = options.keys()
        k.sort()
        for _k in k:
            _s = '%s%s%s' % (_s, _k, options[_k])
        return _s

    def _call(self, terms, options):
        if not hasattr(self, '_v_cache'):
            self._v_cache = {}

        sig = self._signature(terms, options)
        _d = None
        # is it in the cache?
        if not self._v_cache.has_key(sig):
            # nope, well lets go and get it
            d = DictOrg(terms=terms, options=options)
            d.run()
            d.parse()
            self._v_cache[self._signature(terms, options)] = d.data # put it in the cache
            _d = d.data
        else:
            # hooray, just pull it from the cache
            _d = self._v_cache[sig]
        return _d

    def manage_search(self, searchterm=None):
        ''' this calls DictOrg with the information '''
        options = { 'd':self.database, }
        return self._std_htmlify(self._call([searchterm,], options))

    def manage_match(self, matchterm=None, matchtype=None):
        ''' this calls a match with the information '''
        options = { 'd':self.database, 'm':matchtype}
        return self._match_htmlify(self._call([matchterm,], options))