Python re.DEBUG Examples

The following are 17 code examples of re.DEBUG(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module re , or try the search function .
Example #1
Source File: test_re.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
subpattern 1
  literal 46
subpattern None
  branch
    in
      literal 99
      literal 104
  or
    literal 112
    literal 121
subpattern None
  groupref_exists 1
    at at_end
  else
    literal 58
    literal 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #2
Source File: test_re.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
subpattern 1
  literal 46
subpattern None
  branch
    in
      literal 99
      literal 104
  or
    literal 112
    literal 121
subpattern None
  groupref_exists 1
    at at_end
  else
    literal 58
    literal 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #3
Source File: test_re.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
subpattern 1
  literal 46
subpattern None
  branch
    in
      literal 99
      literal 104
  or
    literal 112
    literal 121
subpattern None
  groupref_exists 1
    at at_end
  else
    literal 58
    literal 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #4
Source File: test_re.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
SUBPATTERN 1
  LITERAL 46
SUBPATTERN None
  BRANCH
    IN
      LITERAL 99
      LITERAL 104
  OR
    LITERAL 112
    LITERAL 121
SUBPATTERN None
  GROUPREF_EXISTS 1
    AT AT_END
  ELSE
    LITERAL 58
    LITERAL 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #5
Source File: test_re.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_debug_flag(self):
        with captured_stdout() as out:
            re.compile('foo', re.DEBUG)
        self.assertEqual(out.getvalue().splitlines(),
                         ['literal 102', 'literal 111', 'literal 111'])
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile('foo', re.DEBUG)
        self.assertEqual(out.getvalue().splitlines(),
                         ['literal 102', 'literal 111', 'literal 111']) 
Example #6
Source File: test_re.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
subpattern 1
  literal 46
subpattern None
  branch
    in
      literal 99
      literal 104
  or
    literal 112
    literal 121
subpattern None
  groupref_exists 1
    at at_end
  else
    literal 58
    literal 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #7
Source File: test_re.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
SUBPATTERN 1
  LITERAL 46
SUBPATTERN None
  BRANCH
    IN
      LITERAL 99
      LITERAL 104
  OR
    LITERAL 112
    LITERAL 121
SUBPATTERN None
  GROUPREF_EXISTS 1
    AT AT_END
  ELSE
    LITERAL 58
    LITERAL 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #8
Source File: test_re.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_debug_flag(self):
        pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        dump = '''\
subpattern 1
  literal 46
subpattern None
  branch
    in
      literal 99
      literal 104
  or
    literal 112
    literal 121
subpattern None
  groupref_exists 1
    at at_end
  else
    literal 58
    literal 32
'''
        self.assertEqual(out.getvalue(), dump)
        # Debug output is output again even a second time (bypassing
        # the cache -- issue #20426).
        with captured_stdout() as out:
            re.compile(pat, re.DEBUG)
        self.assertEqual(out.getvalue(), dump) 
Example #9
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, logdispatcher):
        """
        Initialize all object attributes
        @author: ekkehard j. koch
        """
        self.logdispatcher = logdispatcher
        self.command = []
        self.commandblank = True
        self.returncode = 0
        self.output = []
        self.stdout = []
        self.stderr = []
        self.shell = False
        self.setLogPriority(LogPriority.DEBUG)
        self.flags = ["DEBUG", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL",
                      "UNICODE", "VERBOSE"]
        self.flag = ""

        # set this to False if you need to run a command that has no return code
        self.wait = True
        self.cmdtimeout = 0

############################################################################### 
Example #10
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 4 votes vote down vote up
def __calledBy(self):
        """
        Log the caller of the method that calls this method

        @author: Roy Nielsen
        """
        try:
            filename = inspect.stack()[3][1]
            functionName = str(inspect.stack()[3][3])
            lineNumber = str(inspect.stack()[3][2])
        except Exception as err:
            raise err
        else:
            self.logdispatcher.log(LogPriority.DEBUG, "called by: " + \
                                      filename + ": " + \
                                      functionName + " (" + \
                                      lineNumber + ")")
        return " Filename: " + str(filename) + "Line: " + str(lineNumber) + " functionName: " + str(functionName)

############################################################################### 
Example #11
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 4 votes vote down vote up
def convert_bytes_to_string(self, data):
        """

        :param data:
        :return: data
        :rtype: str|list
        """

        self.logdispatcher.log(LogPriority.DEBUG, "Converting any bytes objects into strings...")

        data_type = type(data)

        if data_type is list:
            for e in data:
                if type(e) is bytes:
                    data = [e.decode('utf-8') for e in data]
        elif data_type is bytes:
            data = data.decode('utf-8')
            data = str(data)

        return data 
Example #12
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 3 votes vote down vote up
def getOutputString(self):
        """Get standard out in string format

        :param self: essential if you override this definition
        :returns: stdstring
        :rtype: string
@author: ekkehard j. koch
@change: Breen Malmberg - 12/3/2015

        """

        stdstring = ""

        try:

            if self.stdout:
                if not isinstance(self.stdout, list):
                    self.logdispatcher.log(LogPriority.DEBUG,
                                           "Parameter self.stdout is not a " +
                                           "list. Cannot compile stdout " +
                                           "string. Returning blank stdout " +
                                           "string...")
                    return stdstring

                for line in self.stdout:
                    stdstring += line + "\n"
            else:
                self.logdispatcher.log(LogPriority.DEBUG, "No stdout string to display")

        except Exception:
            raise
        return stdstring

