Example #1
0
class Autocomplete(Observable):
    def __init__(self, host, port, path, templateQuery, shortname, description, htmlTemplateQuery=None, name=None, **kwargs):
        Observable.__init__(self, name=name)
        self._host = host
        self._port = port
        self._path = path
        self._xmlTemplateQuery = templateQuery
        self._htmlTemplateQuery = htmlTemplateQuery
        self._shortname = shortname
        self._description = description
        self._fileServer = FileServer(documentRoot=filesDir)
        if 'defaultField' in kwargs:
            warn("Using old-style Autocomplete, please use PrefixBasedSuggest as observer.", DeprecationWarning)
            from .prefixbasedsuggest import PrefixBasedSuggest
            suggest = PrefixBasedSuggest(**kwargs)
            def _suggest(arguments):
                yield suggest._suggest(arguments=arguments, outside=self.any)
            self._suggest = _suggest
            self._templateQueryForSuggest = suggest.templateQueryForSuggest


    def handleRequest(self, arguments, path, **kwargs):
        filename = path.rsplit('/', 1)[-1]
        if filename in ['jquery.js', 'jquery.autocomplete.js', 'autocomplete.css']:
            yield self._files(filename, **kwargs)
        elif filename == 'opensearchdescription.xml':
            yield self._openSearchDescription(path=path, arguments=arguments, **kwargs)
        else:
            yield self._suggest(arguments=arguments)

    def _suggest(self, arguments):
        yield self.all.suggest(arguments=arguments)

    def _templateQueryForSuggest(self):
        return self.call.templateQueryForSuggest()

    def _openSearchDescription(self, **kwargs):
        server = self._host if str(self._port) == "80" else "{}:{}".format(self._host, self._port)
        yield okXml
        yield """<?xml version="1.0" encoding="UTF-8"?>"""
        yield """<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>%(shortname)s</ShortName>
    <Description>%(description)s</Description>
    <Url type="text/xml" method="get" template="http://%(server)s%(xmlTemplateQuery)s"/>
    %(htmlUrl)s
    <Url type="%(contentTypeSuggest)s" template="http://%(server)s%(path)s?%(templateQueryForSuggest)s"/>
</OpenSearchDescription>""" % {
            'contentTypeSuggest': CONTENT_TYPE_JSON_SUGGESTIONS,
            'shortname': self._shortname,
            'description': self._description,
            'server': server,
            'path': self._path,
            'xmlTemplateQuery': escapeXml(self._xmlTemplateQuery),
            'htmlUrl': '<Url type="text/html" method="get" template="http://{server}{template}"/>'.format(server=server, template=escapeXml(self._htmlTemplateQuery)) if self._htmlTemplateQuery else '',
            'templateQueryForSuggest': escapeXml(self._templateQueryForSuggest()),
        }

    def _files(self, filename, **kwargs):
        yield self._fileServer.handleRequest(path=filename, **kwargs)
Example #2
0
class Autocomplete(Observable):
    def __init__(
        self, host, port, path, templateQuery, shortname, description, htmlTemplateQuery=None, name=None, **kwargs
    ):
        Observable.__init__(self, name=name)
        self._host = host
        self._port = port
        self._path = path
        self._xmlTemplateQuery = templateQuery
        self._htmlTemplateQuery = htmlTemplateQuery
        self._shortname = shortname
        self._description = description
        self._fileServer = FileServer(documentRoot=filesDir)
        if "defaultField" in kwargs:
            warn("Using old-style Autocomplete, please use PrefixBasedSuggest as observer.", DeprecationWarning)
            from .prefixbasedsuggest import PrefixBasedSuggest

            suggest = PrefixBasedSuggest(**kwargs)

            def _suggest(arguments):
                yield suggest._suggest(arguments=arguments, outside=self.any)

            self._suggest = _suggest
            self._templateQueryForSuggest = suggest.templateQueryForSuggest

    def handleRequest(self, arguments, path, **kwargs):
        filename = path.rsplit("/", 1)[-1]
        if filename in ["jquery.js", "jquery.autocomplete.js", "autocomplete.css"]:
            yield self._files(filename, **kwargs)
        elif filename == "opensearchdescription.xml":
            yield self._openSearchDescription(path=path, arguments=arguments, **kwargs)
        else:
            yield self._suggest(arguments=arguments)

    def _suggest(self, arguments):
        yield self.all.suggest(arguments=arguments)

    def _templateQueryForSuggest(self):
        return self.call.templateQueryForSuggest()

    def _openSearchDescription(self, **kwargs):
        server = self._host if str(self._port) == "80" else "{}:{}".format(self._host, self._port)
        yield okXml
        yield """<?xml version="1.0" encoding="UTF-8"?>"""
        yield """<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>%(shortname)s</ShortName>
    <Description>%(description)s</Description>
    <Url type="text/xml" method="get" template="http://%(server)s%(xmlTemplateQuery)s"/>
    %(htmlUrl)s
    <Url type="%(contentTypeSuggest)s" template="http://%(server)s%(path)s?%(templateQueryForSuggest)s"/>
</OpenSearchDescription>""" % {
            "contentTypeSuggest": CONTENT_TYPE_JSON_SUGGESTIONS,
            "shortname": self._shortname,
            "description": self._description,
            "server": server,
            "path": self._path,
            "xmlTemplateQuery": escapeXml(self._xmlTemplateQuery),
            "htmlUrl": '<Url type="text/html" method="get" template="http://{server}{template}"/>'.format(
                server=server, template=escapeXml(self._htmlTemplateQuery)
            )
            if self._htmlTemplateQuery
            else "",
            "templateQueryForSuggest": escapeXml(self._templateQueryForSuggest()),
        }

    def _files(self, filename, **kwargs):
        yield self._fileServer.handleRequest(path=filename, **kwargs)