]> Softwares of Agnibho - ddstorm.git/blob - alias.py
First commit
[ddstorm.git] / alias.py
1 #! /usr/bin/python3
2
3 # DDStorm
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 import sys, os
24 from fnmatch import fnmatch
25 from conf import Conf
26 from const import *
27
28 class Alias:
29 def __init__(self, conf=False):
30 self.data={}
31 if(conf):
32 self.conf=conf
33 else:
34 self.conf=Conf()
35 self.compile()
36
37 def compile(self):
38 for path, subdirs, files in os.walk(self.conf.get("alias_path")):
39 for name in files:
40 if(fnmatch(name, "*.txt")):
41 with open(self.conf.get("alias_path")+name, "r") as f:
42 for line in f:
43 line=line.rstrip().split("#")[0]
44 if(len(line)==0):
45 pass
46 else:
47 terms=[]
48 for i in line.split(";"):
49 if(i.strip()):
50 terms.append(i.strip())
51 if(len(terms)==2):
52 self.data[terms[-1]]=terms[0]
53 elif(len(terms)>2):
54 t=terms.pop(0)
55 for i in terms:
56 self.data[i]=t
57
58 def get(self, term):
59 if(term in self.data):
60 return self.data[term]
61 else:
62 return term
63
64 def main():
65 a=Alias()
66 if(len(sys.argv)>1):
67 print(a.get(sys.argv[1]))
68
69 if(__name__=="__main__"):
70 main()