def get_widget_text_pos_from_kv(self, widget, parent=None, path_to_widget=None): '''To get start and end pos of widget's rule in kv text :param path_to_widget: array with widget path :param parent: parent of widget :param widget: widget to find the kv text ''' if not path_to_widget: path_to_widget = self.get_widget_path(widget) path_to_widget.reverse() # Go to widget's rule's line and determines all its rule's # and it's child if any. Then delete them text = re.sub(r'#.+', '', self.text) lines = text.splitlines() total_lines = len(lines) root_lineno = 0 root_name = self.playground.root_name for lineno, line in enumerate(lines): pos = line.find(root_name) if pos != -1 and get_indentation(line) == 0: root_lineno = lineno break widget_lineno = self._find_widget_place(path_to_widget, lines, total_lines, root_lineno + 1) widget_line = lines[widget_lineno] indent = len(widget_line) - len(widget_line.lstrip()) lineno = widget_lineno _indent = indent + 1 line = widget_line while line.strip() == '' or _indent > indent: lineno += 1 if lineno >= total_lines: break line = lines[lineno] _indent = len(line) - len(line.lstrip()) delete_until_line = lineno - 1 line = lines[delete_until_line] while line.strip() == '': delete_until_line -= 1 line = lines[delete_until_line] widget_line_pos = get_line_start_pos(self.text, widget_lineno) delete_until_line_pos = -1 if delete_until_line == total_lines - 1: delete_until_line_pos = len(self.text) else: delete_until_line_pos = get_line_end_pos(self.text, delete_until_line) self._reload = False return widget_line_pos, delete_until_line_pos
def set_property_value(self, widget, prop, value, proptype): '''To find and change the value of property of widget rule in text ''' # Do not add property if value is empty and # property is not a string property self._reload = False if not isinstance(widget.properties()[prop], StringProperty) and\ value == '': return path_to_widget = self.get_widget_path(widget) path_to_widget.reverse() # Go to the line where widget is declared lines = re.sub(r'#.+', '', self.text.rstrip()).splitlines() total_lines = len(lines) root_name = self.playground.root_name total_lines = len(lines) root_lineno = 0 for lineno, line in enumerate(lines): pos = line.find(root_name) if pos != -1 and get_indentation(line) == 0: root_lineno = lineno break widget_lineno = self._find_widget_place(path_to_widget, lines, total_lines, root_lineno + 1) widget_line = lines[widget_lineno] if not widget_line.strip(): return indent = get_indentation(widget_line) prop_found = False if ':' not in widget_line: # If cannot find ':' then insert it self.cursor = (len(lines[widget_lineno]), widget_lineno) lines[widget_lineno] += ':' self.insert_text(':') else: # Else find if property has already been declared with a value lineno = widget_lineno + 1 # But if widget line is the last line in the text if lineno < total_lines: line = lines[lineno] _indent = get_indentation(line) colon_pos = -1 while lineno < total_lines and (line.strip() == '' or _indent > indent): line = lines[lineno] _indent = get_indentation(line) if line.strip() != '': colon_pos = line.find(':') if colon_pos == -1: break if colon_pos == len(line.rstrip()) - 1: break if prop == line[:colon_pos].strip(): prop_found = True break lineno += 1 if prop_found: # if property found then change its value _pos_prop_value = get_line_start_pos(self.text, lineno) + \ colon_pos + 2 if lineno == total_lines - 1: _line_end_pos = len(self.text) else: _line_end_pos = get_line_end_pos(self.text, lineno) if proptype == 'StringProperty' or \ (proptype == 'OptionProperty' and not isinstance(value, list)): value = "'{}'".format(value.replace("'", "\\'")) self.text = self.text[:_pos_prop_value] + ' ' + str(value) + \ self.text[_line_end_pos:] self.cursor = (0, lineno) else: # if not found then add property after the widgets line _line_start_pos = get_line_start_pos(self.text, widget_lineno) _line_end_pos = get_line_end_pos(self.text, widget_lineno) if proptype == 'StringProperty' or \ (proptype == 'OptionProperty' and not isinstance(value, list)): value = "'{}'".format(value.replace("'", "\\'")) indent_str = '\n' for i in range(indent + 4): indent_str += ' ' self.cursor = (len(lines[widget_lineno]), widget_lineno) self.insert_text(indent_str + prop + ': ' + str(value))
def set_event_handler(self, widget, prop, value): self._reload = False path_to_widget = self.get_widget_path(widget) path_to_widget.reverse() # Go to the line where widget is declared lines = re.sub(r'#.+', '', self.text).splitlines() total_lines = len(lines) root_name = self.playground.root_name total_lines = len(lines) root_lineno = 0 for lineno, line in enumerate(lines): pos = line.find(root_name) if pos != -1 and get_indentation(line) == 0: root_lineno = lineno break widget_lineno = self._find_widget_place(path_to_widget, lines, total_lines, root_lineno + 1) widget_line = lines[widget_lineno] indent = get_indentation(widget_line) prop_found = False if not widget_line.strip(): return if ':' not in widget_line: # If cannot find ':' then insert it self.cursor = (len(lines[widget_lineno]), widget_lineno) lines[widget_lineno] += ':' self.insert_text(':') else: # Else find if property has already been declared with a value lineno = widget_lineno + 1 # But if widget line is the last line in the text if lineno < total_lines: line = lines[lineno] _indent = get_indentation(line) colon_pos = -1 while lineno < total_lines and (line.strip() == '' or _indent > indent): line = lines[lineno] _indent = get_indentation(line) if line.strip() != '': colon_pos = line.find(':') if colon_pos == -1: break if colon_pos == len(line.rstrip()) - 1: break if prop == line[:colon_pos].strip(): prop_found = True break lineno += 1 if prop_found: if lineno == total_lines - 1: _line_end_pos = len(self.text) else: _line_end_pos = get_line_end_pos(self.text, lineno) if value != '': # if property found then change its value _pos_prop_value = get_line_start_pos(self.text, lineno) + \ colon_pos + 2 self.text = self.text[:_pos_prop_value] + ' ' + value + \ self.text[_line_end_pos:] self.cursor = (0, lineno) else: _line_start_pos = get_line_start_pos(self.text, widget_lineno) self.text = \ self.text[:get_line_start_pos(self.text, lineno)] + \ self.text[_line_end_pos:] elif value != '': # if not found then add property after the widgets line _line_end_pos = get_line_end_pos(self.text, widget_lineno) indent_str = '\n' for i in range(indent + 4): indent_str += ' ' self.cursor = (len(lines[widget_lineno]), widget_lineno) self.insert_text(indent_str + prop + ': ' + str(value))
def get_property_value(self, widget, prop): self._reload = False if prop[:3] != 'on_' and \ not isinstance(widget.properties()[prop], StringProperty) and\ value == '': return path_to_widget = self.get_widget_path(widget) path_to_widget.reverse() # Go to the line where widget is declared lines = re.sub(r'#.+', '', self.text).splitlines() total_lines = len(lines) root_name = self.playground.root_name total_lines = len(lines) root_lineno = 0 for lineno, line in enumerate(lines): pos = line.find(root_name) if pos != -1 and get_indentation(line) == 0: root_lineno = lineno break widget_lineno = self._find_widget_place(path_to_widget, lines, total_lines, root_lineno + 1) widget_line = lines[widget_lineno] indent = get_indentation(widget_line) prop_found = False # Else find if property has already been declared with a value lineno = widget_lineno + 1 # But if widget line is the last line in the text if lineno < total_lines: line = lines[lineno] _indent = get_indentation(line) colon_pos = -1 while lineno < total_lines and (line.strip() == '' or _indent > indent): line = lines[lineno] _indent = get_indentation(line) if line.strip() != '': colon_pos = line.find(':') if colon_pos == -1: break if colon_pos == len(line.rstrip()) - 1: break if prop == line[:colon_pos].strip(): prop_found = True break lineno += 1 if prop_found: # if property found then change its value _pos_prop_value = get_line_start_pos(self.text, lineno) + \ colon_pos + 2 if lineno == total_lines - 1: _line_end_pos = len(self.text) else: _line_end_pos = get_line_end_pos(self.text, lineno) return self.text[_pos_prop_value:_line_end_pos] return ''