2021SC@SDUSC
前面对seahub的一些配置文件进行了分析,接下来我们继续展开对seahub的分析。
Seahub
__init__.py
__init__.py
的作用是让一个呈结构化分布(以文件夹形式组织)的代码文件夹变成可以被导入import
的软件包。
from .signals import repo_created, repo_deleted, clean_up_repo_trash, \
repo_restored
from .handlers import repo_created_cb, repo_deleted_cb, clean_up_repo_trash_cb, \
repo_restored_cb
repo_created.connect(repo_created_cb)
repo_deleted.connect(repo_deleted_cb)
clean_up_repo_trash.connect(clean_up_repo_trash_cb)
repo_restored.connect(repo_restored_cb)
try:
# ../conf/seahub_settings.py
from seahub_settings import repo_created_callback
repo_created.connect(repo_created_callback)
except ImportError:
pass
cconvert.py
此文件用于字符的转化。
def __init__(self)用于初始化类。self是形式参数,指的是实例的本身。
import sys, os
import re
import string
class CConvert:
def __init__(self):
self.has_shengdiao = False
self.just_shengmu = False
self.spliter = '-'
"Load data table"
try:
fp=open(os.path.join(os.path.dirname(__file__), 'convert-utf-8.txt'), encoding='utf-8')
except IOError:
print("Can't load data from convert-utf-8.txt\nPlease make sure this file exists.")
sys.exit(1)
else:
self.data=fp.read()
fp.close()
我们可以借助convert-utf-8.txt文件将Unicode转换拼音。convert-utf-8.txt文件包含了拼音转换的对应表(带声调),部分拼音转化的对应情况如下:
丂yu2,kao3
丄shang4
丅xia4
丆shi2
丏mian3
丗shi4
丟diu1
丠qiu1
両liang3
丣you3
並bing4
丩jiu1
丮ji3
丯jie4
丱kuang4,guan4
self.data=fp.read()可以读取文件,这样我们就可以使用convert-utf-8.txt文件了。
def convert1(self, strIn):
"Convert Unicode strIn to PinYin"
length, strOutKey, strOutValue, i=len(strIn), "", "", 0
while i<length:
code1 =ord(strIn[i:i+1])
if code1>=0x4e02 and code1<=0xe863:
strTemp = self.getIndex(strIn[i:i+1])
if not self.has_shengdiao:
strTemp = strTemp[:-1]
strLength = len(strTemp)
if strLength<1:strLength=1
strOutKey += string.center(strIn[i:i+1], strLength)+" "
strOutValue += self.spliter + string.center(strTemp, strLength) + self.spliter
else:#ascii code;
strOutKey+=strIn[i:i+1]+" "
strOutValue+=strIn[i:i+1] + ' '
i+=1
return [strOutValue, strOutKey]
def getIndex(self, strIn):
"Convert single Unicode to PinYin from index"
if strIn==' ':return self.spliter
if set(strIn).issubset("'\"`~!@#$%^&*()=+[]{}\\|;:,.<>/?"):return self.spliter # or return ""
if set(strIn).issubset("-—!##%%&&()*,、。:;?? @@\{{|}}~~‘’“”《》【】++==×¥·… ".decode("utf-8")):return ""
pos=re.search("^"+strIn+"([0-9a-zA-Z]+)", self.data, re.M)
if pos==None:
return strIn
else:
if not self.just_shengmu:
return pos.group(1)
else:
return pos.group(1)[:1]
def convert(self, strIn):
"Convert Unicode strIn to PinYin"
if self.spliter != '-' and self.spliter !='_' and self.spliter != '' and self.spliter != ' ':
self.spliter = '-'
pinyin_list=[]
for c in strIn :
pinyin_list.append(self.getIndex(c))
pinyin=''
for p in pinyin_list:
if p==' ':
pinyin+= self.spliter
continue
if len(p)<2:# only shengmu,just get one char,or number
pinyin += p + ' '
else:
if not self.has_shengdiao: p = p[:-1]
pinyin += self.spliter + p + self.spliter
pinyin = pinyin.replace(' ', '') \
.replace(self.spliter+self.spliter, self.spliter) \
.strip(self.spliter+' ').replace(self.spliter+self.spliter, self.spliter)
return pinyin
这三个函数可以将Unicode字符转换为拼音。其中getIndex函数可以为拼音建立索引。
constants.py
# Default user have common operations, like creating group and library.
DEFAULT_USER = 'default'
# Guest user have limited operations, can not create group and library.
GUEST_USER = 'guest'
# Repo status
REPO_STATUS_NORMAL = 'normal'
REPO_STATUS_READ_ONLY = 'read-only'
# Repo/folder permissions
PERMISSION_PREVIEW = 'preview' # preview only on the web, can not be downloaded
PERMISSION_PREVIEW_EDIT = 'cloud-edit' # preview only with edit on the web
PERMISSION_READ = 'r'
PERMISSION_READ_WRITE = 'rw'
PERMISSION_ADMIN = 'admin'
DEFAULT_ADMIN = 'default_admin'
SYSTEM_ADMIN = 'system_admin'
DAILY_ADMIN = 'daily_admin'
AUDIT_ADMIN = 'audit_admin'
DEFAULT_ORG = 'default'
HASH_URLS = {
'GROUP_MEMBERS': settings.SITE_ROOT + '#group/%(group_id)s/members/',
'GROUP_DISCUSS': settings.SITE_ROOT + '#group/%(group_id)s/discussions/',
'SYS_REPO_ADMIN': settings.SITE_ROOT + 'sysadmin/#all-libs/',
}
此文件用于定义一些常量。
以上是我对seahub的__init__.py、cconvert.py和constants.py文件的分析,之后我会继续其他部分的分析。