# Path: phylocommons/get_treestore.py
# def get_treestore():
#     return Treestore(**settings.TREESTORE_KWARGS)
# 
# def tree_id_from_uri(uri):
#     return Treestore.id_from_uri(uri, base_uri=settings.TREE_URI)
# 
# def uri_from_tree_id(tree_id):
#     return Treestore.uri_from_id(tree_id, base_uri=settings.TREE_URI)


from django.shortcuts import render_to_response, redirect
from django.http import HttpResponse
from django.core.context_processors import csrf
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template import RequestContext
from phylocommons.get_treestore import get_treestore, tree_id_from_uri, uri_from_tree_id
from forms import SearchForm
from cStringIO import StringIO
from phylocommons import settings
from django.forms.forms import NON_FIELD_ERRORS
from django.forms.util import ErrorList
from django.http import Http404
import Bio.Phylo as bp
import urllib
            names = treestore.get_names(tree_uri)
            spp_list = ','.join(names)
            return download_plaintext(request, spp_list)

        trees = treestore.get_trees(tree_uri=tree_uri)
        response = HttpResponse(mimetype='text/plain')
        if format == 'ascii':
            bp._utils.draw_ascii(trees[0], file=response)
        else: 
            bp.write(trees, response, format)

        return download_plaintext(request, response=response)


def download_plaintext(request, text=None, attachment=None, response=None):
    if not response: response = HttpResponse(mimetype='text/plain')
    if attachment:
        response['Content-Disposition'] = 'attachment; filename=%s.%s.csv' % (site, taxon)

    if text: response.write(text)

    return response
    
    
def svgview(request, tree_id, tree_src=None):
    with get_treestore() as treestore:
        if not tree_src: tree_src = '/trees/%s/download?format=newick' % tree_id
        
        return render_to_response(
            'svgview.html',

# Path: phylocommons/get_treestore.py
# def get_treestore():
#     return Treestore(**settings.TREESTORE_KWARGS)
# 
# def tree_id_from_uri(uri):
#     return Treestore.id_from_uri(uri, base_uri=settings.TREE_URI)
# 
# def uri_from_tree_id(tree_id):
#     return Treestore.uri_from_id(tree_id, base_uri=settings.TREE_URI)


# Path: treeview/views.py
from django.shortcuts import render_to_response, redirect
from django.http import HttpResponse
from django.core.context_processors import csrf
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.template import RequestContext
from phylocommons.get_treestore import get_treestore, tree_id_from_uri, uri_from_tree_id
from forms import SearchForm
from cStringIO import StringIO
from phylocommons import settings
from django.forms.forms import NON_FIELD_ERRORS
from django.forms.util import ErrorList
from django.http import Http404
import Bio.Phylo as bp
import urllib

            names = treestore.get_names(tree_uri)
            spp_list = ','.join(names)
            return download_plaintext(request, spp_list)

        trees = treestore.get_trees(tree_uri=tree_uri)
        response = HttpResponse(mimetype='text/plain')
        if format == 'ascii':
            bp._utils.draw_ascii(trees[0], file=response)
        else: 
            bp.write(trees, response, format)

        return download_plaintext(request, response=response)


def download_plaintext(request, text=None, attachment=None, response=None):
    if not response: response = HttpResponse(mimetype='text/plain')
    if attachment:
        response['Content-Disposition'] = 'attachment; filename=%s.%s.csv' % (site, taxon)

    if text: response.write(text)

    return response
    
    
def svgview(request, tree_id, tree_src=None):
    with get_treestore() as treestore:
        if not tree_src: tree_src = '/trees/%s/download?format=newick' % tree_id
        
        return render_to_response(
            'svgview.html',

            {'tree_src': tree_src, 'tree_id': tree_id, 'tree_uri': uri_from_tree_id(tree_id)},