inji.filters   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 0
eloc 87
dl 0
loc 190
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
3
# -*- coding: utf-8 -*-
4
5
# https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters
6
# https://www.webforefront.com/django/usebuiltinjinjafilters.html
7
8 1
import ast
9 1
import builtins
10 1
import csv
11 1
import more_itertools
12 1
import os
13 1
import re
14 1
import sys
15 1
import time
16 1
import tr as _tr
17
18 1
from . import utils
19
20
# TODO
21
# Encapsulating filters in this way is great but it does little for their
22
# test coverage (nice trick to fool pycoverage eh!)
23
# Set these up for proper unit (not end-to-end) tests.
24
25 1
filters = dict(
26
27
  append = ( """ Append values to the input list """,
28
    lambda v, *p: list(v) + list(p)
29
  ),
30
31
  cat  = ( """ Concatenate "STDIN" (i.e. -) with contents of files named """,
32
    lambda v, *f: [ (
33
      lambda item, default:
34
        default if item == '-' else utils.load_file(item)
35
    )(item=x, default=v) for x in f ]
36
  ),
37
38
  count = ( """ Return the count of the number of times x is in list """,
39
    lambda l, *x: l.count(*x)
40
  ),
41
42
  env_override = (
43
  """
44
    Allow for a value set in the environment to override a given value
45
    e.g. url | env_override("URL")
46
    """,
47
    lambda v, k: os.getenv(k, v)
48
  ),
49
50
  extend = ( """ Append items to a list and return the list """,
51
    lambda *n: append(*n)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable append does not seem to be defined.
Loading history...
52
  ),
53
54
  format_dict = (
55
  """
56
    Given a dict as value, return a formatted string as specified in format.
57
    e.g. url | urlsplit | format_dict("{scheme}://{hostname}:8080/{path}/")
58
    """,
59
    lambda v, f: f.format(**v)
60
  ),
61
62
  format_list = (
63
  """
64
    Given a list as value, return a formatted string as specified in format.
65
    e.g. url | urlsplit | format_list("{0}://{1}")
66
    """,
67
    lambda v, f: f.format(*v)
68
  ),
69
70
  from_csv = (
71
    """
72
    Split a string delimited by commas and return items in a list.
73
    e.g. (foo,) | from_csv  # note the (foo,) as csv.reader is expecting lines
74
    """,
75
    lambda v: list(csv.reader(v))[0]  # magic number is for single line of text
76
  ),
77
78
  from_literal = ( """ Parse a string using ast.literal_eval() """,
79
    lambda v: ast.literal_eval(v)
80
  ),
81
82
  grep = ( """ Filter in those items matching text """,
83
    lambda v, *p: [ x for m in p for x in v if x == m ]
84
  ),
85
86
  index = ( """ Return the index of item in list """,
87
    lambda l, *x: l.index(*x)
88
  ),
89
90
  insert = ( """ Insert values to the input list at specified index """,
91
      lambda l, i=0, *v: [ *l[:i], *v, *l[i:] ]
92
  ),
93
94
  items = ( """
95
    Select items specified by indexes from the list passed in
96
    e.g. range(1,10) | items(0,2,5,-3,-2)
97
    """,
98
    lambda *n: list(n[0][x] for x in n[1:])
99
  ),
100
101
  keys = (
102
    """
103
    Return the keys of a dict passed in
104
    """,
105
    lambda d: d.keys()
106
  ),
107
108
  pop = ( """ Pop items at index 1 from an input list and return it """,
109
    lambda l, i=-1: l.pop(i)
110
  ),
111
112
  prepend = ( """ Prepend values to the input list """,
113
    lambda l, *v: [ *v, *l ]
114
  ),
115
116
  push = ( """ Append items to a list and return the list """,
117
    lambda *n: append(*n)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable append does not seem to be defined.
Loading history...
118
  ),
119
120
  remove = ( """ Remove first item x from list """,
121
    lambda l, *x: (l if l.remove(*x)==None else l)
122
  ),
123
124
  shift = ( """ Pop item at index 0 from an input list and return it """,
125
    lambda l: l.pop(0)
126
  ),
127
128
  strftime = ( """ For a given date, return a date string in strftime(3) format """,
129
    lambda v, f='%F %T%z', tz='UTC': v.strftime(f),
130
  ),
131
132
  to_set = ( """ Create a set from list """,
133
    lambda l: builtins.set(l)
134
  ),
135
136
  to_date = ( """ For a given string, try and parse the date """,
137
    # NOTE This isn't resilient against leapseconds
138
    # https://stackoverflow.com/questions/1697815/how-do-you-convert-a-time-struct-time-object-into-a-datetime-object#comment31967564_1697838
139
    lambda v, f='%Y-%m-%d %H:%M:%S.%f': datetime( *(time.strptime(v, f)[:6]) )
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable datetime does not seem to be defined.
Loading history...
140
  ),
141
142
  to_url = ( """
143
    Take values from a dict passed in and
144
    return a string formatted in the form of a URL
145
    """,
146
    lambda v, f='https://{hostname}':
147
                  f.format(**v)
148
  ),
149
150
  tr = ( """ Emulate tr(1) """,
151
    lambda s,x,y,m='': _tr.tr(x,y,s,m)
152
  ),
153
154
  uniq = ( """ Remove duplicates items from set keeping order """,
155
    lambda l: more_itertools.unique_everseen(l)
156
  ),
157
158
  unshift = ( """ Prepend items to a list and return the list """,
159
    lambda *n: prepend(*n)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable prepend does not seem to be defined.
Loading history...
160
  ),
161
162
  values = (
163
    """
164
    Return the values of a dict passed in
165
    """,
166
    lambda d: d.values()
167
  ),
168
169
  wrap = ( """
170
    Wrap value with "parantheses" specified in format (default "()")
171
    """,
172
    lambda v, t='()': '{}{}{}'.format(t[0], v, t[1])
173
  ),
174
175
)
176
177 1
filters.update({
178
  # Seems del is specially treated inside dict()
179
  'del' : ( """ Remove item at index x from list """,
180
    lambda l, x: [ *l[:x], *l[(x+1):] ]
181
  ),
182
})
183
184 1
if 'USE_ANSIBLE_SUPPORT' in os.environ.keys():
185 1
  from .ansible import FilterModule
186 1
  filters.update(FilterModule().filters())
187
188 1
for k,v in filters.items():
189
  setattr(sys.modules[__name__], k, v[1])
190