old_contents: # -*- coding: utf-8 -*-
  # ###
  # Copyright (c) 2013, Rice University
  # This software is subject to the provisions of the GNU Affero General
  # Public License version 3 (AGPLv3).
  # See LICENCE.txt for details.
  # ###
  """Database models and utilities"""
  import os
  import psycopg2
  
  
  CONNECTION_SETTINGS_KEY = 'db-connection-string'
  
  here = os.path.abspath(os.path.dirname(__file__))
  SQL_DIRECTORY = os.path.join(here, 'sql')
  DB_SCHEMA_DIRECTORY = os.path.join(SQL_DIRECTORY, 'schema')
  DB_SCHEMA_FILE_PATHS = (
      os.path.join(DB_SCHEMA_DIRECTORY, 'main.sql'),
      os.path.join(DB_SCHEMA_DIRECTORY, 'trees.sql'),
      os.path.join(DB_SCHEMA_DIRECTORY, 'shred_collxml.sql')
      )
  
  
  def _read_sql_file(name):
      path = os.path.join(SQL_DIRECTORY, '{}.sql'.format(name))
      with open(path, 'r') as fp:
          return fp.read()
  SQL = {
      'get-module': _read_sql_file('get-module'),
      'get-module-metadata': _read_sql_file('get-module-metadata'),
      'get-resource': _read_sql_file('get-resource'),
      'get-resource-by-filename': _read_sql_file('get-resource-by-filename'),
      }
  
  
  def initdb(settings):
      """Initialize the database from the given settings.
      If settings is None, the settings will be looked up via pyramid.
      """
      with psycopg2.connect(settings[CONNECTION_SETTINGS_KEY]) as db_connection:
          with db_connection.cursor() as cursor:
              for schema_filepath in DB_SCHEMA_FILE_PATHS:
                  with open(schema_filepath, 'r') as f:
                      cursor.execute(f.read())
              sql_constants = [os.path.join(DB_SCHEMA_DIRECTORY, filename)
                               for filename in os.listdir(DB_SCHEMA_DIRECTORY)
                               if filename.startswith('constant-')]
              for filepath in sql_constants:
                  with open(filepath, 'r') as f:
                      cursor.execute(f.read())
  
new_contents: # -*- coding: utf-8 -*-
  # ###
  # Copyright (c) 2013, Rice University
  # This software is subject to the provisions of the GNU Affero General
  # Public License version 3 (AGPLv3).
  # See LICENCE.txt for details.
  # ###
  """Database models and utilities"""
  import os
  import psycopg2
  
  
  CONNECTION_SETTINGS_KEY = 'db-connection-string'
  
  here = os.path.abspath(os.path.dirname(__file__))
  SQL_DIRECTORY = os.path.join(here, 'sql')
  DB_SCHEMA_DIRECTORY = os.path.join(SQL_DIRECTORY, 'schema')
  DB_SCHEMA_FILE_PATHS = (
      os.path.join(DB_SCHEMA_DIRECTORY, 'main.sql'),
      os.path.join(DB_SCHEMA_DIRECTORY, 'trees.sql'),
      os.path.join(DB_SCHEMA_DIRECTORY, 'shred_collxml.sql'),
      os.path.join(DB_SCHEMA_DIRECTORY, 'tree_to_json.sql'),
      )
  
  
  def _read_sql_file(name):
      path = os.path.join(SQL_DIRECTORY, '{}.sql'.format(name))
      with open(path, 'r') as fp:
          return fp.read()
  SQL = {
      'get-module': _read_sql_file('get-module'),
      'get-module-metadata': _read_sql_file('get-module-metadata'),
      'get-resource': _read_sql_file('get-resource'),
      'get-resource-by-filename': _read_sql_file('get-resource-by-filename'),
      }
  
  
  def initdb(settings):
      """Initialize the database from the given settings.
      If settings is None, the settings will be looked up via pyramid.
      """
      with psycopg2.connect(settings[CONNECTION_SETTINGS_KEY]) as db_connection:
          with db_connection.cursor() as cursor:
              for schema_filepath in DB_SCHEMA_FILE_PATHS:
                  with open(schema_filepath, 'r') as f:
                      cursor.execute(f.read())
              sql_constants = [os.path.join(DB_SCHEMA_DIRECTORY, filename)
                               for filename in os.listdir(DB_SCHEMA_DIRECTORY)
                               if filename.startswith('constant-')]
              for filepath in sql_constants:
                  with open(filepath, 'r') as f:
                      cursor.execute(f.read())
  
subject: Add the tree_to_json function to the database initialization.