import json
import os
import re
import shutil
import threading
import warnings
from jieba.analyse import ChineseAnalyzer
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.datetime_safe import date, datetime
from django.utils.encoding import force_str
from haystack.backends import (
BaseEngine,
BaseSearchBackend,
BaseSearchQuery,
EmptyResults,
log_query,
)
from haystack.constants import (
DJANGO_CT,
DJANGO_ID,
FUZZY_WHOOSH_MAX_EDITS,
FUZZY_WHOOSH_MIN_PREFIX,
ID,
)
from haystack.exceptions import MissingDependency, SearchBackendError, SkipDocument
from haystack.inputs import Clean, Exact, PythonData, Raw
from haystack.models import SearchResult
from haystack.utils import get_identifier, get_model_ct
from haystack.utils import log as logging
from haystack.utils.app_loading import haystack_get_model
try:
import whoosh
except ImportError:
raise MissingDependency(
"The 'whoosh' backend requires the installation of 'Whoosh'. Please refer to the documentation."
)
# Handle minimum requirement.
if not hasattr(whoosh, "__version__") or whoosh.__version__ < (2, 5, 0):
raise MissingDependency("The 'whoosh' backend requires version 2.5.0 or greater.")
# Bubble up the correct error.
from whoosh import index
from whoosh.analysis import StemmingAnalyzer
from whoosh.fields import BOOLEAN, DATETIME
from whoosh.fields import ID as WHOOSH_ID
from whoosh.fields import IDLIST, KEYWORD, NGRAM, NGRAMWORDS, NUMERIC, TEXT, Schema
from whoosh.filedb.filestore import FileStorage, RamStorage
from whoosh.highlight import ContextFragmenter, HtmlFormatter
from whoosh.highlight import highlight as whoosh_highlight
from whoosh.qparser import FuzzyTermPlugin, QueryParser
from whoosh.searching import ResultsPage
from whoosh.sorting import Count, DateRangeFacet, FieldFacet
from whoosh.support.relativedelta import relativedelta as RelativeDelta
from whoosh.writing import AsyncWriter
DATETIME_REGEX = re.compile(
r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(\.\d{3,6}Z?)?$"
)
LOCALS = threading.local()
LOCALS.RAM_STORE = None
class WhooshHtmlFormatter(HtmlFormatter):
"""
This is a HtmlFormatter simpler than the whoosh.HtmlFormatter.
We use it to have consistent results across backends. Specifically,
Solr, Xapian and Elasticsearch are using this formatting.
"""
template = "<%(tag)s>%(t)s</%(tag)s>"
class WhooshSearchBackend(BaseSearchBackend):
# Word reserved by Whoosh for special use.
RESERVED_WORDS = ("AND", "NOT", "OR", "TO")
# Characters reserved by Whoosh for special use.
# The '\\' must come first, so as not to overwrite the other slash replacements.
RESERVED_CHARACTERS = (
"\\",
"+",
"-",
"&&",
"||",
"!",
"(",
")",
"{",
"}",
"[",
"]",
"^",
'"',
"~",
"*",
"?",
":",
".",
)
def __init__(self, connection_alias, **connection_options):
super().__init__(connection_alias, **connection_options)
self.setup_complete = False
self.use_file_storage = True
self.post_limit = getattr(connection_options, "POST_LIMIT", 128 * 1024 * 1024)
self.path = connection_options.get("PATH")
if connection_options.get("STORAGE", "file") != "file":
self.use_file_storage = False
if self.use_file_storage and not self.path:
raise ImproperlyConfigured(
"You must specify a 'PATH' in your settings for connection '%s'."
% connection_alias
)
self.log = logging.getLogger("haystack")
def setup(self):
"""
Defers loading until needed.
"""
from haystack import connections
new_index = False
# Make sure the index is there.
if self.use_file_storage and not os.path.exists(self.path):
os.makedirs(self.path)
new_index = True
if self.use_file_storage and not os.access(self.path, os.W_OK):
raise IOError(
"The path to your Whoosh index '%s' is not writable for the current user/group."
% self.path
)
if self.use_file_storage:
self.storage = FileStorage(self.path)
else:
global LOCALS
if getattr(LOCALS, "RAM_STORE", None) is None:
LOCALS.RAM_STORE = RamStorage()
self.storage = LOCALS.RAM_STORE
self.content_field_name, self.schema = self.build_schema(
connections[self.connection_alias].get_unified_index().all_searchfields()
)
self.parser = QueryParser(self.content_field_name, schema=self.schema)
self.parser.add_plugins([FuzzyTermPlugin])
if new_index is True:
self.index = self.storage.create_index(self.schema)
else:
try:
self.index = self.storage.open_index(schema=self.schema)
except index.EmptyIndexError:
self.index = self.storage.create_index(self.schema)
self.setup_complete = True
def build_schema(self, fields):
schema_fields = {
ID: WHOOSH_ID(stored=True, unique=True),
DJANGO_CT: WHOOSH_ID(stored=True),
DJANGO_ID: WHOOSH_ID(stored=True),
}
# Grab the number of keys that are hard-coded into Haystack.
# We'll use this to (possibly) fail slightly more gracefully later.
initial_key_count = len(schema_fields)
content_field_name = ""
for _, field_class in fields.items():
if field_class.is_multivalued:
if field_class.indexed is False:
schema_fields[field_class.index_fieldname] = IDLIST(
stored=True, field_boost=field_class.boost
)
else:
schema_fields[field_class.index_fieldname] = KEYWORD(
stored=True,
commas=True,
scorable=True,
field_boost=field_class.boost,
)
elif field_class.field_type in ["date", "datetime"]:
schema_fields[field_class.index_fieldname] = DATETIME(
stored=field_class.stored, sortable=True
)
elif field_class.field_type == "integer":
schema_fields[field_class.index_fieldname] = NUMERIC(
stored=field_class.stored,
numtype=int,
field_boost=field_class.boost,
)
elif field_class.field_type == "float":
schema_fields[field_class.index_fieldname] = NUMERIC(
stored=field_class.stored,
numtype=float,
field_boost=field_class.boost,
)
elif field_class.field_type == "boolean":
# Field boost isn't supported on BOOLEAN as of 1.8.2.
schema_fields[field_class.index_fieldname] = BOOLEAN(
stored=field_class.stored
)
elif field_class.field_type == "ngram":
schema_fields[field_class.index_fieldname] = NGRAM(
minsize=3,
maxsize=15,
stored=field_class.stored,
field_boost=field_class.boost,
)
elif field_class.field_type == "edge_ngram":
schema_fields[field_class.index_fieldname] = NGRAMWORDS(
minsize=2,
maxsize=15,
at="start",
stored=field_class.stored,
field_boost=field_class.boost,
)
else:
没有合适的资源?快使用搜索试试~ 我知道了~
Python智能家居系统后端源码,基于Django+MQTT+ESP8266的智能家居系统源码
共1968个文件
js:1290个
css:234个
png:128个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 18 下载量 107 浏览量
2023-01-12
10:08:06
上传
评论 7
收藏 8.15MB ZIP 举报
温馨提示
Python智能家居系统后端源码,基于Django+MQTT+ESP8266的智能家居系统源码
资源推荐
资源详情
资源评论
收起资源包目录
Python智能家居系统后端源码,基于Django+MQTT+ESP8266的智能家居系统源码 (1968个子文件)
index.css 227KB
bootstrap.min.css 115KB
all.min.css 54KB
editor_ie8.css 49KB
editor_iequirks.css 49KB
editor_ie.css 48KB
editor_gecko.css 47KB
editor.css 47KB
editor_ie7.css 45KB
editor_iequirks.css 44KB
editor_ie8.css 43KB
editor_ie.css 43KB
editor_gecko.css 42KB
editor.css 42KB
cascader.css 29KB
date-picker.css 28KB
transfer.css 27KB
jquery-ui-1.10.4.min.css 27KB
col.css 25KB
pagination.css 23KB
table.css 22KB
message-box.css 21KB
time-picker.css 21KB
bootstrap-theme.min.css 19KB
select.css 18KB
slider.css 18KB
responsive.css 18KB
jquery.datetimepicker.css 17KB
select2.css 17KB
tabs.css 16KB
base.css 16KB
base.css 16KB
dialog_ie8.css 15KB
select2.min.css 15KB
dropdown.css 15KB
dialog_ie7.css 15KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog_ie8.css 14KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog.css 13KB
dialog.css 13KB
time-select.css 13KB
table-column.css 13KB
cascader-panel.css 13KB
tree.css 13KB
upload.css 12KB
icon.css 12KB
calendar.css 12KB
bootstrap-datetimepicker.min.css 11KB
autocomplete.css 10KB
button.css 10KB
input-number.css 10KB
widgets.css 10KB
menu.css 9KB
forms.css 8KB
autocomplete.css 8KB
color-picker.css 7KB
checkbox.css 7KB
input.css 7KB
index.css 6KB
changelists.css 6KB
step.css 5KB
collapse.css 5KB
drawer.css 5KB
aircraft.css 5KB
tag.css 5KB
orange.css 4KB
purple.css 4KB
x-blue.css 4KB
ant.design.css 4KB
e-purple.css 4KB
e-green.css 4KB
e-red.css 4KB
x-red.css 4KB
e-black.css 4KB
e-blue.css 4KB
e-purple-pro.css 4KB
x-green.css 4KB
e-red-pro.css 4KB
e-green-pro.css 4KB
e-black-pro.css 4KB
highdmin.css 4KB
admin.lte.css 4KB
layui.css 4KB
e-blue-pro.css 4KB
gray.css 4KB
green.css 4KB
dark.green.css 4KB
simpleui.css 4KB
element.css 4KB
black.css 4KB
light.css 4KB
base.css 4KB
waves.min.css 4KB
rtl.css 4KB
image.css 3KB
galleriffic-5.css 3KB
galleriffic-4.css 3KB
共 1968 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
Python代码大全
- 粉丝: 2726
- 资源: 689
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页