Skip to content

Commit f8fcbcc

Browse files
committed
Remove 'get_lan_ip' and add common misc_utils.py
Signed-off-by: Jin Hai <haijin.chn@gmail.com>
1 parent 55eb525 commit f8fcbcc

File tree

3 files changed

+122
-34
lines changed

3 files changed

+122
-34
lines changed

api/utils/__init__.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,13 @@
1515
#
1616
import base64
1717
import hashlib
18-
import os
19-
import socket
2018
import uuid
2119
import requests
2220

2321
import importlib
2422

2523
from .common import string_to_bytes
2624

27-
def get_lan_ip():
28-
if os.name != "nt":
29-
import fcntl
30-
import struct
31-
32-
def get_interface_ip(ifname):
33-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
34-
return socket.inet_ntoa(
35-
fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', string_to_bytes(ifname[:15])))[20:24])
36-
37-
ip = socket.gethostbyname(socket.getfqdn())
38-
if ip.startswith("127.") and os.name != "nt":
39-
interfaces = [
40-
"bond1",
41-
"eth0",
42-
"eth1",
43-
"eth2",
44-
"wlan0",
45-
"wlan1",
46-
"wifi0",
47-
"ath0",
48-
"ath1",
49-
"ppp0",
50-
]
51-
for ifname in interfaces:
52-
try:
53-
ip = get_interface_ip(ifname)
54-
break
55-
except IOError:
56-
pass
57-
return ip or ''
58-
5925

6026
def from_dict_hook(in_dict: dict):
6127
if "type" in in_dict and "data" in in_dict:

common/misc_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import base64
18+
import hashlib
19+
import uuid
20+
import requests
21+
22+
def get_uuid():
23+
return uuid.uuid1().hex
24+
25+
26+
def download_img(url):
27+
if not url:
28+
return ""
29+
response = requests.get(url)
30+
return "data:" + \
31+
response.headers.get('Content-Type', 'image/jpg') + ";" + \
32+
"base64," + base64.b64encode(response.content).decode("utf-8")
33+
34+
35+
def hash_str2int(line: str, mod: int = 10 ** 8) -> int:
36+
return int(hashlib.sha1(line.encode("utf-8")).hexdigest(), 16) % mod
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#
2+
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
import uuid
17+
from common.misc_utils import get_uuid
18+
19+
class TestGetUuid:
20+
"""Test cases for get_uuid function"""
21+
22+
def test_returns_string(self):
23+
"""Test that function returns a string"""
24+
result = get_uuid()
25+
assert isinstance(result, str)
26+
27+
def test_hex_format(self):
28+
"""Test that returned string is in hex format"""
29+
result = get_uuid()
30+
# UUID v1 hex should be 32 characters (without dashes)
31+
assert len(result) == 32
32+
# Should only contain hexadecimal characters
33+
assert all(c in '0123456789abcdef' for c in result)
34+
35+
def test_no_dashes_in_result(self):
36+
"""Test that result contains no dashes"""
37+
result = get_uuid()
38+
assert '-' not in result
39+
40+
def test_unique_results(self):
41+
"""Test that multiple calls return different UUIDs"""
42+
results = [get_uuid() for _ in range(10)]
43+
44+
# All results should be unique
45+
assert len(results) == len(set(results))
46+
47+
# All should be valid hex strings of correct length
48+
for result in results:
49+
assert len(result) == 32
50+
assert all(c in '0123456789abcdef' for c in result)
51+
52+
def test_valid_uuid_structure(self):
53+
"""Test that the hex string can be converted back to UUID"""
54+
result = get_uuid()
55+
56+
# Should be able to create UUID from the hex string
57+
reconstructed_uuid = uuid.UUID(hex=result)
58+
assert isinstance(reconstructed_uuid, uuid.UUID)
59+
60+
# The hex representation should match the original
61+
assert reconstructed_uuid.hex == result
62+
63+
def test_uuid1_specific_characteristics(self):
64+
"""Test that UUID v1 characteristics are present"""
65+
result = get_uuid()
66+
uuid_obj = uuid.UUID(hex=result)
67+
68+
# UUID v1 should have version 1
69+
assert uuid_obj.version == 1
70+
71+
# Variant should be RFC 4122
72+
assert uuid_obj.variant == 'specified in RFC 4122'
73+
74+
def test_result_length_consistency(self):
75+
"""Test that all generated UUIDs have consistent length"""
76+
for _ in range(100):
77+
result = get_uuid()
78+
assert len(result) == 32
79+
80+
def test_hex_characters_only(self):
81+
"""Test that only valid hex characters are used"""
82+
for _ in range(100):
83+
result = get_uuid()
84+
# Should only contain lowercase hex characters (UUID hex is lowercase)
85+
assert result.islower()
86+
assert all(c in '0123456789abcdef' for c in result)

0 commit comments

Comments
 (0)