Пример #1
0
 def testParser(self):
   p2 = Parser({"START_TAG": r"\[\*",
                "END_TAG": r"\*\]",
                "ANYCASE": 1,
                "PRE_CHOMP": 1,
                "V1DOLLAR": 1})
   # Test new/old styles.
   s1 = p2.new_style({"TAG_STYLE": "metatext",
                      "PRE_CHOMP": 0,
                      "POST_CHOMP": 1})
   self.assert_(s1)
   self.assertEquals(r"%%", s1["START_TAG"])
   self.assertEquals(0, s1["PRE_CHOMP"])
   self.assertEquals(1, s1["POST_CHOMP"])
   s2 = p2.old_style()
   self.assert_(s2)
   self.assertEquals(r"\[\*", s2["START_TAG"])
   self.assertEquals(1, s2["PRE_CHOMP"])
   self.assertEquals(0, s2["POST_CHOMP"])
   p3 = Config.parser(
     {"TAG_STYLE": "html", "POST_CHOMP": 1, "ANYCASE": 1, "INTERPOLATE": 1})
   p4 = Config.parser({"ANYCASE": 0})
   tt = (("tt1", Template({"ANYCASE": 1})),
         ("tt2", Template({"PARSER": p2})),
         ("tt3", Template({"PARSER": p3})),
         ("tt4", Template({"PARSER": p4})))
   replace = self._callsign()
   replace["alist"] = ["foo", 0, "bar", 0]
   replace["wintxt"] = "foo\r\n\r\nbar\r\n\r\nbaz"
   self.Expect(DATA, tt, replace)
Пример #2
0
 def testPrefix(self):
     src_prov = Config.provider({"INCLUDE_PATH": "test/src"})
     lib_prov = Config.provider({"INCLUDE_PATH": "test/lib"})
     config = {
         "LOAD_TEMPLATES": [src_prov, lib_prov],
         "PREFIX_MAP": {
             "src": "0",
             "lib": "1",
             "all": "0, 1"
         }
     }
     self.Expect(DATA, config)
Пример #3
0
    def _compile(self, data, compfile=None):
        """Private method called to parse the template text and compile it
    into a runtime form.

    Creates and delegates a template.parser.Parser object to handle
    the compilation, or uses the object passed in PARSER.  On success,
    the compiled template is stored in the 'data' attribute of the
    'data' object and returned.  On error, an exception is raised, or
    None is returned if the TOLERANT flag is set.  The optional
    'compiled' parameter may be passed to specify the name of a
    compiled template file to which the generated Python code should
    be written.  Errors are (for now...) silently ignored, assuming
    that failures to open a file for writing are intentional (e.g
    directory write permission).
    """
        if data is None:
            return None

        text = data.text
        error = None

        if not self.__parser:
            self.__parser = Config.parser(self.__params)

        # discard the template text - we don't need it any more
        # del data.text

        parsedoc = self.__parser.parse(text, data)
        parsedoc["METADATA"].setdefault("name", data.name)
        parsedoc["METADATA"].setdefault("modtime", data.time)
        # write the Python code to the file compfile, if defined
        if compfile:
            basedir = os.path.dirname(compfile)
            if not os.path.isdir(basedir):
                try:
                    os.makedirs(basedir)
                except IOError as e:
                    error = Error("failed to create compiled templates "
                                  "directory: %s (%s)" % (basedir, e))
            if not error:
                try:
                    self.__document.write_python_file(compfile, parsedoc)
                except Exception as e:
                    error = Error("cache failed to write %s: %s" %
                                  (os.path.basename(compfile), e))
            if error is None and data.time is not None:
                if not compfile:
                    raise Error("invalid null filename")
                ctime = int(data.time)
                os.utime(compfile, (ctime, ctime))

        if not error:
            data.data = Document(parsedoc)
            return data

        if self.__tolerant:
            return None
        else:
            raise error
Пример #4
0
 def __init__(self, config=None):
   config = config or {}
   # Prepare a namespace handler for any CONSTANTS definition.
   constants = config.get("CONSTANTS")
   if constants:
     namespace = config.get("CONSTANTS_NAMESPACE", "constants")
     config.setdefault("NAMESPACE", {})[namespace] = (
       Config.constants(constants))
   self.__service = Service(config)
   self.__output = config.get("OUTPUT")
   self.__output_path = config.get("OUTPUT_PATH")
