Example #1
0
 def test_get_codes(self, monkeypatch):
     stream_no_tty = StringIO()
     stream_no_tty.fileno = lambda : 0 # fake whatever fileno
     stream_no_tty.isatty = lambda : False
     tty = StringIO()
     tty.fileno = lambda : 0 # fake whatever fileno
     tty.isatty = lambda : True
     # class used is based on stream being a tty or not
     assert 'curses' == Term(stream=tty).code
     assert 'dumb' == Term(stream=stream_no_tty).code
     # force use of capabilities
     assert 'curses' == Term(stream=tty, use_colors=True).code
     assert 'curses' == Term(stream=stream_no_tty, use_colors=True).code
     # use of ansi codes forced/no curses available
     assert 'ansi' == Term(stream=tty, code='ansi').code
     import curses
     monkeypatch.setattr(curses, 'tparm', lambda : 5/0)
     assert 'ansi' == Term(stream=tty).code
Example #2
0
 def setUp(self):
     self.console = Console()
     self.fxn = function_name
     self.kls = MyClass()
     new_out, new_err = StringIO(), StringIO()
     new_out.isatty = sys.stdout.isatty
     self.old_out, self.old_err = sys.stdout, sys.stderr
     sys.stdout, sys.stderr = new_out, new_err
     self.out = new_out
     self.err = new_err
Example #3
0
 def test_mode_stdin(self):
     with open(BIRD_YAR) as f:
         data = f.read()
     stream = StringIO(data)
     stream.isatty = lambda :True
     try:
         sys.stdin = stream              
         ret, stdout, stderr = run_main('-r', BIRD_YAR, 
                 '--chunk-size=10', '--readahead-limit=20', 
                 '--chunk-overlap=0', '--mode=stdin', 
                 '--simple')
         self.assertTrue("stream[150:160]: main.Bird01" in stdout)
         self.assertEqual(ret, 0)
     finally:
         sys.stdin = sys.__stdin__
Example #4
0
    def test_lines_cols(self, monkeypatch):
        # using curses
        tty = StringIO()
        tty.fileno = lambda : 0 # fake whatever fileno
        tty.isatty = lambda : True
        term = Term(stream=tty)
        assert isinstance(term.cols(), int)
        assert isinstance(term.lines(), int)

        # curses not available gets None
        import curses
        monkeypatch.setattr(curses, 'tigetnum', lambda : 5/0)
        assert None == term.cols()
        assert None == term.lines()

        # uses stty
        term2 = Term(stream=tty, code='ansi')
        assert isinstance(term2.cols(), int)
        assert isinstance(term2.lines(), int)
Example #5
0
def stdinstr(s):
    fh = StringIO(s)
    fh.isatty = lambda: False
    return fh
Example #6
0
def stdinstr(s):
    fh = StringIO(s)
    fh.isatty = lambda: False
    return fh
Example #7
0
class RegistryFile:
   """ This class manage a file in a Win32 registry base """
   READ   = 1
   WRITE  = 2
   APPEND = 3
   
   def __init__(self, key, sub_key, flag):
      """
Constructor :
   key     : HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
   sub_key : The name of a key that this method opens
   flag    : "r" for read
             "w" for write
             "a" for append
             "b" for binary
      """
      from win32api import RegQueryValueEx, RegCreateKey, RegOpenKeyEx
      from win32con import KEY_ALL_ACCESS
      from StringIO import StringIO
      
      # Save files informations
      self.key = key
      self.sub_key = sub_key
      
      # Initialization of internal data
      self.deleted = 0
      self.handle = None
      self.data = None
      
      # Check mode
      self.__checkMode(flag)
      
      # Create an empty file
      self.data = StringIO()
      
      try:
         # Open registry key
         self.handle = RegOpenKeyEx(key,sub_key,0,KEY_ALL_ACCESS)
         
         # Read key content
         data = RegQueryValueEx(self.handle, "")[0]
         
         # If the file is in read mode
         if self.flag == self.READ:
            # Read data
            self.data = StringIO(data)
         # If the file is in append mode
         elif self.flag == self.APPEND:
            self.data.write (data)
      except:  # If the registry key not found
         # If the file must be read
         if self.flag == self.READ:
            # The file is not found
            raise FileNotFoundError(key,sub_key)
         else:
            # Create a new registry key
            self.handle = RegCreateKey(key, sub_key)
      
   def __del__(self):
      """ Destructor """
      self.close()
      
   def __checkMode(self, flag):
      """ Check the file mode """
      from string import lower
      self.binary = 0
      
      for i in flag:
         try:
            # Obtain flag
            self.flag = {"r":self.READ,"w":self.WRITE,"a":self.APPEND}[lower(i)]
         except:
            # If binary file selected
            if lower(i) == "b":
               # Set binary file
               self.binary = 1
            else:
               # Invalid mode
               raise InvalidModeError(flag)
         
   def __checkRead(self):
      """ Check if the file can be read """
      if self.flag != self.READ:
         raise BadFileDescriptorError
         
   def __checkWrite(self):
      """ Check if the file can be writed """
      if not self.flag in (self.WRITE,self.APPEND):
         raise BadFileDescriptorError
         
   def close(self):      
      """ Close file and write in registry """
      from win32con import REG_SZ, REG_BINARY
      from win32api import RegSetValueEx
      
      # If the file has been opened correctly
      if self.handle != None and self.data != None:
         # If the file has not been deleted
         if self.deleted == 0:
            # If the file is opened with binary mode
            if self.binary:
               typ = REG_BINARY
            else:
               typ = REG_SZ
            
            # Write data in registry base
            RegSetValueEx (self.handle, "", 0, typ, self.data.getvalue())
         
         # Close file
         result = self.data.close()
         self.data = None
         return result
   
   def delete(self):
      """ Delete the registry file """
      from win32api import RegDeleteKey
      
      # Destroy file
      RegDeleteKey(self.key, self.sub_key)
      self.deleted = 1
      
   def isatty(self):     
      return self.data.isatty()
      
   def seek(self, pos, mode = 0):
      return self.seek(pos, mode)

   def tell(self):
      return self.data.tell()

   def read(self, size = -1):
      self.__checkRead()
      return self.data.read(size)

   def readline(self, length=None):
      self.__checkRead()
      return self.data.readline(length)

   def readlines(self, sizehint = 0):
      self.__checkRead()
      return self.data.readlines(sizehint)

   def truncate(self, size=None):
      self.__checkWrite()
      return self.data.truncate(size)

   def write(self, s):
      self.__checkWrite()
      return self.data.write(s)

   def writelines(self, list):
      self.__checkWrite()
      return self.data.writelines(list)

   def flush(self):
      self.__checkWrite()
      return self.data.flush()
