# Path: deep_architect/contrib/communicators/communicator.py
# class Communicator:
# 
#     def __init__(self, num_workers, rank):
#         self.num_workers = num_workers
#         self.rank = rank
#         self.finished = 0
# 
#     def is_master(self):
#         return self.rank == 0
# 
#     def is_worker(self):
#         return self.rank > 0
# 
#     def get_rank(self):
#         return self.rank
# 
#     def publish_results_to_master(self, results, evaluation_id,
#                                   searcher_eval_token):
#         if not self.is_worker():
#             raise ValueError("Master cannot publish results")
#         return self._publish_results_to_master(results, evaluation_id,
#                                                searcher_eval_token)
# 
#     def _publish_results_to_master(self, results, evaluation_id,
#                                    searcher_eval_token):
#         raise NotImplementedError
# 
#     def receive_architecture_in_worker(self):
#         if not self.is_worker():
#             raise ValueError("Master cannot receive architecture")
#         return self._receive_architecture_in_worker()
# 
#     def _receive_architecture_in_worker(self):
#         raise NotImplementedError
# 
#     def is_ready_to_publish_architecture(self):
#         if not self.is_master():
#             raise ValueError("Worker cannot publish architecture")
#         return self._is_ready_to_publish_architecture()
# 
#     def _is_ready_to_publish_architecture(self):
#         raise NotImplementedError
# 
#     def publish_architecture_to_worker(self, vs, current_evaluation_id,
#                                        searcher_eval_token):
#         if not self.is_master():
#             raise ValueError("Worker cannot publish architecture")
#         return self._publish_architecture_to_worker(vs, current_evaluation_id,
#                                                     searcher_eval_token)
# 
#     def _publish_architecture_to_worker(self, vs, current_evaluation_id,
#                                         searcher_eval_token):
#         raise NotImplementedError
# 
#     def receive_results_in_master(self, src):
#         if not self.is_master():
#             raise ValueError("Worker cannot receive results")
#         return self._receive_results_in_master(src)
# 
#     def _receive_results_in_master(self, src):
#         raise NotImplementedError
# 
#     def kill_worker(self):
#         if not self.is_master():
#             raise ValueError("Worker cannot kill another worker")
#         return self._kill_worker()
# 
#     def _kill_worker(self):
#         raise NotImplementedError
# 
# Path: deep_architect/contrib/communicators/file_utils.py
# def consume_file(filename):
#     lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#     lock.acquire()
#     fh = lock.fh
#     fh.seek(0)
#     if len(fh.read()) is 0:
#         file_data = None
#     else:
#         fh.seek(0)
#         file_data = pickle.load(fh)
#     if file_data is not None:
#         clear_file(fh)
#         pickle.dump(None, fh)
#     lock.release()
#     return file_data
# 
# def read_file(filename):
#     lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#     lock.acquire()
#     fh = lock.fh
#     fh.seek(0)
#     if len(fh.read()) is 0:
#         file_data = None
#     else:
#         fh.seek(0)
#         file_data = pickle.load(fh)
#     lock.release()
#     return file_data
# 
# def write_file(filename, obj):
#     file_data = 0
#     while file_data is not None:
#         lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#         lock.acquire()
#         fh = lock.fh
#         fh.seek(0)
#         if len(fh.read()) is 0:
#             file_data = None
#         else:
#             fh.seek(0)
#             file_data = pickle.load(fh)
#         if file_data is None:
#             clear_file(fh)
#             pickle.dump(obj, fh)
#         lock.release()


import os
import portalocker
from deep_architect.contrib.communicators.communicator import Communicator
from deep_architect.contrib.communicators.file_utils import (consume_file,
                                                             read_file,
                                                             write_file)
        write_file(self.worker_results_prefix + str(self.rank),
                   (results, evaluation_id, searcher_eval_token))

    def _receive_architecture_in_worker(self):
        while not self.done:
            file_data = consume_file(self.worker_queue_file)
            # continue looping until there is something in the queue file
            if file_data is None:
                continue

            # if kill signal is given, return None, otherwise return contents of file
            vs, evaluation_id, searcher_eval_token, kill = file_data
            if kill:
                write_file(self.worker_results_prefix + str(self.rank), 'done')
                self.done = True
                return None

            return vs, evaluation_id, searcher_eval_token
        return None

    def _is_ready_to_publish_architecture(self):
        file_data = read_file(self.worker_queue_file)
        return file_data is None

    def _publish_architecture_to_worker(self, vs, current_evaluation_id,
                                        searcher_eval_token):
        write_file(self.worker_queue_file,
                   (vs, current_evaluation_id, searcher_eval_token, False))

    def _receive_results_in_master(self, src):

