From b3471fde838d00d0b640a9d6aff957b5799a9c0d Mon Sep 17 00:00:00 2001 From: "tc@google.com" Date: Fri, 16 Jan 2009 00:29:32 +0000 Subject: Add data pack file format to the grit output type and enable it for webkit_resources.grd. This only adds support for nodes. nodes are forthcoming. The design doc describing the format is here: http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalizedstrings I hooked into the tag because we need to generate an index at the beginning of the file. The index contains the position of all the resources, so we walk the file at that point. This currently only handles tags, but will be extended to handle as well. Review URL: http://codereview.chromium.org/18215 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8147 0039d316-1c4b-4281-b951-d872f2087c98 --- tools/grit/grit/format/data_pack.py | 67 ++++++++++++++++++++++++++++ tools/grit/grit/format/data_pack_unittest.py | 29 ++++++++++++ tools/grit/grit/node/include.py | 12 +++++ tools/grit/grit/node/misc.py | 6 +++ tools/grit/grit/test_suite_all.py | 2 + tools/grit/grit/tool/build.py | 7 +-- 6 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 tools/grit/grit/format/data_pack.py create mode 100644 tools/grit/grit/format/data_pack_unittest.py (limited to 'tools/grit') diff --git a/tools/grit/grit/format/data_pack.py b/tools/grit/grit/format/data_pack.py new file mode 100644 index 0000000..1f11a9b --- /dev/null +++ b/tools/grit/grit/format/data_pack.py @@ -0,0 +1,67 @@ +#!/usr/bin/python2.4 +# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +'''Support for formatting a data pack file used for platform agnostic resource +files. +''' + +import struct + +from grit.format import interface +from grit.node import misc +from grit.node import include + + +PACK_FILE_VERSION = 1 + + +class DataPack(interface.ItemFormatter): + '''Writes out the data pack file format (platform agnostic resource file).''' + def Format(self, item, lang='en', begin_item=True, output_dir='.'): + if not begin_item: + return '' + assert isinstance(item, misc.ReleaseNode) + + nodes = DataPack.GetDataNodes(item) + data = {} + for node in nodes: + id, value = node.GetDataPackPair() + data[id] = value + return DataPack.WriteDataPack(data) + + @staticmethod + def GetDataNodes(item): + '''Returns a list of nodes that can be packed into the data pack file.''' + nodes = [] + if isinstance(item, include.IncludeNode): + return [item] + # TODO(tc): Handle message nodes. + for child in item.children: + nodes.extend(DataPack.GetDataNodes(child)) + return nodes + + @staticmethod + def WriteDataPack(resources): + """Write a map of id=>data into a string in the data pack format and return + it.""" + ids = sorted(resources.keys()) + ret = [] + + # Write file header. + ret.append(struct.pack(" root element.''' diff --git a/tools/grit/grit/test_suite_all.py b/tools/grit/grit/test_suite_all.py index 943ac07..a94b160 100644 --- a/tools/grit/grit/test_suite_all.py +++ b/tools/grit/grit/test_suite_all.py @@ -33,6 +33,7 @@ class TestSuiteAll(unittest.TestSuite): from grit.node import message_unittest from grit import tclib_unittest import grit.format.rc_unittest + import grit.format.data_pack_unittest from grit.tool import rc2grd_unittest from grit.tool import transl2tc_unittest from grit.gather import txt_unittest @@ -58,6 +59,7 @@ class TestSuiteAll(unittest.TestSuite): message_unittest.MessageUnittest, tclib_unittest.TclibUnittest, grit.format.rc_unittest.FormatRcUnittest, + grit.format.data_pack_unittest.FormatDataPackUnittest, rc2grd_unittest.Rc2GrdUnittest, transl2tc_unittest.TranslationToTcUnittest, txt_unittest.TxtUnittest, diff --git a/tools/grit/grit/tool/build.py b/tools/grit/grit/tool/build.py index 810c80b..30d2fb3 100644 --- a/tools/grit/grit/tool/build.py +++ b/tools/grit/grit/tool/build.py @@ -171,9 +171,10 @@ are exported to translation interchange files (e.g. XMB files), etc. oldname = None else: encoding = 'utf_16' - outfile = util.WrapOutputStream( - self.fo_create(output.GetOutputFilename(), 'wb'), - encoding) + outfile = self.fo_create(output.GetOutputFilename(), 'wb') + + if output.GetType() != 'data_package': + outfile = util.WrapOutputStream(outfile, encoding) # Set the context, for conditional inclusion of resources self.res.SetOutputContext(output.GetLanguage(), self.defines) -- cgit v1.1