Example #8
0
class RegistryFile:
    """ This class manage a file in a Win32 registry base """
    READ = 1
    WRITE = 2
    APPEND = 3

    def __init__(self, key, sub_key, flag):
        """
Constructor :
   key     : HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
   sub_key : The name of a key that this method opens
   flag    : "r" for read
             "w" for write
             "a" for append
             "b" for binary
      """
        from win32api import RegQueryValueEx, RegCreateKey, RegOpenKeyEx
        from win32con import KEY_ALL_ACCESS
        from StringIO import StringIO

        # Save files informations
        self.key = key
        self.sub_key = sub_key

        # Initialization of internal data
        self.deleted = 0
        self.handle = None
        self.data = None

        # Check mode
        self.__checkMode(flag)

        # Create an empty file
        self.data = StringIO()

        try:
            # Open registry key
            self.handle = RegOpenKeyEx(key, sub_key, 0, KEY_ALL_ACCESS)

            # Read key content
            data = RegQueryValueEx(self.handle, "")[0]

            # If the file is in read mode
            if self.flag == self.READ:
                # Read data
                self.data = StringIO(data)
            # If the file is in append mode
            elif self.flag == self.APPEND:
                self.data.write(data)
        except:  # If the registry key not found
            # If the file must be read
            if self.flag == self.READ:
                # The file is not found
                raise FileNotFoundError(key, sub_key)
            else:
                # Create a new registry key
                self.handle = RegCreateKey(key, sub_key)

    def __del__(self):
        """ Destructor """
        self.close()

    def __checkMode(self, flag):
        """ Check the file mode """
        from string import lower
        self.binary = 0

        for i in flag:
            try:
                # Obtain flag
                self.flag = {
                    "r": self.READ,
                    "w": self.WRITE,
                    "a": self.APPEND
                }[lower(i)]
            except:
                # If binary file selected
                if lower(i) == "b":
                    # Set binary file
                    self.binary = 1
                else:
                    # Invalid mode
                    raise InvalidModeError(flag)

    def __checkRead(self):
        """ Check if the file can be read """
        if self.flag != self.READ:
            raise BadFileDescriptorError

    def __checkWrite(self):
        """ Check if the file can be writed """
        if not self.flag in (self.WRITE, self.APPEND):
            raise BadFileDescriptorError

    def close(self):
        """ Close file and write in registry """
        from win32con import REG_SZ, REG_BINARY
        from win32api import RegSetValueEx

        # If the file has been opened correctly
        if self.handle != None and self.data != None:
            # If the file has not been deleted
            if self.deleted == 0:
                # If the file is opened with binary mode
                if self.binary:
                    typ = REG_BINARY
                else:
                    typ = REG_SZ

                # Write data in registry base
                RegSetValueEx(self.handle, "", 0, typ, self.data.getvalue())

            # Close file
            result = self.data.close()
            self.data = None
            return result

    def delete(self):
        """ Delete the registry file """
        from win32api import RegDeleteKey

        # Destroy file
        RegDeleteKey(self.key, self.sub_key)
        self.deleted = 1

    def isatty(self):
        return self.data.isatty()

    def seek(self, pos, mode=0):
        return self.seek(pos, mode)

    def tell(self):
        return self.data.tell()

    def read(self, size=-1):
        self.__checkRead()
        return self.data.read(size)

    def readline(self, length=None):
        self.__checkRead()
        return self.data.readline(length)

    def readlines(self, sizehint=0):
        self.__checkRead()
        return self.data.readlines(sizehint)

    def truncate(self, size=None):
        self.__checkWrite()
        return self.data.truncate(size)

    def write(self, s):
        self.__checkWrite()
        return self.data.write(s)

    def writelines(self, list):
        self.__checkWrite()
        return self.data.writelines(list)

    def flush(self):
        self.__checkWrite()
        return self.data.flush()
Example #9
0
 def test_color(self):
     tty = StringIO()
     tty.fileno = lambda : 0 # fake whatever fileno
     tty.isatty = lambda : True
     term = Term(stream=tty)
     term.demo()