~ubuntu-branches/ubuntu/raring/wxwidgets2.8/raring

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ebmlib/logfile.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: Cody Precord                                                          #
 
3
# Purpose: Log File                                                           #
 
4
# Author: Cody Precord <cprecord@editra.org>                                  #
 
5
# Copyright: (c) 2010 Cody Precord <staff@editra.org>                         #
 
6
# License: wxWindows License                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
Editra Business Model Library: LogFile
 
11
 
 
12
Log file class for managing log files or other transient files that should
 
13
be purged after a given period of time.
 
14
 
 
15
"""
 
16
 
 
17
__author__ = "Cody Precord <cprecord@editra.org>"
 
18
__svnid__ = "$Id: logfile.py 66868 2011-02-09 16:01:49Z CJP $"
 
19
__revision__ = "$Revision: 66868 $"
 
20
 
 
21
__all__ = ['LogFile',]
 
22
 
 
23
#--------------------------------------------------------------------------#
 
24
# Imports
 
25
import os
 
26
import time
 
27
import datetime
 
28
import re
 
29
import tempfile
 
30
 
 
31
#--------------------------------------------------------------------------#
 
32
 
 
33
class LogFile(object):
 
34
    """Log file class"""
 
35
    def __init__(self, prefix, logdir=None):
 
36
        """Create a log file
 
37
        @param prefix: filename prefix
 
38
        @keyword logdir: abs path to log output dir
 
39
        @note: if logdir is None then the system temp directory will be used
 
40
 
 
41
        """
 
42
        super(LogFile, self).__init__()
 
43
 
 
44
        # Attributes
 
45
        self.prefix = prefix
 
46
        self.logdir = logdir
 
47
 
 
48
        # Setup
 
49
        if self.logdir is None:
 
50
            self.logdir = tempfile.gettempdir()
 
51
 
 
52
    #---- Properties ----#
 
53
    LogDirectory = property(lambda self: self.logdir,
 
54
                            lambda self, dname: setattr(self, 'logdir', dname))
 
55
    Prefix = property(lambda self: self.prefix,
 
56
                      lambda self, prefix: setattr(self, 'prefix', prefix))
 
57
 
 
58
    #---- Public Interface ----#
 
59
    def WriteMessage(self, msg):
 
60
        """Append the message to the current log file
 
61
        @param msg: string object
 
62
 
 
63
        """
 
64
        # Files are named as prefix_YYYY_MM_DD.log
 
65
        logstamp = "%d_%d_%d" % time.localtime()[:3]
 
66
        logname = "%s_%s.log" % (self.prefix, logstamp)
 
67
        logpath = os.path.join(self.logdir, logname)
 
68
        if os.path.exists(logpath):
 
69
            opencmd = "ab"
 
70
        else:
 
71
            opencmd = "wb"
 
72
 
 
73
#        with open(logpath, opencmd) as handle:
 
74
#            handle.write(msg.rstrip() + os.linesep)
 
75
        try:
 
76
            handle = open(logpath, opencmd)
 
77
            handle.write(msg.rstrip() + os.linesep)
 
78
            handle.close()
 
79
        except IOError:
 
80
            pass
 
81
 
 
82
    def PurgeOldLogs(self, days):
 
83
        """Purge all log files older than n days
 
84
        @param days: number of days
 
85
 
 
86
        """
 
87
        logpattern = re.compile("%s_[0-9]{4}_[0-9]{1,2}_[0-9]{1,2}.log" % self.prefix)
 
88
        paths = list()
 
89
        cdate = datetime.date(*time.localtime()[:3])
 
90
        for path in os.listdir(self.logdir):
 
91
            if logpattern.match(path):
 
92
                ymd = [int(x) for x in path[len(self.prefix)+1:-4].split('_')]
 
93
                fdate = datetime.date(*ymd)
 
94
                span = cdate - fdate
 
95
                if span.days > days:
 
96
                    fpath = os.path.join(self.logdir, path)
 
97
                    paths.append(fpath)
 
98
 
 
99
        # Attempt to cleanup the old files
 
100
        for log in paths:
 
101
            try:
 
102
                os.remove(log)
 
103
            except OSError:
 
104
                pass