Skip to content

Commit

Permalink
translation on code generated. refs alejandroautalan/pygubu-designer#120
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroautalan committed May 14, 2022
1 parent ab8db40 commit dd50595
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions pygubu/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,6 @@ def code_classname_for(self, bobject):

def code_create_callback(self, widgetid, cbname, cbtype, args=None):
raise NotImplementedError()

def code_translate_str(self, value: str) -> str:
raise NotImplementedError()
21 changes: 12 additions & 9 deletions pygubu/builder/builderobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class CB_TYPES:
SCALE = 'scale'
BIND_EVENT = 'bind_event'


TRANSLATABLE_PROPERTIES = ['label', 'text', 'title']

#
# Base class
#
Expand Down Expand Up @@ -342,11 +345,8 @@ def connect_bindings(self, cb_bag):
# Code generation methods
#
@staticmethod
def code_escape_str(value):
rval = repr(value)[1:-1]
rval.replace('"', '\\"')
rval.replace("'", "\\'")
return rval
def code_escape_str(value: str) -> str:
return repr(value)

def _code_get_init_args(self, code_identifier):
"""Creates dict with properties marked as readonly"""
Expand Down Expand Up @@ -496,7 +496,7 @@ def _code_process_properties(self, properties, targetid):
return (code_bag, kwproperties, complex_properties)

def _code_process_property_value(self, targetid, pname, value):
propvalue = "'{}'".format(value)
propvalue = None
if pname in self.tkvar_properties:
propvalue = self._code_set_tkvariable_property(pname, value)
elif pname in self.command_properties:
Expand All @@ -506,8 +506,11 @@ def _code_process_property_value(self, targetid, pname, value):
propvalue = self.builder.code_create_image(value)
elif pname == 'takefocus':
propvalue = str(tk.getboolean(value))
elif pname == 'text':
propvalue = self.code_escape_str(propvalue)
elif pname in TRANSLATABLE_PROPERTIES:
propvalue = self.builder.code_translate_str(value)
# default processing
if propvalue is None:
propvalue = f"'{value}'"
return propvalue

def _code_set_property(self, targetid, pname, value, code_bag):
Expand All @@ -518,7 +521,7 @@ def _code_set_tkvariable_property(self, pname, value):
Can be used from subclases for custom tk variable properties.'''
varvalue = None
if 'text' in self.wmeta.properties and pname == 'textvariable':
varvalue = self.code_escape_str(self.wmeta.properties['text'])
varvalue = self.builder.code_translate_str(self.wmeta.properties['text'])
elif 'value' in self.wmeta.properties and pname == 'variable':
varvalue = self.code_escape_str(self.wmeta.properties['value'])
propvalue = self.builder.code_create_variable(value, varvalue)
Expand Down
26 changes: 13 additions & 13 deletions pygubu/builder/tkstdwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,23 +515,23 @@ def _code_set_property(self, targetid, pname, value, code_bag):
state_value = ''
if 'state' in self.wmeta.properties:
state_value = self.wmeta.properties['state']
lines = []
line = "_text_ = '''{0}'''".format(value)
lines.append(line)
sval = self.builder.code_translate_str(value)
lines = [
f"_text_ = {sval}",
]
if state_value == tk.DISABLED:
line = "{0}.configure(state='normal')".format(targetid)
lines.append(line)
line = "{0}.insert('0.0', _text_)".format(targetid)
lines.append(line)
line = "{0}.configure(state='disabled')".format(targetid)
lines.append(line)
lines.extend(
(
f"{targetid}.configure(state='normal')",
f"{targetid}.insert('0.0', _text_)",
f"{targetid}.configure(state='disabled')",
)
)
else:
line = "{0}.insert('0.0', _text_)".format(targetid)
lines.append(line)
lines.append(f"{targetid}.insert('0.0', _text_)")
code_bag[pname] = lines
else:
super(TKText, self)._code_set_property(targetid, pname,
value, code_bag)
super(TKText, self)._code_set_property(targetid, pname, value, code_bag)


register_widget('tk.Text', TKText, 'Text', ('Control & Display', 'tk', 'ttk'))
Expand Down
4 changes: 2 additions & 2 deletions pygubu/builder/uidefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import xml.etree.ElementTree as ET

from pygubu.builder.builderobject import CLASS_MAP
from pygubu.builder.builderobject import CLASS_MAP, TRANSLATABLE_PROPERTIES
from pygubu.builder.widgetmeta import BindingMeta, GridRCLine, WidgetMeta


Expand Down Expand Up @@ -290,7 +290,7 @@ def _prop_to_xml(self, pname, pvalue):
pnode = ET.Element('property')
pnode.set('name', pname)
pnode.text = pvalue
if pname in self.TRANSLATABLE_PROPERTIES:
if pname in TRANSLATABLE_PROPERTIES:
pnode.set('translatable', 'yes')
# if pvalue is a json do special
try:
Expand Down

0 comments on commit dd50595

Please sign in to comment.