3 from xml
.dom
import minidom
, Node
, XML_NAMESPACE
5 # The 4Suite parser is much faster, if present
6 from Ft
.Xml
.cDomlette
import nonvalParse
7 from Ft
.Xml
.InputSource
import InputSourceFactory
9 isrc
= InputSourceFactory()
10 return nonvalParse(isrc
.fromUri(file))
16 home_mime
= os
.path
.join(os
.environ
['HOME'], '.mime')
17 user_override
= os
.path
.join(home_mime
, 'packages', 'Override.xml')
19 FREE_NS
='http://www.freedesktop.org/standards/shared-mime-info'
22 def __init__(self
, node
):
26 return "<%s>" % self
.node
.localName
31 """Return all the text directly inside this DOM Node."""
32 return ''.join([text
.nodeValue
for text
in node
.childNodes
33 if text
.nodeType
== Node
.TEXT_NODE
])
37 types
[name
] = MIME_Type(name
)
41 def __init__(self
, name
):
42 assert name
not in types
43 self
.media
, self
.subtype
= name
.split('/')
46 def add_from_node(self
, file, node
):
47 self
.sources
.append(Source(file, node
))
49 def get_comment(self
):
50 for src
in self
.sources
:
51 comment
= src
.get_comment()
54 return self
.get_name()
57 return self
.media
+ '/' + self
.subtype
60 if self
.sources
and self
.sources
[-1].file == user_override
:
62 self
.sources
.append(Source(user_override
, None))
64 def get_comments(self
): return self
.merge('get_comments')
65 def get_globs(self
): return self
.merge('get_globs')
66 def get_magic(self
): return self
.merge('get_magic')
67 def get_xml(self
): return self
.merge('get_xml')
68 def get_others(self
): return self
.merge('get_others')
72 for s
in self
.sources
:
73 out
+= getattr(s
, fn
)()
77 def __init__(self
, node
, src
):
82 return "<%s>" % self
.node
.localName
84 def add_subtree(self
, model
, iter):
88 return cmp(str(self
), str(b
))
92 lang
= self
.node
.getAttributeNS(XML_NAMESPACE
, 'lang')
94 lang
= '[' + lang
+ '] '
97 return lang
+ data(self
.node
)
101 return "Match '%s'" % self
.node
.getAttributeNS(None, 'pattern')
105 prio
= self
.node
.getAttributeNS(None, 'priority') or '50'
106 return "Match with priority %s" % prio
108 def add_subtree(self
, model
, parent
):
109 def build(node
, parent
):
110 for c
in node
.childNodes
:
111 if c
.nodeType
!= Node
.ELEMENT_NODE
: continue
112 text
= '%s at %s = %s' % (c
.getAttributeNS(None, 'type'),
113 c
.getAttributeNS(None, 'offset'),
114 c
.getAttributeNS(None, 'value'))
115 mask
= c
.getAttributeNS(None, 'mask')
117 text
+= ' masked with ' + mask
118 iter = model
.append(parent
)
119 model
.set(iter, 0, text
)
121 build(self
.node
, parent
)
123 def __cmp__(self
, b
):
124 ret
= cmp(str(self
), str(b
))
126 return cmp(self
.node
, b
.node
)
130 ns
= self
.node
.getAttributeNS(None, 'namespaceURI')
131 name
= self
.node
.getAttributeNS(None, 'localName')
132 return "<%s> with namespace '%s'" % (name
, ns
)
134 class Others(Field
): pass
137 def __init__(self
, file, node
):
140 newdoc
= minidom
.Document()
141 node
= newdoc
.createElementNS(FREE_NS
, 'mime-type')
149 for c
in node
.childNodes
:
150 if c
.nodeType
!= Node
.ELEMENT_NODE
: continue
151 if c
.namespaceURI
== FREE_NS
:
152 if c
.localName
== 'comment':
153 self
.comments
.append(c
)
154 elif c
.localName
== 'glob':
156 elif c
.localName
== 'magic':
158 elif c
.localName
== 'root-XML':
161 self
.others
.append(c
)
163 self
.others
.append(c
)
165 def get_comment(self
):
166 for field
in self
.comments
:
169 def make(self
, const
, list):
170 return [const(node
, self
) for node
in list]
172 def get_comments(self
): return self
.make(Comment
, self
.comments
)
173 def get_globs(self
): return self
.make(Glob
, self
.globs
)
174 def get_magic(self
): return self
.make(Magic
, self
.magic
)
175 def get_xml(self
): return self
.make(XML
, self
.xml
)
176 def get_others(self
): return self
.make(Others
, self
.others
)
179 for mime_dir
in ['/usr/share/mime', '/usr/local/share/mime', home_mime
]:
180 packages_dir
= os
.path
.join(mime_dir
, 'packages')
181 if not os
.path
.isdir(packages_dir
):
183 for package
in os
.listdir(packages_dir
):
184 if package
== 'Override.xml' and mime_dir
is home_mime
: continue
185 scan_file(os
.path
.join(packages_dir
, package
))
186 scan_file(user_override
)
188 for t
in types
.values():
192 if not file.endswith('.xml'): return
193 if not os
.path
.exists(file):
197 root
= doc
.documentElement
198 for node
in root
.childNodes
:
199 if node
.nodeType
!= Node
.ELEMENT_NODE
:
201 type_name
= node
.getAttributeNS(None, 'type')
203 type = get_type(type_name
)
204 type.add_from_node(file, node
)
206 print "Skipping", node
208 rox
.report_exception()