--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/eric7/CodeFormatting/BlackUtilities.py Mon Jul 11 16:42:50 2022 +0200 @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing some utility functions for the Black based code formatting. +""" + +import re + +from PyQt6.QtCore import QCoreApplication + +import black + + +def getDefaultConfiguration(): + """ + Function to generate a default set of configuration parameters. + + @return dictionary containing the default parameters + @rtype dict + """ + return { + "target-version": set(), + "line-length": black.DEFAULT_LINE_LENGTH, + "skip-string-normalization": False, + "skip-magic-trailing-comma": False, + "extend-exclude": "", + "exclude": black.DEFAULT_EXCLUDES, # not shown in config dialog + "force-exclude": "", # not shown in config dialog + } + + +def compileRegExp(regexp): + """ + Function to compile a given regular expression. + + @param regexp regular expression to be compiled + @type str + @return compiled regular expression object + @rtype re.Pattern + """ + if "\n" in regexp: + # multi line regexp + regexp = f"(?x){regexp}" + compiled = re.compile(regexp) + return compiled + + +def validateRegExp(regexp): + """ + Function to validate a given regular expression. + + @param regexp regular expression to be validated + @type str + @return tuple containing a flag indicating validity and an error message + @rtype tuple of (bool, str) + """ + if regexp: + try: + compileRegExp(regexp) + return True, "" + except re.error as e: + return ( + False, + QCoreApplication.translate( + "BlackUtilities", + "Invalid regular expression: {0}" + ).format(str(e)) + ) + except IndexError: + return ( + False, + QCoreApplication.translate( + "BlackUtilities", + "Invalid regular expression: missing group name" + ) + ) + else: + return ( + False, + QCoreApplication.translate( + "BlackUtilities", + "A regular expression must be given." + ) + )