X-Git-Url: https://code.agnibho.com/repo?p=ddstorm.git;a=blobdiff_plain;f=index.py;fp=index.py;h=f1da7492f8c23d3cbf858bb280651737dfae9888;hp=0000000000000000000000000000000000000000;hb=49346f79fdffaccfa0a65dc0ba412311aa27a668;hpb=1071367015995fb627b2f1f41086727756ccba09
diff --git a/index.py b/index.py
new file mode 100644
index 0000000..f1da749
--- /dev/null
+++ b/index.py
@@ -0,0 +1,89 @@
+#! /usr/bin/python3
+
+# DDStorm
+# -------
+# Copyright (c) 2015 Agnibho Mondal
+# All rights reserved
+
+# This file is part of DDStorm.
+
+# DDStorm is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# DDStorm is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with DDStorm. If not, see .
+
+import sys, os
+from fnmatch import fnmatch
+from conf import Conf
+from const import *
+
+class Index:
+ def __init__(self, conf=False):
+ if(conf):
+ self.conf=conf
+ else:
+ self.conf=Conf()
+ self.data={}
+ self.compile()
+
+ def compile(self):
+ for path, subdirs, files in os.walk(self.conf.get("index_path")):
+ for name in files:
+ if(fnmatch(name, "*.txt")):
+ with open(self.conf.get("index_path")+name, "r") as f:
+ buff=[]
+ buff.append([])
+ buff.append([])
+ for line in f:
+ line=line.rstrip().split("#")[0]
+ if(len(line)==0):
+ pass
+ else:
+ ws=len(line)-len(line.lstrip())
+ line=line.lstrip().capitalize()
+ if(ws==0):
+ del buff[0][:]
+ buff[0].append(line.lstrip())
+ del buff[1][:]
+ buff[1].append(0)
+ elif(ws>buff[1][-1]):
+ buff[0].append(line.lstrip())
+ buff[1].append(ws)
+ self.data[buff[0][-1]]=list(reversed(buff[0]))
+ elif(ws==buff[1][-1]):
+ buff[0][-1]=line.lstrip()
+ buff[1][-1]=ws
+ self.data[buff[0][-1]]=list(reversed(buff[0]))
+ elif(ws=ws):
+ buff[0].pop()
+ buff[1].pop()
+ else:
+ buff[0].append(line.lstrip())
+ buff[1].append(ws)
+ break
+ self.data[buff[0][-1]]=list(reversed(buff[0]))
+
+ def upstream(self, name):
+ if(len(name)>0):
+ if name in self.data:
+ return self.data[name]
+ else:
+ return []
+
+def main():
+ i=Index()
+ if(len(sys.argv)>1):
+ print(i.upstream(sys.argv[1]))
+
+if(__name__=="__main__"):
+ main()