from __future__ import absolute_import

import os
import json
import logging

import sentry


BASE_URL = 'https://docs.getsentry.com/hosted/_platforms/{}'

# Also see INTEGRATION_DOC_FOLDER in setup.py
DOC_FOLDER = os.path.abspath(os.path.join(os.path.dirname(sentry.__file__),
                                          'integration-docs'))


logger = logging.getLogger('sentry')


def dump_doc(path, data):
    fn = os.path.join(DOC_FOLDER, path + '.json')
    directory = os.path.dirname(fn)
    try:
        os.makedirs(directory)
    except OSError:
        pass
    with open(fn, 'wb') as f:
        json.dump(data, f, indent=2)
        f.write('\n')


def load_doc(path):
    if '/' in path:
        return None
    fn = os.path.join(DOC_FOLDER, path + '.json')
    try:
        with open(fn, 'rb') as f:
            return json.load(f)
    except IOError:
        return None


def get_integration_id(platform_id, integration_id):
    if integration_id == '_self':
        return platform_id
    return '{}-{}'.format(platform_id, integration_id)


def sync_docs():
    from requests import Session

    session = Session()

    print 'syncing documentation (platform index)'
    data = session.get(BASE_URL.format('_index.json')).json()
    platform_list = []
    for platform_id, integrations in data['platforms'].iteritems():
        platform_list.append({
            'id': platform_id,
            'name': integrations['_self']['name'],
            'integrations': [
                {
                    'id': get_integration_id(platform_id, i_id),
                    'name': i_data['name'],
                    'type': i_data['type'],
                    'link': i_data['doc_link'],
                } for i_id, i_data in sorted(
                    integrations.iteritems(),
                    key=lambda x: x[1]['name']
                )
            ],
        })

    platform_list.sort(key=lambda x: x['name'])

    dump_doc('_platforms', {'platforms': platform_list})

    for platform_id, platform_data in data['platforms'].iteritems():
        for integration_id, integration in platform_data.iteritems():
            sync_integration_docs(session, platform_id, integration_id,
                                  integration['details'])


def sync_integration_docs(session, platform_id, integration_id, path):
    print '  syncing documentation for %s.%s integration' % (
        platform_id, integration_id)

    data = session.get(BASE_URL.format(path)).json()

    key = get_integration_id(platform_id, integration_id)

    dump_doc(key, {
        'id': key,
        'name': data['name'],
        'html': data['body'],
        'link': data['doc_link'],
    })

@@ -1,12 +1,146 @@
+# NOTE: This is run external to sentry as well as part of the setup%0A# process.  Thus we do not want to import non stdlib things here.%0A
 from __futur
@@ -189,16 +189,30 @@
 rt json%0A
+import urllib%0A
 import l
@@ -1218,67 +1218,8 @@
 ():%0A
-    from requests import Session%0A%0A    session = Session()%0A%0A
     
@@ -1272,35 +1272,48 @@
 %0A    data = 
-session.get
+json.load(urllib.urlopen
 (BASE_URL.fo
@@ -1332,22 +1332,16 @@
 .json'))
-.json(
 )%0A    pl
@@ -2247,33 +2247,24 @@
 ration_docs(
-session, 
 platform_id,
@@ -2370,17 +2370,8 @@
 ocs(
-session, 
 plat
@@ -2517,19 +2517,32 @@
 a = 
-session.get
+json.load(urllib.urlopen
 (BAS
@@ -2564,14 +2564,8 @@
 th))
-.json(
 )%0A%0A 

Use urllib for fetching in integrationdocs