aboutsummaryrefslogtreecommitdiff
path: root/xboomx/bin/xboomx_sort.py
blob: 8ca9ea805d9377b8380dee1f02b3d2e3fe11c7ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import fileinput
from xboomx.sqlitemgr import get_session, PathItem
from xboomx.config import config

def main():
    session = get_session()
    dbitems = session.query(PathItem).all()

    memitems = {}
    for i in dbitems:
        memitems[i.name] = i.count

    # read lines and set weight according to db
    items = []
    for input_item in fileinput.input([]):
        input_item = input_item.strip('\n')

        try:

            count = memitems[input_item]
            items.append((count, input_item))
        except KeyError:
            items.append((0, input_item))

    # sort items
    items.sort(key=lambda x: x[0], reverse=True)

    # complete commands
    complete_offpath = config.get('complete_offpath', False)
    if complete_offpath:
        for key in memitems:
            # check if any item (from previous queries) is not yet in items
            if not [item[1] for item in items if item[1] == key]:
                items.append((memitems[key], key))

    # print items to be shown on dmenu
    for item in items:
        print(item[1])

    session.close()


if __name__ == '__main__':
    main()