zetasql/resolved_ast/gen_resolved_ast.py
def Generate(self, input_file_paths, output_file_paths):
    """Materialize the templates to generate the output files."""

    def ShouldAutoescape(template_name):
      return template_name and '.html' in template_name

    jinja_env = jinja2.Environment(
        undefined=jinja2.StrictUndefined,
        autoescape=ShouldAutoescape,
        trim_blocks=True,
        lstrip_blocks=True,
        line_statement_prefix='# ',
        loader=jinja2.FileSystemLoader(''))

    # This can be used to filter a list of fields to only those with
    # <is_constructor_arg = true>.  Used in templates like this:
    #   for node.fields | is_constructor_arg
    def IsConstructorArg(field_list):
      return [field for field in field_list if field['is_constructor_arg']]

    def IsNodeVectorConstructorArg(field_list):
      return [
          field for field in field_list
          if field['is_node_vector'] and field['is_constructor_arg']
      ]

    jinja_env.filters['is_constructor_arg'] = IsConstructorArg
    jinja_env.filters[
        'is_node_vector_constructor_arg'] = IsNodeVectorConstructorArg

    # These can be used to make relative links and targets within a doc.
    # {{name|as_link}} will make emit name with a link to "#name".
    # {{name|as_target}} will make emit name and make it a target for "#name".
    def AsLink(text):
      # Markup makes this not get re-escaped.
      return markupsafe.Markup('<a href="#%s">%s</a>' % (text, text))

    def AsTarget(text):
      return markupsafe.Markup('<a name="%s">%s</a>' % (text, text))

    jinja_env.filters['as_link'] = AsLink
    jinja_env.filters['as_target'] = AsTarget

    # This can be used to find node names in a string (e.g. a c++ return type)
    # and turn them into relative links inside the doc.
    linkify_re = re.compile('(Resolved[a-zA-Z]*)')

    def LinkifyNodeNames(text):
      # Escape the text which may include strings like std::vector<xxx>,
      # and Markup the output to not be escaped further.
      text = markupsafe.escape(text)
      match = linkify_re.search(text)
      if match:
        name = match.groups()[0]
        return markupsafe.Markup(text.replace(name, AsLink(name)))
      else:
        return text

    jinja_env.filters['linkify_node_names'] = LinkifyNodeNames

    # {{items|sort_by_name}} can be used to sort a list of objects by name.
    def SortByName(items):
      return sorted(items, key=operator.itemgetter('name'))

    jinja_env.filters['sort_by_name'] = SortByName

    # {{items|sort_by_tag}} can be used to sort a list of objects by tag.
    def SortByTagId(items):
      return sorted(items, key=operator.itemgetter('tag_id'))

    jinja_env.filters['sort_by_tag'] = SortByTagId

    # {{string|upper_camel_case}} turns some_string or SOME_STRING into
    # SomeString.
    def UpperCamelCase(value):
      split_value = value.lower().split('_')
      return ''.join([part.capitalize() for part in split_value])

    jinja_env.filters['upper_camel_case'] = UpperCamelCase

    # {{string|lower_camel_case}} turns some_string or SOME_STRING into
    # someString.
    def LowerCamelCase(value):
      split_value = value.lower().split('_')
      result = [split_value[0]]
      for part in split_value[1:]:
        result.append(part.capitalize())
      return ''.join(result)

    jinja_env.filters['lower_camel_case'] = LowerCamelCase

    # {{defining_node(some_field)}} will be the node a field is defined in.
    # We have this, rather than a direct field reference, to prevent the data
    # structure from becoming recursive.
    def DefiningNode(field):
      return self.node_map[field['defining_node_name']]

    jinja_env.globals['defining_node'] = DefiningNode

    self._ComputeTreeData()

    context = {
        'nodes': self.nodes,
        'root_node_name': ROOT_NODE_NAME,
        'root_child_nodes': self.root_child_nodes,
        'java_enum_classes': self._JavaEnumClasses(),
        'timestamp': time.ctime(),
        # For when we need to force a blank line and jinja wants to
        # eat blank lines from the template.
        'blank_line': '\n'
    }
    assert len(input_file_paths) == len(output_file_paths)

    for (in_path, out_path) in zip(input_file_paths, output_file_paths):
      if (os.path.basename(in_path) !=
          os.path.basename(out_path) + '.template'):
        logging.fatal(
            'Input must match output with ".template" postfix.'
            'Saw: input=%s output=%s', os.path.basename(in_path),
            os.path.basename(out_path))

      template = jinja_env.get_template(in_path)
      out = open(out_path, 'wt')
      out.write(_Trim(template.render(context)))
      out.close()
