OSDN Git Service

error collected
[simple-tornado-bot/simple-tornado-bot.git] / linebot / utils.py
1 # -*- coding: utf-8 -*-
2
3 #  Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #  not use this file except in compliance with the License. You may obtain
5 #  a copy of the License at
6 #
7 #       https://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #  License for the specific language governing permissions and limitations
13 #  under the License.
14
15 """linebot.http_client module."""
16
17 from __future__ import unicode_literals
18
19 import logging
20 import re
21 import sys
22
23 LOGGER = logging.getLogger('linebot')
24
25 PY3 = sys.version_info[0] == 3
26
27
28 def to_snake_case(text):
29     """Convert to snake case.
30
31     :param str text:
32     :rtype: str
33     :return:
34     """
35     s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
36     return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
37
38
39 def to_camel_case(text):
40     """Convert to camel case.
41
42     :param str text:
43     :rtype: str
44     :return:
45     """
46     split = text.split('_')
47     return split[0] + "".join(x.title() for x in split[1:])
48
49
50 def safe_compare_digest(val1, val2):
51     """safe_compare_digest method.
52
53     :param val1: string or bytes for compare
54     :type val1: str | bytes
55     :param val2: string or bytes for compare
56     :type val2: str | bytes
57     """
58     if len(val1) != len(val2):
59         return False
60
61     result = 0
62     if PY3 and isinstance(val1, bytes) and isinstance(val2, bytes):
63         for i, j in zip(val1, val2):
64             result |= i ^ j
65     else:
66         for i, j in zip(val1, val2):
67             result |= (ord(i) ^ ord(j))
68
69     return result == 0