]> Softwares of Agnibho - ddstorm.git/blob - index.py
First commit
[ddstorm.git] / index.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 Index:
29 def __init__(self, conf=False):
30 if(conf):
31 self.conf=conf
32 else:
33 self.conf=Conf()
34 self.data={}
35 self.compile()
36
37 def compile(self):
38 for path, subdirs, files in os.walk(self.conf.get("index_path")):
39 for name in files:
40 if(fnmatch(name, "*.txt")):
41 with open(self.conf.get("index_path")+name, "r") as f:
42 buff=[]
43 buff.append([])
44 buff.append([])
45 for line in f:
46 line=line.rstrip().split("#")[0]
47 if(len(line)==0):
48 pass
49 else:
50 ws=len(line)-len(line.lstrip())
51 line=line.lstrip().capitalize()
52 if(ws==0):
53 del buff[0][:]
54 buff[0].append(line.lstrip())
55 del buff[1][:]
56 buff[1].append(0)
57 elif(ws>buff[1][-1]):
58 buff[0].append(line.lstrip())
59 buff[1].append(ws)
60 self.data[buff[0][-1]]=list(reversed(buff[0]))
61 elif(ws==buff[1][-1]):
62 buff[0][-1]=line.lstrip()
63 buff[1][-1]=ws
64 self.data[buff[0][-1]]=list(reversed(buff[0]))
65 elif(ws<buff[1][-1]):
66 for i in reversed(buff[1]):
67 if(i>=ws):
68 buff[0].pop()
69 buff[1].pop()
70 else:
71 buff[0].append(line.lstrip())
72 buff[1].append(ws)
73 break
74 self.data[buff[0][-1]]=list(reversed(buff[0]))
75
76 def upstream(self, name):
77 if(len(name)>0):
78 if name in self.data:
79 return self.data[name]
80 else:
81 return []
82
83 def main():
84 i=Index()
85 if(len(sys.argv)>1):
86 print(i.upstream(sys.argv[1]))
87
88 if(__name__=="__main__"):
89 main()