# Path: offset/core/util.py
# def nanotime(s=None):
#     """ convert seconds to nanoseconds. If s is None, current time is
#     returned """
#     if s is not None:
#         return s * 1000000000
#     return time.time() * 1000000000
# 
# def from_nanotime(n):
#     """ convert from nanotime to seconds """
#     return n / 1.0e9
# 
# Path: offset/core/timer.py
# def _ready(now, t, g):
# def sleep(d):
#     def __init__(self, callback, interval=None, period=None, args=None,
#             kwargs=None):
#     def start(self):
#     def stop(self):
#     def __lt__(self, other):
#     def __init__(self):
#     def add(self, t):
#     def _add_timer(self, t):
#     def remove(self, t):
#     def timerproc(self):
# class Timer(object):
# class Timers(object):
# 
# Path: offset/core/chan.py
# def makechan(size=None, label=None):
#     return Channel(size=size, label=label)
# 
# def select(*cases):
#     """ A select function lets a goroutine wait on multiple
#     communication operations.
# 
#     A select blocks until one of its cases can run, then it
#     executes that case. It chooses one at random if multiple are ready"""
# 
#     # reorder cases
# 
# 
#     c_ordered = [(i, cas) for i, cas in enumerate(cases)]
#     random.shuffle(c_ordered)
#     cases = [cas for _, cas in c_ordered]
# 
#     while True:
#         # pass 1 - look for something already waiting
#         for cas in cases:
#             if cas.op == 0:
#                 # RECV
#                 if cas.ch.size > 0 and len(cas.ch._buf) > 0:
#                     # buffered channel
#                     cas.value = cas.ch._buf.popleft()
# 
#                     # dequeue from the sendq
#                     sg = None
#                     try:
#                         sg = cas.ch.sendq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.ready()
# 
#                     # return the case
#                     return cas
#                 else:
#                     #
#                     sg = None
#                     try:
#                         sg = cas.ch.sendq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.param = sg
#                         gp.ready()
#                         cas.elem = sg.elem
#                         return cas
# 
#                     if cas.ch.closed:
#                         return
# 
#             elif cas.op == 1:
#                 if cas.ch.closed:
#                     return
# 
#                 # SEND
#                 if cas.ch.size > 0 and len(cas.ch._buf) < cas.ch.size:
#                     # buffered channnel, we can fill the buffer
#                     cas.ch._buf.append(cas.elem)
# 
#                     # eventually trigger a receiver
#                     sg = None
#                     try:
#                         sg = cas.ch.recvq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.ready()
# 
#                     # return
#                     return cas
#                 else:
#                     sg = None
#                     try:
#                         sg = cas.ch.recvq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         sg.elem = cas.elem
#                         gp.param = sg
#                         gp.ready()
#                         return cas
#             else:
#                 # default case
#                 return cas
# 
#         # pass 2 - enqueue on all channels
#         g = proc.current()
#         g.param = None
#         g.sleeping = True
#         for cas in cases:
#             sg = SudoG(g, cas.elem)
#             cas.sg = sg
#             if cas.op == 0:
#                 cas.ch.recvq.append(sg)
#             else:
#                 cas.ch.sendq.append(sg)
# 
#         # sleep until a communication happen
#         g.park()
# 
#         sg = g.param
# 
#         # pass 3 - dequeue from unsucessful channels
#         # to not iddle in them
#         selected = None
#         for cas in cases:
#             if cas.sg != sg:
#                 try:
#                     if cas.op == 0:
#                         cas.ch.recvq.remove(cas.sg)
#                     else:
#                         cas.ch.sendq.remove(cas.sg)
#                 except ValueError:
#                     pass
#             else:
#                 selected = cas
# 
#         if sg is None:
#             continue
# 
#         if selected.ch.size > 0:
#             raise RuntimeError("select shouldn't happen")
# 
#         if selected.op == 0:
#             selected.value = sg.elem
# 
#         return selected


from .core.util import nanotime, from_nanotime
from .core import timer
from .core.chan import makechan, select
# -*- coding: utf-8 -
#
# This file is part of offset. See the NOTICE for more information.



NANOSECOND = 1
MICROSECOND = 1000 * NANOSECOND
MILLISECOND = 1000 * MICROSECOND
SECOND = 1000 * MILLISECOND
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE

nano = nanotime
sleep = timer.sleep

def _sendtime(now, t, c):
    select(c.if_send(from_nanotime(now)))

class Timer(object):
    """ The Timer instance represents a single event.
    When the timer expires, the current time will be sent on c """

    def __init__(self, interval):
        self.c = makechan(1)
        self.t = timer.Timer(_sendtime, interval, args=(self.c,))
        self.t.start()

    def reset(self, interval):
        """ reset the timer interval """

