예제 #1
0
class widget(object):
    def setValue(self, name, value):
        if value is None:
            value = ''
        elif not isinstance(value, str):
            value = str(value)
        try:
            self._hdf.setValue(name, value)
        except AttributeError:
            self._hdf = HDF()
            self._hdf.setValue(name, value)
    def render_cs(self, fileType, fileName = None):
        if fileName is None:
            fileName = type(self).__name__
        path = '{0}/templates/{1}.{2}'.format(
                web.ctx.path, fileName, fileType)
        if not isfile(path):
            return None
        cs = CS(self._hdf)
        cs.parseFile(path)
        return cs.render()
    def render(self, fileName = None):
        self.setValue('Script.Name', web.ctx.script_name)
        js = self.render_cs('js', fileName)
        css = self.render_cs('css', fileName)
        html = self.render_cs('html', fileName)
        return (js, css, html)
예제 #2
0
def hdf2dict(hdf_as_str):
    """
    Usage
    >>> json_data = hdf2json('''artists {
    ... 0 {
    ... name = Persuader, The
    ... }
    ... }
    ... title = Stockholm
    ... labels {
    ... 0 {
    ... name = Svek
    ... catno = SK032
    ... }
    ... }''')
    """

    if not isinstance(hdf_as_str, basestring):
        raise ValueError('argument must be a string')

    hdf_obj = HDF()
    hdf_obj.readString(hdf_as_str)

    node = hdf_obj

    def traverse_node(node):
        dict_part = {}
        list_part = []

        while node and node.name().isdigit():
            if node.value():
                list_part.append(node.value())
            else:
                list_part.append(traverse_node(node.child()))
            node = node.next()

        if list_part:
            return list_part

        while node:
            if node.value() is not None and not node.child():
                val = node.value()
                dict_part[node.name()] = val
            else:
                dict_part[node.name()] = traverse_node(node.child())
            node = node.next()

        return dict_part

    return traverse_node(node.child())
예제 #3
0
 def setValue(self, name, value):
     if value is None:
         value = ''
     elif not isinstance(value, str):
         value = str(value)
     try:
         self._hdf.setValue(name, value)
     except AttributeError:
         self._hdf = HDF()
         self._hdf.setValue(name, value)
예제 #4
0
def pyobj_to_hdf(pyobj, hdf=None):

    def make_key(lst):
        return '.'.join([str(i) for i in lst])

    def process_value(value, current_key, hdf):

        if isinstance(value, list):
            for idx, item in enumerate(value):
                process_value(item, current_key + [idx], hdf)
        elif isinstance(value, dict):
            for key, item in value.iteritems():
                process_value(item, current_key + [key], hdf)
        elif isinstance(value, str):
            hdf.setValue(make_key(current_key), value)
        else:
            hdf.setValue(make_key(current_key), str(value))

    if not hdf:
        hdf = HDF()
    process_value(pyobj, [], hdf)
    return hdf
예제 #5
0
def hdfFromKwargs(hdf=None, **kwargs):
    """If given an instance that has toHDF() method that method is invoked to get that object's HDF representation"""
    if not hdf:
        hdf = HDF()
    for key, value in kwargs.iteritems():
        if isinstance(value, dict):
            #print "dict:",value
            for k,v in value.iteritems():
                dkey = "%s.%s"%(key,k)
                #print "k,v,dkey:",k,v,dkey
                args = {dkey:v}
                hdfFromKwargs(hdf=hdf, **args)
        elif isinstance(value, (list, tuple)):
            #print "list:",value
            for i, item in enumerate(value):
                ikey = "%s.%s"%(key,i)
                #print "i,item:",i,item, ikey
                if isinstance(item, (list, tuple)):
                    args = {ikey:item}
                    hdfFromKwargs(hdf=hdf, **args)
                elif isinstance(item, dict):
                    args = {ikey:item}
                    hdfFromKwargs(hdf=hdf, **args)
                elif getattr(item, "HDF_ATTRIBUTES", False):
                    attrs = {}
                    for attr in item.HDF_ATTRIBUTES:
                        attrs[attr] = getattr(item, attr, "")
                    hdfFromKwargs(hdf=hdf, **{ikey:attrs})
                else:
                    hdf.setValue(ikey, str(item))
        elif getattr(value, "HDF_ATTRIBUTES", False):
            attrs = {}
            for attr in value.HDF_ATTRIBUTES:
                attrs[attr] = getattr(value, attr, "")
            hdfFromKwargs(hdf=hdf, **{key:attrs})
        else:
            hdf.setValue(key, str(value))
    #print "HDF:",hdf.dump()
    return hdf