"Fossies" - the Fresh Open Source Software Archive

Member "buku-4.4/bukuserver/filters.py" (15 Jun 2020, 7778 Bytes) of package /linux/privat/buku-4.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "filters.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.2.2_vs_4.3.

    1 from enum import Enum
    2 
    3 from flask_admin.model import filters
    4 
    5 
    6 class BookmarkField(Enum):
    7     ID = 0
    8     URL = 1
    9     TITLE = 2
   10     TAGS = 3
   11     DESCRIPTION = 4
   12 
   13 
   14 def equal_func(query, value, index):
   15     return filter(lambda x: x[index] == value, query)
   16 
   17 
   18 def not_equal_func(query, value, index):
   19     return filter(lambda x: x[index] != value, query)
   20 
   21 
   22 def greater_func(query, value, index):
   23     return filter(lambda x: x[index] > value, query)
   24 
   25 
   26 def smaller_func(query, value, index):
   27     return filter(lambda x: x[index] < value, query)
   28 
   29 
   30 def in_list_func(query, value, index):
   31     return filter(lambda x: x[index] in value, query)
   32 
   33 
   34 def not_in_list_func(query, value, index):
   35     return filter(lambda x: x[index] not in value, query)
   36 
   37 
   38 def top_x_func(query, value, index):
   39     items = sorted(set(x[index] for x in query), reverse=True)
   40     top_x = items[:value]
   41     return filter(lambda x: x[index] in top_x, query)
   42 
   43 
   44 def bottom_x_func(query, value, index):
   45     items = sorted(set(x[index] for x in query), reverse=False)
   46     top_x = items[:value]
   47     return filter(lambda x: x[index] in top_x, query)
   48 
   49 
   50 class FilterType(Enum):
   51 
   52     EQUAL = {'func': equal_func, 'text':'equals'}
   53     NOT_EQUAL = {'func': not_equal_func, 'text':'not equal'}
   54     GREATER = {'func': greater_func, 'text':'greater than'}
   55     SMALLER = {'func': smaller_func, 'text':'smaller than'}
   56     IN_LIST = {'func': in_list_func, 'text':'in list'}
   57     NOT_IN_LIST = {'func': not_in_list_func, 'text':'not in list'}
   58     TOP_X = {'func': top_x_func, 'text': 'top x'}
   59     BOTTOM_X = {'func': bottom_x_func, 'text': 'bottom x'}
   60 
   61 
   62 class BaseFilter(filters.BaseFilter):
   63 
   64     def operation(self):
   65         return getattr(self, 'operation_text')
   66 
   67     def apply(self, query, value):
   68         return getattr(self, 'apply_func')(query, value, getattr(self, 'index'))
   69 
   70 
   71 class TagBaseFilter(BaseFilter):
   72 
   73     def __init__(
   74             self,
   75             name,
   76             operation_text=None,
   77             apply_func=None,
   78             filter_type=None,
   79             options=None,
   80             data_type=None):
   81         if operation_text in ('in list', 'not in list'):
   82             super().__init__(name, options, data_type='select2-tags')
   83         else:
   84             super().__init__(name, options, data_type)
   85         if name == 'name':
   86             self.index = 0
   87         elif name == 'usage_count':
   88             self.index = 1
   89         else:
   90             raise ValueError('name: {}'.format(name))
   91         self.filter_type = None
   92         if filter_type:
   93             self.apply_func = filter_type.value['func']
   94             self.operation_text = filter_type.value['text']
   95             self.filter_type = filter_type
   96         else:
   97             self.apply_func = apply_func
   98             self.operation_text = operation_text
   99 
  100     def clean(self, value):
  101         if (
  102                 self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and
  103                 self.name == 'usage_count'):
  104             value = [int(v.strip()) for v in value.split(',') if v.strip()]
  105         elif self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST):
  106             value = [v.strip() for v in value.split(',') if v.strip()]
  107         elif self.name == 'usage_count':
  108             value = int(value)
  109             if self.filter_type in (FilterType.TOP_X, FilterType.BOTTOM_X) and value < 1:
  110                 raise ValueError
  111         if isinstance(value, str):
  112             return value.strip()
  113         return value
  114 
  115 
  116 class BookmarkBukuFilter(BaseFilter):
  117 
  118     def __init__(self, *args, **kwargs):
  119         self.keys = {
  120             'all_keywords': 'match all',
  121             'deep': 'deep',
  122             'regex': 'regex'
  123         }
  124         for key, value in kwargs.items():
  125             if key in self.keys and value:
  126                 setattr(self, key, value)
  127             else:
  128                 setattr(self, key, False)
  129         list(map(lambda x: kwargs.pop(x), self.keys))
  130         super().__init__('buku', *args, **kwargs)
  131 
  132     def operation(self):
  133         parts = []
  134         for key, value in self.keys.items():
  135             if getattr(self, key):
  136                 parts.append(value)
  137         if not parts:
  138             return 'search'
  139         return 'search ' + ', '.join(parts)
  140 
  141     def apply(self, query, value):
  142         return query
  143 
  144 
  145 class BookmarkBaseFilter(BaseFilter):
  146 
  147     def __init__(
  148             self,
  149             name,
  150             operation_text=None,
  151             apply_func=None,
  152             filter_type=None,
  153             options=None,
  154             data_type=None):
  155         if operation_text in ('in list', 'not in list'):
  156             super().__init__(name, options, data_type='select2-tags')
  157         else:
  158             super().__init__(name, options, data_type)
  159         bm_fields_dict = {x.name.lower(): x.value for x in BookmarkField}
  160         if name in bm_fields_dict:
  161             self.index = bm_fields_dict[name]
  162         else:
  163             raise ValueError('name: {}'.format(name))
  164         self.filter_type = None
  165         if filter_type:
  166             self.apply_func = filter_type.value['func']
  167             self.operation_text = filter_type.value['text']
  168         else:
  169             self.apply_func = apply_func
  170             self.operation_text = operation_text
  171 
  172     def clean(self, value):
  173         if (
  174                 self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST) and
  175                 self.name == BookmarkField.ID.name.lower()):
  176             value = [int(v.strip()) for v in value.split(',') if v.strip()]
  177         elif self.filter_type in (FilterType.IN_LIST, FilterType.NOT_IN_LIST):
  178             value = [v.strip() for v in value.split(',') if v.strip()]
  179         elif self.name == BookmarkField.ID.name.lower():
  180             value = int(value)
  181             if self.filter_type in (FilterType.TOP_X, FilterType.BOTTOM_X) and value < 1:
  182                 raise ValueError
  183         if isinstance(value, str):
  184             return value.strip()
  185         return value
  186 
  187 
  188 class BookmarkTagNumberEqualFilter(BookmarkBaseFilter):
  189 
  190     def __init__(self, *args, **kwargs):
  191         super().__init__(*args, **kwargs)
  192 
  193         def apply_func(query, value, index):
  194             for item in query:
  195                 tags = [tag for tag in item[index].split(',') if tag]
  196                 if len(tags) == value:
  197                     yield item
  198 
  199         self.apply_func = apply_func
  200 
  201     def clean(self, value):
  202         value = int(value)
  203         if value < 0:
  204             raise ValueError
  205         return value
  206 
  207 
  208 class BookmarkTagNumberGreaterFilter(BookmarkTagNumberEqualFilter):
  209 
  210     def __init__(self, *args, **kwargs):
  211         super().__init__(*args, **kwargs)
  212 
  213         def apply_func(query, value, index):
  214             for item in query:
  215                 tags = [tag for tag in item[index].split(',') if tag]
  216                 if len(tags) > value:
  217                     yield item
  218 
  219         self.apply_func = apply_func
  220 
  221 
  222 class BookmarkTagNumberNotEqualFilter(BookmarkTagNumberEqualFilter):
  223 
  224     def __init__(self, *args, **kwargs):
  225         super().__init__(*args, **kwargs)
  226 
  227         def apply_func(query, value, index):
  228             for item in query:
  229                 tags = [tag for tag in item[index].split(',') if tag]
  230                 if len(tags) != value:
  231                     yield item
  232 
  233         self. apply_func = apply_func
  234 
  235 
  236 class BookmarkTagNumberSmallerFilter(BookmarkBaseFilter):
  237 
  238     def __init__(self, *args, **kwargs):
  239         super().__init__(*args, **kwargs)
  240 
  241         def apply_func(query, value, index):
  242             for item in query:
  243                 tags = [tag for tag in item[index].split(',') if tag]
  244                 if len(tags) < value:
  245                     yield item
  246 
  247         self.apply_func = apply_func
  248 
  249     def clean(self, value):
  250         value = int(value)
  251         if value < 1:
  252             raise ValueError
  253         return value