Exemple #1
0
def open_populate_generic(request, patterns, fb_name, path):
    """
    This function returns a list of JSON objects to generate a dynamic Ajax treeview.
    The argument path is a path to directory structure (see views.py for further details). it 
    is equal to the value of 'openlink' in a JSON branch definition.

    This function is specific to tafelTreeview.

    patterns: file search patterns (comma-separated list)
    fb_name: FileBrowser global variable name
    path: path to data
    """
    if path[0] == '/':
        path = path[1:]
    #
    # The POST request will always contain a branch_id variable when the present function 
    # is called dynamically by the tree issuing the Ajax query
    #
    try:
        nodeId = request.POST['branch_id']
    except:
        # May be usefull for debugging purpose
        return HttpResponse("Path debug: %s, %s, %s" % (path, patternList, fb_name))

    json = []

    patternList = patterns.split(',')
    regSearchPatternList = []
    for pat in patternList:
        regSearchPatternList.append(pat.replace('.', '\.').replace('*', '.*'))

    # Look for data
    data = glob.glob("/%s/*" % str(path))

    dirs = []
    for e in data:
        if os.path.isdir(e):
            dirs.append(e)
        elif os.path.isfile(e):
            for rsp in regSearchPatternList:
                if re.match(rsp, e):
                    # This is a file
                    json.append( {
                        'id'  : os.path.basename(e),
                        'txt' : os.path.basename(e),
                        'img' : 'forward.png'
                    })
                    break

    for d in dirs:
        # Check if this directory contains any file matching searchPattern
        files = []
        for pat in patternList:
            files.extend(glob.glob("%s/%s" % (d, pat)))

        label = os.path.split(d)[1]
        id = "%s_%s" % (str(path), label)
        id = id.replace('/', '_')
        if len(files):
            nodeText = """%s <font class="image_count">(%d)</font>""" % (label, len(files))
        else:
            nodeText = label

        json.append( {
            'id' : id,
            'txt' : nodeText,
            'canhavechildren' : 1,
            'onopenpopulate' : str(fb_name) + '.getResultHandler()',
            'syspath' : "/%s/%s/" % (str(path), label),
            'openlink' : settings.AUP + "/populate_generic/%s/%s/%s/%s/" % (str(patterns), str(fb_name), str(path), label),
            'num_children' : len(files)
        })
    return HttpResponse(str(json), mimetype = 'text/plain')
Exemple #2
0
def open_populate(request, behaviour, tv_name, path):
    """
    DEPRECATED: open_populate_generic instead.

    This function returns a list of JSON objects to generate a dynamic Ajax treeview.
    The argument path is a path to directory structure (see views.py for further details). it 
    is equal to the value of 'openlink' in a JSON branch definition.

    This function is specific to tafelTreeview.
    """

    #
    # The POST request will always contain a branch_id variable when the present function 
    # is called dynamically by the tree issuing the Ajax query
    #
    try:
        nodeId = request.POST['branch_id']
    except:
        # May be usefull for debugging purpose
        return HttpResponse("Path debug: %s, %s, %s" % (path, behaviour, tv_name))

    json = []
    fitsSearchPattern = '*.fits'
    regFitsSearchPattern = '^.*\.fits$'

    if behaviour == 'binary_tables':
        # Patterns change
        fitsSearchPattern = 'mcmd.*.fits'
        regFitsSearchPattern = '^mcmd\..*\.fits$'

    # Look for data
    data = glob.glob("/%s/*" % str(path))

    dirs = []
    for e in data:
        if os.path.isdir(e):
            dirs.append(e)
        elif os.path.isfile(e):
            if re.match(regFitsSearchPattern, e):
                # This is a fits file
                json.append( {
                    'id'  : os.path.basename(e),
                    'txt' : os.path.basename(e),
                    'img' : 'forward.png'
                })


    for d in dirs:
        # Check if this directory contains any FITS file
        fitsFiles = glob.glob("%s/%s" % (d, fitsSearchPattern))

        label = os.path.split(d)[1]
        id = "%s_%s" % (str(path), label)
        id = id.replace('/', '_')
        if len(fitsFiles):
            nodeText = """%s <font class="image_count">(%d)</font>""" % (label, len(fitsFiles))
        else:
            nodeText = label

        json.append( {
            'id' : id,
            'txt' : nodeText,
            'canhavechildren' : 1,
            'onopenpopulate' : str(tv_name) + '.branchPopulate',
            'syspath' : "/%s/%s/" % (str(path), label),
            'openlink' : settings.AUP + "/populate/%s/%s/%s/%s/" % (str(behaviour), str(tv_name), str(path), label),
            'num_fits_children' : len(fitsFiles)
        })
    return HttpResponse(str(json), mimetype = 'text/plain')