Пример #5
0
 def __init__(self, config=None):
     config = config or {}
     # Prepare a namespace handler for any CONSTANTS definition.
     constants = config.get("CONSTANTS")
     if constants:
         namespace = config.get("CONSTANTS_NAMESPACE", "constants")
         config.setdefault("NAMESPACE",
                           {})[namespace] = (Config.constants(constants))
     self.__service = Service(config)
     self.__output = config.get("OUTPUT")
     self.__output_path = config.get("OUTPUT_PATH")
Пример #6
0
 def testParser(self):
     p2 = Parser({
         "START_TAG": r"\[\*",
         "END_TAG": r"\*\]",
         "ANYCASE": 1,
         "PRE_CHOMP": 1,
         "V1DOLLAR": 1
     })
     # Test new/old styles.
     s1 = p2.new_style({
         "TAG_STYLE": "metatext",
         "PRE_CHOMP": 0,
         "POST_CHOMP": 1
     })
     self.assert_(s1)
     self.assertEquals(r"%%", s1["START_TAG"])
     self.assertEquals(0, s1["PRE_CHOMP"])
     self.assertEquals(1, s1["POST_CHOMP"])
     s2 = p2.old_style()
     self.assert_(s2)
     self.assertEquals(r"\[\*", s2["START_TAG"])
     self.assertEquals(1, s2["PRE_CHOMP"])
     self.assertEquals(0, s2["POST_CHOMP"])
     p3 = Config.parser({
         "TAG_STYLE": "html",
         "POST_CHOMP": 1,
         "ANYCASE": 1,
         "INTERPOLATE": 1
     })
     p4 = Config.parser({"ANYCASE": 0})
     tt = (("tt1", Template({"ANYCASE":
                             1})), ("tt2", Template({"PARSER": p2})),
           ("tt3", Template({"PARSER":
                             p3})), ("tt4", Template({"PARSER": p4})))
     replace = self._callsign()
     replace["alist"] = ["foo", 0, "bar", 0]
     replace["wintxt"] = "foo\r\n\r\nbar\r\n\r\nbaz"
     self.Expect(DATA, tt, replace)
Пример #7
0
  def _compile(self, data, compfile=None):
    """Private method called to parse the template text and compile it
    into a runtime form.

    Creates and delegates a template.parser.Parser object to handle
    the compilation, or uses the object passed in PARSER.  On success,
    the compiled template is stored in the 'data' attribute of the
    'data' object and returned.  On error, an exception is raised, or
    None is returned if the TOLERANT flag is set.  The optional
    'compiled' parameter may be passed to specify the name of a
    compiled template file to which the generated Python code should
    be written.  Errors are (for now...) silently ignored, assuming
    that failures to open a file for writing are intentional (e.g
    directory write permission).
    """
    if data is None:
      return None

    text = data.text
    error = None

    if not self.__parser:
      self.__parser = Config.parser(self.__params)

    # discard the template text - we don't need it any more
    # del data.text

    parsedoc = self.__parser.parse(text, data)
    parsedoc["METADATA"].setdefault("name", data.name)
    parsedoc["METADATA"].setdefault("modtime", data.time)
    # write the Python code to the file compfile, if defined
    if compfile:
      basedir = os.path.dirname(compfile)
      if not os.path.isdir(basedir):
        try:
          os.makedirs(basedir)
        except IOError, e:
          error = Error("failed to create compiled templates "
                        "directory: %s (%s)" % (basedir, e))
      if not error:
        try:
          self.__document.write_python_file(compfile, parsedoc)
        except Exception, e:
          error = Error("cache failed to write %s: %s" % (
            os.path.basename(compfile), e))
