"""
Generic OPAL utilities
"""
from collections import namedtuple
import importlib
import re

import ffs
from ffs.contrib import archive

camelcase_to_underscore = lambda str: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', str).lower().strip('_')

def stringport(module):
    try:
        return importlib.import_module(module)
    except ImportError, e:
        raise ImportError("Could not import module '%s'\
                   (Is it on sys.path? Does it have syntax errors?):\
                    %s" % (module, e))

Tag = namedtuple('Tag', 'name title subtags')

def json_to_csv(episodes):
    """
    Given a list of episodes as JSON, write these to our CSV
    format.
    """
    from opal import models
    target = str(ffs.Path.newfile())

    episode_csv = [['id', 'date of admission', 'discharge date',
                   'name', 'hospital number', 'date of birth',
                   'country of birth', 'ethnicity', 'category',
                   'tags', 'hospital', 'ward', 'bed']]
    for e in episodes:
        historic = models.Tagging.historic_tags_for_episodes([e['id']])[e['id']].keys()
        current = e['location'][0]['tags'].keys()
        current = set(current + historic)
        tags = ";".join(current)
        episode_csv.append([
                str(e['id']),
                str(e['date_of_admission']),
                str(e['discharge_date']),
                e['demographics'][0]['name'],
                e['demographics'][0]['hospital_number'],
                str(e['demographics'][0]['date_of_birth']),
                e['demographics'][0]['country_of_birth'],
                e['demographics'][0]['ethnicity'],
                e['location'][0]['category'],
                tags,
                e['location'][0]['hospital'],
                e['location'][0]['ward'],
                e['location'][0]['bed'],
                ])
    episode_csv = "\n".join([",".join(r) for r in episode_csv])

    with ffs.Path.temp() as tempdir:
        with tempdir:
            zipfile = archive.ZipPath('episodes.zip')
            zipfile/'episodes.csv' << episode_csv
            ffs.mv(zipfile, target)
    return target

@@ -1391,32 +1391,36 @@
                 
+str(
 e%5B'demographics'
@@ -1431,16 +1431,17 @@
 %5B'name'%5D
+)
 ,%0A      
@@ -1442,32 +1442,36 @@
                 
+str(
 e%5B'demographics'
@@ -1493,16 +1493,17 @@
 number'%5D
+)
 ,%0A      
@@ -1564,32 +1564,36 @@
                 
+str(
 e%5B'demographics'
@@ -1616,16 +1616,17 @@
 _birth'%5D
+)
 ,%0A      
@@ -1635,16 +1635,20 @@
         
+str(
 e%5B'demog
@@ -1672,16 +1672,17 @@
 nicity'%5D
+)
 ,%0A      
@@ -1683,32 +1683,36 @@
                 
+str(
 e%5B'location'%5D%5B0%5D
@@ -1723,16 +1723,17 @@
 tegory'%5D
+)
 ,%0A      
@@ -1756,32 +1756,36 @@
                 
+str(
 e%5B'location'%5D%5B0%5D
@@ -1796,16 +1796,17 @@
 spital'%5D
+)
 ,%0A      
@@ -1807,32 +1807,36 @@
                 
+str(
 e%5B'location'%5D%5B0%5D
@@ -1843,16 +1843,17 @@
 %5B'ward'%5D
+)
 ,%0A      
@@ -1862,16 +1862,20 @@
         
+str(
 e%5B'locat
@@ -1889,16 +1889,17 @@
 %5D%5B'bed'%5D
+)
 ,%0A      

Stringify all the csv outputs