2021SC@SDUSC
前面我们已经开始分析了seafes部分,现在我们继续分析。
Seafes
config.py
之前我们已经对config.py的部分代码有了一定的分析,这次我们接着继续。
def print_config(self):
logger.info('index text of office and pdf files: %s',
'yes' if self.index_office_pdf else 'no')
上述代码可以将日志信息写入配置的文件中,这里写入的信息是office和pdf文件的索引文本的相关信息。
def config_get_boolean(self, config, item, key):
try:
return config.getboolean(item, key)
except configparser.NoOptionError:
return False
except configparser.NoSectionError:
return False
def config_get_string(self, config, item, key):
try:
return config.get(item, key)
except configparser.NoOptionError:
return ''
except configparser.NoSectionError:
return ''
def config_get_int(self, config, item, key):
try:
return config.getint(item, key)
except configparser.NoOptionError:
return 0
except configparser.NoSectionError:
return 0
这三个函数可以分别获取配置文件的boolean、string和int类型的数据。
def load_seafevents_conf(self, events_conf):
defaults = {
'index_office_pdf': 'false',
'external_es_server': 'false',
'es_host': '127.0.0.1',
'es_port': '9200',
'debug': 'false',
'lang': '',
'office_file_size_limit': '10', # 10 MB
'index_workers': '2',
'content_extract_time': '5',
'highlight': 'plain'
}
cp = configparser.ConfigParser(defaults)
cp.read(events_conf)
section_name = 'INDEX FILES'
index_office_pdf = cp.getboolean(section_name, 'index_office_pdf')
external_es_server = cp.getboolean(section_name, 'external_es_server')
host = '127.0.0.1'
port = 9200
if external_es_server:
host = cp.get(section_name, 'es_host')
port = cp.getint(section_name, 'es_port')
if port == 9500:
# Seafile pro server earlier than 6.1.0 uses elasticsearch
# thrift api. In Seafile Pro 6.1.0 we upgrade ES to 2.x, which
# no longer supports thirft, thus we have to use elasticsearch
# http api.
port = 9200
lang = cp.get(section_name, 'lang').lower()
if lang:
if lang not in SUPPORTED_LANGS:
logger.warning('[seafes] invalid language ' + lang)
lang = ''
else:
logger.info('[seafes] use language ' + lang)
index_workers = cp.getint(section_name, 'index_workers')
content_extract_time = cp.getint(section_name, 'content_extract_time')
if index_workers <= 0:
logger.warning("index workers can't less than zero.")
index_workers = 2
if content_extract_time <= 0:
logger.warning("content extract time can't less than zero.")
content_extract_time = 5
self.index_office_pdf = index_office_pdf
self.host = host
self.port = port
self.office_file_size_limit = cp.getint(section_name, 'office_file_size_limit') * 1024 * 1024
self.debug = cp.getboolean(section_name, 'debug')
self.lang = lang
self.index_workers = index_workers
self.content_extract_time = content_extract_time
self.highlight = 'plain'
config_highlight = cp.get(section_name, 'highlight')
if config_highlight in ['plain', 'fvh']:
self.highlight = config_highlight
logger.info('[seafes] use highlighter ' + config_highlight)
else:
logger.warning('[seafes] invalid highlighter ' + config_highlight)
上述代码是加载events_conf的相关配置。events_conf我们之前已经介绍过,它主要负责搜索管理和文件预览管理等。
def load_conf_with_environ(self, environ_name):
events_conf = os.environ.get(environ_name, None)
if not events_conf:
raise Exception('%s not set in os.environ' % environ_name)
cp = configparser.ConfigParser()
cp.read(events_conf)
return cp
上述代码用来加载配置文件的环境变量。
def load_index_master_conf(self):
cp = self.load_conf_with_environ('INDEX_MASTER_CONFIG_FILE')
self.subscribe_mq = self.config_get_string(cp, 'DEFAULT', 'mq_type').upper()
if self.subscribe_mq != 'REDIS':
logger.critical("Unknown database backend: %s" % self.subscribe_mq)
raise RuntimeError("Unknown database backend: %s" % self.subscribe_mq)
self.subscribe_server = self.config_get_string(cp, self.subscribe_mq, 'server')
self.subscribe_port = self.config_get_string(cp, self.subscribe_mq, 'port')
self.subscribe_password = self.config_get_string(cp, self.subscribe_mq, 'password')
if not self.subscribe_server or not self.subscribe_port:
logger.critical("Server address and port can't be empty.")
raise RuntimeError("Server address and port can't be empty.")
def load_index_slave_conf(self):
cp = self.load_conf_with_environ('INDEX_SLAVE_CONFIG_FILE')
self.subscribe_mq = self.config_get_string(cp, 'DEFAULT', 'mq_type').upper()
if self.subscribe_mq != 'REDIS':
logger.critical("Unknown database backend: %s" % self.subscribe_mq)
raise RuntimeError("Unknown database backend: %s" % self.subscribe_mq)
index_slave_workers = self.config_get_int(cp, 'DEFAULT', 'index_workers')
if index_slave_workers <= 0:
logger.warning("index workers can't less than zero.")
index_slave_workers = 2
self.index_slave_workers = index_slave_workers
self.subscribe_server = self.config_get_string(cp, self.subscribe_mq, 'server')
self.subscribe_port = self.config_get_string(cp, self.subscribe_mq, 'port')
self.subscribe_password = self.config_get_string(cp, self.subscribe_mq, 'password')
if not self.subscribe_server or not self.subscribe_port:
logger.critical("Server address and port can't be empty.")
raise RuntimeError("Server address and port can't be empty.")
上述两个函数可以用来分别加载主索引的配置和辅助索引的配置。
至此Seafes的主要基础配置文件config.py基本分析完毕,后续我们会继续对其他部分进行分析。