# Path: offset/core/util.py
# def nanotime(s=None):
#     """ convert seconds to nanoseconds. If s is None, current time is
#     returned """
#     if s is not None:
#         return s * 1000000000
#     return time.time() * 1000000000
# 
# def from_nanotime(n):
#     """ convert from nanotime to seconds """
#     return n / 1.0e9
# 
# Path: offset/core/timer.py
# def _ready(now, t, g):
# def sleep(d):
#     def __init__(self, callback, interval=None, period=None, args=None,
#             kwargs=None):
#     def start(self):
#     def stop(self):
#     def __lt__(self, other):
#     def __init__(self):
#     def add(self, t):
#     def _add_timer(self, t):
#     def remove(self, t):
#     def timerproc(self):
# class Timer(object):
# class Timers(object):
# 
# Path: offset/core/chan.py
# def makechan(size=None, label=None):
#     return Channel(size=size, label=label)
# 
# def select(*cases):
#     """ A select function lets a goroutine wait on multiple
#     communication operations.
# 
#     A select blocks until one of its cases can run, then it
#     executes that case. It chooses one at random if multiple are ready"""
# 
#     # reorder cases
# 
# 
#     c_ordered = [(i, cas) for i, cas in enumerate(cases)]
#     random.shuffle(c_ordered)
#     cases = [cas for _, cas in c_ordered]
# 
#     while True:
#         # pass 1 - look for something already waiting
#         for cas in cases:
#             if cas.op == 0:
#                 # RECV
#                 if cas.ch.size > 0 and len(cas.ch._buf) > 0:
#                     # buffered channel
#                     cas.value = cas.ch._buf.popleft()
# 
#                     # dequeue from the sendq
#                     sg = None
#                     try:
#                         sg = cas.ch.sendq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.ready()
# 
#                     # return the case
#                     return cas
#                 else:
#                     #
#                     sg = None
#                     try:
#                         sg = cas.ch.sendq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.param = sg
#                         gp.ready()
#                         cas.elem = sg.elem
#                         return cas
# 
#                     if cas.ch.closed:
#                         return
# 
#             elif cas.op == 1:
#                 if cas.ch.closed:
#                     return
# 
#                 # SEND
#                 if cas.ch.size > 0 and len(cas.ch._buf) < cas.ch.size:
#                     # buffered channnel, we can fill the buffer
#                     cas.ch._buf.append(cas.elem)
# 
#                     # eventually trigger a receiver
#                     sg = None
#                     try:
#                         sg = cas.ch.recvq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         gp.ready()
# 
#                     # return
#                     return cas
#                 else:
#                     sg = None
#                     try:
#                         sg = cas.ch.recvq.popleft()
#                     except IndexError:
#                         pass
# 
#                     if sg is not None:
#                         gp = sg.g
#                         sg.elem = cas.elem
#                         gp.param = sg
#                         gp.ready()
#                         return cas
#             else:
#                 # default case
#                 return cas
# 
#         # pass 2 - enqueue on all channels
#         g = proc.current()
#         g.param = None
#         g.sleeping = True
#         for cas in cases:
#             sg = SudoG(g, cas.elem)
#             cas.sg = sg
#             if cas.op == 0:
#                 cas.ch.recvq.append(sg)
#             else:
#                 cas.ch.sendq.append(sg)
# 
#         # sleep until a communication happen
#         g.park()
# 
#         sg = g.param
# 
#         # pass 3 - dequeue from unsucessful channels
#         # to not iddle in them
#         selected = None
#         for cas in cases:
#             if cas.sg != sg:
#                 try:
#                     if cas.op == 0:
#                         cas.ch.recvq.remove(cas.sg)
#                     else:
#                         cas.ch.sendq.remove(cas.sg)
#                 except ValueError:
#                     pass
#             else:
#                 selected = cas
# 
#         if sg is None:
#             continue
# 
#         if selected.ch.size > 0:
#             raise RuntimeError("select shouldn't happen")
# 
#         if selected.op == 0:
#             selected.value = sg.elem
# 
#         return selected


# Path: offset/time.py
from .core.util import nanotime, from_nanotime
from .core import timer
from .core.chan import makechan, select

# -*- coding: utf-8 -
#
# This file is part of offset. See the NOTICE for more information.



NANOSECOND = 1
MICROSECOND = 1000 * NANOSECOND
MILLISECOND = 1000 * MICROSECOND
SECOND = 1000 * MILLISECOND
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE

nano = nanotime
sleep = timer.sleep

def _sendtime(now, t, c):
    select(c.if_send(from_nanotime(now)))

class Timer(object):
    """ The Timer instance represents a single event.
    When the timer expires, the current time will be sent on c """

    def __init__(self, interval):
        self.c = makechan(1)
        self.t = timer.Timer(_sendtime, interval, args=(self.c,))
        self.t.start()

    def reset(self, interval):
        """ reset the timer interval """

        w = nanotime() + interval