Materialize the templates to generate the output files.
Materialize the templates to generate the output files.
Materialize the templates to generate the output files .
def Generate(self, input_file_paths, output_file_paths):
    def ShouldAutoescape(template_name):
      return template_name and '.html' in template_name
    jinja_env = jinja2.Environment(
        undefined=jinja2.StrictUndefined,
        autoescape=ShouldAutoescape,
        trim_blocks=True,
        lstrip_blocks=True,
        line_statement_prefix='# ',
        loader=jinja2.FileSystemLoader(''))
    def IsConstructorArg(field_list):
      return [field for field in field_list if field['is_constructor_arg']]
    def IsNodeVectorConstructorArg(field_list):
      return [
          field for field in field_list
          if field['is_node_vector'] and field['is_constructor_arg']
      ]
    jinja_env.filters['is_constructor_arg'] = IsConstructorArg
    jinja_env.filters[
        'is_node_vector_constructor_arg'] = IsNodeVectorConstructorArg
    def AsLink(text):
      return markupsafe.Markup('<a href="#%s">%s</a>' % (text, text))
    def AsTarget(text):
      return markupsafe.Markup('<a name="%s">%s</a>' % (text, text))
    jinja_env.filters['as_link'] = AsLink
    jinja_env.filters['as_target'] = AsTarget
    linkify_re = re.compile('(Resolved[a-zA-Z]*)')
    def LinkifyNodeNames(text):
      text = markupsafe.escape(text)
      match = linkify_re.search(text)
      if match:
        name = match.groups()[0]
        return markupsafe.Markup(text.replace(name, AsLink(name)))
      else:
        return text
    jinja_env.filters['linkify_node_names'] = LinkifyNodeNames
    def SortByName(items):
      return sorted(items, key=operator.itemgetter('name'))
    jinja_env.filters['sort_by_name'] = SortByName
    def SortByTagId(items):
      return sorted(items, key=operator.itemgetter('tag_id'))
    jinja_env.filters['sort_by_tag'] = SortByTagId
    def UpperCamelCase(value):
      split_value = value.lower().split('_')
      return ''.join([part.capitalize() for part in split_value])
    jinja_env.filters['upper_camel_case'] = UpperCamelCase
    def LowerCamelCase(value):
      split_value = value.lower().split('_')
      result = [split_value[0]]
      for part in split_value[1:]:
        result.append(part.capitalize())
      return ''.join(result)
    jinja_env.filters['lower_camel_case'] = LowerCamelCase
    def DefiningNode(field):
      return self.node_map[field['defining_node_name']]
    jinja_env.globals['defining_node'] = DefiningNode
    self._ComputeTreeData()
    context = {
        'nodes': self.nodes,
        'root_node_name': ROOT_NODE_NAME,
        'root_child_nodes': self.root_child_nodes,
        'java_enum_classes': self._JavaEnumClasses(),
        'timestamp': time.ctime(),
        'blank_line': '\n'
    }
    assert len(input_file_paths) == len(output_file_paths)
    for (in_path, out_path) in zip(input_file_paths, output_file_paths):
      if (os.path.basename(in_path) !=
          os.path.basename(out_path) + '.template'):
        logging.fatal(
            'Input must match output with ".template" postfix.'
            'Saw: input=%s output=%s', os.path.basename(in_path),
            os.path.basename(out_path))
      template = jinja_env.get_template(in_path)
      out = open(out_path, 'wt')
      out.write(_Trim(template.render(context)))
      out.close()
