示例#1
0
 def __getattr__(self, attr):
   k, plural = parseUC_sAttr(attr)
   if k is None or not plural:
     raise AttributeError(attr)
   values = tuple(self.__attrvals(attr))
   if values and not isNode(values[0]):
     return values
   return _Nodes(values)
示例#2
0
 def __getattr__(self, attr):
   k, plural = parseUC_sAttr(attr)
   if k is None:
     return self.__dict__[k]
   if plural:
     if k not in self.__M:
       return ()
     return self.__M[k]
   return the(self.__M[k])
示例#3
0
def dumpNodeAttrs(N, ofp):
    # TODO: use attr_value_to_text() somehow
    ofp.write("# %s\n" % (N, ))
    for attr in sorted(N.keys()):
        k, plural = parseUC_sAttr(attr)
        assert k is not None, "bad attribute: %s" % (attr, )
        assert not plural, "unexpected plural attribute: %s" % (attr, )
        first_value = True
        for value in N[attr]:
            ofp.write('%-15s %s\n' %
                      ((attr if first_value else ''),
                       N.nodedb.totoken(value, node=N, attr=attr)))
            first_value = False
 def __getattr__(self, attr):
   ''' Ad hoc attributes.
       Upper case attributes named "FOO" parse the text and find the (sole) node named "foo".
       Upper case attributes named "FOOs" parse the text and find all the nodes named "foo".
   '''
   k, plural = parseUC_sAttr(attr)
   if k:
     P = self.parsed
     nodes = P.find_all(k.lower())
     if plural:
       return nodes
     return the(nodes)
   raise AttributeError(attr)
示例#5
0
 def __getattr__(self, attr):
   ''' Ad hoc attributes.
       Upper case attributes named "FOO" parse the text and find the (sole) node named "foo".
       Upper case attributes named "FOOs" parse the text and find all the nodes named "foo".
   '''
   k, plural = parseUC_sAttr(attr)
   if k:
     P = self.parsed
     nodes = P.find_all(k.lower())
     if plural:
       return nodes
     return the(nodes)
   # look up method on equivalent Unicode string
   return getattr(unicode(self), attr)
示例#6
0
 def __setattr__(self, attr, value):
   k, plural = parseUC_sAttr(attr)
   if k is None:
     self.__dict__[attr] = value
     return
   if plural:
     if isinstance(type, StringTypes):
       raise ValueError(
           "invalid string %r assigned to plural attribute %r" %
           (value, attr)
       )
     T = tuple(value)
     if len(T) == 0 and not self.keepEmpty:
       if k in self.__M:
         del self.__M[k]
     else:
       self.__M[k] = T
   else:
     self.__M[k] = (value,)
示例#7
0
def loadNodeAttrs(N, ifp, doCreate=False):
    ''' Read input of the form:
        ATTR    value
                value
                ...
      and return a mapping of ATTR->[values...].
      This is used by editNode().
  '''
    new_attrs = {}
    prev_attr = None
    for line in ifp:
        assert line.endswith('\n'), "%s: unexpected EOF" % (str(ifp), )
        line = line.rstrip()
        if len(line) == 0:
            # skip blank lines
            continue
        ch1 = line[0]
        if ch1 == '#':
            # skip comments
            continue
        if ch1.isspace():
            # indented ==> continuation line, get attribute name from previous line
            assert prev_attr is not None, "%s: unexpected indented line" % (
                str(ifp), )
            attr = prev_attr
            value_text = line.lstrip()
        else:
            # split into attribute name and value
            attr, value_text = line.split(None, 1)
            k, plural = parseUC_sAttr(attr)
            assert k, "%s: invalid attribute name \"%s\"" % (str(ifp), attr)
            ks = k + 's'
            assert not k.endswith('_ID'), \
                   "%s: invalid attribute name \"%s\" - FOO_ID forbidden" \
                     % (str(ifp), attr)
            prev_attr = attr

        new_attrs \
          .setdefault(k, []) \
          .extend(text_to_values(value_text, N, k, doCreate=doCreate))

    return new_attrs
示例#8
0
    def default(self, basename):
        # lib.js, lib-css.js
        if basename in ("lib.js", "lib-css.js"):
            cherrypy.response.headers['content-type'] = 'text/javascript'
            with open(os.path.join(os.path.dirname(__file__),
                                   basename)) as jsfp:
                js = jsfp.read()
            return js

        if '.' in basename:
            # TYPEs.{csv,txt,html}
            prefix, suffix = basename.rsplit('.', 1)
            if suffix == 'csv':
                content_type = 'text/csv'
            elif suffix == 'txt':
                content_type = 'text/plain'
            elif suffix == 'html':
                content_type = 'text/html'
            else:
                content_type = None
            k, plural = parseUC_sAttr(prefix)
            if k is not None and plural:
                # TYPEs.{txt,csv}
                if content_type in ('text/csv', 'text/plain'):
                    fp = StringIO()
                    export_csv_wide(fp, sorted(self.nodedb.type(k), by_name))
                    out = fp.getvalue()
                    fp.close()
                    return out
                # TYPEs.html
                if content_type == 'text/html':
                    self._start()
                    self._tokens.append(
                        TABLE_from_Nodes_wide(
                            sorted(self.nodedb.type(k), by_type_then_name),
                            self,
                            leadattrs=self.nodelist_leadattrs))
                    return self._flushtokens()
                raise cherrypy.HTTPError(501, basename)

        raise cherrypy.HTTPError(404, basename)
示例#9
0
 def __delattr__(self, attr):
   k, _ = parseUC_sAttr(attr)
   if k is None:
     del self.__dict__[k]
   else:
     del self.__M[k]
示例#10
0
 def __hasattr__(self, attr):
   k, _ = parseUC_sAttr(attr)
   if k is None:
     return k in self.__dict__
   return k in self.__M