-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_searches.py
71 lines (57 loc) · 2.63 KB
/
manage_searches.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import json
import os
import requests
from pprint import pprint
from lib.logging_utils import create_logger
from lib.search import get_all_searches, delete_saved_search
import lib.constants as constants
def list_searches(session, verbose=False):
all_searches = get_all_searches(session)
if verbose:
logger.debug('Saved searches:\n')
logger.debug(pprint(all_searches))
logger.info('Saved searches:\n{}'.format('\n'.join(['{}: {}'.format(all_searches[s]['name'], s) for s in all_searches])))
def write_searches(session, out_json):
all_searches = get_all_searches(session)
logger.info('Writing saved searches to: {}'.format(out_json))
with open(out_json, 'w') as oj:
json.dump(all_searches, oj)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-sl', '--searches_list', action='store_true',
help='List all saved searches as name: id')
parser.add_argument('-sj', '--searches_json', type=os.path.abspath,
help='Write searches of all searches to file passed. Specify "verbose" for full parameters.')
parser.add_argument('-di', '--delete_search_id', type=str,
help='Delete the passed ID')
parser.add_argument('-dn', '--delete_search_name', type=str,
help='Delete the passed search name.')
parser.add_argument('-v', '--verbose', action='store_true',
help='Set logging to debug. This will print full search parameters.')
parser.add_argument('-dr', '--dryrun', action='store_true',
help='Run commands without executing.')
args = parser.parse_args()
seraches_list = args.searches_list
searches_json = args.searches_json
delete_search_id = args.delete_search_id
delete_search_name = args.delete_search_name
verbose = args.verbose
dryrun = args.dryrun
if verbose:
log_lvl = 'DEBUG'
else:
log_lvl = 'INFO'
logger = create_logger(__name__, 'sh', log_lvl)
s = requests.Session()
s.auth = (os.getenv(constants.PL_API_KEY), '')
if seraches_list:
list_searches(session=s, verbose=verbose)
if searches_json:
write_searches(session=s, out_json=searches_json)
if delete_search_name:
logger.info('Deleting search by name: {}'.format(delete_search_name))
delete_saved_search(session=s, search_name=args.delete_search_name, dryrun=dryrun)
if delete_search_id:
logger.info('Deleting search by ID: {}'.format(delete_search_id))
delete_saved_search(session=s, search_id=args.delete_search_id, dryrun=dryrun)