Skip to content

Commit bbbfb0a

Browse files
rdbendeRedFantom
authored andcommitted
Simplify Tcl to Python dict conversion (PR #3)
1 parent 95bdb71 commit bbbfb0a

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

tkextrafont/__init__.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,6 @@
2121
_FILE_DIR = os.path.abspath(__file__)
2222

2323

24-
def _tk_dict_to_dict(d):
25-
"""Build a dictionary from a Tcl dictionary by parsing its str repr"""
26-
# type: (tk._tkinter.Tcl_Obj) -> Dict[str, str]
27-
string = str(d) # Every Tcl_Obj supports conversion to string
28-
elements = string.split(" ")
29-
results, key, brackets, total = dict(), None, False, str()
30-
for e in elements:
31-
if key is None: # New dictionary key
32-
key = e
33-
elif brackets is True: # Brackets were opened
34-
if e.endswith("}"): # Closing brackets
35-
brackets = False
36-
results[key] = total + " " + e[:-1]
37-
key, total = None, str()
38-
else: # Still within brackets
39-
total += " " + e
40-
elif e.startswith("{"): # Open Brackets
41-
brackets = True
42-
total = e[1:]
43-
else: # No brackets, just a simple value
44-
results[key] = e
45-
key = None
46-
return results
47-
48-
4924
@contextmanager
5025
def chdir(target):
5126
# type: (str) -> None
@@ -102,7 +77,14 @@ def loaded_fonts(self) -> List[str]:
10277

10378
def font_info(self, fname: str) -> List[Dict[str, str]]:
10479
"""Return info of a font file"""
105-
return list(map(_tk_dict_to_dict, self._tk.call("extrafont::nameinfo", fname)))
80+
tk_result_list = self._tk.splitlist(
81+
self._tk.call("extrafont::nameinfo", fname)[0]
82+
)
83+
font_info_dict = {}
84+
85+
for key, value in zip(tk_result_list[0::2], tk_result_list[1::2]):
86+
font_info_dict[key] = str(value)
87+
return font_info_dict
10688

10789
def is_font_available(self, font_name) -> bool:
10890
"""Return a boolean whether a font is available"""

0 commit comments

Comments
 (0)