]> Softwares of Agnibho - ddstorm.git/blob - alias.py
Added documentation
[ddstorm.git] / alias.py
1 #! /usr/bin/python3
2
3 ''' This module handles the aliases of the symptoms. '''
4 '''
5 Copyright (c) 2015 Agnibho Mondal
6 All rights reserved
7
8 This file is part of DDStorm.
9
10 DDStorm is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 DDStorm is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
22 '''
23
24 import sys
25 import os
26 from fnmatch import fnmatch
27
28 from conf import Conf
29 from const import *
30
31 class Alias:
32 ''' Provides the class to handle symptom aliases '''
33
34 def __init__(self, conf=False):
35 '''
36 Initiates the alias object
37 Accepts a Conf object as parameter
38 '''
39 self.data={}
40 if(conf):
41 self.conf=conf
42 else:
43 self.conf=Conf()
44 self.compile()
45
46 def compile(self):
47 ''' Compile the plaintext index files to a program usable format '''
48 # Loop over the alias files
49 for path, subdirs, files in os.walk(self.conf.get("alias_path")):
50 for name in files:
51 if(fnmatch(name, "*.txt")):
52 # Open the *.txt files
53 with open(self.conf.get("alias_path")+name, "r") as f:
54 for line in f:
55 # Ignore lines starting with #
56 line=line.rstrip().split("#")[0]
57 if(len(line)==0):
58 pass
59 else:
60 terms=[]
61 # Split words separated by ; and add to the terms
62 for i in line.split(";"):
63 if(i.strip()):
64 terms.append(i.strip())
65 # If alias present, add terms to the data
66 if(len(terms)==2):
67 self.data[terms[-1]]=terms[0]
68 elif(len(terms)>2):
69 t=terms.pop(0)
70 for i in terms:
71 self.data[i]=t
72
73 def get(self, term):
74 '''
75 Return the alias of the queried symptom
76
77 Parameter:
78 term - Queried string
79
80 Return value:
81 String containing the alias of the term
82 '''
83 if(term in self.data):
84 return self.data[term]
85 else:
86 return term
87
88 def main():
89 ''' Print the alias of the command line argument '''
90 a=Alias()
91 if(len(sys.argv)>1):
92 print(a.get(sys.argv[1]))
93
94 if(__name__=="__main__"):
95 main()