[Pymoul-svn] SF.net SVN: pymoul: [133] pymoul/trunk/src/moul/file
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-02-04 15:41:45
|
Revision: 133 http://pymoul.svn.sourceforge.net/pymoul/?rev=133&view=rev Author: tiran Date: 2007-02-04 07:41:40 -0800 (Sun, 04 Feb 2007) Log Message: ----------- Changed fixed KI image name Enhanced chatlog and plasmalog code for UI integration Added utils module Modified Paths: -------------- pymoul/trunk/src/moul/file/chatlog.py pymoul/trunk/src/moul/file/kiimage.py pymoul/trunk/src/moul/file/plasmalog.py Added Paths: ----------- pymoul/trunk/src/moul/file/utils.py Modified: pymoul/trunk/src/moul/file/chatlog.py =================================================================== --- pymoul/trunk/src/moul/file/chatlog.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/chatlog.py 2007-02-04 15:41:40 UTC (rev 133) @@ -36,9 +36,9 @@ import re from fnmatch import fnmatch from shutil import move -from stat import ST_MTIME from time import localtime +from moul.file.utils import fileModTime from moul.log import getLogger @@ -79,10 +79,6 @@ LOG = getLogger('moul.chat') -def modtime(pathname): - """Modification time - """ - return os.stat(pathname)[ST_MTIME] class ChatlogMover(object): """ @@ -116,38 +112,37 @@ continue LOG.debug("Found chatlog %s" % fpath) fd = open(fpath, 'r') - created = self._findCreated(fd) - if not created: - LOG.warning("Failed to parse chatlog %s" % fpath) - mtime = localtime(modtime(fpath)) + created = self._findCreated(fpath) + mtime = fileModTime(fpath) newname = self._makeFilename(created, mtime) - details = { 'fpath' : fpath, 'filename' : file, 'modtime' : mtime, 'created' : created, 'newname' : newname, - 'newpath' : os.path.join(self._dest, newname), + 'v' : os.path.join(self._dest, newname), } self._logs.append(details) - def _findCreated(self, fd): + def _findCreated(self, fpath): """Parse chatlog to find "started" date """ + fd = open(fpath, 'r') + line = fd.readline() + mo = CHAT_RE.match(line) created = None - while True: - line = fd.readline() - if not line: - break - mo = CHAT_RE.match(line) - if mo: - data = mo.groupdict() - if data['msg'].startswith(CHATLOG_STARTED): - d = mo.groupdict() - created = (None, int(d['M']), int(d['D']), int(d['h']), - int(d['m']), int(d['s']), None, None, None) + if mo: + data = mo.groupdict() + if data['msg'].startswith(CHATLOG_STARTED): + d = mo.groupdict() + created = (None, int(d['M']), int(d['D']), int(d['h']), + int(d['m']), int(d['s']), None, None, None) + if created is None: + LOG.warning("Failed to parse chatlog %s" % fpath) + created = (None, 0, 0, 0, + 0, 0, None, None, None) return created def _makeFilename(self, created, stopped): @@ -162,21 +157,37 @@ } return self._filename % mapping + def moveChatlog(self, details): + """Move a chatlogs from old to new location + """ + old = details['fpath'] + new = details['newpath'] + if os.path.isfile(new): + LOG.warning("%s exists - overwriting" % new) + LOG.info("Move chatlog %s to %s" % (old, new)) + move(old, new) + def moveChatlogs(self): - """Move chatlogs from old to new location and name + """Move all chatlogs """ for details in self._logs: - old = details['fpath'] - new = details['newpath'] - if os.path.isfile(new): - LOG.warning("%s exists - overwriting" % new) - LOG.info("Move chatlog %s to %s" % (old, new)) - move(old, new) + self.moveChatlog(details) - def __call__(self): - self.findLogs() - self.moveChatlogs() + def __len__(self): + """len() suppor + """ + return len(self._logs) + def __iter__(self): + """Iterate over the files to move + + Calls moveChatlog() for each element. This method is used to + update a progress bar in the UI. + """ + for details in self._logs: + yield details['newname'] + self.moveChatlog(details) + class ChatlogDirectoryView(object): """A view of a chat log directory Modified: pymoul/trunk/src/moul/file/kiimage.py =================================================================== --- pymoul/trunk/src/moul/file/kiimage.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/kiimage.py 2007-02-04 15:41:40 UTC (rev 133) @@ -22,15 +22,16 @@ __revision__ = "$Revision$" import os +import re import struct import tempfile from fnmatch import fnmatch -from stat import ST_MTIME -from stat import ST_SIZE +from moul.file.utils import fileModTime +from moul.file.utils import fileSize from moul.log import getLogger - +KINUMBER_RE = re.compile("(\d+)\.jpg$", re.IGNORECASE) JPEG_HEADER = "\377\330\377" LOG = getLogger("moul.kiimage") @@ -41,11 +42,9 @@ def fixedNewer(ki, fixed): """Check file size and mod date to see if the fixed image is newer """ - kistat = os.stat(ki) - fixedstat = os.stat(fixed) - if kistat[ST_SIZE] +4 == fixedstat[ST_SIZE]: + if fileSize(ki) +4 == fileSize(fixed): # ok, KI is 4 bytes larger - if fixed[ST_MTIME] > ki[ST_MTIME]: + if fileModTime(fixed) > fileModTime(ki): # ok, fixed is newer return True else: @@ -230,7 +229,27 @@ if not os.path.isdir(destdir): LOG.info("Creating fixed images directory %s" % destdir) os.mkdir(destdir) - + + @staticmethod + def mkFixedName(path, name, dest): + """Make new new based on the name of the KI image + + @param path: directory where the file lives + @type path: str + @param name: name of the image w/o path + @type name: str + @return: KIimage_moddate_number.ext + """ + fpath = os.path.join(path, name) + date = fileModTime(fpath, format="%Y%m%d_%H%M%S") + _, ext = os.path.splitext(name) # "kiimage0001" , ".jpg" + mo = KINUMBER_RE.search(name.lower()) + if mo is not None: + number = int(mo.group(1)) + else: + number = 0 + return os.path.join(dest, "KIimage_%s_%04i.%s" % (date, number, ext[1:])) + def findFiles(self): """Find files """ @@ -243,8 +262,9 @@ if fnmatch(name, pat)] if matches: ki = os.path.join(root, name) - fixed = os.path.join(self._destdir, name) + fixed = self.mkFixedName(root, name, self._destdir) self._found.append(ki) + # XXX: move checks to copy method! if os.path.isfile(fixed): if fixedNewer(ki, fixed): LOG.debug("File %s exists but was changed." % name) @@ -285,9 +305,7 @@ Calls checkAndCopyFiles() for each element. This method is used to update a progress bar in the UI. - - Yields a NULL seperated string containing "kiname\x00fixedname" """ for kiname, fixedname in self._tocheck: - yield fixedname # "%s"x00%s" % (kiname, fixedname) + yield kiname self.checkAndCopyFile(kiname, fixedname) Modified: pymoul/trunk/src/moul/file/plasmalog.py =================================================================== --- pymoul/trunk/src/moul/file/plasmalog.py 2007-02-03 21:56:56 UTC (rev 132) +++ pymoul/trunk/src/moul/file/plasmalog.py 2007-02-04 15:41:40 UTC (rev 133) @@ -28,6 +28,7 @@ from stat import ST_MTIME from moul.crypt.elf import decryptElf +from moul.file.utils import fileModTime from moul.log import getLogger @@ -44,6 +45,9 @@ def __init__(self, srcdir, destdir): self._srcdir = srcdir self._destdir = destdir + self._doremove = True + self._dozip = True + LOG.debug("PlasmalogZipper: %s -> %s" % (srcdir, destdir)) self._zipped_dirs = {} # dirname -> zipfile self._not_removed = [] # files with full path @@ -59,13 +63,16 @@ LOG.info("Creating chatlog directory %s" % destdir) os.mkdir(destdir) - @staticmethod - def getTimeStamp(path): - """Get time stamp yyyymmdd_hhmm based in the modification time + def setZip(self, boolean): + """Set ZIP flag """ - sec = os.stat(path)[ST_MTIME] - return time.strftime("%Y%m%d_%H%M", time.localtime(sec)) + self._dozip = bool(boolean) + def setRemoe(self, boolean): + """Set remoe flag + """ + self._doremove = bool(boolean) + def isPlasmaLogDir(self, path=None): """Check if a path is a valid plasma log directory @@ -77,7 +84,7 @@ pl = os.path.join(path, PLASMA_LOG) if not os.path.isfile(pl): return False - return self.getTimeStamp(pl) + return fileModTime(pl, format="%Y%m%d_%H%M") def zipLogDir(self): """Zip all log files @@ -133,6 +140,17 @@ else: return True - def __call__(self): - self.zipLogDir() - self.removeLogs() + def __len__(self): + """TODO: implementation + """ + return 1 + + def __iter__(self): + """TODO: implementation + """ + for logdir in ("dummy",): + yield logdir + if self._dozip: + self.zipLogDir() + if self._doremove: + self.removeLogs() Added: pymoul/trunk/src/moul/file/utils.py =================================================================== --- pymoul/trunk/src/moul/file/utils.py (rev 0) +++ pymoul/trunk/src/moul/file/utils.py 2007-02-04 15:41:40 UTC (rev 133) @@ -0,0 +1,52 @@ +# pyMoul - Python interface to Myst Online URU Live +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> + +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 59 +# Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""misc utils functions +""" +from __future__ import absolute_import + +__author__ = "Christian Heimes" +__version__ = "$Id: kiimage.py 129 2007-02-03 18:41:23Z tiran $" +__revision__ = "$Revision: 129 $" + +import os +import time +from stat import ST_MTIME +from stat import ST_SIZE + +def fileModTime(path, format=None): + """Get modification time of file + + @param path: path to file or directory + @type path: string + @param format: strftime string ("%Y%m%d_%H%M") + @type format: str or None for time struct + @return: str or time struct + """ + sec = os.stat(path)[ST_MTIME] + if not format: + return time.localtime(sec) + else: + return time.strftime(format, time.localtime(sec)) + +def fileSize(path): + """Get size of file + + @param path: path to file + @type path: string + """ + return os.stat(path)[ST_SIZE] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |