diff --git a/fishbase/fish_http_status.py b/fishbase/fish_http_status.py new file mode 100644 index 0000000..1a4b069 --- /dev/null +++ b/fishbase/fish_http_status.py @@ -0,0 +1,163 @@ +""" + +``fish_http_status`` 包含最通用的一些网络状态码 + +https://github.com/openstack/swift/blob/master/swift/common/http.py + +""" + + +def is_informational(status): + """ + 检查状态码是否信息提示 + + :param: + * status: http 状态码 + :return: + * result: True or False + """ + return 100 <= status <= 199 + + +def is_success(status): + """ + 检查状态码是否成功 + + :param: + * status: http 状态码 + :return: + * result: True or False + """ + return 200 <= status <= 299 + + +def is_redirection(status): + """ + 检查状态码是否重定向 + + :param: + * status: http 状态码 + :return: + * result: True or False + """ + return 300 <= status <= 399 + + +def is_client_error(status): + """ + 检查状态码是否客户端错误 + + :param: + * status: http 状态码 + :return: + * result: True or False + """ + return 400 <= status <= 499 + + +def is_server_error(status): + """ + 检查状态码是否服务端错误 + + :param: + * status: http 状态码 + :return: + * result: True or False + """ + return 500 <= status <= 599 + + +# List of HTTP status codes + +############################################################################### +# 1xx Informational +############################################################################### + + +HTTP_CONTINUE = 100 +HTTP_SWITCHING_PROTOCOLS = 101 +HTTP_PROCESSING = 102 # WebDAV +HTTP_CHECKPOINT = 103 +HTTP_REQUEST_URI_TOO_LONG = 122 + +############################################################################### +# 2xx Success +############################################################################### + +HTTP_OK = 200 +HTTP_CREATED = 201 +HTTP_ACCEPTED = 202 +HTTP_NON_AUTHORITATIVE_INFORMATION = 203 +HTTP_NO_CONTENT = 204 +HTTP_RESET_CONTENT = 205 +HTTP_PARTIAL_CONTENT = 206 +HTTP_MULTI_STATUS = 207 # WebDAV +HTTP_IM_USED = 226 + +############################################################################### +# 3xx Redirection +############################################################################### + +HTTP_MULTIPLE_CHOICES = 300 +HTTP_MOVED_PERMANENTLY = 301 +HTTP_FOUND = 302 +HTTP_SEE_OTHER = 303 +HTTP_NOT_MODIFIED = 304 +HTTP_USE_PROXY = 305 +HTTP_SWITCH_PROXY = 306 +HTTP_TEMPORARY_REDIRECT = 307 +HTTP_RESUME_INCOMPLETE = 308 + +############################################################################### +# 4xx Client Error +############################################################################### + +HTTP_BAD_REQUEST = 400 +HTTP_UNAUTHORIZED = 401 +HTTP_PAYMENT_REQUIRED = 402 +HTTP_FORBIDDEN = 403 +HTTP_NOT_FOUND = 404 +HTTP_METHOD_NOT_ALLOWED = 405 +HTTP_NOT_ACCEPTABLE = 406 +HTTP_PROXY_AUTHENTICATION_REQUIRED = 407 +HTTP_REQUEST_TIMEOUT = 408 +HTTP_CONFLICT = 409 +HTTP_GONE = 410 +HTTP_LENGTH_REQUIRED = 411 +HTTP_PRECONDITION_FAILED = 412 +HTTP_REQUEST_ENTITY_TOO_LARGE = 413 +HTTP_REQUEST_URI_TOO_LONG = 414 +HTTP_UNSUPPORTED_MEDIA_TYPE = 415 +HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416 +HTTP_EXPECTATION_FAILED = 417 +HTTP_IM_A_TEAPOT = 418 +HTTP_UNPROCESSABLE_ENTITY = 422 # WebDAV +HTTP_LOCKED = 423 # WebDAV +HTTP_FAILED_DEPENDENCY = 424 # WebDAV +HTTP_UNORDERED_COLLECTION = 425 +HTTP_UPGRADE_REQUIED = 426 +HTTP_PRECONDITION_REQUIRED = 428 +HTTP_TOO_MANY_REQUESTS = 429 +HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431 +HTTP_NO_RESPONSE = 444 +HTTP_RETRY_WITH = 449 +HTTP_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = 450 +HTTP_CLIENT_CLOSED_REQUEST = 499 + +############################################################################### +# 5xx Server Error +############################################################################### + +HTTP_INTERNAL_SERVER_ERROR = 500 +HTTP_NOT_IMPLEMENTED = 501 +HTTP_BAD_GATEWAY = 502 +HTTP_SERVICE_UNAVAILABLE = 503 +HTTP_GATEWAY_TIMEOUT = 504 +HTTP_VERSION_NOT_SUPPORTED = 505 +HTTP_VARIANT_ALSO_NEGOTIATES = 506 +HTTP_INSUFFICIENT_STORAGE = 507 # WebDAV +HTTP_BANDWIDTH_LIMIT_EXCEEDED = 509 +HTTP_NOT_EXTENDED = 510 +HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511 +HTTP_NETWORK_READ_TIMEOUT_ERROR = 598 # not used in RFC +HTTP_NETWORK_CONNECT_TIMEOUT_ERROR = 599 # not used in RFC diff --git a/fishbase/fish_project.py b/fishbase/fish_project.py index 8d4c54b..2117286 100644 --- a/fishbase/fish_project.py +++ b/fishbase/fish_project.py @@ -10,6 +10,10 @@ import yaml import os +try: + from yaml import CLoader as Loader, CDumper as Dumper +except ImportError: + from yaml import Loader, Dumper package_yml = """ project: hellopackage @@ -102,7 +106,9 @@ def init_project_by_yml(project_config=None, dist=None): if os.path.isfile(project_config): project_config = open(project_config) try: - yml_data = yaml.load(project_config) + yml_data = yaml.load(project_config, Loader=Loader) + if not yml_data or type(yml_data) != dict: + raise RuntimeError('fail to load') project_name = yml_data['project'] project_tree = yml_data['tree'] except Exception as e: diff --git a/test/test_http_status.py b/test/test_http_status.py new file mode 100644 index 0000000..bbeb75c --- /dev/null +++ b/test/test_http_status.py @@ -0,0 +1,30 @@ +from fishbase.fish_http_status import * + + +class TestHttpStatus(object): + + def setup_method(self): + self.BAD_HTTP_STATUS = -1 + + def teardown_method(self): + self.BAD_HTTP_STATUS = None + + def test_status_informational(self): + assert is_informational(HTTP_CONTINUE) is True + assert is_informational(self.BAD_HTTP_STATUS) is False + + def test_status_success(self): + assert is_success(HTTP_OK) is True + assert is_success(self.BAD_HTTP_STATUS) is False + + def test_status_redirection(self): + assert is_redirection(HTTP_MULTIPLE_CHOICES) is True + assert is_redirection(self.BAD_HTTP_STATUS) is False + + def test_status_client_error(self): + assert is_client_error(HTTP_BAD_REQUEST) is True + assert is_client_error(self.BAD_HTTP_STATUS) is False + + def test_status_server_error(self): + assert is_server_error(HTTP_INTERNAL_SERVER_ERROR) is True + assert is_server_error(self.BAD_HTTP_STATUS) is False diff --git a/test/test_project.py b/test/test_project.py index ea52fe3..7410ba9 100644 --- a/test/test_project.py +++ b/test/test_project.py @@ -1,12 +1,30 @@ # coding=utf-8 import os import shutil + +import pytest + from fishbase.fish_project import init_project_by_yml # 2018.6.27 v1.0.14 #73 create by Jia ChunYing class TestProject(object): + def test_load_bad_01(self): + """ + empty file + """ + base_dir = os.path.dirname(os.path.abspath(__file__)) + target_file = base_dir + os.sep + 'test_project_with_empty_file.yaml' + with open(target_file, 'wb') as f: + f.close() + with pytest.raises(KeyError) as e: + init_project_by_yml(target_file, '.') + exec_msg = e.value.args[0] + assert exec_msg == 'project config format Error: fail to load' + os.remove(target_file) + + def test_init_project_by_yml(self): # define yml string package_yml = '''