diff options
Diffstat (limited to 'writeparse.py')
-rwxr-xr-x | writeparse.py | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/writeparse.py b/writeparse.py new file mode 100755 index 0000000..d3cf1d9 --- /dev/null +++ b/writeparse.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python +#Copyright (c) 2012, Walter Bender + +# 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 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this library; if not, write to the Free Software +# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA + +# Simple parser of Write backups + +import os +import glob +import time + + +DIROFINTEREST = 'datastore-current' + + +class ParseMain(): + + def __init__(self): + self._dsdict = {} + + abspath = os.path.abspath('.') + for path in glob.glob(os.path.join(abspath, '*')): + if isdsdir(path): + self._dsdict[os.path.basename(path)] = [] + dsobjdirs = glob.glob( + os.path.join(path, DIROFINTEREST, '??')) + for dsobjdir in dsobjdirs: + dsobjs = glob.glob(os.path.join(dsobjdir, '*')) + for dsobj in dsobjs: + if not isactivity(dsobj) == 'AbiWordActivity': + continue + self._dsdict[os.path.basename(path)].append({}) + activity = isactivity(dsobj) + if not activity: + self._dsdict[os.path.basename(path)][-1][ + 'activity'] = 'media object' + else: + self._dsdict[os.path.basename(path)][-1][ + 'activity'] = activity + if activity == 'AbiWordActivity': + score = hasmenuitems(dsobj) + if score: + self._dsdict[os.path.basename(path)][-1][ + 'score'] = score + mime_type = hascomponent(dsobj, 'mime_type') + if mime_type: + self._dsdict[os.path.basename(path)][-1][ + 'mime_type'] = mime_type + mtime = hascomponent(dsobj, 'mtime') + if mtime: + self._dsdict[os.path.basename(path)][-1][ + 'mtime'] = mtime + creation_time = hascomponent(dsobj, 'creation_time') + if creation_time: + self._dsdict[os.path.basename(path)][-1][ + 'creation_time'] = \ + time.strftime('%Y-%m-%dT%H:%M:%S', + time.gmtime(float(creation_time))) + activity_count = hascomponent(dsobj, 'activity count') + if activity_count: + self._dsdict[os.path.basename(path)][-1][ + 'activity count'] = activity_count + + for k, v in self._dsdict.iteritems(): + for i in v: + line = '%s, ' % (k) + if 'activity' in i: + line += '%s, ' % (i['activity']) + else: + line += ', ' + if 'mime_type' in i: + line += '%s, ' % (i['mime_type']) + else: + line += ', ' + if 'activity count' in i: + line += '%s, ' % (i['activity count']) + else: + line += ', ' + if 'creation_time' in i: + line += '%s, ' % (i['creation_time']) + else: + line += ', ' + if 'mtime' in i: + line += '%s, ' % (i['mtime']) + else: + line += ', ' + if 'score' in i: + line += '%d, ' % (i['score']) + else: + line += ', ' + print line + + +def hascomponent(path, component): + ''' Return metadata attribute, if any ''' + if not os.path.exists(os.path.join(path, 'metadata')): + return False + if not os.path.exists(os.path.join(path, 'metadata', component)): + return False + fd = open(os.path.join(path, 'metadata', component)) + data = fd.readline() + fd.close() + if len(data) == 0: + return False + return data + + +WRITEMENUS = {'image': 0, 'table': 1, 'font-style': 2, 'font-color': 2, + 'font': 2, 'fontsize': 2, 'bold': 2, 'italic': 2, 'underline': 2, + 'Heading': 4, 'List': 4, 'style-name': 2, 'Text': 4, + 'left-align': 3, 'right-align': 3, 'center-align': 3, + 'justify-align': 3} +WRITEPOINTS = [15, 15, 20, 10, 10] + + +def hasmenuitems(path): + ''' Parse Write data and generate score based on rubric ''' + + if not os.path.exists(os.path.join(path, 'metadata', 'menu items')): + return None + fd = open(os.path.join(path, 'metadata', 'menu items')) + menuitems = [] + for line in fd: + tokens = line.split(' ') + if len(tokens) > 0: + for token in tokens: + menuitems.append(token) + + scores = [] + for i in range(len(WRITEMENUS)): + scores.append(0) + + for i in menuitems: + if i in WRITEMENUS: + scores[WRITEMENUS[i]] = WRITEPOINTS[WRITEMENUS[i]] + + score = 0 + for i in scores: + score += i + + return score + + +def isactivity(path): + ''' Return activity name ''' + activity = hascomponent(path, 'activity') + if not activity: + return False + else: + return activity.split('.')[-1] + + +def isdsdir(path): + ''' Only interested if it is a datastore directory ''' + if not os.path.isdir(path): + return False + if not os.path.exists(os.path.join(path, DIROFINTEREST)): + return False + return True + + +if __name__ == '__main__': + ParseMain() |