-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetimaporgs.py
99 lines (92 loc) · 4.48 KB
/
getimaporgs.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Returns list of country Indicator Mapping (IMAP) organizations available in the specified
OCL environment. This is determined by the 'datim_moh_object' == True custom attribute of
the org. Orgs typically have an ID in the format 'DATIM-MOH-xx-FYyy', where 'xx' is the
country code (eg. CM, BI, UG) and 'yy' is the fiscal year (eg. 18, 19, 20), though this is
not required by this script. Optional arguments 'period_filter' and 'country_code_filter'
may be either a string or a list and will filter the country list accordingly. For example,
setting period_filter to ['FY18', 'FY19'] will only return IMAP orgs from those fiscal years.
Similarly, setting country_code_filter to ['UG', 'BI', 'UA'] will only return those three
matching country codes.
Example Usage:
- To get list of all IMAP orgs (for all country codes and periods):
python getimaporgs.py --env=staging -t="your-token-here" --format=text
- Filter list of IMAP orgs by period:
python getimaporgs.py --env=staging -t="your-token-here" --format=text --period=FY18,FY19
- Filter list of IMAP orgs by country code:
python getimaporgs.py --env=staging -t="your-token-here" --format=text --country_code=BDI
Arguments:
-h, --help show this help message and exit
-c COUNTRY_CODE, --country_code COUNTRY_CODE
Country code, eg "UG", "BI"
--env ENV Name of the OCL API environment: production, staging,
demo, qa
--envurl ENVURL URL of the OCL API environment
-p PERIOD, --period PERIOD
Period, eg "FY18", "FY19"
-t TOKEN, --token TOKEN
OCL API token
-f FORMAT, --format FORMAT
Format of the export: csv, json, text
-v VERBOSITY, --verbosity VERBOSITY
Verbosity level: 0 (default), 1, or 2
--version show program's version number and exit
"""
import argparse
import json
import common
import iol
# Script argument parser
parser = argparse.ArgumentParser("imap-orgs", description="Export IMAP country list from OCL")
parser.add_argument(
'-c', '--country_code', help='Country code, eg "ETH", "KEN"', required=False, default='')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--env', help='Name of the OCL API environment: production, staging, demo, qa',
type=common.ocl_environment)
group.add_argument('--envurl', help='URL of the OCL API environment')
parser.add_argument(
'-p', '--period', help='Period, eg "FY18", "FY19"', required=False, default='')
parser.add_argument('-t', '--token', help='OCL API token', required=False)
parser.add_argument('-f', '--format', help='Format of the export: TEXT (default), CSV, JSON',
default='text', required=False)
parser.add_argument(
'-v', '--verbosity', help='Verbosity level: 0 (default), 1, or 2', default=0, type=int)
parser.add_argument('--version', action='version', version='%(prog)s v' + common.APP_VERSION)
args = parser.parse_args()
ocl_env_url = args.env if args.env else args.env_url
if args.verbosity > 1:
print(args)
# Prepare filters
period_filter = ''
if args.period:
period_filter = [x.strip() for x in args.period.split(',')]
country_code_filter = ''
if args.country_code:
country_code_filter = [x.strip() for x in args.country_code.split(',')]
# Get the IMAP orgs
ocl_imap_orgs = common.get_imap_orgs(
ocl_env_url=ocl_env_url, ocl_api_token=args.token, verbose=bool(args.verbosity),
period_filter=period_filter, country_code_filter=country_code_filter)
# Display the results
if isinstance(ocl_imap_orgs, list):
output_format = args.format.lower()
if output_format == 'csv':
# DATIM-MOH-TESTER2-FY20: TESTER2 TESTER2 FY20
print(iol.get_as_csv(
ocl_imap_orgs, start_columns=['id', 'name'],
include_columns=['id', 'name', 'attr:datim_moh_period', 'attr:datim_moh_country_code', 'url']))
elif output_format == 'text':
for ocl_org in ocl_imap_orgs:
datim_moh_country_code = None
datim_moh_period = None
if 'extras' in ocl_org and ocl_org['extras']:
datim_moh_country_code = ocl_org['extras'].get('datim_moh_country_code')
datim_moh_period = ocl_org['extras'].get('datim_moh_period')
print('%s: %s %s %s' % (
ocl_org['id'],
ocl_org.get('location', ''),
datim_moh_country_code,
datim_moh_period))
else:
print(json.dumps(ocl_imap_orgs, indent=4))