Ϫf;8dZddlZddlZddlmZddlmZddlmZm Z dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZe ezZeezZe e zZ e e zezezezezezezezZ!e dfe dfe dfe dfedfedfed fed!fed"fed#fed$fed%fed&fed'fed(fed)fed*fed+fed,fed-fgZ"d.Z#Gd/d0Z$Gd1d2eZ%ejLZ&gd3Z'y)4a  This module provides support for Twisted to linux inotify API. In order to use this support, simply do the following (and start a reactor at some point):: from twisted.internet import inotify from twisted.python import filepath def notify(ignored, filepath, mask): """ For historical reasons, an opaque handle is passed as first parameter. This object should never be used. @param filepath: FilePath on which the event happened. @param mask: inotify event as hexadecimal masks """ print("event %s on %s" % ( ', '.join(inotify.humanReadableMask(mask)), filepath)) notifier = inotify.INotify() notifier.startReading() notifier.watch(filepath.FilePath("/some/directory"), callbacks=[notify]) notifier.watch(filepath.FilePath(b"/some/directory2"), callbacks=[notify]) Note that in the above example, a L{FilePath} which is a L{bytes} path name or L{str} path name may be used. However, no matter what type of L{FilePath} is passed to this module, internally the L{FilePath} is converted to L{bytes} according to L{sys.getfilesystemencoding}. For any L{FilePath} returned by this module, the caller is responsible for converting from a L{bytes} path name to a L{str} path name. @since: 10.1 N)fdesc)FileDescriptor)_inotifylog @iiii i@iiii i@laccessmodifyattrib close_write close_nowriteopen moved_frommoved_tocreatedelete delete_self move_selfunmountqueue_overflowignoredonly_dir dont_followmask_addis_dirone_shotcTg}tD]\}}||zs |j||S)zh Auxiliary function that converts a hexadecimal mask into a series of human readable flags. )_FLAG_TO_HUMANappend)maskskvs :/usr/lib/python3/dist-packages/twisted/internet/inotify.pyhumanReadableMaskr,qs5 A1 t8 HHQK Hc$eZdZdZeddfdZdZy)_Watcha Watch object that represents a Watch point in the filesystem. The user should let INotify to create these objects @ivar path: The path over which this watch point is monitoring @ivar mask: The events monitored by this watchpoint @ivar autoAdd: Flag that determines whether this watch point should automatically add created subdirectories @ivar callbacks: L{list} of callback functions that will be called when an event occurs on this watch. FNc`|j|_||_||_|g}||_yN) asBytesModepathr'autoAdd callbacks)selfr3r'r4r5s r+__init__z_Watch.__init__s2$$&    I"r-cZ|j}|jD] }||||y)zL Callback function used by L{INotify} to dispatch an event. N)r2r5)r6filepatheventscallbacks r+_notifyz_Watch._notifys1'') -H T8V , -r-)__name__ __module__ __qualname____doc__ IN_WATCH_MASKr7r<r-r+r/r/}s #0$#-r-r/cbeZdZdZeZddZdZdZdZdZ dZ d Z d Z e d dd fd Zd ZdZy)INotifya The INotify file descriptor, it basically does everything related to INotify, from reading to notifying watch points. @ivar _buffer: a L{bytes} containing the data read from the inotify fd. @ivar _watchpoints: a L{dict} that maps from inotify watch ids to watchpoints objects @ivar _watchpaths: a L{dict} that maps from watched paths to the inotify watch ids Nc2tj|||jj|_t j |jt j|jd|_d|_ d|_ i|_ i|_ y)N)reactorrTr-) rr7rinit_fdrsetNonBlocking_setCloseOnExec connected_writeDisconnected_buffer _watchpoints _watchpaths)r6rFs r+r7zINotify.__init__stg6==%%' TXX& dhh' "& r-c|j}|jj|j||}t ||||}||j |<||j |<|S)a~ Private helper that abstracts the use of ctypes. Calls the internal inotify API and checks for any errors after the call. If there's an error L{INotify._addWatch} can raise an INotifyError. If there's no error it proceeds creating a watchpoint and adding a watchpath for inverse lookup of the file descriptor from the path. )r2raddrHr/rNrO)r6r3r'r4r5wdiwps r+ _addWatchzINotify._addWatchsc! ]]  txxt 4T4)4 #"!# r-c|jj|j||jj |}|j j |j y)a Private helper that abstracts the use of ctypes. Calls the internal inotify API to remove an fd from inotify then removes the corresponding watchpoint from the internal mapping together with the file descriptor from the watchpath. N)rremoverHrNpoprOr3)r6rRrSs r+_rmWatchzINotify._rmWatchsJ TXXr*##B' SXX&r-ctj|||jdk\r! tj|jyy#t $r }t j|dYd}~yd}~wwxYw)zR Release the inotify file descriptor and do the necessary cleanup rz'Couldn't close INotify file descriptor.N)rconnectionLostrHoscloseOSErrorrerr)r6reasones r+rZzINotify.connectionLosts` %%dF3 88q= F"  FDEE FsA A1A,,A1c|jS)z Get the underlying file descriptor from this inotify observer. Required by L{abstract.FileDescriptor} subclasses. )rHr6s r+filenozINotify.filenos xxr-cXtj|j|jy)zC Read some data from the observed file descriptors N)r readFromFDrH_doReadrbs r+doReadzINotify.doReads 4<<0r-ch|xj|z c_t|jdk\rttjd|jdd\}}}}|r"|jdd|zj d}nd}|jd|zd|_ |j |}|jj}|r|j|}|j|||jro|tzrf|tzr]|j||jd|j } |j"j%d|j&|j | |t(zr!|j+||j-t|jdk\rsyy#t $rYwxYw)zF Work on the data just read from the file descriptor. r z=LLLLrNTr'r4r5)rMlenstructunpackrstriprNKeyErrorr3r2childr<r4IN_ISDIR IN_CREATEwatchr'r5rF callLater _addChildrenIN_DELETE_SELFrXloseConnection) r6in_rRr'cookiesizenamerSr3new_wds r+rfzINotify._doReadsw  $,,2%%+]]7DLL29: ;    sC C('C(Fc|r=|jD])}|js|j||||d+y|j|}|r|S|tz}|j ||||S)aL Watch the 'mask' events in given path. Can raise C{INotifyError} when there's a problem while adding a directory. @param path: The path needing monitoring @type path: L{FilePath} @param mask: The events that should be watched @type mask: L{int} @param autoAdd: if True automatically add newly created subdirectories @type autoAdd: L{bool} @param callbacks: A list of callbacks that should be called when an event happens in the given path. The callback should accept 3 arguments: (ignored, filepath, mask) @type callbacks: L{list} of callables @param recursive: Also add all the subdirectories in this path @type recursive: L{bool} F) recursiveN)walkrrs _isWatchedrvrT)r6r3r'r4r5rrprRs r+rsz INotify.watchEsv4  Q;;=JJudGY%JP Q&B .(D>>$gyA Ar-c|j}|j|}|t|d|j|y)z Remove the watch point monitoring the given path @param path: The path that should be ignored @type path: L{FilePath} Nz is not watched)r2rrorX)r6r3rRs r+ignorezINotify.ignorersC! __T " :dX_56 6 MM" r-cZ|j}|jj|dS)z Helper function that checks if the path is already monitored and returns its watchdescriptor if so or None otherwise. @param path: The path that should be checked @type path: L{FilePath} N)r2rOget)r6r3s r+rzINotify._isWatcheds+!##D$//r-r1)r=r>r?r@rr7rTrXrZrcrgrfrurArsrrrBr-r+rDrDsV H*( ' F1 )&V ;F'QV+BZ  0r-rD)rDr,rA IN_ACCESS IN_MODIFY IN_ATTRIBIN_CLOSE_NOWRITErIN_OPEN IN_MOVED_FROM IN_MOVED_TOrr IN_DELETErv IN_MOVE_SELF IN_UNMOUNT IN_Q_OVERFLOW IN_IGNORED IN_ONLYDIRIN_DONT_FOLLOW IN_MASK_ADDrq IN_ONESHOTIN_CLOSEIN_MOVED IN_CHANGED)(r@r[rltwisted.internetrtwisted.internet.abstractrtwisted.pythonrrrrrrrrrrrrrrvrrrrrrrrqrrrrrAr%r,r/rD INotifyError__all__rBr-r+rs !H "4(                 , , ; &  "      ]#' fL!*]#;$%]#* x)0  --