# Path: deep_architect/contrib/communicators/communicator.py
# class Communicator:
# 
#     def __init__(self, num_workers, rank):
#         self.num_workers = num_workers
#         self.rank = rank
#         self.finished = 0
# 
#     def is_master(self):
#         return self.rank == 0
# 
#     def is_worker(self):
#         return self.rank > 0
# 
#     def get_rank(self):
#         return self.rank
# 
#     def publish_results_to_master(self, results, evaluation_id,
#                                   searcher_eval_token):
#         if not self.is_worker():
#             raise ValueError("Master cannot publish results")
#         return self._publish_results_to_master(results, evaluation_id,
#                                                searcher_eval_token)
# 
#     def _publish_results_to_master(self, results, evaluation_id,
#                                    searcher_eval_token):
#         raise NotImplementedError
# 
#     def receive_architecture_in_worker(self):
#         if not self.is_worker():
#             raise ValueError("Master cannot receive architecture")
#         return self._receive_architecture_in_worker()
# 
#     def _receive_architecture_in_worker(self):
#         raise NotImplementedError
# 
#     def is_ready_to_publish_architecture(self):
#         if not self.is_master():
#             raise ValueError("Worker cannot publish architecture")
#         return self._is_ready_to_publish_architecture()
# 
#     def _is_ready_to_publish_architecture(self):
#         raise NotImplementedError
# 
#     def publish_architecture_to_worker(self, vs, current_evaluation_id,
#                                        searcher_eval_token):
#         if not self.is_master():
#             raise ValueError("Worker cannot publish architecture")
#         return self._publish_architecture_to_worker(vs, current_evaluation_id,
#                                                     searcher_eval_token)
# 
#     def _publish_architecture_to_worker(self, vs, current_evaluation_id,
#                                         searcher_eval_token):
#         raise NotImplementedError
# 
#     def receive_results_in_master(self, src):
#         if not self.is_master():
#             raise ValueError("Worker cannot receive results")
#         return self._receive_results_in_master(src)
# 
#     def _receive_results_in_master(self, src):
#         raise NotImplementedError
# 
#     def kill_worker(self):
#         if not self.is_master():
#             raise ValueError("Worker cannot kill another worker")
#         return self._kill_worker()
# 
#     def _kill_worker(self):
#         raise NotImplementedError
# 
# Path: deep_architect/contrib/communicators/file_utils.py
# def consume_file(filename):
#     lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#     lock.acquire()
#     fh = lock.fh
#     fh.seek(0)
#     if len(fh.read()) is 0:
#         file_data = None
#     else:
#         fh.seek(0)
#         file_data = pickle.load(fh)
#     if file_data is not None:
#         clear_file(fh)
#         pickle.dump(None, fh)
#     lock.release()
#     return file_data
# 
# def read_file(filename):
#     lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#     lock.acquire()
#     fh = lock.fh
#     fh.seek(0)
#     if len(fh.read()) is 0:
#         file_data = None
#     else:
#         fh.seek(0)
#         file_data = pickle.load(fh)
#     lock.release()
#     return file_data
# 
# def write_file(filename, obj):
#     file_data = 0
#     while file_data is not None:
#         lock = portalocker.Lock(filename, mode='a+b', flags=portalocker.LOCK_EX)
#         lock.acquire()
#         fh = lock.fh
#         fh.seek(0)
#         if len(fh.read()) is 0:
#             file_data = None
#         else:
#             fh.seek(0)
#             file_data = pickle.load(fh)
#         if file_data is None:
#             clear_file(fh)
#             pickle.dump(obj, fh)
#         lock.release()


# Path: deep_architect/contrib/communicators/file_communicator.py
import os
import portalocker
from deep_architect.contrib.communicators.communicator import Communicator
from deep_architect.contrib.communicators.file_utils import (consume_file,
                                                             read_file,
                                                             write_file)

        write_file(self.worker_results_prefix + str(self.rank),
                   (results, evaluation_id, searcher_eval_token))

    def _receive_architecture_in_worker(self):
        while not self.done:
            file_data = consume_file(self.worker_queue_file)
            # continue looping until there is something in the queue file
            if file_data is None:
                continue

            # if kill signal is given, return None, otherwise return contents of file
            vs, evaluation_id, searcher_eval_token, kill = file_data
            if kill:
                write_file(self.worker_results_prefix + str(self.rank), 'done')
                self.done = True
                return None

            return vs, evaluation_id, searcher_eval_token
        return None

    def _is_ready_to_publish_architecture(self):
        file_data = read_file(self.worker_queue_file)
        return file_data is None

    def _publish_architecture_to_worker(self, vs, current_evaluation_id,
                                        searcher_eval_token):
        write_file(self.worker_queue_file,
                   (vs, current_evaluation_id, searcher_eval_token, False))

    def _receive_results_in_master(self, src):

        result = consume_file(self.worker_results_prefix + str(src + 1))