############################################################################### 
Example #13
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 3 votes vote down vote up
def getAllList(self):
        """Get both the stdout and stderr together as one list

        :param self: essential if you override this definition
        :returns: alllist
        :rtype: list
@author: Breen Malmberg

        """

        alllist = []

        stdoutlist = self.getOutput()
        stderrlist = self.getError()

        try:

            if not isinstance(stdoutlist, list):
                self.logdispatcher.log(LogPriority.DEBUG, "Content of parameter stdoutlist is not in list format. Will not include content in output!")
                stdoutlist = []
            if not isinstance(stderrlist, list):
                self.logdispatcher.log(LogPriority.DEBUG, "Content of parameter stderrlist is not in list format. Will not include content in output!")
                stderrlist = []

            if stdoutlist:
                for line in stdoutlist:
                    alllist.append(line)
            if stderrlist:
                for line in stderrlist:
                    alllist.append(line)

            if not alllist:
                self.logdispatcher.log(LogPriority.DEBUG, "There was no output to return")

        except Exception:
            raise
        return alllist 
Example #14
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 3 votes vote down vote up
def validate_command(self, command):
        """
        A valid format for a command is:
        list of non-empty strings
        -or-
        non-empty string

        :param command: the command to evaluate
        """

        self.valid_command = True

        self.logdispatcher.log(LogPriority.DEBUG, "Validating command format...")

        command_type = type(command)

        valid_types = [list, str]

        if command_type not in valid_types:
            self.logdispatcher.log(LogPriority.DEBUG, "Invalid data type for command. Expecting: str or list. Got: " + str(command_type))
            self.valid_command = False

        if command == "":
            self.logdispatcher.log(LogPriority.DEBUG, "Command was an empty string. Cannot run nothing")
            self.valid_command = False
        elif command == []:
            self.logdispatcher.log(LogPriority.DEBUG, "Command was an empty list. Cannot run nothing")
            self.valid_command = False

        if not self.valid_command:
            self.logdispatcher.log(LogPriority.DEBUG, "Command is not a valid format") 
Example #15
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 3 votes vote down vote up
def setLogPriority(self, logpriority=None):
        """Setting log priority use LogPriority.DEBUG, LogPrority.ERROR, etc.

        :param logpriority: of type LogPriority.xxx (Default value = None)
        :returns: success
        :rtype: bool
@author: ekkehard j. koch

        """

        success = True

        logprioritytype = type(logpriority)
        #print("logprioritytype: ", logprioritytype, "\n")
        if (logpriority is None):
            self.logpriority = LogPriority.DEBUG
        elif isinstance(logpriority, str):
            self.logpriority = logpriority
        else:
            self.logpriority = LogPriority.DEBUG
            success = False
            raise TypeError("LogPriority is set to '" +
                            str(self.logpriority) +
                            "'! Invalid LogPriority Object of type '" +
                            str(logprioritytype) + "' specified!")
        return success

############################################################################### 
Example #16
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 2 votes vote down vote up
def getOutputGroup(self, expression, groupnumber, searchgroup="output"):
        """getOutputGroup (expression,groupnumber) finds an expression in the
        returns the specified group after using regular expression on output

        :param self: essential if you override this definition
        :param expression: string: expression to search for in searchgroup
        :param groupnumber: integer: number of group to return
        :param searchgroup: string: group to search in output, stdout, stderr (Default value = "output")
        :returns: returnlist
        :rtype: list
@author: rsn
@change: Breen Malmberg - 12/3/2015

        """

        returnlist = []

        try:

            if searchgroup == "output":
                searchstream = self.output
            elif searchgroup == "stdout":
                searchstream = self.stdout
            elif searchgroup == "stderr":
                searchstream = self.stderr
            else:
                searchstream = self.output
            for line in searchstream:
                reresult = re.search(expression, line)
                groupstr = reresult.group(groupnumber)
                msg = "Group(" + str(groupnumber) + ")='" + \
                groupstr + "'; line='" + line + "'"
                self.logdispatcher.log(LogPriority.DEBUG, msg)
                returnlist.append(groupstr)
            msg = "expression = " + str(expression) + ", " + \
            "groupnumber = " + str(groupnumber) + ", " + \
            "searchgroup = " + str(searchgroup) + " = " + \
            "returnlist = " + str(returnlist) + ";"
            self.logdispatcher.log(LogPriority.DEBUG, msg)
        except Exception:
            raise
        return returnlist

############################################################################### 
Example #17
Source File: CommandHelper.py    From stonix with GNU General Public License v2.0 2 votes vote down vote up
def getFirstOutputGroup(self, expression, groupnumber, searchgroup="output"):
        """getOutputGroup (expression, groupnumber) finds an expression in the
        returns the first instance (string) of the group specified in the
        regular expression that is found in the output.

        :param str expression: expression to search for
        :param groupnumber: integer: number of group to return
        :param searchgroup: string: group to search in output, stdout, stderr (Default value = "output")

        :return: returnstring
        :rtype: bool

        """

        returnstring = ""

        try:

            if searchgroup == "output":
                searchstream = self.output
            elif searchgroup == "stdout":
                searchstream = self.stdout
            elif searchgroup == "stderr":
                searchstream = self.stderr
            else:
                searchstream = self.output
            for line in searchstream:
                reresult = re.search(expression, line)
                if reresult:
                    groupstr = reresult.group(groupnumber)
                    msg = "Group(" + str(groupnumber) + ")='" + \
                    groupstr + "'; line='" + line + "'"
                    self.logdispatcher.log(LogPriority.DEBUG, msg)
                    returnstring = groupstr
                    break
            msg = "expression = " + str(expression) + ", " + \
            "groupnumber = " + str(groupnumber) + ", " + \
            "searchgroup = " + str(searchgroup) + " = " + \
            "returnstring = " + str(returnstring) + ";"
            self.logdispatcher.log(LogPriority.DEBUG, msg)
        except Exception:
            raise
        return returnstring

###############################################################################