def Generate ( self , input_file_paths , output_file_paths ) : def ShouldAutoescape ( template_name ) : return template_name and '.html' in template_name jinja_env = jinja2 . Environment ( undefined = jinja2 . StrictUndefined , autoescape = ShouldAutoescape , trim_blocks = True , lstrip_blocks = True , line_statement_prefix = '# ' , loader = jinja2 . FileSystemLoader ( '' ) ) def IsConstructorArg ( field_list ) : return [ field for field in field_list if field [ 'is_constructor_arg' ] ] def IsNodeVectorConstructorArg ( field_list ) : return [ field for field in field_list if field [ 'is_node_vector' ] and field [ 'is_constructor_arg' ] ] jinja_env . filters [ 'is_constructor_arg' ] = IsConstructorArg jinja_env . filters [ 'is_node_vector_constructor_arg' ] = IsNodeVectorConstructorArg def AsLink ( text ) : return markupsafe . Markup ( '<a href="#%s">%s</a>' % ( text , text ) ) def AsTarget ( text ) : return markupsafe . Markup ( '<a name="%s">%s</a>' % ( text , text ) ) jinja_env . filters [ 'as_link' ] = AsLink jinja_env . filters [ 'as_target' ] = AsTarget linkify_re = re . compile ( '(Resolved[a-zA-Z]*)' ) def LinkifyNodeNames ( text ) : text = markupsafe . escape ( text ) match = linkify_re . search ( text ) if match : name = match . groups ( ) [ 0 ] return markupsafe . Markup ( text . replace ( name , AsLink ( name ) ) ) else : return text jinja_env . filters [ 'linkify_node_names' ] = LinkifyNodeNames def SortByName ( items ) : return sorted ( items , key = operator . itemgetter ( 'name' ) ) jinja_env . filters [ 'sort_by_name' ] = SortByName def SortByTagId ( items ) : return sorted ( items , key = operator . itemgetter ( 'tag_id' ) ) jinja_env . filters [ 'sort_by_tag' ] = SortByTagId def UpperCamelCase ( value ) : split_value = value . lower ( ) . split ( '_' ) return '' . join ( [ part . capitalize ( ) for part in split_value ] ) jinja_env . filters [ 'upper_camel_case' ] = UpperCamelCase def LowerCamelCase ( value ) : split_value = value . lower ( ) . split ( '_' ) result = [ split_value [ 0 ] ] for part in split_value [ 1 : ] : result . append ( part . capitalize ( ) ) return '' . join ( result ) jinja_env . filters [ 'lower_camel_case' ] = LowerCamelCase def DefiningNode ( field ) : return self . node_map [ field [ 'defining_node_name' ] ] jinja_env . globals [ 'defining_node' ] = DefiningNode self . _ComputeTreeData ( ) context = { 'nodes' : self . nodes , 'root_node_name' : ROOT_NODE_NAME , 'root_child_nodes' : self . root_child_nodes , 'java_enum_classes' : self . _JavaEnumClasses ( ) , 'timestamp' : time . ctime ( ) , 'blank_line' : '\n' } assert len ( input_file_paths ) == len ( output_file_paths ) for ( in_path , out_path ) in zip ( input_file_paths , output_file_paths ) : if ( os . path . basename ( in_path ) != os . path . basename ( out_path ) + '.template' ) : logging . fatal ( 'Input must match output with ".template" postfix.' 'Saw: input=%s output=%s' , os . path . basename ( in_path ) , os . path . basename ( out_path ) ) template = jinja_env . get_template ( in_path ) out = open ( out_path , 'wt' ) out . write ( _Trim ( template . render ( context ) ) ) out . close ( )
Materialize the templates to generate the output files.
Materialize the templates to generate the output files .
"""Materialize the templates to generate the output files.""" # This can be used to filter a list of fields to only those with # <is_constructor_arg = true>.  Used in templates like this: #   for node.fields | is_constructor_arg # These can be used to make relative links and targets within a doc. # {{name|as_link}} will make emit name with a link to "#name". # {{name|as_target}} will make emit name and make it a target for "#name". # Markup makes this not get re-escaped. # This can be used to find node names in a string (e.g. a c++ return type) # and turn them into relative links inside the doc. # Escape the text which may include strings like std::vector<xxx>, # and Markup the output to not be escaped further. # {{items|sort_by_name}} can be used to sort a list of objects by name. # {{items|sort_by_tag}} can be used to sort a list of objects by tag. # {{string|upper_camel_case}} turns some_string or SOME_STRING into # SomeString. # {{string|lower_camel_case}} turns some_string or SOME_STRING into # someString. # {{defining_node(some_field)}} will be the node a field is defined in. # We have this, rather than a direct field reference, to prevent the data # structure from becoming recursive. # For when we need to force a blank line and jinja wants to # eat blank lines from the template.
self
input_file_paths
output_file_paths