<commit_before># -*- coding: utf-8 -*-
import urllib2
import socket
from opentreewebapputil import (get_opentree_services_method_urls, 
                                fetch_current_TNRS_context_names)

default_view_dict = get_opentree_services_method_urls(request)
default_view_dict['taxonSearchContextNames'] = fetch_current_TNRS_context_names(request)

### required - do no delete
def user(): return dict(form=auth())
def download(): return response.download(request,db)
def call(): return service()
### end requires

def index():
    # interpret incoming URL as a tree view, in this format
    #   http://[hostname]/opentree/[{viewer}/]{domSource}@{nodeID}/{taxon name}
    # some valid examples:
    #   http://opentree.com/opentree/argus/ottol@123456/Homo+sapiens
    #   http://opentree.com/opentree/ottol@123456/Homo+sapiens
    #   http://opentree.com/opentree/ottol@123456
    #
    # TODO: add another optional arg 'viewport', like so
    #   http://opentree.com/opentree/argus/0,23,100,400/ottol@123456/Homo+sapiens

    # modify the normal view dictionary to include location+view hints from the URL
    treeview_dict = default_view_dict.copy()
    treeview_dict['viewer'] = 'argus'
    treeview_dict['domSource'] = ''
    treeview_dict['nodeID'] = ''
    treeview_dict['nodeName'] = ''
    treeview_dict['viewport'] = ''
    treeview_dict['nudgingToLatestSyntheticTree'] = False
    treeview_dict['incomingDomSource'] = 'none'

    # add a flag to determine whether to force the viewer to this node (vs. using the
    # browser's stored state for this URL, or a default starting node)
    treeview_dict['forcedByURL'] = False

    # handle the first arg (path part) found
    if len(request.args) > 0:
        if request.args[0] in ['argus',]:  # TODO: add 'onezoom','phylet', others?
            treeview_dict['viewer'] = request.args[0]
        elif '@' in request.args[0]:
            treeview_dict['domSource'], treeview_dict['nodeID'] = request.args[0].split('@')
        else:
            # first arg is neither a viewer nor a proper node, which is a Bad Thing
            raise HTTP(404)

    if len(request.args) > 1:
        if not treeview_dict['nodeID']:
        #if (not treeview_dict['nodeID']) and '@' in request.args[1]:
            treeview_dict['domSource'], treeview_dict['nodeID'] = request.args[1].split('@')
        else:
            treeview_dict['nodeName'] = request.args[1]

    if len(request.args) > 2:
        if not treeview_dict['nodeName']:
            treeview_dict['nodeName'] = request.args[2]

    # retrieve latest synthetic-tree ID (and its 'life' node ID)
    # TODO: Only refresh this periodically? Or only when needed for initial destination?
    latestSyntheticTreeVersion, startingNodeID = fetch_current_synthetic_tree_ids()
    treeview_dict['draftTreeName'] = latestSyntheticTreeVersion
    treeview_dict['startingNodeID'] = startingNodeID

    # replace any invalid 'domSource' (typically this is "ottol" or a synth-tree version) 
    # with the latest synthetic tree version, and notify the user on the page
    #
    # N.B. that if this is unspecified ('none'), the user requested a shortened
    # URL (e.g. https://tree.opentreeoflife.org/) that resolves to the latest
    # synthetic tree.
    incomingDomSource = treeview_dict.get('domSource', None) or latestSyntheticTreeVersion
    treeview_dict['incomingDomSource'] = incomingDomSource
    if incomingDomSource not in ('ottol', latestSyntheticTreeVersion, ):
        treeview_dict['domSource'] = latestSyntheticTreeVersion
        treeview_dict['nudgingToLatestSyntheticTree'] = True
        # mark this as a redirect to a different resource
        response.status = 303

    # when all is said and done, do we have enough information to force the location?
    if treeview_dict['domSource'] and treeview_dict['nodeID']:
        treeview_dict['forcedByURL'] = True

    treeview_dict['taxonSearchContextNames'] = fetch_current_TNRS_context_names(request)
    return treeview_dict

def error():
    return dict()

def download_subtree():
    id_type = request.args(0)  # 'ottol-id' or 'node-id'
    node_or_ottol_id = request.args(1)
    node_name = request.args(2)
    import cStringIO<commit_msg>Fix handling of synth-tree URLs w/ empty version.<commit_after># -*- coding: utf-8 -*-
