#!/usr/bin/env python

import logging, argparse, json
from datadog import initialize, api

def loadjson(f):
    with open(f, 'r') as json_file:
        json_str = json_file.read()
        json_obj = json.loads(json_str)
        return json_obj

def authenticate(cred_json):
    initialize(**cred_json)

def import_json(timeboard_json):
    dash_json = timeboard_json['dash']
    title, description, graphs = dash_json['title'], dash_json['description'], dash_json['graphs']
    api.Timeboard.create(title=title, description=description, graphs=graphs)

def export_json(dash_id, f, t):
    dash_json = api.Timeboard.get(dash_id) if t == "t" else api.Screenboard.get(dash_id)
    if dash_json:
        with open(f, 'w') as json_file:
            json.dump(dash_json, json_file, indent=4)

def main():
    logging.basicConfig(level=logging.INFO)
    parser = argparse.ArgumentParser(description=
    """
    A tool for importing datadog dashboards from json files and exporting them to json files.
    """
    )
    parser.add_argument("-c", "--credentials", required=True, help="the json file containing api_key and app_key as dictionary entries")
    parser.add_argument("-i", "--import_file", help="the json file to import dashboard definition from")
    parser.add_argument("-e", "--export_file", help="the json file to export dashboard definition to")
    parser.add_argument("-d", "--dash_id", help="the id of the dashboard to be exported")
    parser.add_argument("-t", "--dash_type", help="the type (t or s) of the dashboard to be exported")
    args = parser.parse_args()

    authenticate(loadjson(args.credentials))

    if args.import_file:
        import_json(loadjson(args.import_file))
    elif args.export_file and args.dash_id:
        export_json(args.dash_id, args.export_file, args.dash_type)

if __name__ == "__main__":
    main()

@@ -312,16 +312,56 @@
 rt_json(
+dash_json, t):%0A    if t == %22t%22:%0A        
 timeboar
@@ -366,22 +366,18 @@
 ard_json
-):%0A  
  
+=
  dash_js
@@ -382,63 +382,267 @@
 json
- = timeboard_json%5B'dash'%5D%0A    title, description, graph
+%5B'dash'%5D%0A        title, description, graphs = timeboard_json%5B'title'%5D, timeboard_json%5B'description'%5D, timeboard_json%5B'graphs'%5D%0A        api.Timeboard.create(title=title, description=description, graphs=graphs)%0A    else:%0A        board_title, description, widget
 s = 
@@ -648,24 +648,30 @@
  dash_json%5B'
+board_
 title'%5D, das
@@ -708,25 +708,30 @@
 on%5B'
-graphs'%5D%0A
+widgets'%5D%0A    
     api.
 Time
@@ -718,36 +718,38 @@
 s'%5D%0A        api.
-Time
+Screen
 board.create(tit
@@ -737,38 +737,50 @@
 eenboard.create(
-title=
+board_title=board_
 title, descripti
@@ -795,28 +795,30 @@
 iption, 
-graphs=graph
+widgets=widget
 s)%0A%0Adef 
@@ -1406,30 +1406,104 @@
 tries%22)%0A    
-parser
+mutex_group = parser.add_mutually_exclusive_group(required=True)%0A    mutex_group
 .add_argumen
@@ -1585,30 +1585,35 @@
  from%22)%0A    
-parser
+mutex_group
 .add_argumen
@@ -1827,16 +1827,49 @@
 h_type%22,
+ choices=%5B't', 's'%5D, default='t',
  help=%22t
@@ -1879,17 +1879,8 @@
 type
- (t or s)
  of 
@@ -1891,32 +1891,44 @@
 dashboard to be 
+imported or 
 exported%22)%0A    a
@@ -1954,16 +1954,109 @@
 args()%0A%0A
+    if args.export_file and not args.dash_id:%0A       parser.print_help()%0A       sys.exit(1)%0A%0A
     auth
@@ -2164,16 +2164,32 @@
 rt_file)
+, args.dash_type
 )%0A    el

Improve arg parsing; add support for screenboard import