]> Softwares of Agnibho - ddstorm.git/blob - index.py
Added documentation
[ddstorm.git] / index.py
1 #! /usr/bin/python3
2
3 '''
4 This module handles the upstream ancestors of symptoms.
5
6 The symptoms can be classified in different levels, with the more
7 generalized symtoms at upstream and the more specialized symptoms at
8 the downstream.
9
10 For example:
11 'Acute peritonitis' is a special type of 'peritonitis'
12 Hence 'peritionitis' will be at upstream and 'acute peritonitis' will be
13 at downstream.
14 '''
15 '''
16 Copyright (c) 2015 Agnibho Mondal
17 All rights reserved
18
19 This file is part of DDStorm.
20
21 DDStorm is free software: you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation, either version 3 of the License, or
24 (at your option) any later version.
25
26 DDStorm is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
30
31 You should have received a copy of the GNU General Public License
32 along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
33 '''
34
35 import sys
36 import os
37 from fnmatch import fnmatch
38
39 from conf import Conf
40 from const import *
41
42 class Index:
43 ''' Provides an index of the upstream ancestors of symptoms '''
44 def __init__(self, conf=False):
45 '''
46 Initiate the index object. Accepts a Conf object as an
47 optional parameter.
48 '''
49 if(conf):
50 self.conf=conf
51 else:
52 self.conf=Conf()
53 self.data={}
54 self.compile()
55
56 def compile(self):
57 '''
58 Compile the text index files to a application usable format.
59 '''
60 # Loop over all files under index_path
61 for path, subdirs, files in os.walk(self.conf.get("index_path")):
62 for name in files:
63 # Only files with *.txt suffix
64 if(fnmatch(name, "*.txt")):
65 with open(self.conf.get("index_path")+name, "r") as f:
66 # Two dimensional list buffer to hold the index
67 buff=[]
68 buff.append([])
69 buff.append([])
70 # Loop over all lines as separate entries
71 for line in f:
72 # Ignore commnents starting with #
73 line=line.rstrip().split("#")[0]
74 if(len(line)==0):
75 pass
76 else:
77 # Find number of leading whitespaces
78 ws=len(line)-len(line.lstrip())
79 # Format the entry
80 line=line.lstrip().capitalize()
81
82 # No leading whitespace means a top level entry i.e. no upstream ancestor
83 if(ws==0):
84 # Empty the buffer and add the entry
85 del buff[0][:]
86 buff[0].append(line.lstrip())
87 # Reset the whitespace index
88 del buff[1][:]
89 buff[1].append(0)
90 # If leading whitespace > indexed whitespace, the entry is a subcategory of previous entry
91 elif(ws>buff[1][-1]):
92 # Append entry to buffer as new item
93 buff[0].append(line.lstrip())
94 # Record leading whitespace to buffer
95 buff[1].append(ws)
96 # Copy buffer to data list
97 self.data[buff[0][-1]]=list(reversed(buff[0]))
98 # If leading whitespace == indexed whitespace, the entry is at the same level with the previous entry
99 elif(ws==buff[1][-1]):
100 # Append entry to last item of buffer
101 buff[0][-1]=line.lstrip()
102 buff[1][-1]=ws
103 # Copy buffer to data list
104 self.data[buff[0][-1]]=list(reversed(buff[0]))
105 # If leading whitespace < indexed whitespace, the entry is at an upper category than the previous entry
106 elif(ws<buff[1][-1]):
107 # Loop over the buffer in reverse
108 for i in reversed(buff[1]):
109 # Discard all lower category items
110 if(i>=ws):
111 buff[0].pop()
112 buff[1].pop()
113 #Append the entry to buffer
114 else:
115 buff[0].append(line.lstrip())
116 buff[1].append(ws)
117 break
118 # Copy buffer to data list
119 self.data[buff[0][-1]]=list(reversed(buff[0]))
120
121 def upstream(self, name):
122 '''
123 Return the upstream list of a symptom
124
125 Parameter:
126 name - the name of the symptom as string
127
128 Return value:
129 List of strings containing the upstream items
130 '''
131 if(len(name)>0):
132 if name in self.data:
133 return self.data[name]
134 else:
135 return []
136
137 def names(self):
138 ''' Return all indexed symptoms name '''
139 return list(self.data.keys())
140
141 def main():
142 '''
143 Prints upstream items of a symptom provided as command line
144 argument.
145 If no argument provided, returns a list of all indexed symptoms.
146 '''
147 i=Index()
148 if(len(sys.argv)>1):
149 print(i.upstream(sys.argv[1]))
150 else:
151 print(i.names())
152
153 if(__name__=="__main__"):
154 main()