Пример #8
0
    def __init__(self, config=None):
        config = config or {}
        delim = config.get("DELIMITER", ":")

        # coerce PRE_PROCESS, PROCESS, and POST_PROCESS to lists if necessary,
        # by splitting on non-word characters
        self.__preprocess = Split(config.get("PRE_PROCESS"), delim)
        self.__process = Split(config.get("PROCESS"), delim)
        self.__postprocess = Split(config.get("POST_PROCESS"), delim)
        self.__wrapper = Split(config.get("WRAPPER"), delim)

        # unset PROCESS option unless explicitly specified in config
        if config.get("PROCESS") is None:
            self.__process = None

        self.__error = config.get("ERROR") or config.get("ERRORS")
        self.__autoreset = config.get("AUTO_RESET") is None or \
            config.get("AUTO_RESET")
        self.__debug = config.get("DEBUG", 0) & DEBUG_SERVICE
        self.__context = config.get("CONTEXT") or Config.context(config)
        if not self.__context:
            raise TemplateException()
Пример #9
0
    def testProvider(self):
        dir = "test/src"
        lib = "test/lib"
        file = "foo"
        relfile = os.path.join(".", dir, file)
        absfile = os.path.join(os.path.abspath(dir), file)
        newfile = os.path.join(dir, "foobar")

        def update_file(*args):
            time.sleep(2)  # ensure file time stamps are different
            f = open(newfile, "w")
            for arg in args:
                f.write(str(arg))
            f.close()

        vars = {
            "file": file,
            "relfile": relfile,
            "absfile": absfile,
            "fixfile": update_file
        }

        update_file("This is the old content")

        #------------------------------------------------------------------------
        # instantiate a bunch of providers, using various different techniques,
        # with different load options but sharing the same parser;  then set them
        # to work fetching some files and check they respond as expected
        #------------------------------------------------------------------------

        parser = Config.parser({"POST_CHOMP": 1})
        provinc = Config.provider({
            "INCLUDE_PATH": dir,
            "PARSER": parser,
            "TOLERANT": 1
        })
        provabs = Config.provider({"ABSOLUTE": 1, "PARSER": parser})
        provrel = Config.provider({"RELATIVE": 1, "PARSER": parser})
        self.assertTrue(provinc.parser() is provabs.parser())
        self.assertTrue(provabs.parser() is provrel.parser())

        self.assertTrue(delivered(provinc, file))
        self.assertTrue(declined(provinc, absfile))
        self.assertTrue(declined(provinc, relfile))
        self.assertTrue(declined(provabs, file))
        self.assertTrue(delivered(provabs, absfile))
        self.assertTrue(denied(provabs, relfile))
        self.assertTrue(declined(provrel, file))
        self.assertTrue(denied(provrel, absfile))
        self.assertTrue(delivered(provrel, relfile))

        # Test if can fetch from a file handle.
        ttfile = Template()
        path = os.path.join(os.path.abspath(dir), "baz")
        file = open(path)
        outstr = ttfile.process(file, {"a": "filetest"})
        file.close()
        self.assertEqual("This is the baz file, a: filetest\n", outstr)

        #------------------------------------------------------------------------
        # now we'll fold those providers up into some Template objects that
        # we can pass to text_expect() to do some template driven testing
        #------------------------------------------------------------------------

        ttinc = Template({"LOAD_TEMPLATES": [provinc]})
        ttabs = Template({"LOAD_TEMPLATES": [provabs]})
        ttrel = Template({"LOAD_TEMPLATES": [provrel]})

        def dpaths():
            return [os.path.join(lib, x) for x in ["one", "two"]]

        def badpaths():
            return [badpaths]

        ttd1 = Template({"INCLUDE_PATH": [dpaths, dir], "PARSER": parser})
        ttd2 = Template({
            "INCLUDE_PATH": [
                DynamicPaths(os.path.join(lib, "two"),
                             os.path.join(lib, "one")), dir
            ],
            "PARSER":
            parser
        })
        ttd3 = Template({"INCLUDE_PATH": [badpaths], "PARSER": parser})
        uselist = (("ttinc", ttinc), ("ttabs", ttabs), ("ttrel", ttrel),
                   ("ttd1", ttd1), ("ttd2", ttd2), ("ttdbad", ttd3))
        self.Expect(DATA, uselist, vars)
