from gnuradio_core import *
from exceptions import *
#from hier_block2 import *
#from top_block import *
from gateway import basic_block, sync_block, decim_block, interp_block
from tag_utils import tag_to_python, tag_to_pmt

import gras

RT_OK = 0
RT_NOT_IMPLEMENTED = 1
RT_NO_PRIVS = 2
RT_OTHER_ERROR = 3


def enable_realtime_scheduling():
    """
    This call is for backward compat purposes.
    See gras/thread_pool.hpp for greater options.
    """

    #any prio greater than 0 means realtime scheduling
    prio_value = 0.5

    #test that prio
    if not gras.ThreadPool.test_thread_priority(prio_value):
        return RT_NO_PRIVS

    #create a new thread pool with thread priority set
    config = gras.ThreadPoolConfig()
    config.thread_priority = prio_value
    tp = gras.ThreadPool(config)
    tp.set_active()

    return RT_OK

class top_block(gras.TopBlock):
    def __init__(self, name="Top"):
        gras.TopBlock.__init__(self, name)

    def lock(self):
        pass

    def unlock(self):
        self.commit()

    def start(self, *args):
        if args: self.global_config().maximum_output_items = args[0]
        gras.TopBlock.start(self)

    def run(self, *args):
        if args: self.global_config().maximum_output_items = args[0]
        gras.TopBlock.run(self)

class hier_block2(gras.HierBlock):
    def __init__(self, name="Hier", in_sig=None, out_sig=None):
        gras.HierBlock.__init__(self, name)

        self.__in_sig = in_sig
        self.__out_sig = out_sig

        #backwards compatible silliness
        import weakref
        self._hb = weakref.proxy(self)

    def lock(self):
        pass

    def unlock(self):
        self.commit()

    def input_signature(self): return self.__in_sig
    def output_signature(self): return self.__out_sig

# create a couple of aliases
serial_to_parallel = stream_to_vector
parallel_to_serial = vector_to_stream

# Force the preference database to be initialized
prefs = gr_prefs.singleton

#alias old gr_add_vXX and gr_multiply_vXX
add_vcc = add_cc
add_vff = add_ff
add_vii = add_ii
add_vss = add_ss
multiply_vcc = multiply_cc
multiply_vff = multiply_ff
multiply_vii = multiply_ii
multiply_vss = multiply_ss
