This repository has been archived by the owner on Feb 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathomf-import
executable file
·150 lines (130 loc) · 5.05 KB
/
omf-import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/python
#
# This file is part of the Lampadas Documentation System.
#
# Copyright (c) 2000, 2001, 2002 David Merrill <[email protected]>.
#
# 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
#
from Globals import *
import sys
import os
import fnmatch
from xml.dom.minidom import parse
IGNORE_NODES = ('omf',
'resource',
'subject',
'format',
'person',
'#text')
class Creator:
def __init__(self):
self.firstname = ''
self.lastname = ''
self.email = ''
class OMF:
def __init__(self):
self.title = ''
self.description = ''
self.type = ''
self.filename = ''
self.lang = ''
self.categories = []
self.creators = []
def read_dom(self, dom):
for node in dom.childNodes:
self.handle_node(node)
def handle_node(self, node):
node.nodeName = string.lower(node.nodeName)
if node.nodeName in IGNORE_NODES:
pass
elif node.nodeName=='title':
self.title = self.get_node_text(node)
elif node.nodeName=='description':
self.description = self.get_node_text(node)
elif node.nodeName=='type':
self.type = self.get_node_text(node)
elif node.nodeName=='category':
self.categories = self.categories + [self.get_node_text(node)]
elif node.nodeName=='identifier':
self.filename = self.get_attribute_text(node, 'url')
elif node.nodeName=='language':
self.lang = self.get_attribute_text(node, 'code')
elif node.nodeName=='creator':
creator = Creator()
self.creators = self.creators + [creator]
elif node.nodeName=='firstname' and node.parentNode.nodeName=='person' and node.parentNode.parentNode.nodeName=='creator':
self.creators[-1].firstname = self.get_node_text(node)
elif node.nodeName=='lastname' and node.parentNode.nodeName=='person' and node.parentNode.parentNode.nodeName=='creator':
self.creators[-1].lastname = self.get_node_text(node)
elif node.nodeName=='email' and node.parentNode.nodeName=='person' and node.parentNode.parentNode.nodeName=='creator':
self.creators[-1].lastname = self.get_node_text(node)
else:
print 'ERROR, unrecognized element: %s' % node.nodeName
sys.exit(1)
for child in node.childNodes:
self.handle_node(child)
def get_node_text(self, node):
value = ''
for child in node.childNodes:
if child.nodeType==node.TEXT_NODE:
value = value + child.data
value = trim(value)
return value
def get_attribute_text(self, node, name):
value = ''
if node.hasAttributes:
len = node.attributes.length
for item in range(len):
attribute = node.attributes.item(item)
if attribute.name==name:
value = attribute.value
return value
def print_debug(self):
print 'title: %s' % self.title
print 'type: %s' % self.type
print 'description: %s' % self.description
print 'categories: %s' % self.categories
print 'filename: %s' % self.filename
print 'language: %s' % self.lang
def callback(arg, directory, filenames):
for filename in filenames:
if fnmatch.fnmatch(filename, arg):
print 'Reading omf: %s/%s' % (directory, filename)
omf = OMF()
dom = parse(directory + '/' + filename)
omf.read_dom(dom)
if omf.filename > '':
omf.filename = 'file://' + directory + '/' + omf.filename
omf.print_debug()
# Not all versions of Python do garbage collection
# on circular references, so unlink dom manually.
dom.unlink()
def usage():
print """Usage: gnome-import [OMF-DIR] [FILESPEC}
OMF-DIR is a directory where we start recursively processing
OMF files.
FILESPEC is the glob used to scan files, e.g., *.omf.
Your shell might require you to wrap it in quotes to avoid
glob expansion.
"""
sys.exit()
# Options passed on the command line
if len(sys.argv) <> 3:
usage()
gnome_dir = sys.argv[1]
filespec = sys.argv[2]
# Read in the omf files.
os.path.walk(gnome_dir, callback, filespec)