import urllib2
import socket
from opentreewebapputil import (get_opentree_services_method_urls, 
                                fetch_current_TNRS_context_names)

default_view_dict = get_opentree_services_method_urls(request)
default_view_dict['taxonSearchContextNames'] = fetch_current_TNRS_context_names(request)

### required - do no delete
def user(): return dict(form=auth())
def download(): return response.download(request,db)
def call(): return service()
### end requires

def index():
    # interpret incoming URL as a tree view, in this format
    #   http://[hostname]/opentree/[{viewer}/]{domSource}@{nodeID}/{taxon name}
    # some valid examples:
    #   http://opentree.com/opentree/argus/ottol@123456/Homo+sapiens
    #   http://opentree.com/opentree/ottol@123456/Homo+sapiens
    #   http://opentree.com/opentree/ottol@123456
    #
    # TODO: add another optional arg 'viewport', like so
    #   http://opentree.com/opentree/argus/0,23,100,400/ottol@123456/Homo+sapiens

    # modify the normal view dictionary to include location+view hints from the URL
    treeview_dict = default_view_dict.copy()
    treeview_dict['viewer'] = 'argus'
    treeview_dict['domSource'] = ''
    treeview_dict['nodeID'] = ''
    treeview_dict['nodeName'] = ''
    treeview_dict['viewport'] = ''
    treeview_dict['nudgingToLatestSyntheticTree'] = False
    treeview_dict['incomingDomSource'] = 'none'

    # add a flag to determine whether to force the viewer to this node (vs. using the
    # browser's stored state for this URL, or a default starting node)
    treeview_dict['forcedByURL'] = False

    # handle the first arg (path part) found
    if len(request.args) > 0:
        if request.args[0] in ['argus',]:  # TODO: add 'onezoom','phylet', others?
            treeview_dict['viewer'] = request.args[0]
        elif '@' in request.args[0]:
            treeview_dict['domSource'], treeview_dict['nodeID'] = request.args[0].split('@')
        else:
            # first arg is neither a viewer nor a proper node, which is a Bad Thing
            raise HTTP(404)

    if len(request.args) > 1:
        if not treeview_dict['nodeID']:
        #if (not treeview_dict['nodeID']) and '@' in request.args[1]:
            treeview_dict['domSource'], treeview_dict['nodeID'] = request.args[1].split('@')
        else:
            treeview_dict['nodeName'] = request.args[1]

    if len(request.args) > 2:
        if not treeview_dict['nodeName']:
            treeview_dict['nodeName'] = request.args[2]

    # retrieve latest synthetic-tree ID (and its 'life' node ID)
    # TODO: Only refresh this periodically? Or only when needed for initial destination?
    latestSyntheticTreeVersion, startingNodeID = fetch_current_synthetic_tree_ids()
    treeview_dict['draftTreeName'] = latestSyntheticTreeVersion
    treeview_dict['startingNodeID'] = startingNodeID

    # replace any invalid 'domSource' (typically this is "ottol" or a synth-tree version) 
    # with the latest synthetic tree version, and notify the user on the page
    #
    # N.B. that if this is unspecified ('none'), the user requested a shortened
    # URL (e.g. https://tree.opentreeoflife.org/) that resolves to the latest
    # synthetic tree.
    incomingDomSource = treeview_dict.get('domSource', None) or latestSyntheticTreeVersion
    treeview_dict['incomingDomSource'] = incomingDomSource
    if incomingDomSource not in ('ottol', latestSyntheticTreeVersion, ):
        treeview_dict['domSource'] = latestSyntheticTreeVersion
        treeview_dict['nudgingToLatestSyntheticTree'] = True
        # mark this as a redirect to a different resource
        response.status = 303

    # when all is said and done, do we have enough information to force the location?
    if incomingDomSource and treeview_dict['nodeID']:
        treeview_dict['forcedByURL'] = True

    treeview_dict['taxonSearchContextNames'] = fetch_current_TNRS_context_names(request)
    return treeview_dict

def error():
    return dict()

def download_subtree():
    id_type = request.args(0)  # 'ottol-id' or 'node-id'
    node_or_ottol_id = request.args(1)
    node_name = request.args(2)
    import cStringIO
{'fraction_non_alphanumeric': 0.11076959451471804, 'fraction_numerical': 0.013003901170351105, 'mean_word_length': 3.6457990115321253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '336549', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2569, 'n_words': 668}