Пример #1
0
  def loadFromPath(self, path, strip='auto', prefix=[], *args, **kwds):
    """
    Load CSV data from the path which is either a file or a whole directory.
    """
    assert(isinstance(path, str))
    assert(isinstance(prefix, list))
    assert(strip == 'auto' or isinstance(strip, int))

    # Automatically strip everything off from up to the last path component if
    # specified otherwise
    if strip == 'auto':
      strip = len(os.path.normpath(path).lstrip("/.").split(os.path.sep))-1
      # Also remove the last component if this is a directory, or if a prefix was
      # specified.
      if os.path.isdir(path) or prefix != []:
        strip = strip+1

    self.logger.debug("CSV datasource strip: %s" % strip)

    # Loop thru csv files found in the dir (or just read in the file if the path
    # points to a single file)
    csvext=(lambda p: re.match(r'.+\.csv$',p.lower()) != None)
    for f in findfiles(path,csvext):
      self.logger.debug("attemting to load CSV file: %s" % f)
      (base, ignored_ext) = f.rsplit(".", 1)
      base = base.lstrip("/.")
      components = prefix + base.split(os.path.sep)[strip:]

      self.logger.debug("CSV datasource path: %s" % str.join(".", components))

      # descend to leaf
      leaf = self.data
      for c in components[0:-1]:
        if not leaf.has_key(c):
          leaf[c] = {}
        leaf = leaf[c]

      # copy rows
      leaf[components[-1]] = []
      for row in csv.DictReader(open(f,"rb"), *args, **kwds):
        newrow = {}
        for (key, val) in row.iteritems():
          parts = key.split(".")
          if len(parts) == 1:
            newrow[key] = val
            continue

          level = newrow
          for part in parts[0:-1]:
            if not level.has_key(part):
              level[part] = {}
            level = level[part]
          level[parts[-1]] = val

        leaf[components[-1]].append(newrow)
      self.logger.debug("successfully loaded CSV file: %s" % f)
Пример #2
0
    def loadFromPath(self, path, strip="auto", prefix=[]):
        """
    Recursively add cheetah and python templates from path.
    """
        # add base path to python environment
        basedir = os.path.normpath(path)
        if os.path.isfile(path):
            basedir = os.path.dirname(path)
        sys.path.append(basedir)
        self.logger.debug("basedir: %s" % basedir)

        # Automatically strip everything off from up to the last path component if
        # specified otherwise
        if strip == "auto":
            strip = len(os.path.normpath(path).lstrip("/.").split(os.path.sep)) - 1
            # Also remove the last component if this is a directory, or if a prefix was
            # specified.
            if os.path.isdir(path) or prefix != []:
                strip = strip + 1

        self.logger.debug("template strip: %d" % strip)
        gotone = False

        # filter for tmpl and py extensions
        for f in findfiles(path, TemplateRegistry.__template_extension):
            # construct key from filename with path seperators replaced by dots and
            # file extension striped
            (base, ext) = f.rsplit(".", 1)
            base = base.lstrip("/.")
            key = str.join(".", prefix + base.split(os.path.sep)[strip:])
            if self.templates.has_key(key):
                raise ConfigError("%s: duplicate template entry for key %s" % (path, key))
            self.logger.debug("derived template key: %s" % key)

            # try to load template class
            self.logger.debug("attempting to load template: %s" % f)
            if ext.lower() == "tmpl":
                tcls = Template.compile(file=f)
            else:
                try:
                    tcls = self.__load_py(f, basepath=basedir)
                except ImportError, err:
                    self.logger.warn("failed to load template: %s" % f)
                    continue

            if not issubclass(tcls, Template):
                self.logger.warn("no template class found in: %s" % f)
                continue

            # store template class into templates dictionary
            self.templates[key] = tcls
            gotone = True
            self.logger.debug("successfully loaded template: %s" % f)