1
1
# for the Labelbox Python SDK
2
- import inspect
3
2
import json
4
3
import logging
5
4
import os
6
- import re
7
- import sys
8
5
from datetime import datetime , timezone
9
6
from types import MappingProxyType
10
- from typing import Callable , Dict , Optional , TypedDict
7
+ from typing import Callable , Dict , Optional
11
8
12
9
import requests
13
10
import requests .exceptions
14
11
from google .api_core import retry
15
- from lbox import exceptions # type: ignore
12
+ from lbox import exceptions
13
+ from lbox .call_info import call_info_as_str , python_version_info # type: ignore
16
14
17
15
logger = logging .getLogger (__name__ )
18
16
19
17
_LABELBOX_API_KEY = "LABELBOX_API_KEY"
20
18
21
19
22
- def python_version_info ():
23
- version_info = sys .version_info
24
-
25
- return f"{ version_info .major } .{ version_info .minor } .{ version_info .micro } -{ version_info .releaselevel } "
26
-
27
-
28
- LABELBOX_CALL_PATTERN = re .compile (r"/labelbox/" )
29
- TEST_FILE_PATTERN = re .compile (r".*test.*\.py$" )
30
-
31
-
32
- class _RequestInfo (TypedDict ):
33
- prefix : str
34
- class_name : str
35
- method_name : str
36
-
37
-
38
- def call_info ():
39
- method_name = "Unknown"
40
- prefix = ""
41
- class_name = ""
42
- skip_methods = ["wrapper" , "__init__" , "execute" ]
43
- skip_classes = ["PaginatedCollection" , "_CursorPagination" , "_OffsetPagination" ]
44
-
45
- try :
46
- call_info = None
47
- for stack in reversed (inspect .stack ()):
48
- if LABELBOX_CALL_PATTERN .search (stack .filename ):
49
- call_info = stack
50
- method_name = call_info .function
51
- class_name = call_info .frame .f_locals .get (
52
- "self" , None
53
- ).__class__ .__name__
54
- print (call_info .frame .f_locals )
55
- import pdb ; pdb .set_trace ()
56
- if method_name not in skip_methods :
57
- if class_name not in skip_classes :
58
- if TEST_FILE_PATTERN .search (call_info .filename ):
59
- prefix = "test:"
60
- else :
61
- if class_name == "NoneType" :
62
- class_name = ""
63
- break
64
-
65
- except Exception :
66
- pass
67
- return _RequestInfo (prefix = prefix , class_name = class_name , method_name = method_name )
68
-
69
-
70
- def call_info_as_str ():
71
- info = call_info ()
72
- return f"{ info ['prefix' ]} { info ['class_name' ]} :{ info ['method_name' ]} "
73
-
74
-
75
20
class RequestClient :
76
21
"""A Labelbox request client.
77
22
@@ -116,6 +61,7 @@ def __init__(
116
61
self .endpoint = endpoint
117
62
self .rest_endpoint = rest_endpoint
118
63
self .sdk_version = sdk_version
64
+ self ._sdk_method = None
119
65
self ._connection : requests .Session = self ._init_connection ()
120
66
121
67
def _init_connection (self ) -> requests .Session :
@@ -128,6 +74,14 @@ def _init_connection(self) -> requests.Session:
128
74
def headers (self ) -> MappingProxyType :
129
75
return self ._connection .headers
130
76
77
+ @property
78
+ def sdk_method (self ):
79
+ return self ._sdk_method
80
+
81
+ @sdk_method .setter
82
+ def sdk_method (self , value ):
83
+ self ._sdk_method = value
84
+
131
85
def _default_headers (self ):
132
86
return {
133
87
"Authorization" : "Bearer %s" % self .api_key ,
@@ -236,7 +190,9 @@ def convert_value(value):
236
190
if files :
237
191
del headers ["Content-Type" ]
238
192
del headers ["Accept" ]
239
- headers ["X-SDK-Method" ] = call_info_as_str ()
193
+ headers ["X-SDK-Method" ] = (
194
+ self .sdk_method if self .sdk_method else call_info_as_str ()
195
+ )
240
196
241
197
request = requests .Request (
242
198
"POST" ,
0 commit comments