Newer
Older
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# mtypes.py ---
#
# Filename: mtypes.py
# Description:
# Author:
# Maintainer:
# Created: Fri Feb 8 11:29:36 2013 (+0530)
# Version:
# Last-Updated: Tue Mar 1 02:52:35 2016 (-0500)
# By: subha
# Update #: 182
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
# Utility to detect the model type in a file
#
#
# Change log:
#
#
#
#
# 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, 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; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth
# Floor, Boston, MA 02110-1301, USA.
#
#
# Code:
from __future__ import print_function
import re
import moose
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## !! This was stolen from
## http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/#id2
import sys
PY3 = sys.version_info[0] >= 3
# A function that takes an integer in the 8-bit range and returns
# a single-character byte object in py3 / a single-character string
# in py2.
#
int2byte = (lambda x: bytes((x,))) if PY3 else chr
_text_characters = (
b''.join(int2byte(i) for i in range(32, 127)) +
b'\n\r\t\f\b')
def istextfile(fileobj, blocksize=512):
""" Uses heuristics to guess whether the given file is text or binary,
by reading a single block of bytes from the file.
If more than 30% of the chars in the block are non-text, or there
are NUL ('\x00') bytes in the block, assume this is a binary file.
- Originally written by Eli Bendersky from the algorithm used
in Perl:
http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/#id2
"""
block = fileobj.read(blocksize)
if b'\x00' in block:
# Files with null bytes are binary
return False
elif not block:
# An empty file is considered a valid text file
return True
# Use translate's 'deletechars' argument to efficiently remove all
# occurrences of _text_characters from the block
nontext = block.translate(None, _text_characters)
return float(len(nontext)) / len(block) <= 0.30
## Eli Bendersky till here !!
##!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def getType(filename, mode='t'):
"""Returns the type of the model in file `filename`. Returns None
if type is not known.
mode: 'b' for binary, 't' for text. Not used currently.
"""
mtype = None
msubtype = None
if mode == 't':
for typename, typefunc in list(typeChecks.items()):
if typefunc(filename):
return typename
return None
def getSubtype(filename, typename):
"""Returns what subtype of the specified `typename` is the model file.
None if could not resolve the subtype.
"""
for subtype in subtypes[typename]:
subtypeFunc = subtypeChecks['%s/%s' % (typename, subtype)]
if subtypeFunc(filename):
return subtype
return ''
# Dictionary of model description types and functions to detect them.
#
# Fri Feb 8 11:07:41 IST 2013 - as of now we only recognize GENESIS .g
# XML .xml extensions
#
# Fri Feb 22 16:29:43 IST 2013 - added .cspace
isCSPACE = lambda x: x.lower().endswith('.cspace')
isGENESIS = lambda x: x.lower().endswith('.g') or x.endswith('.p')
isXML = lambda x: x.lower().endswith('.xml')
isProto = lambda x: x.lower().endswith('.p')
import xml.dom.minidom as md
def isNeuroML(filename):
"""Check if a model is in neuroml format. An xml document is
considered a neuroml if the top level element is either
'networkml', morphml', 'channelml' or 'neuroml'.
"""
doc = md.parse(filename)
for child in doc.childNodes:
print(child.nodeName, child.nodeType == child.ELEMENT_NODE)
if child.nodeType == child.ELEMENT_NODE and \
(child.nodeName == 'networkml' or \
child.nodeName == 'morphml' or \
child.nodeName == 'channelml'or \
child.nodeName == 'neuroml'):
return True
return False
def isSBML(filename):
"""Check model in `filename` is in SBML format."""
doc = md.parse(filename)
for child in doc.childNodes:
if child.nodeType == child.ELEMENT_NODE and \
child.nodeName == 'sbml':
return True
return False
def isKKIT(filename):
"""Check if `filename` is a GENESIS/KINETIKIT file.
"""
pattern = re.compile('include\s+kkit') # KKIT files must have "include kkit" statement somewhere
with open(filename, 'r') as infile:
while True:
sentence = ''
contd = False # Flags if we are inside a multi-line entry
line = infile.readline()
if not line: # End of file
return False
line = line.strip()
# print 'read:', line
if line.find('//') == 0: # skip c++ style comment lines
# print 'c++ comment'
continue
# Skip C style multi-line comments
comment_start = line.find('/*')
if comment_start >= 0:
sentence = line[:comment_start]
# print 'cstart', comment_start, sentence
while comment_start >= 0 and line:
# print '#', line
comment_end = line.find('*/')
if comment_end >= 0:
comment_start = -1;
line = line[comment_end+2:] # keep the rest of the line
break
line = infile.readline()
if line:
line = line.strip()
# Keep extending the sentence with next line if current
# line ends with continuation token '\'
if line:
contd = line.endswith('\\')
while line and contd:
sentence += ' ' + line[:-1]
line = infile.readline()
if line:
line = line.strip()
contd = line.endswith('\\')
# if contd turned false, the last line came out of the
# while loop unprocessed
if line:
sentence += ' ' + line
# print '>', sentence
if re.search(pattern, sentence):
return True
return False
# Mapping model types to functions to check them. These functions
# should take the filename as the single argument and return True or
# False. Define new functions for new model type checks and add them
# here.
typeChecks = {
'cspace': isCSPACE,
'genesis': isGENESIS,
'xml': isXML,
}
# These are "type/subtype": function maps for checking model subtype
# New model types with subtypes should be added here with
# corresponding checker function.
subtypeChecks = {
'genesis/kkit': isKKIT,
'genesis/proto': isProto,
'xml/neuroml': isNeuroML,
'xml/sbml': isSBML,
}
# Mapping types to list of subtypes.
subtypes = {
'cspace': [],
'genesis': ['kkit', 'proto'],
'xml': ['neuroml', 'sbml'],
}
#
# mtypes.py ends here