def __setitem__(self, key, value): """ Set the value for the given key """ if not (value is None or value in self.getValidValuesForKey(key)): raise ValueError("Invalid value %s %s" % (key, self.getValidValuesForKey(key))) # get the variable dictionary variables = getattr(self.obj, '_variables', {}) if variables.get(key, None) == value: return context = ObservingContext(key=key, old=variables.get(key, None), new=value) willChange(self.obj, 'variables', context) if value is None: del variables[key] else: variables[key] = value if key == "PoS" and value == "Dummy": variables["Person"] = "3rd" variables["Head"] = "yes" # variables["Gender"] = "masculine" variables["Number"] = "singular" # set the new variables dictionary if len(variables) == 0: del self.obj._variables else: self.obj._variables = variables didChange(self.obj, 'variables', context)
def __setitem__(self, key, value): """ Set the value for the given key """ if not (value is None or value in self.getValidValuesForKey(key)): raise ValueError("Invalid value") # get the variable dictionary variables = getattr(self.obj, '_variables', {}) if variables.get(key, None) == value: return context = ObservingContext(key=key, old=variables.get(key, None), new=value) willChange(self.obj, 'variables', context) if value is None: del variables[key] else: variables[key] = value # set the new variables dictionary if len(variables) == 0: del self.obj._variables else: self.obj._variables = variables didChange(self.obj, 'variables', context)
def event(self, ev): """General event handler. This is reimplemented to: - prevent inserting the hard line separator, which makes no sense in plain text - prevent handling Undo and Redo, they work better via the menu actions - handle Tab and Backtab to change the indent """ if ev in ( # avoid the line separator, makes no sense in plain text QKeySequence.InsertLineSeparator, # those can better be called via the menu actions, then they # work better QKeySequence.Undo, QKeySequence.Redo, ): return False # handle Tab and Backtab if ev.type() == QEvent.KeyPress: cursor = self.textCursor() if ev.key() == Qt.Key_Tab and ev.modifiers() == Qt.NoModifier: # tab pressed, insert a tab when no selection and in text, # else increase the indent if not cursor.hasSelection(): block = cursor.block() text = block.text()[:cursor.position() - block.position()] if text and not text.isspace(): if variables.get(self.document(), 'document-tabs', True): cursor.insertText('\t') else: tabwidth = variables.get(self.document(), 'tab-width', 8) spaces = tabwidth - len( text.expandtabs(tabwidth)) % tabwidth cursor.insertText(' ' * spaces) self.setTextCursor(cursor) return True import indent indent.increase_indent(cursor) if not cursor.hasSelection(): cursortools.strip_indent(cursor) self.setTextCursor(cursor) return True elif ev.key() == Qt.Key_Backtab and ev.modifiers( ) == Qt.ShiftModifier: # shift-tab pressed, decrease the indent import indent indent.decrease_indent(cursor) if not cursor.hasSelection(): cursortools.strip_indent(cursor) self.setTextCursor(cursor) return True return super(View, self).event(ev)
def event(self, ev): """General event handler. This is reimplemented to: - prevent inserting the hard line separator, which makes no sense in plain text - prevent handling Undo and Redo, they work better via the menu actions - handle Tab and Backtab to change the indent """ if ev in ( # avoid the line separator, makes no sense in plain text QKeySequence.InsertLineSeparator, # those can better be called via the menu actions, then they # work better QKeySequence.Undo, QKeySequence.Redo, ): return False # handle Tab and Backtab if ev.type() == QEvent.KeyPress: cursor = self.textCursor() if ev.key() == Qt.Key_Tab and ev.modifiers() == Qt.NoModifier: # tab pressed, insert a tab when no selection and in text, # else increase the indent if not cursor.hasSelection(): block = cursor.block() text = block.text()[:cursor.position() - block.position()] if text and not text.isspace(): if variables.get(self.document(), 'document-tabs', True): cursor.insertText('\t') else: tabwidth = variables.get(self.document(), 'tab-width', 8) spaces = tabwidth - len(text.expandtabs(tabwidth)) % tabwidth cursor.insertText(' ' * spaces) self.setTextCursor(cursor) return True import indent indent.increase_indent(cursor) if not cursor.hasSelection(): cursortools.strip_indent(cursor) self.setTextCursor(cursor) return True elif ev.key() == Qt.Key_Backtab and ev.modifiers() == Qt.ShiftModifier: # shift-tab pressed, decrease the indent import indent indent.decrease_indent(cursor) if not cursor.hasSelection(): cursortools.strip_indent(cursor) self.setTextCursor(cursor) return True return super(View, self).event(ev)
def version(self): """Returns the LilyPond version if set in the document, as a tuple of ints. First the functions searches inside LilyPond syntax. Then it looks at the 'version' document variable. Then, if the document is not a LilyPond document, it simply searches for a \\version command string, possibly embedded in a comment. The version is cached until the documents contents change. """ mkver = lambda strings: tuple(map(int, strings)) version = ly.parse.version(tokeniter.all_tokens(self.document())) if version: return mkver(re.findall(r"\d+", version)) # look at document variables version = variables.get(self.document(), "version") if version: return mkver(re.findall(r"\d+", version)) # parse whole document for non-lilypond documents if self.mode() != "lilypond": m = re.search(r'\\version\s*"(\d+\.\d+(\.\d+)*)"', self.document().toPlainText()) if m: return mkver(m.group(1).split('.'))
def Get_param(self, btn, n, other=None): # handle the repeating last parameter avl = len(self.auto_validate) m = min(n, avl) av = self.auto_validate[m - 1] if self.Param_count(btn) < n: param = None else: param = btn.symbols[SYM_PARAMS][n] if param == None: ret = other else: if av[AV_VAR_OK] == AVV_REQD: ret = variables.get(param, btn.symbols[SYM_LOCAL], btn.symbols[SYM_GLOBAL][1]) else: if type(param) == str and av[AV_TYPE][AVT_DESC] in { PT_STR[AVT_DESC], PT_STRS[AVT_DESC] } and param[0:1] == '"': ret = param[1:] else: ret = param return av[AV_TYPE][AVT_CONV](ret)
def Auto_recall(v_name, symbols): # automatically recalls the variable from the "right" place with symbols[SYM_GLOBAL][0]: # lock the globals while we do this a = variables.get(v_name, symbols[SYM_LOCAL], symbols[SYM_GLOBAL][1]) # try local, then global return a
def basenames(self): """Returns a list of basenames that our document is expected to create. The list is created based on include files and the define output-suffix and \bookOutputName and \bookOutputSuffix commands. You should add '.ext' and/or '-[0-9]+.ext' to find created files. """ # if the file defines an 'output' variable, it is used instead output = variables.get(self.document(), 'output') filename = self.jobinfo()[0] if output: dirname = os.path.dirname(filename) return [os.path.join(dirname, name.strip()) for name in output.split(',')] mode = self.mode() if mode == "lilypond": return fileinfo.basenames(self.lydocinfo(), self.includefiles(), filename) elif mode == "html": pass elif mode == "texinfo": pass elif mode == "latex": pass elif mode == "docbook": pass return []
def Global_recall(v_name, symbols): # automatically recalls the global variable with symbols[SYM_GLOBAL][0]: # lock the globals while we do this a = variables.get( v_name, None, symbols[SYM_GLOBAL][1]) # grab the value from the global vars return a
def master(self): """Returns the master filename for the document, if it exists.""" filename = self.document().url().toLocalFile() redir = variables.get(self.document(), "master") if filename and redir: path = os.path.normpath(os.path.join(os.path.dirname(filename), redir)) if os.path.exists(path) and path != filename: return path
def rcl_l(self, symbols, cmd, cmds): # recalls a local variable (not overly useful, but avoids ambiguity) ret = 1 ret, v = variables.next_cmd(ret, cmds) a = variables.get(v, symbols[SYM_LOCAL], None, param_convs._any) # as an integer variables.push(symbols, a) return ret
def rcl(self, symbols, cmd, cmds): # recalls a variable. Try local first, then global ret = 1 ret, v = variables.next_cmd(ret, cmds) with symbols[SYM_GLOBAL][0]: # lock the globals while we do this a = variables.get(v, symbols[SYM_LOCAL], symbols[SYM_GLOBAL][1], param_convs._any) # as an integer variables.push(symbols, a) return ret
def rcl_g(self, symbols, cmd, cmds): # recalls a global variable (useful if you define an identical local var) ret = 1 ret, v = variables.next_cmd(ret, cmds) with symbols[SYM_GLOBAL][0]: # lock the globals while we do this a = variables.get( v, None, symbols[SYM_GLOBAL][1], param_convs._any ) # grab the value from the global vars as an integer variables.push(symbols, a) # and push onto the stack return ret
def document(self): """Return the Document that should be engraved.""" doc = self.stickyDocument() if not doc: doc = self.mainwindow().currentDocument() if not doc.url().isEmpty(): master = variables.get(doc, "master") if master: url = doc.url().resolved(QUrl(master)) doc = app.openUrl(url) return doc
def mode(self, guess=True): """Returns the type of document ('lilypond, 'html', etc.). The mode can be set using the "mode" document variable. If guess is True (default), the mode is auto-recognized based on the contents if not set explicitly using the "mode" variable. In this case, this function always returns an existing mode. If guess is False, auto-recognizing is not done and the function returns None if the mode wasn't set explicitly. """ mode = variables.get(self.document(), "mode") if mode in ly.lex.modes: return mode if guess: return self.lydocinfo().mode()
def encoding(self): return variables.get(self, "coding") or self._encoding
def setTabWidth(self): """(Internal) Reads the tab-width variable and the font settings to set the tabStopWidth.""" tabwidth = self.fontMetrics().width(" ") * variables.get(self.document(), 'tab-width', 8) self.setTabStopWidth(tabwidth)
def setTabWidth(self): """(Internal) Reads the tab-width variable and the font settings to set the tabStopWidth.""" tabwidth = QSettings().value("indent/tab_width", 8, int) tabwidth = self.fontMetrics().width(" ") * variables.get( self.document(), 'tab-width', tabwidth) self.setTabStopWidth(tabwidth)
def setTabWidth(self): """(Internal) Reads the tab-width variable and the font settings to set the tabStopWidth.""" tabwidth = QSettings().value("indent/tab_width", 8, int) tabwidth = self.fontMetrics().width(" ") * variables.get(self.document(), 'tab-width', tabwidth) self.setTabStopWidth(tabwidth)
def Local_recall(v_name, symbols): # automatically recalls the local variable a = variables.get(v_name, symbols[SYM_LOCAL], None) # get the value from the local vars return a