Пример #10
0
 def Create(expr):
   expr = unscalar(expr)
   if isinstance(expr, Iterator):
     return expr
   else:
     return Config.iterator(expr)
Пример #11
0
 def __init__(self, config):
     self.__stash = Config.stash(config)
Пример #12
0
 def Create(expr):
   expr = unscalar(expr)
   if isinstance(expr, Iterator):
     return expr
   else:
     return Config.iterator(expr)
Пример #13
0
 def __init__(self, config):
   self.__stash = Config.stash(config)
Пример #14
0
  def testProvider(self):
    dir = "test/src"
    lib = "test/lib"
    file = "foo"
    relfile = os.path.join(".", dir, file)
    absfile = os.path.join(os.path.abspath(dir), file)
    newfile = os.path.join(dir, "foobar")

    def update_file(*args):
      time.sleep(2)  # ensure file time stamps are different
      f = open(newfile, "w")
      for arg in args:
        f.write(str(arg))
      f.close()

    vars = { "file": file,
             "relfile": relfile,
             "absfile": absfile,
             "fixfile": update_file }

    update_file("This is the old content")

    #------------------------------------------------------------------------
    # instantiate a bunch of providers, using various different techniques,
    # with different load options but sharing the same parser;  then set them
    # to work fetching some files and check they respond as expected
    #------------------------------------------------------------------------

    parser = Config.parser({"POST_CHOMP": 1})
    provinc = Config.provider({ "INCLUDE_PATH": dir,
                                "PARSER": parser,
                                "TOLERANT": 1 })
    provabs = Config.provider({ "ABSOLUTE": 1, "PARSER": parser })
    provrel = Config.provider({ "RELATIVE": 1, "PARSER": parser })
    self.assert_(provinc.parser() is provabs.parser())
    self.assert_(provabs.parser() is provrel.parser())

    self.assert_(delivered(provinc, file))
    self.assert_(declined(provinc, absfile))
    self.assert_(declined(provinc, relfile))
    self.assert_(declined(provabs, file))
    self.assert_(delivered(provabs, absfile))
    self.assert_(denied(provabs, relfile))
    self.assert_(declined(provrel, file))
    self.assert_(denied(provrel, absfile))
    self.assert_(delivered(provrel, relfile))

    # Test if can fetch from a file handle.
    ttfile = Template()
    path = os.path.join(os.path.abspath(dir), "baz")
    file = open(path)
    outstr = ttfile.process(file, { "a": "filetest" })
    file.close()
    self.assertEquals("This is the baz file, a: filetest\n", outstr)

    #------------------------------------------------------------------------
    # now we'll fold those providers up into some Template objects that
    # we can pass to text_expect() to do some template driven testing
    #------------------------------------------------------------------------

    ttinc = Template({ "LOAD_TEMPLATES": [provinc] })
    ttabs = Template({ "LOAD_TEMPLATES": [provabs] })
    ttrel = Template({ "LOAD_TEMPLATES": [provrel] })

    def dpaths():
      return [os.path.join(lib, x) for x in "one", "two"]

    def badpaths():
      return [badpaths]

    ttd1 = Template({ "INCLUDE_PATH": [dpaths, dir], "PARSER": parser })
    ttd2 = Template(
      { "INCLUDE_PATH": [DynamicPaths(os.path.join(lib, "two"),
                                      os.path.join(lib, "one")), dir],
        "PARSER": parser })
    ttd3 = Template({ "INCLUDE_PATH": [ badpaths ], "PARSER": parser })
    uselist = (("ttinc", ttinc),
               ("ttabs", ttabs),
               ("ttrel", ttrel),
               ("ttd1", ttd1),
               ("ttd2", ttd2),
               ("ttdbad", ttd3))
    self.Expect(DATA, uselist, vars)
Пример #15
0
 def testPrefix(self):
   src_prov = Config.provider({ "INCLUDE_PATH": "test/src" })
   lib_prov = Config.provider({ "INCLUDE_PATH": "test/lib" })
   config = { "LOAD_TEMPLATES": [ src_prov, lib_prov ],
              "PREFIX_MAP": { "src": "0", "lib": "1", "all": "0, 1" } }
   self.Expect(DATA, config)