
from __future__ import absolute_import
from __future__ import division

import io
import json
import os
import sys
from xml.dom import minidom

from vdsm.common import hooks
from vdsm.common.commands import execCmd
from vdsm.common.conv import tobool

execCmd
tobool


def read_domxml():
    with io.open(os.environ['_hook_domxml'], 'rb') as f:
        return minidom.parseString(f.read().decode('utf-8'))


def write_domxml(domxml):
    with io.open(os.environ['_hook_domxml'], 'wb') as f:
        f.write(domxml.toxml(encoding='utf-8'))


def read_json():
    with open(os.environ['_hook_json']) as f:
        return json.loads(f.read())


def write_json(data):
    with open(os.environ['_hook_json'], 'w') as f:
        f.write(json.dumps(data))


def log(message):
    sys.stderr.write(message + '\n')


def exit_hook(message, return_code=2):
    sys.stderr.write(message + "\n")
    sys.exit(return_code)


def load_vm_launch_flags_from_file(vm_id):
    return hooks.load_vm_launch_flags_from_file(vm_id)


def dump_vm_launch_flags_to_file(vm_id, flags):
    hooks.dump_vm_launch_flags_to_file(vm_id, flags)
