#!/usr/bin/env python
# -*- coding: utf-8 -*-
import read_conf
FILTER_TYPE_FILE = 1
FILTER_TYPE_SQL = 2
#View
DONE_VIEW = 0
RCV_VIEW = 1
SEND_VIEW = 2
LSTSTRVIEW = ("done", "recv", "send")
class HylapexFilters(object):
""" Filter object interface
"""
def __init__(self, filter_type, *args, **kw):
"""
"""
super(HylapexFilters, self).__init__()
self._alreadyClose = False
if filter_type == FILTER_TYPE_FILE:
self._StartFilterFile(*args, **kw)
elif filter_type == FILTER_TYPE_SQL:
self._StartFilterSQL(*args, **kw)
else:
raise ValueError
#
# Init
#
#File methods
def _StartFilterFile(self, *args, **kw):
"""
"""
file_path = kw.get("file_path")
self._opt_file = read_conf.conf_parser(file_path,'filters')
if self._opt_file.exist():
def_dict = {"filter_list_send": "","filter_list_done": "",
"filter_list_recv": "",
"case_sensitive": "0"}
self._opt_file.init_vals(def_dict)
#SQL methods
def _StartFilterSQL(self, *args, **kw):
raise NotImplementedError
#
# Return methods
#
#File methods
def GetFiltersOnlyView(self):
""" Return the dict with the data that can view
"""
return self._GetFilterByType("only_view")
def GetFiltersNotView(self):
""" Return the dict with the data that cannot view
"""
return self._GetFilterByType("not_view")
def GetFiltersOnlyViewByCol(self, view, col):
""" Return the dict with the data that can view
"""
return self._GetFilterByType("only_view")[view][col]
def GetFiltersNotViewByCol(self, view, col):
""" Return the dict with the data that cannot view
"""
return self._GetFilterByType("not_view")[view][col]
def _GetFilterByType(self, typ):
all_dict = {}
dRet = {}
for view, list_view in enumerate(LSTSTRVIEW):
lst = self._opt_file.getList("filter_list_" + list_view)
if lst == ['']: lst = []
for col in lst:
curr_f = "%s_%s" % (list_view, col)
if not typ in self._opt_file.options(curr_f):
print typ, self._opt_file.options(curr_f)
print "no option not_view for filter %s at %s" % (col, list_view)
lstView = []
else:
lstView = self._opt_file.getList(typ, curr_f)
dRet[int(col)] = lstView
all_dict[view] = dRet
dRet = {}
return all_dict
def GetCaseSensitive(self):
""" Return if I'm case sensitive
"""
return self._opt_file.getint("case_sensitive")
def Close(self):
if self._alreadyClose: return
self._opt_file.exit()
self._alreadyClose = True
def __del__(self):
if self._alreadyClose: return
self._opt_file.exit()
self._alreadyClose = True
|