source: trunk/src/testing/bin/fileServer/misc/mmpython/image/bins.py @ 4

Revision 4, 4.5 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1#if 0 /*
2# -----------------------------------------------------------------------
3# bins.py - Python interface to bins data files.
4# -----------------------------------------------------------------------
5# $Id: bins.py,v 1.2 2004/05/04 22:02:59 dischi Exp $
6#
7# -----------------------------------------------------------------------
8# $Log: bins.py,v $
9# Revision 1.2  2004/05/04 22:02:59  dischi
10# fix crash with empty exif tag
11#
12# Revision 1.1  2003/06/08 19:55:22  dischi
13# added bins metadata support
14#
15#
16# -----------------------------------------------------------------------
17# This file Copyright (C) 2002 John Cooper
18#
19# This program is free software; you can redistribute it and/or modify
20# it under the terms of the GNU General Public License as published by
21# the Free Software Foundation; either version 2 of the License, or
22# (at your option) any later version.
23#
24# This program is distributed in the hope that it will be useful, but
25# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
26# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
27# Public License for more details.
28#
29# You should have received a copy of the GNU General Public License along
30# with this program; if not, write to the Free Software Foundation, Inc.,
31# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32#
33# ----------------------------------------------------------------------- */
34#endif
35
36
37from xml.sax import make_parser, ContentHandler
38from xml.sax.handler import feature_namespaces
39import string
40import os
41import re
42
43
44def normalize_whitespace(text):
45    # Remove Redundant whitespace from a string
46    return ' '.join(text.split())
47
48RE_TEXT = re.compile("^[ \n\t]*(.*[^ \n\t])[ \n\t]*$").match
49
50# remove redundant whitespaces/tabs/newlines at the beginning and the end
51def normalize_text(text):
52    m = RE_TEXT(text)
53    if m:
54        return m.group(1)
55    return text
56
57
58def format_text(text):
59    while len(text) and text[0] in (' ', '\t', '\n'):
60        text = text[1:]
61    text = re.sub('\n[\t *]', ' ', text)
62    while len(text) and text[-1] in (' ', '\t', '\n'):
63        text = text[:-1]
64    return text
65
66
67class BinsDiscription(ContentHandler):
68    """
69    This is a handler for getting the information from a bins Album.
70    """
71    def __init__(self):
72        self.desc = {}
73        self.exif = {}
74        self.inDisc = 0
75        self.inField = 0
76        self.inExif = 0
77        self.inTag = 0
78
79    def startElement(self,name,attrs):
80        # Check that we  have a discription section
81        if name == u'description':
82            self.inDisc = 1
83        if name == u'field':
84            self.thisField = normalize_whitespace(attrs.get('name', ''))
85            self.inField = 1
86            self.desc[self.thisField] = ''
87        if name == u'exif':
88            self.inExif = 1
89        if name == u'tag':
90            self.inTag = 1
91            self.thisTag = normalize_whitespace(attrs.get('name', ''))
92            self.exif[self.thisTag] = ''
93
94
95    def characters(self,ch):
96        if self.inDisc:
97            if self.inField:
98                self.desc[self.thisField] = self.desc[self.thisField] + ch
99        if self.inExif:
100            if self.inTag:
101                self.exif[self.thisTag] = self.exif[self.thisTag] + ch
102
103 
104    def endElement(self,name):
105        if name == 'discription':
106            self.inDisc = 0
107        if name == 'field':
108            self.desc[self.thisField] = normalize_text(self.desc[self.thisField])
109            self.inField = 0
110        if name == 'exif':
111            try:
112                self.exif[self.thisTag] = normalize_text(self.exif[self.thisTag])
113            except:
114                pass
115            self.inExif = 0
116               
117        if name == 'tag':
118            self.inTag = 0
119
120def get_bins_desc(binsname):
121     parser = make_parser()
122     parser.setFeature(feature_namespaces,0)
123     dh = BinsDiscription()
124     parser.setContentHandler(dh)
125     # check that the xml file exists for a dir or image
126     if os.path.isfile(binsname + '/album.xml'):
127         binsname = binsname + '/album.xml'
128     elif os.path.isfile(binsname + '.xml'):
129         binsname = binsname + '.xml'
130     else:
131         dh.desc['title'] == os.path.basename(dirname)
132
133     # Check that there is a title
134     parser.parse(binsname)
135
136     # remove whitespace at the beginning
137     for d in dh.desc:
138         dh.desc[d] = format_text(dh.desc[d])
139     for d in dh.exif:
140         dh.exif[d] = format_text(dh.exif[d])
141
142     return {'desc':dh.desc , 'exif':dh.exif}
143
144
145if __name__ == '__main__':
146    parser = make_parser()
147    parser.setFeature(feature_namespaces,0)
148    dh = GetAlbum()
149    parser.setContentHandler(dh)
150    parser.parse('album.xml')
151    print dh.desc
Note: See TracBrowser for help on using the repository browser.