示例#1
0
    def test(self):
        value = B()
        ret = unserialize(serialize(value))

        self.assertTrue(isinstance(ret, value.__class__))
        self.assertTrue(isinstance(ret, Object))
        self.assertEqual(ret.__dict__, value.__dict__)
示例#2
0
    def getRouteCollection(self):

        if None is not self._collection:
            return self._collection

        if None is self._options['cache_dir'] or None is self._options[
                'routes_cache_class']:
            self._collection = self._doGetRouteCollection()
            return self._collection

        cacheClass = self._options['routes_cache_class']
        cache = ConfigCache(
            self._options['cache_dir'] + '/' + cacheClass + '.dat',
            self._options['debug'])
        fresh = True
        if not cache.isFresh():
            self._collection = self._doGetRouteCollection()

            cache.write(serialize(self._collection),
                        self._collection.getResources())

            fresh = False

        if fresh:
            f = open(str(cache))
            try:
                content = f.read()
            finally:
                f.close()
            self._collection = unserialize(content)

        return self._collection
示例#3
0
    def test(self):
        value = B();
        ret = unserialize(serialize(value));

        self.assertTrue(isinstance(ret, value.__class__));
        self.assertTrue(isinstance(ret, Object));
        self.assertEqual(ret.__dict__, value.__dict__);
示例#4
0
    def testObjectSupportEnabled(self):

        dump = self._dumper.dump(OrderedDict([('foo', A()), ('bar', 1)]), 0, 0,
                                 False, True)

        self.assertEqual(
            '{{ foo: !!python/object:{0}, bar: 1 }}'.format(serialize(A())),
            dump, '->dump() is able to dump objects')
示例#5
0
    def testObjectSupportDisabledButNoExceptions(self):

        inputv = """
foo: !!python/object:{0}
bar: 1
""".format(serialize(B()));

        self.assertEqual(OrderedDict([('foo' , None), ('bar' , 1)]), self._parser.parse(inputv), '->parse() does not parse objects');
示例#6
0
    def testObjectSupportDisabledButNoExceptions(self):

        inputv = """
foo: !!python/object:{0}
bar: 1
""".format(serialize(B()))

        self.assertEqual(OrderedDict([('foo', None), ('bar', 1)]),
                         self._parser.parse(inputv),
                         '->parse() does not parse objects')
示例#7
0
    def testObjectsSupportDisabledWithExceptions(self):
        """@expectedException: Symfony\Component\Yaml\Exception\ParseException

        """

        try:
            self._parser.parse('foo: !!python/object:'+serialize(B()), True, False);

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, ParseException));
示例#8
0
    def testObjectsSupportDisabledWithExceptions(self):
        """@expectedException: Symfony\Component\Yaml\Exception\ParseException

        """

        try:
            self._parser.parse('foo: !!python/object:' + serialize(B()), True,
                               False)

            self.fail()
        except Exception as e:
            self.assertTrue(isinstance(e, ParseException))
示例#9
0
    def testObjectSupportEnabled(self):

        inputv = """
foo: !!python/object:{0}
bar: 1
""".format(serialize(B()));

        parsed = self._parser.parse(inputv, False, True);

        self.assertTrue(isinstance(parsed['foo'], B), '->parse() is able to parse objects');
        self.assertEqual(parsed['foo'].b, 'foo', '->parse() is able to parse objects');
        self.assertEqual(parsed['bar'], 1, '->parse() is able to parse objects');
示例#10
0
    def testObjectSupportEnabled(self):

        inputv = """
foo: !!python/object:{0}
bar: 1
""".format(serialize(B()))

        parsed = self._parser.parse(inputv, False, True)

        self.assertTrue(isinstance(parsed['foo'], B),
                        '->parse() is able to parse objects')
        self.assertEqual(parsed['foo'].b, 'foo',
                         '->parse() is able to parse objects')
        self.assertEqual(parsed['bar'], 1,
                         '->parse() is able to parse objects')
示例#11
0
    def _dumpContainer(self, cache, container, className, baseClass):
        """Dumps the service container to PHP code in the cache.

        @param ConfigCache cache The config cache
        @param ContainerBuilder container The service container
        @param string class The name of the class to generate
        @param string baseClass The name of the container's base class

        """
        assert isinstance(container, ContainerBuilder);
        assert isinstance(cache, ConfigCache);

        # cache the container
        content = serialize(container);

        cache.write(content, container.getResources());
示例#12
0
    def _dumpContainer(self, cache, container, className, baseClass):
        """Dumps the service container to PHP code in the cache.

        @param ConfigCache cache The config cache
        @param ContainerBuilder container The service container
        @param string class The name of the class to generate
        @param string baseClass The name of the container's base class

        """
        assert isinstance(container, ContainerBuilder)
        assert isinstance(cache, ConfigCache)

        # cache the container
        content = serialize(container)

        cache.write(content, container.getResources())
