pycrossword  0.4
Pure-Python implementation of a crossword puzzle generator and editor
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes | List of all members
pycross.utils.utils.JsonHiliter Class Reference

Syntax highlighter class for JSON. More...

Inheritance diagram for pycross.utils.utils.JsonHiliter:

Public Member Functions

def __init__ (self, QtGui.QTextDocument parent, decode_errors=False, on_decode_error=None, on_decode_success=None)
 
def highlightBlock (self, text)
 Override of QtGui.QSyntaxHighlighter::highlightBlock() method: does the syntax highlighting. More...
 

Public Attributes

 decode_errors
 bool whether to highlight and process JSON decode errors More...
 
 decoder
 json.JSONDecoder JSON decoder More...
 

Static Public Attributes

list PATTERNS
 Regex-based patterns and their corresponding color values. More...
 
 sig_parse_error = QtCore.pyqtSignal(QtGui.QSyntaxHighlighter, str, str, int, int, int)
 Qt signal emitted on a syntax parser error. More...
 
 sig_parse_success = QtCore.pyqtSignal(QtGui.QSyntaxHighlighter)
 Qt signal emitted on a syntax parser success. More...
 

Private Attributes

 _error_format
 QtGui.QTextCharFormat error highlighting text format More...
 

Detailed Description

Syntax highlighter class for JSON.

Used in pycross::forms::WordSrcDialog (DB table definition).

See also
QSyntaxHighlighter docs

Constructor & Destructor Documentation

◆ __init__()

def pycross.utils.utils.JsonHiliter.__init__ (   self,
QtGui.QTextDocument  parent,
  decode_errors = False,
  on_decode_error = None,
  on_decode_success = None 
)
Parameters
parentQtGui.QTextDocument parent document that the highlighter binds to
decode_errorsbool whether to highlight and process JSON decode errors
on_decode_errorQtCore.pyQtSlot slot for the JsonHiliter::sig_parse_error signal
on_decode_successQtCore.pyQtSlot slot for the JsonHiliter::sig_parse_success signal

Member Function Documentation

◆ highlightBlock()

def pycross.utils.utils.JsonHiliter.highlightBlock (   self,
  text 
)

Override of QtGui.QSyntaxHighlighter::highlightBlock() method: does the syntax highlighting.

Parameters
textstr the text string to be parsed and highlighted

Member Data Documentation

◆ _error_format

pycross.utils.utils.JsonHiliter._error_format
private

QtGui.QTextCharFormat error highlighting text format

◆ decode_errors

pycross.utils.utils.JsonHiliter.decode_errors

bool whether to highlight and process JSON decode errors

◆ decoder

pycross.utils.utils.JsonHiliter.decoder

json.JSONDecoder JSON decoder

◆ PATTERNS

list pycross.utils.utils.JsonHiliter.PATTERNS
static
Initial value:
= [
# operators (dot, comma, colon)
(re.compile(r'([\.,\:])'), 1, QtGui.QColor(QtCore.Qt.red)),
# brackets
(re.compile(r'([\{\}\[\]\(\)])'), 1, QtGui.QColor(QtCore.Qt.gray)),
# numbers
(re.compile(r'([\s,\:\{\[\(])([\-\+]?[\d\.]+)([\s,\:\}\]\)]?)'), 2, QtGui.QColor(QtCore.Qt.blue)),
(re.compile(r'([\s,\:\{\[\(])([\-\+]?[\d\.]+$)'), 2, QtGui.QColor(QtCore.Qt.blue)),
(re.compile(r'(^[\-\+]?[\d\.]+)([\s,\:\}\]\)]?)'), 1, QtGui.QColor(QtCore.Qt.blue)),
(re.compile(r'(^[\-\+]?[\d\.]+$)'), 1, QtGui.QColor(QtCore.Qt.blue)),
# boolean
(re.compile(r'([\s,\:\{\[\(])(true|false)([\s,\:\}\]\)])'), 2, QtGui.QColor(QtCore.Qt.magenta)),
(re.compile(r'([\s,\:\{\[\(])(true$|false$)'), 2, QtGui.QColor(QtCore.Qt.magenta)),
(re.compile(r'(^true|^false)([\s,\:\}\]\)])'), 1, QtGui.QColor(QtCore.Qt.magenta)),
(re.compile(r'(^true$|^false$)'), 1, QtGui.QColor(QtCore.Qt.magenta)),
# null values
(re.compile(r'([\s,\:\{\[\(])(null)([\s,\:\}\]\)])'), 2, QtGui.QColor(QtCore.Qt.gray)),
(re.compile(r'([\s,\:\{\[\(])(null$)'), 2, QtGui.QColor(QtCore.Qt.gray)),
(re.compile(r'(^null)([\s,\:\}\]\)])'), 1, QtGui.QColor(QtCore.Qt.gray)),
(re.compile(r'(^null$)'), 1, QtGui.QColor(QtCore.Qt.gray)),
# string values
(re.compile(r'([\s,\:\[\(])(\".*?\")'), 2, QtGui.QColor(QtCore.Qt.darkCyan)),
(re.compile(r'(^\".*?\")'), 1, QtGui.QColor(QtCore.Qt.darkCyan)),
# key names
(re.compile(r'(\".*?\")(\s*\:)'), 1, QtGui.QColor(QtCore.Qt.darkGreen))
]

Regex-based patterns and their corresponding color values.

Each record has 3 elements:

  1. Python regex object compiled regex pattern
  2. int group number in regex match results to highlight (0 = whole match, 1 = first expression in parentheses, etc...)
  3. QtGui.QColor color to apply to matched text

◆ sig_parse_error

pycross.utils.utils.JsonHiliter.sig_parse_error = QtCore.pyqtSignal(QtGui.QSyntaxHighlighter, str, str, int, int, int)
static

Qt signal emitted on a syntax parser error.

Arguments:

  • QtGui.QSyntaxHighlighter this instance
  • str error message string
  • str error docs string
  • int absolute position of the error in the source code
  • int line number in the source code
  • int column number in the source code

◆ sig_parse_success

pycross.utils.utils.JsonHiliter.sig_parse_success = QtCore.pyqtSignal(QtGui.QSyntaxHighlighter)
static

Qt signal emitted on a syntax parser success.

Arguments:

  • QtGui.QSyntaxHighlighter this instance

The documentation for this class was generated from the following file: