-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParseKIDS.py
114 lines (106 loc) · 4.43 KB
/
ParseKIDS.py
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
#!/usr/bin/env python
# Parse KIDS files (.KID)
#
#
#---------------------------------------------------------------------------
# Copyright 2011 The Open Source Electronic Health Record Agent
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#---------------------------------------------------------------------------
import sys
import os
def unpack(kid, routineDir):
# method variables
routineLines = 0
routineName = ""
routine = None
output = sys.stdout
with open(kid, 'r') as f:
for line in f:
# identifier is the line preceding the data (RTN,BLD,KRN,RPC,etc) that provides processing information
# actual data is always on the next line
# Handle Routines Block
if line.startswith('''"RTN"'''):
if '''"RTN")''' in line:
# Next line contains the number of routines in the build
line = f.next()
numberOfRoutines = line
output.write('Number of routines: ' + numberOfRoutines)
line = f.next()
#output.write("line before while: " + line)
# Loop over RTN section
while line.startswith('''"RTN",'''):
identifier = line.split(',')
if len(identifier) == 2:
# Beginning of Routine Header
# "RTN","RoutineName")
# Data (line+1): Unknown^Unknown^ChecksumAfter^ChecksumBefore
line = f.next()
routineHeader = line.strip()
routineName = identifier[1].strip()
routineName = routineName.strip(')\"')
output.write('Routine Header: ' + routineHeader + "\n")
output.write('Routine Name: ' + routineName + "\n")
if routine:
routine.close()
routine = None
routine = open(routineDir + "/" + routineName + ".m", 'w')
if identifier[1].strip(' \n\")') != routineName:
output.write('identifier: ' + identifier[1] + "\n")
output.write('Number of lines in routine: ' + str(routineLines) + "\n")
routineLines = 0
if len(identifier) == 4:
routineLines += 1
# Actual Routine Data
# "RTN","RoutineName",LineNumber,0)
# Data (line+1): Line of M code
line = f.next()
code = line
routine.write(code)
line=f.next()
def checksum(routine):
checksum = 0
lineNumber = 0
characterPosition = 0
savedcharater=""
with open(routine, 'r') as f:
for line in f:
lineNumber += 1
# ignore the second line
if lineNumber == 2:
continue
lengthOfLine = mFind(line," ")
if mExtract(line,lengthOfLine) != ";":
lengthOfLine = line.__len__()-1
elif mExtract(line, 1+lengthOfLine) == ";":
lengthOfLine = line.__len__()-1
else:
lengthOfLine = lengthOfLine - 1
for character in xrange(lengthOfLine):
characterPosition +=1
# normal code
temp = lineNumber+characterPosition
checksum += ord(mExtract(line,character))*temp
characterPosition = 0
sys.stdout.write("Checksum is: "+str(checksum)+"\n")
def mExtract(string,position):
chars = list(string)
return chars[--position]
def mFind(string,substring):
f = string.index(substring)
return f +1
def main():
#unpack(sys.argv[1], sys.argv[2])
checksum(sys.argv[1])
if __name__ == '__main__':
main()