~ubuntu-branches/ubuntu/raring/iso-codes/raring

« back to all changes in this revision

Viewing changes to iso_3166/iso3166tab.py

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2004-08-27 12:15:33 UTC
  • Revision ID: james.westby@ubuntu.com-20040827121533-culsmj9wcnwqxya3
Tags: upstream-0.40
ImportĀ upstreamĀ versionĀ 0.40

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Read iso-codes data file and output a .tab file
 
4
 
5
# Copyright (C) 2004 Alastair McKinstry <mckinstry@debian.org>
 
6
# Released under the GPL.
 
7
# $Id: iso3166tab.py,v 1.1 2004/05/20 21:24:39 mckinstry Exp $
 
8
 
 
9
from xml.sax import saxutils, make_parser, saxlib, saxexts, ContentHandler
 
10
from xml.sax.handler import feature_namespaces
 
11
import sys, os, getopt, urllib2
 
12
 
 
13
class printLines(saxutils.DefaultHandler):
 
14
        def __init__(self, ofile):
 
15
                self.ofile = ofile
 
16
 
 
17
        def startElement(self, name, attrs):
 
18
                if name != 'iso_3166_entry':
 
19
                        return
 
20
                code = attrs.get('alpha_2_code', None)
 
21
                if code == None:
 
22
                        raise RunTimeError, "Bad file"  
 
23
                if type(code) == unicode:
 
24
                        code = code.encode('UTF-8')
 
25
                name = attrs.get('name', None)
 
26
                if name == None:
 
27
                        raise RunTimeError, " BadFile"
 
28
                if type(name) == unicode:
 
29
                        name = name.encode('UTF-8')
 
30
                common_name = attrs.get('common_name', None)
 
31
                if common_name != None:
 
32
                        if type(common_name) == unicode:
 
33
                                name = common_name.encode('UTF-8')
 
34
                        else:
 
35
                                name = common_name
 
36
                self.ofile.write (code + '\t' + name + '\n')
 
37
 
 
38
 
 
39
## 
 
40
## MAIN
 
41
##
 
42
 
 
43
 
 
44
ofile = sys.stdout
 
45
p = make_parser()
 
46
p.setErrorHandler(saxutils.ErrorPrinter())
 
47
try:
 
48
        dh = printLines(ofile)
 
49
        p.setContentHandler(dh)
 
50
        p.parse('iso_3166.xml')
 
51
except IOError,e:
 
52
        print in_sysID+": "+str(e)
 
53
except saxlib.SAXException,e:
 
54
        print str(e)
 
55
 
 
56
ofile.close()