Пример #1
0
class RootLogger(object):
    '''global logger

    Instantiate in a packages __init__ for global use
    from the __init__log snippet
    '''
    _LOGGER_NAME = 'root'
    USER_TEMP_FOLDER = _normpath(_tempfile.gettempdir())

    def __init__(self, logpath, sizeKB=50, nrbaks=3):
        '''(str)
        full log file path
        e.g. C:\temp\app.log
        '''
        self._logpath = logpath
        self._sizeKB = sizeKB
        self._nrbaks = nrbaks
        self._load()

    def __str__(self):
        info = _os.stat(self._logpath)
        kb = lambda x: '{:n}'.format(x / 1024) + ' Kb'
        s = ('Logging to file: %s\n'
             'Max Size: %s\n'
             'Current Size: %s\n'
             'Remaining: %s' %
             (self._logpath, kb(self._sizeKB), kb(
                 info.st_size), kb(self._sizeKB - info.st_size)))
        return s

    def _load(self):
        # logger settings
        log_format = "%(asctime)s [%(levelname)s]: %(filename)s(%(funcName)s:%(lineno)s) >> %(message)s"
        log_filemode = "w"  # w: overwrite; a: append

        _logging.basicConfig(filename=self._logpath,
                             format=log_format,
                             filemode=log_filemode,
                             level=_logging.DEBUG)
        rotate_file = _handlers.RotatingFileHandler(self._logpath,
                                                    maxBytes=self._sizeKB,
                                                    backupCount=self._nrbaks)
        logger = _logging.getLogger(self._LOGGER_NAME)
        logger.addHandler(rotate_file)

        #print log messages to console
        consoleHandler = _logging.StreamHandler()
        logFormatter = _logging.Formatter(log_format)
        consoleHandler.setFormatter(logFormatter)
        logger.addHandler(consoleHandler)
        logger.propagate
        self.logger = logger
Пример #2
0
def getWinCommonDocs():
    """Convenience function to return the windows common docs folder.
    Same source as above.

    @return: common docs folder, or raise OSError.
    @rtype: str
    """

    buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
    hresult = ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_COMMON_DOCUMENTS, 0, SHGFP_TYPE_CURRENT, buf)
    if hresult != 0:
        raise OSError("Failed to find common Documents folder")

    return _normpath(buf.value)
Пример #3
0
    def __init__(
        self,
        path,
        mode='r',
        bufsize=-1,
    ):
        """Setup DistFile object"""

        # Make sure we don't run into trouble if server doesn't recognize
        # that /test//file/ is equivalent to /test/file

        path = _normpath(path)

        # Some of the file attributes inherited from file are
        # read-only (mode, closed, ...) so we must call parent
        # constructor to set them. However, we don't want to
        # create actual local file so we use a dummy.

        ServerFile.__init__(self, _devnull, mode, bufsize)

        self.providers = []

        # semi random session id for now - we should use cert access
        # instead!

        self.session_id = BASE_ID
        self.session_home = BASE_HOME
        self.session_type = None
        self.path = path
        self.bufsize = bufsize
        self.offset = 0
        self.locking = LOCK_UN

        (status, data) = self.__open_remote_file(path, mode)
        if status == 0:
            self.providers = data.split()
        else:

            # TODO: Dist FS always sets exclusive access on open for
            # now

            raise IOError('Distfile: init failed with error %s: %s' %
                          (status, data))
        if mode.startswith('w'):

            # Mimic standard python behaviour of truncating files
            # opened in write mode. Use "r+" to avoid truncating!

            self.__truncate_remote_file(self.providers, path, 0)
Пример #4
0
def getWinUserDocs():
    """C/P from
    http://stackoverflow.com/questions/3858851/python-get-windows-special-folders-for-currently-logged-in-user#3859336

    Convenience function to get current user's documents folder.

    @return: user's docs folder.
    @rtype: str
    """

    buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)

    hresult = ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, buf)
    if hresult != 0:  # SHGetFolderPathW returned error
        raise OSError("Failed to find user's Documents folder")

    return _normpath(buf.value)
Пример #5
0
def walkdirs(rootdir, rpathes=None, excrpathes=[]):
    if rpathes is None: dirs0 = [rootdir]
    else: dirs0 = map(lambda rpath: pathjoin(rootdir, rpath), rpathes)
    dirs0 = filter(lambda dirpath: isdir(dirpath), dirs0)
    # uniform on windows and linux to sort pathes
    dirs0 = map(lambda path: path.replace('\\', '/'), dirs0)
    dirs0.sort()
    # convert path sep as platform
    dirs0 = map(lambda path: _normpath(path), dirs0)
    while dirs0:
        dirs1 = []
        for dirpath in dirs0:
            flist = listdir(dirpath)
            flist.sort()
            for fname in flist:
                fpath = pathjoin(dirpath, fname)
                if relpath(fpath, rootdir) in excrpathes: continue
                elif isdir(fpath): dirs1.append(fpath)
                elif isfile(fpath): yield fpath
        dirs0 = dirs1
    yield None
Пример #6
0
def walkdirs(rootdir, rpathes = None, excrpathes = []):
    if rpathes is None: dirs0 = [rootdir]
    else: dirs0 = map(lambda rpath: pathjoin(rootdir, rpath), rpathes)
    dirs0 = filter(lambda dirpath: isdir(dirpath), dirs0)
    # uniform on windows and linux to sort pathes
    dirs0 = map(lambda path: path.replace('\\', '/'), dirs0)
    dirs0.sort()
    # convert path sep as platform
    dirs0 = map(lambda path: _normpath(path), dirs0)
    while dirs0:
        dirs1 = []
        for dirpath in dirs0:
            flist = listdir(dirpath)
            flist.sort()
            for fname in flist:
                fpath = pathjoin(dirpath, fname)
                if relpath(fpath, rootdir) in excrpathes: continue
                elif isdir(fpath): dirs1.append(fpath)
                elif isfile(fpath): yield fpath
        dirs0 = dirs1
    yield None
Пример #7
0
def normpath(fpath):
    return _normpath(fpath.replace('\\', '/'))
Пример #8
0
from os import getcwd as _getcwd
from os import getenv as _getenv
from os.path import normpath as _normpath

SUBREDDIT: str = _getenv("SUBREDDIT", "")
WEBHOOK_URL: str = _getenv("WEBHOOK_URL", "")

DB_URI: str = _getenv("DB_URI",
                      "sqlite:///" + (_normpath(_getcwd() + "/app.db")))

FEED_LIMIT: int = int(_getenv("FEED_LIMIT", 100))
REFRESH_TIME: int = int(_getenv("REFRESH_TIME", 60))

POST: bool = _getenv("POST", "true").lower() == "true"
FEED: bool = _getenv("FEED_ONLY", "true").lower() == "true"

DIRTY: int = int(_getenv("DIRTY", "86400"))
Пример #9
0
def normpath(fpath):
    return _normpath(fpath.replace('\\', '/'))