def execException(exceptionView): label = ApplyStyleSheetFromAttribute(ExecutionStyle.labelStyle, StaticText('EXCEPTION:')) return ApplyStyleSheetFromAttribute( ExecutionStyle.exceptionBorderStyle, Border( Column([label, exceptionView.padX(5.0, 0.0).alignHExpand() ]).alignHExpand()).alignHExpand())
def intLiteral(format, value): valuePres = ApplyStyleSheetFromAttribute(PythonEditorStyle.numLiteralStyle, Text(value)) boxContents = [valuePres] if format is not None: boxContents.append( ApplyStyleSheetFromAttribute(PythonEditorStyle.literalFormatStyle, Text(format))) return Row(boxContents)
def _richStringLines(labelText, richString, textStyleAttribute, bUseDefaultPerspectiveForResult): label = ApplyStyleSheetFromAttribute(ExecutionStyle.labelStyle, StaticText(labelText)) lines = [ _richStringItem(item, textStyleAttribute, bUseDefaultPerspectiveForResult) for item in richString.getItems() ] return Overlay( [Column(lines).alignHExpand(), label.alignHRight().alignVTop()])
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 execStderr(richString, bUseDefaultPerspectiveForResult): return ApplyStyleSheetFromAttribute( ExecutionStyle.stdErrStyle, Border( _richStringLines('STDERR', richString, ExecutionStyle.stdErrStyle, bUseDefaultPerspectiveForResult).alignHExpand()). alignHExpand())
def executionResultBox(streams, exception, resultInTuple, bUseDefaultPerspecitveForException, bUseDefaultPerspectiveForResult): boxContents = [] for stream in streams: if stream.name == 'out': boxContents.append( execStdout(stream.richString, bUseDefaultPerspectiveForResult)) elif stream.name == 'err': boxContents.append( execStderr(stream.richString, bUseDefaultPerspectiveForResult)) else: raise ValueError, 'Unreckognised stream \'{0}\''.format( stream.name) if exception is not None: exceptionView = Pres.coerce(exception).alignHPack() if bUseDefaultPerspecitveForException: exceptionView = ApplyPerspective.defaultPerspective(exceptionView) boxContents.append(execException(exceptionView)) if resultInTuple is not None: resultView = Pres.coercePresentingNull(resultInTuple[0]).alignHPack() if bUseDefaultPerspectiveForResult: resultView = ApplyPerspective.defaultPerspective(resultView) boxContents.append(execResult(resultView)) if len(boxContents) > 0: return ApplyStyleSheetFromAttribute(ExecutionStyle.resultBoxStyle, Column(boxContents).alignHExpand()) else: return None
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 relativeModule(name): return ApplyStyleSheetFromAttribute(PythonEditorStyle.importStyle, Text(name))
def augAssignStmt(op, target, value): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return Span([target, _space, opView, _space, value])
def kwParamList(name): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.paramStyle, Text(name)) return Span([_doubleAsterisk, nameView])
def singleTarget(name): return ApplyStyleSheetFromAttribute(PythonEditorStyle.targetStyle, Text(name))
def _initKeywords(keywords): for keyword in keywords: #keyword = keyword[0].upper() + keyword[1:] _keywordMap[keyword] = ApplyStyleSheetFromAttribute( PythonEditorStyle.keywordStyle, Text(keyword, keyword))
def unparseableText(text): return ApplyStyleSheetFromAttribute(PythonEditorStyle.unparseableStyle, Text(text))
def spanPrefixOp(x, op): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return LineBreakCostSpan([opView, x])
def callKWArg(name, value): nameView = ApplyStyleSheetFromAttribute(PythonEditorStyle.kwNameStyle, Text(name)) return Span([nameView, _equals, value])
def subscriptTuple(items, bTrailingSeparator): seq = SpanSequenceView( items, None, None, _comma, _space, TrailingSeparator.ALWAYS if bTrailingSeparator else TrailingSeparator.NEVER) return ApplyStyleSheetFromAttribute(PythonEditorStyle.sequenceStyle, seq)
def attributeRef(target, name): attribView = ApplyStyleSheetFromAttribute(PythonEditorStyle.attributeStyle, Text(name)) return Span([target, _dot, attribView])
def setLiteral(items, bTrailingSeparator): seq = SpanSequenceView( items, _openBrace, _closeBrace, _comma, _space, TrailingSeparator.ALWAYS if bTrailingSeparator else TrailingSeparator.NEVER) return ApplyStyleSheetFromAttribute(PythonEditorStyle.sequenceStyle, seq)
def load(name): return ApplyStyleSheetFromAttribute(PythonEditorStyle.varStyle, Text(name))
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 moduleContentImport(name): return ApplyStyleSheetFromAttribute(PythonEditorStyle.importStyle, Text(name))
def spanCmpOp(op, y): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return Span([Text(' '), opView, Text(' '), _lineBreak, y])
def execResult(resultView): return ApplyStyleSheetFromAttribute( ExecutionStyle.resultBorderStyle, Border(Bin(Paragraph([resultView]))).alignHExpand())
def div(x, y, fractionBarContent): bar = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Fraction.FractionBar('/')) return Fraction(x, y, bar)
def _textLines(text, textStyleAttribute): if text.endswith('\n'): text = text[:-1] return ApplyStyleSheetFromAttribute( textStyleAttribute, Column([StaticText(line) for line in text.split('\n')]))
def simpleParam(name): return ApplyStyleSheetFromAttribute(PythonEditorStyle.paramStyle, Text(name))
def spanBinOp(x, y, op): opView = ApplyStyleSheetFromAttribute(PythonEditorStyle.operatorStyle, Text(op)) return LineBreakCostSpan([x, Text(' '), opView, Text(' '), _lineBreak, y])
def imaginaryLiteral(value): return ApplyStyleSheetFromAttribute(PythonEditorStyle.numLiteralStyle, Text(value))