MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / parse_init

Function parse_init

diffusers/utils/check_inits.py:59–187  ·  view source on GitHub ↗

Read an init_file and parse (per backend) the _import_structure objects defined and the TYPE_CHECKING objects defined

(init_file)

Source from the content-addressed store, hash-verified

57
58
59def parse_init(init_file):
60 """
61 Read an init_file and parse (per backend) the _import_structure objects defined and the TYPE_CHECKING objects
62 defined
63 """
64 with open(init_file, "r", encoding="utf-8", newline="\n") as f:
65 lines = f.readlines()
66
67 line_index = 0
68 while line_index < len(lines) and not lines[line_index].startswith("_import_structure = {"):
69 line_index += 1
70
71 # If this is a traditional init, just return.
72 if line_index >= len(lines):
73 return None
74
75 # First grab the objects without a specific backend in _import_structure
76 objects = []
77 while not lines[line_index].startswith("if TYPE_CHECKING") and find_backend(lines[line_index]) is None:
78 line = lines[line_index]
79 # If we have everything on a single line, let's deal with it.
80 if _re_one_line_import_struct.search(line):
81 content = _re_one_line_import_struct.search(line).groups()[0]
82 imports = re.findall(r"\[([^\]]+)\]", content)
83 for imp in imports:
84 objects.extend([obj[1:-1] for obj in imp.split(", ")])
85 line_index += 1
86 continue
87 single_line_import_search = _re_import_struct_key_value.search(line)
88 if single_line_import_search is not None:
89 imports = [obj[1:-1] for obj in single_line_import_search.groups()[0].split(", ") if len(obj) > 0]
90 objects.extend(imports)
91 elif line.startswith(" " * 8 + '"'):
92 objects.append(line[9:-3])
93 line_index += 1
94
95 import_dict_objects = {"none": objects}
96 # Let's continue with backend-specific objects in _import_structure
97 while not lines[line_index].startswith("if TYPE_CHECKING"):
98 # If the line is an if not is_backend_available, we grab all objects associated.
99 backend = find_backend(lines[line_index])
100 # Check if the backend declaration is inside a try block:
101 if _re_try.search(lines[line_index - 1]) is None:
102 backend = None
103
104 if backend is not None:
105 line_index += 1
106
107 # Scroll until we hit the else block of try-except-else
108 while _re_else.search(lines[line_index]) is None:
109 line_index += 1
110
111 line_index += 1
112
113 objects = []
114 # Until we unindent, add backend objects to the list
115 while len(lines[line_index]) <= 1 or lines[line_index].startswith(" " * 4):
116 line = lines[line_index]

Callers 1

check_all_initsFunction · 0.85

Calls 1

find_backendFunction · 0.70

Tested by

no test coverage detected