#!/usr/bin/env python
# Copyright (c) 2012 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 an RC file for compilation.
'''

import os
import types
import re
from functools import partial

from grit import util
from grit.node import misc


def Format(root, lang='en', output_dir='.'):
  from grit.node import empty, include, message, structure

  yield _FormatHeader(root, lang, output_dir)

  for item in root.ActiveDescendants():
    if isinstance(item, empty.MessagesNode):
      # Write one STRINGTABLE per <messages> container.
      # This is hacky: it iterates over the children twice.
      yield 'STRINGTABLE\nBEGIN\n'
      for subitem in item.ActiveDescendants():
        if isinstance(subitem, message.MessageNode):
          with subitem:
            yield FormatMessage(subitem, lang)
      yield 'END\n\n'
    elif isinstance(item, include.IncludeNode):
      with item:
        yield FormatInclude(item, lang, output_dir)
    elif isinstance(item, structure.StructureNode):
      with item:
        yield FormatStructure(item, lang, output_dir)


'''
This dictionary defines the language charset pair lookup table, which is used
for replacing the GRIT expand variables for language info in Product Version
resource. The key is the language ISO country code, and the value
is the language and character-set pair, which is a hexadecimal string
consisting of the concatenation of the language and character-set identifiers.
The first 4 digit of the value is the hex value of LCID, the remaining