File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ import re
1
2
import typing as t
2
3
4
+ _module_import_regex = re .compile ("(((\\ w+)?(\\ .<\\ w+>)?(\\ .\\ w+))+)" , re .IGNORECASE )
5
+
3
6
4
7
class ImportFromStringError (Exception ):
5
8
pass
@@ -49,3 +52,21 @@ def module_import(module_str: str) -> t.Any:
49
52
raise exc from None
50
53
message = 'Could not import module "{module_str}".'
51
54
raise ImportFromStringError (message .format (module_str = module_str ))
55
+
56
+
57
+ @t .no_type_check
58
+ def get_class_import (klass : t .Union [t .Type , t .Any ]) -> str :
59
+ """
60
+ Generates String to import a class object
61
+ :param klass:
62
+ :return: string
63
+ """
64
+ if hasattr (klass , "__class__" ) and not isinstance (klass , type ):
65
+ klass = klass .__class__
66
+
67
+ regex_path = _module_import_regex .search (str (klass ))
68
+ result = regex_path .group ()
69
+ split_result = result .rsplit ("." , maxsplit = 1 )
70
+ if len (split_result ) == 2 :
71
+ return f"{ split_result [0 ]} :{ split_result [1 ]} "
72
+ return result
You can’t perform that action at this time.
0 commit comments