Переделан конфиг (под dev и production)

This commit is contained in:
Jack Stdin
2016-01-31 23:23:52 +03:00
parent c65f78dd06
commit 1a221d9a93
16 changed files with 117 additions and 106 deletions

View File

@@ -4,19 +4,20 @@ from bottle import template
from aore.dbutils.dbimpl import DBImpl
from aore.fias.search import SphinxSearch
from aore.config import db as dbparams
from aore.config import db_conf
class FiasFactory:
def __init__(self):
self.db = DBImpl(psycopg2, dbparams)
self.db = DBImpl(psycopg2, db_conf)
self.searcher = SphinxSearch(self.db)
self.expand_templ = template('aore/templates/postgre/expand_query.sql', aoid="//aoid")
self.normalize_templ = template('aore/templates/postgre/normalize_query.sql', aoid="//aoid")
# text - строка поиска
# strong - строгий поиск (True) или "мягкий" (False) (с допущением ошибок, опечаток)
# out_format - "full" or "simple" - полный (подробно для каждого подпункта) или простой (только строка и AOID)
def find(self, text, strong=False, out_format="simple"):
# Строгий используется при импорте из внешних систем (автоматически), где ошибка критична
def find(self, text, strong=False):
try:
results = self.searcher.find(text, strong)
except Exception, err:
@@ -26,12 +27,26 @@ class FiasFactory:
# Нормализует подаваемый AOID или AOGUID в актуальный AOID
def normalize(self, aoid_guid):
pass
try:
sql_query = self.normalize_templ.replace("//aoid", aoid_guid)
rows = self.db.get_rows(sql_query, True)
except Exception, err:
return dict(error=err.args[0])
if len(rows) == 0:
return []
else:
return rows[0]
# Разворачивает AOID в представление (перед этим нормализует)
def expand(self, aoid_guid):
try:
sql_query = self.expand_templ.replace("//aoid", aoid_guid)
normalized_id = self.normalize(aoid_guid)
if 'aoid' not in normalized_id:
raise BaseException("Invalid AOID or AOGUID")
else:
normalized_id = normalized_id['aoid']
sql_query = self.expand_templ.replace("//aoid", normalized_id)
rows = self.db.get_rows(sql_query, True)
except Exception, err:
return dict(error=err.args[0])

View File

@@ -5,7 +5,7 @@ import re
import Levenshtein
import sphinxapi
from aore.config import sphinx
from aore.config import sphinx_conf
from aore.fias.wordentry import WordEntry
from aore.miscutils.trigram import trigram
@@ -26,17 +26,17 @@ class SphinxSearch:
self.db = db
self.client_sugg = sphinxapi.SphinxClient()
self.client_sugg.SetServer(sphinx.host, sphinx.port)
self.client_sugg.SetServer(sphinx_conf.host_name, sphinx_conf.port)
self.client_sugg.SetLimits(0, 10)
self.client_sugg.SetConnectTimeout(3.0)
self.client_show = sphinxapi.SphinxClient()
self.client_show.SetServer(sphinx.host, sphinx.port)
self.client_show.SetServer(sphinx_conf.host_name, sphinx_conf.port)
self.client_show.SetLimits(0, 10)
self.client_show.SetConnectTimeout(3.0)
def __configure(self, index_name, wlen=None):
if index_name == sphinx.index_sugg:
if index_name == sphinx_conf.index_sugg:
if wlen:
self.client_sugg.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
self.client_sugg.SetRankingMode(sphinxapi.SPH_RANK_WORDCOUNT)
@@ -52,8 +52,8 @@ class SphinxSearch:
word_len = str(len(word) / 2)
trigrammed_word = '"{}"/1'.format(trigram(word))
self.__configure(sphinx.index_sugg, word_len)
result = self.client_sugg.Query(trigrammed_word, sphinx.index_sugg)
self.__configure(sphinx_conf.index_sugg, word_len)
result = self.client_sugg.Query(trigrammed_word, sphinx_conf.index_sugg)
# Если по данному слову не найдено подсказок (а такое бывает?)
# возвращаем []
@@ -67,7 +67,7 @@ class SphinxSearch:
outlist = list()
for match in result['matches']:
if len(outlist) >= count:
break;
break
if maxrank - match['attrs']['krank'] < self.default_rating_delta:
jaro_rating = Levenshtein.jaro(word, match['attrs']['word'])
@@ -117,9 +117,9 @@ class SphinxSearch:
word_entries = self.__get_word_entries(words, strong)
sentence = "{}".format(" MAYBE ".join(x.get_variations() for x in word_entries))
self.__configure(sphinx.index_addjobj)
self.__configure(sphinx_conf.index_addjobj)
logging.info("QUERY " + sentence)
rs = self.client_show.Query(sentence, sphinx.index_addjobj)
rs = self.client_show.Query(sentence, sphinx_conf.index_addjobj)
logging.info("OK")
results = []
@@ -128,4 +128,5 @@ class SphinxSearch:
if strong:
results.sort(key=lambda x: Levenshtein.ratio(text, x['text']), reverse=True)
return results

View File

@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import re
import logging
class WordEntry: