Skip to content

Commit

Permalink
feature(): update docs to fix sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastwu5788 committed May 9, 2020
1 parent 0cb225b commit 6f28800
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 52 deletions.
7 changes: 4 additions & 3 deletions pre_request/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@


class PreRequestException(Exception):
""" Pre-request 异常基类
""" PreRequest base exception
"""


class ParamsValueError(ValueError):
"""自定义异常"""
""" Invalid input params value exception
"""

def __init__(self, code, **context):
super().__init__()
self.code = code
self.context = context

def form_message(self, fuzzy=False): # noqa: disable
""" 格式化JSON格式的错误消息
""" format error message
"""
message = "参数验证失败,请检查您的输入!"

Expand Down
12 changes: 6 additions & 6 deletions pre_request/regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
import re


# 邮箱正则表达式
# email regexp
K_EMAIL_REG = r'^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$'


# 手机号正则表达式
# mobile regexp
K_MOBILE_REG = r'^0\d{2,3}\d{7,8}$|^1[3578]\d{9}$|^14[579]\d{8}$'


# 文件路径正则表达式
# file path regexp
K_FILE_REG = r'^(?<1>.*[\\/])(?<2>.+)\.(?<3>.+)?$|^(?<1>.*[\\/])(?<2>.+)$|^(?<2>.+)\.(?<3>.+)?$|^(?<2>.+)$'


# MAC地址正则表达式
# mac address regexp
K_MAC_REG = r'^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$'


# 地理维度正则表达式
# latitude regexp
K_LATITUDE_REG = r'^[\-\+]?((0|([1-8]\d?))(\.\d{1,10})?|90(\.0{1,10})?)$'
# 地址经度正则表达式
# longitude regexp
K_LONGITUDE_REG = r'^[\-\+]?(0(\.\d{1,10})?|([1-9](\d)?)(\.\d{1,10})?|1[0-7]\d{1}(\.\d{1,10})?|180\.0{1,10})$'


Expand Down
35 changes: 21 additions & 14 deletions pre_request/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class PreRequest:
"""

def __init__(self, fuzzy=False, store_key=None, content_type=None):
""" PreRequest init function
:param fuzzy: formatter error message with fuzzy style
:param store_key: which key will store formatter result
:param content_type: response content type json/html
"""
self.filters = simple_filters
self.complex_filters = complex_filters
self.fuzzy = fuzzy
Expand Down Expand Up @@ -73,10 +79,10 @@ def add_filter(self, cus_filter, index=None):
self.filters.append(cus_filter)

def remove_filter(self, cus_filter=None, index=None):
""" 移除指定过滤器
""" Remove filters from object with index or filter name
:param cus_filter: 过滤器名称
:param index: 过滤器位置
:param cus_filter: user filter name
:param index: filter index
"""
if cus_filter and (isinstance(cus_filter, str) or issubclass(cus_filter, BaseFilter)):
self.filters.remove(cus_filter)
Expand All @@ -86,12 +92,12 @@ def remove_filter(self, cus_filter=None, index=None):

@staticmethod
def _location_params(key, location, default=None, deep=True):
""" 读取指定位置的参数
""" Read params form special location ex: args/forms/header/cookies
:param key: 数据的key
:param location: 读取数据的位置
:param default: 未读取到时的默认值
:param deep: 执行深度递归查询
:param key: params key
:param location: special location
:param default: default value if special value is not exists
:param deep: read params with deep search
"""
from flask import request # pylint: disable=import-outside-toplevel

Expand Down Expand Up @@ -141,10 +147,10 @@ def _fmt_params(self, key, rule, default=None):
return default

def _handler_simple_filter(self, k, r):
""" 处理具体规则
""" Handler filter rules with simple ways
:param k: 参数key
:param r: 参数规则
:param k: params key
:param r: params rule
"""
if isinstance(r, dict):
fmt_result = dict()
Expand Down Expand Up @@ -185,10 +191,11 @@ def _handler_simple_filter(self, k, r):
return value

def _handler_complex_filter(self, k, r, rst):
""" 处理复合过滤器
""" Handler complex rule filters
:param r: 参数规则
:param rst: 所有合规参数
:param k: params key
:param r: params rule
:param rst: handler result
"""
if isinstance(r, dict):
for key, value in r.items():
Expand Down
24 changes: 12 additions & 12 deletions pre_request/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@


class BaseResponse:
"""
错误响应基类
""" PreRequest basic response class
"""

def __call__(self, fuzzy=False, error=None):
"""
:param error: 错误
""" Use __call__ function to generate response
:param error: error
:type error: ParamsValueError
"""
if error:
Expand All @@ -19,13 +19,13 @@ def __call__(self, fuzzy=False, error=None):


class JSONResponse(BaseResponse):
""" 以JSON格式响应出错的情况
""" Handler response with json format
"""

def __call__(self, fuzzy=False, formatter=None, error=None):
"""
:type error: 错误
:return:
""" Use __call__ function to generate response
:type error: special error
"""
result = super(JSONResponse, self).__call__(fuzzy, error)

Expand All @@ -40,13 +40,13 @@ def __call__(self, fuzzy=False, formatter=None, error=None):


class HTMLResponse(BaseResponse):
""" 以HTML格式响应出错的情况
""" Handler response with html format
"""

def __call__(self, fuzzy=False, formatter=None, error=None):
"""
:type error: 错误
:return:
""" Use __call__ function to generate response
:type error: special error
"""
result = super(HTMLResponse, self).__call__(fuzzy, error)

Expand Down
11 changes: 1 addition & 10 deletions pre_request/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@


class Rule: # pylint: disable=too-many-instance-attributes
"""
字段遵守的规则定义类
""" This class is designed to describe special rule that params must follow
"""

def __init__(self, **kwargs):
Expand Down Expand Up @@ -99,8 +98,6 @@ def __init__(self, **kwargs):

@property
def gt(self):
""" 将`gt`属性变更为动态属性
"""
return self._gt

@gt.setter
Expand All @@ -122,8 +119,6 @@ def gt(self, value):

@property
def gte(self):
""" 将`gte`属性变更为动态属性
"""
return self._gte

@gte.setter
Expand All @@ -145,8 +140,6 @@ def gte(self, value):

@property
def lt(self):
""" 将`lt`属性变更为动态属性
"""
return self._lt

@lt.setter
Expand All @@ -168,8 +161,6 @@ def lt(self, value):

@property
def lte(self):
""" 将`lte`属性变更为动态属性
"""
return self._lte

@lte.setter
Expand Down
14 changes: 7 additions & 7 deletions pre_request/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@


def get_deep_value(key, params, default=None, deep=True):
""" 获取多层嵌套的参数值
""" Extract value from complex structure with deep search
:param key: 多层次的key
:param params: 读取的数据源
:param default: 默认值
:param deep: 是否进行深度递归查询
:param key: special key, use '.' to separate different depth
:param params: origin data
:param default: default value if special key is not exists
:param deep: extract deep value
"""
if key is None:
return default

# 不进行深度递归查询,直接返回最后一层结果
# extract value from first depth
if not deep:
return params.get(key.split(".")[-1], default)

# 无效的入参
# invalid input params type
if not params or not isinstance(params, dict):
raise ValueError("Can't read deep value from path: '%s'" % key)

Expand Down

0 comments on commit 6f28800

Please sign in to comment.