#!/neo/opt/bin/python
# Copyright (C) 2001 by Neotonic Software Corporation
# All Rights Reserved.
#
# hdfhelp.py
#
#
# A set of classes and methods helpful for using HDF in our stuff...
#

import string
import neo_cgi
import neo_cs
import odb

import UserList


class HdfRow(odb.Row):
    def hdfExport(self,prefix,hdf_dataset,skip_fields = None, translate_dict = None):
	for col_name,value in self.items():
            if skip_fields:
                if col_name in skip_fields:
                    continue
            
	    if (col_name != "value") and (value is not None):
		if type(value) in [ type(0), type(0L) ]:
		    hdf_dataset.setValue(prefix + "." + col_name,"%d" % value)
		else:
                    if translate_dict:
                        for k,v in translate_dict.items():
                            value = string.replace(value,k,v)
		    hdf_dataset.setValue(prefix + "." + col_name,neo_cgi.htmlEscape(value))



class HdfItemList(UserList.UserList):
    def hdfExport(self,prefix,hdf_dataset,*extra,**extranamed):
	n = 0
	for row in self:
	    row.hdfExport("%s.%d" % (prefix,n),hdf_dataset,*extra,**extranamed)
	    n = n + 1


def eval_cs(hdf,a_cs_string):
    cs = neo_cs.CS(hdf)
    try:
      cs.parseStr(a_cs_string)
      return cs.render()
    except:
      return "Error in CS tags: %s" % neo_cgi.htmlEscape(repr(a_cs_string))

# ----------------------------

def test():
    import neo_util
    hdf = neo_util.HDF()
    hdf.setValue("foo","1")
    print eval_cs(hdf,"this should say 1  ===> <?cs var:foo ?>")
    

if __name__ == "__main__":
    test()