示例#13
0
    def write(self, content, metadata=None):
        """Writes cache.

        @param string              content  The content to write in the cache
        @param ResourceInterface[] metadata An array of ResourceInterface instances

        @raise RuntimeException When cache file can't be wrote

        """
        assert isinstance(metadata, list) or metadata is None

        dirname = os.path.dirname(self.__file)
        if not os.path.isdir(dirname):
            try:
                os.makedirs(dirname, 0o777)
            except os.error:
                raise RuntimeException(
                    'Unable to create the {0} directory'.format(dirname))

        elif not os.access(dirname, os.W_OK):
            raise RuntimeException(
                'Unable to write in the {0} directory'.format(dirname))

        try:
            suffix = 0
            while os.path.exists(self.__file + str(suffix)):
                suffix += 1
            tmpFile = self.__file + str(suffix)
            f = open(tmpFile, 'w')
            f.write(content)
            f.close()
            if os.path.exists(self.__file):
                os.remove(self.__file)
            os.rename(tmpFile, self.__file)
            if hasattr(os, 'chmod'):
                umask = os.umask(0o220)
                os.umask(umask)
                os.chmod(self.__file, 0o666 & ~umask)
        except Exception:
            raise RuntimeException('Failed to write cache file "{0}".'.format(
                self.__file))
        else:
            try:
                if hasattr(os, 'chmod'):
                    umask = os.umask(0o220)
                    os.umask(umask)
                    os.chmod(self.__file, 0o666 & ~umask)
            except Exception:
                pass
        finally:
            try:
                f.close()
            except Exception:
                pass
            if os.path.exists(tmpFile):
                os.remove(tmpFile)

        if None is not metadata and True is self.__debug:
            filename = self.__file + '.meta'
            content = serialize(metadata)

            try:
                suffix = 0
                while os.path.exists(filename + str(suffix)):
                    suffix += 1
                tmpFile = filename + str(suffix)
                f = open(tmpFile, 'w')
                f.write(content)
                f.close()
                if os.path.exists(filename):
                    os.remove(filename)
                os.rename(tmpFile, filename)
            except Exception:
                pass
            else:
                try:
                    if hasattr(os, 'chmod'):
                        umask = os.umask(0o220)
                        os.umask(umask)
                        os.chmod(filename, 0o666 & ~umask)
                except Exception:
                    pass
            finally:
                try:
                    f.close()
                except Exception:
                    pass
                if os.path.exists(tmpFile):
                    os.remove(tmpFile)
示例#14
0
    def write(self, content, metadata = None):
        """Writes cache.

        @param string              content  The content to write in the cache
        @param ResourceInterface[] metadata An array of ResourceInterface instances

        @raise RuntimeException When cache file can't be wrote

        """
        assert isinstance(metadata, list) or metadata is None;

        dirname = os.path.dirname(self.__file);
        if not os.path.isdir(dirname) :
            try:
                os.makedirs(dirname, 0o777);
            except os.error:
                raise RuntimeException('Unable to create the {0} directory'.format(dirname));

        elif not os.access(dirname, os.W_OK) :
            raise RuntimeException('Unable to write in the {0} directory'.format(dirname));

        try:
            suffix = 0;
            while os.path.exists(self.__file+str(suffix)):
                suffix += 1;
            tmpFile = self.__file+str(suffix);
            f = open(tmpFile, 'w');
            f.write(content);
            f.close();
            if os.path.exists(self.__file):
                os.remove(self.__file);
            os.rename(tmpFile, self.__file);
            if hasattr(os, 'chmod'):
                umask = os.umask(0o220);
                os.umask(umask);
                os.chmod(self.__file, 0o666 & ~umask);
        except Exception:
            raise RuntimeException('Failed to write cache file "{0}".'.format(self.__file));
        else:
            try:
                if hasattr(os, 'chmod'):
                    umask = os.umask(0o220);
                    os.umask(umask);
                    os.chmod(self.__file, 0o666 & ~umask);
            except Exception:
                pass;
        finally:
            try:
                f.close();
            except Exception:
                pass;
            if os.path.exists(tmpFile):
                os.remove(tmpFile);


        if None is not metadata and True is self.__debug :
            filename = self.__file+'.meta';
            content = serialize(metadata);

            try:
                suffix = 0;
                while os.path.exists(filename+str(suffix)):
                    suffix += 1;
                tmpFile = filename+str(suffix);
                f = open(tmpFile, 'w');
                f.write(content);
                f.close();
                if os.path.exists(filename):
                    os.remove(filename);
                os.rename(tmpFile, filename);
            except Exception:
                pass;
            else:
                try:
                    if hasattr(os, 'chmod'):
                        umask = os.umask(0o220);
                        os.umask(umask);
                        os.chmod(filename, 0o666 & ~umask);
                except Exception:
                    pass;
            finally:
                try:
                    f.close();
                except Exception:
                    pass;
                if os.path.exists(tmpFile):
                    os.remove(tmpFile);
示例#15
0
    def testObjectSupportEnabled(self):

        dump = self._dumper.dump(OrderedDict([('foo' , A()), ('bar' , 1)]), 0, 0, False, True);

        self.assertEqual('{{ foo: !!python/object:{0}, bar: 1 }}'.format(serialize(A())), dump, '->dump() is able to dump objects');