Skip to content

Commit

Permalink
Simplify Tcl to Python dict conversion (PR #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende authored and RedFantom committed Mar 16, 2022
1 parent 95bdb71 commit bbbfb0a
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions tkextrafont/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,6 @@
_FILE_DIR = os.path.abspath(__file__)


def _tk_dict_to_dict(d):
"""Build a dictionary from a Tcl dictionary by parsing its str repr"""
# type: (tk._tkinter.Tcl_Obj) -> Dict[str, str]
string = str(d) # Every Tcl_Obj supports conversion to string
elements = string.split(" ")
results, key, brackets, total = dict(), None, False, str()
for e in elements:
if key is None: # New dictionary key
key = e
elif brackets is True: # Brackets were opened
if e.endswith("}"): # Closing brackets
brackets = False
results[key] = total + " " + e[:-1]
key, total = None, str()
else: # Still within brackets
total += " " + e
elif e.startswith("{"): # Open Brackets
brackets = True
total = e[1:]
else: # No brackets, just a simple value
results[key] = e
key = None
return results


@contextmanager
def chdir(target):
# type: (str) -> None
Expand Down Expand Up @@ -102,7 +77,14 @@ def loaded_fonts(self) -> List[str]:

def font_info(self, fname: str) -> List[Dict[str, str]]:
"""Return info of a font file"""
return list(map(_tk_dict_to_dict, self._tk.call("extrafont::nameinfo", fname)))
tk_result_list = self._tk.splitlist(
self._tk.call("extrafont::nameinfo", fname)[0]
)
font_info_dict = {}

for key, value in zip(tk_result_list[0::2], tk_result_list[1::2]):
font_info_dict[key] = str(value)
return font_info_dict

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

0 comments on commit bbbfb0a

Please sign in to comment.