def assignStmt(targets, value): if len(targets) == 1: return Span([targets[0], _space, _equals, _space, value]) else: targetElements = [] for t in targets: targetElements.extend([t, _space, _equals, _space, _lineBreak]) return Span(targetElements + [value])
def __pair(self, kv): key, value = kv eq = self._punc_style.applyTo(Label('=')) return Span([ self._name_style(Label(key)), eq, self._value_style(Label(value)) ])
def execStmt(source, globals, locals): elements = [_keyword('exec'), _space, source] if globals is not None: elements.extend([_space, _keyword('in'), _space, _lineBreak, globals]) if locals is not None: elements.extend([_comma, _space, _lineBreak, locals]) return Span(elements)
def globalStmt(vars): varElements = [] if len(vars) > 0: for v in vars[:-1]: varElements.extend([v, _comma, _space, _lineBreak]) varElements.append(vars[-1]) return Span([_keyword('global'), _space] + varElements)
def exceptStmtHeader(exception, target): elements = [_keyword('except')] if exception is not None: elements.extend([_space, exception]) if target is not None: elements.extend([_space, _keyword('as'), _space, _lineBreak, target]) elements.append(_colon) return Span(elements)
def raiseStmt(excType, excValue, traceback): xs = [x for x in [excType, excValue, traceback] if x is not None] xElements = [] if len(xs) > 0: for x in xs[:-1]: xElements.extend([x, _comma, _space]) xElements.append(xs[-1]) return Span([_keyword('raise'), _space] + xElements)
def importStmt(modules): moduleElements = [] if len(modules) > 0: for m in modules[:-1]: moduleElements.extend([m, _comma, _space, _lineBreak]) moduleElements.append(modules[-1]) return Span( [_keyword('import'), _space, ParagraphIndentMatchSpan(moduleElements)])
def __present__(self, fragment, inh): space = Label(' ') br = LineBreak() items = self.__attrs.items() contents = [self.__pair(items[0])] for x in items[1:]: contents.extend([space, br, self.__pair(x)]) return Span(contents)
def withStmtHeader(contexts): elements = [_keyword('with'), _space] first = True for ctx in contexts: if not first: elements.append(_comma) elements.append(_space) elements.append(ctx) first = False elements.append(_colon) return Span(elements)
def fromImportStmt(module, imports): importElements = [] if len(imports) > 0: for i in imports[:-1]: importElements.extend([i, _comma, _space, _lineBreak]) importElements.append(imports[-1]) return Span([ _keyword('from'), _space, module, _space, _keyword('import'), _space, ParagraphIndentMatchSpan(importElements) ])
def tupleParam(params, bParamsTrailingSeparator): elements = [] if len(params) > 0: for p in params[:-1]: elements.append(p) elements.append(_comma) elements.append(_space) elements.append(_lineBreak) elements.append(params[-1]) if bParamsTrailingSeparator: elements.append(_comma) return Span([_openParen, ParagraphIndentMatchSpan(elements), _closeParen])
def classStmtHeader(name, bases, bBasesTrailingSeparator): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.varStyle, Text(name)) elements = [_keyword('class'), _space, nameView] if bases is not None: basesSeq = SpanSequenceView( bases, None, None, _comma, _space, TrailingSeparator.ALWAYS if bBasesTrailingSeparator else TrailingSeparator.NEVER) basesView = ApplyStyleSheetFromAttribute( PythonEditorStyle.sequenceStyle, basesSeq) elements.extend([_space, _openParen, basesView, _closeParen]) elements.append(_colon) return Span(elements)
def decoStmtHeader(name, args, bArgsTrailingSeparator): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.varStyle, Text(name)) elements = [_at, nameView] if args is not None: argsSeq = SpanSequenceView( args, _openParen, _closeParen, _comma, _space, TrailingSeparator.ALWAYS if bArgsTrailingSeparator else TrailingSeparator.NEVER) argsView = ApplyStyleSheetFromAttribute( PythonEditorStyle.sequenceStyle, argsSeq) elements.append(argsView) return Span(elements)
def defStmtHeader(name, params, bParamsTrailingSeparator): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.varStyle, Text(name)) elements = [_keyword('def'), _space, nameView, _openParen] if len(params) > 0: paramsSeq = SpanSequenceView( params, None, None, _comma, _space, TrailingSeparator.ALWAYS if bParamsTrailingSeparator else TrailingSeparator.NEVER) paramsView = ApplyStyleSheetFromAttribute( PythonEditorStyle.sequenceStyle, paramsSeq) elements.append(paramsView) elements.extend([_closeParen, _colon]) return Span(elements)
def printStmt(destination, values): elements = [_keyword('print')] if destination is not None or len(values) > 0: elements.append(_space) if destination is not None: elements.extend([_doubleRightChevron, _space, destination]) if len(values) > 0: elements.extend([_comma, _space, _lineBreak]) bFirst = True for v in values: if not bFirst: elements.extend([_comma, _space, _lineBreak]) elements.append(v) bFirst = False return Span(elements)
def __present__(self, fragment, inh): space = Label(' ') open_angle = self._punc_style.applyTo(Label('<')) close_angle = self._punc_style.applyTo(Label('>')) slash = self._punc_style.applyTo(Label('/')) tag = self._tag_style.applyTo(Label(self.__tag)) br = LineBreak() complex = False for x in self.__contents: if isinstance(x, XmlElem): complex = True break if complex: end = Row([open_angle, slash, tag, close_angle]) content = Column( [NormalText([x]) for x in self.__contents if x != '']) if len(self.__attrs) == 0: start = Row([open_angle, tag, close_angle]) else: start = Paragraph( [open_angle, tag, space, br, self.__attrs, close_angle]) return Column([start, content.padX(20.0, 0.0), end]) else: if len(self.__contents) == 0: if len(self.__attrs) == 0: return Row([open_angle, tag, slash, close_angle]) else: return Paragraph([ open_angle, tag, space, br, self.__attrs, slash, close_angle ]) else: end = Row([open_angle, slash, tag, close_angle]) content = [RichSpan([x]) for x in self.__contents] if len(self.__attrs) == 0: start = Row([open_angle, tag, close_angle]) else: start = Span([ open_angle, tag, space, br, self.__attrs, close_angle ]) return Paragraph([start] + content + [end])
def stringLiteral(format, quotation, value, isUnicode, raw): boxContents = [] if format is not None and format != '': boxContents.append( ApplyStyleSheetFromAttribute(PythonEditorStyle.literalFormatStyle, Text(format))) # Split the value into pieces of escaped and non-escaped content if raw: valuePres = ApplyStyleSheetFromAttribute( PythonEditorStyle.stringLiteralStyle, Text(value)) else: segments = _non_escaped_string_re.split(value) if len(segments) == 1: valuePres = ApplyStyleSheetFromAttribute( PythonEditorStyle.stringLiteralStyle, Text(value)) else: escape = False segsAsPres = [] for seg in segments: if seg is not None and len(seg) > 0: if escape: segsAsPres.append( ApplyStyleSheetFromAttribute( PythonEditorStyle.stringLiteralEscapeStyle, Border(Text(seg)))) else: segsAsPres.append(Text(seg)) escape = not escape valuePres = ApplyStyleSheetFromAttribute( PythonEditorStyle.stringLiteralStyle, Span(segsAsPres)) quotationPres = ApplyStyleSheetFromAttribute( PythonEditorStyle.quotationStyle, Text(quotation)) boxContents.extend([quotationPres, valuePres, quotationPres]) return Row(boxContents)
def moduleImportAs(name, asName): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.importStyle, Text(name)) asNameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.importStyle, Text(asName)) return Span([nameView, _space, _keyword('as'), _space, asNameView])
def yieldStmt(value): if value is not None: return Span([_keyword('yield'), _space, value]) else: return _keyword('yield')
def returnStmt(value): if value is not None: return Span([_keyword('return'), _space, value]) else: return _keyword('return')
def delStmt(target): return Span([_keyword('del'), _space, target])
def callKWArgList(value): return Span([_doubleAsterisk, value])
def assertStmt(condition, fail): elements = [_keyword('assert'), _space, condition] if fail is not None: elements.extend([_comma, _space, _lineBreak, fail]) return Span(elements)
def kwParamList(name): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.paramStyle, Text(name)) return Span([_doubleAsterisk, nameView])
def unparsedElements(elts): return Span( elts )
def spanCmpOp(op, y): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return Span([Text(' '), opView, Text(' '), _lineBreak, y])
def augAssignStmt(op, target, value): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return Span([target, _space, opView, _space, value])
def callArgList(value): return Span([_asterisk, value])
def group(subexp, capturing): contents = [ subexp ] if capturing else [ _controlCharStyle( Text( '?:' ) ), subexp ] b = _groupBorder if capturing else _nonCapturingGroupBorder return b.surround( Row( [ Segment( Span( [ _controlCharStyle( Text( '(' ) ) ] + contents + [ _controlCharStyle( Text( ')' ) ) ] ) ) ] ) )
def defaultValueParam(param, defaultValue): return Span([param, _equals, defaultValue])