<commit_before>#from random import randint

#def make_board(board):
  #  mid_line = '------\n'
    #output = ''
   # for x in range(9):
     #   if board[x] not in ['X', 'O']:
        #    output += ' '
        #else:
          #  output += board[x]
        #if x in [2, 5]:
          #  output += '\n'
            #output += mid_line
        #elif x != 8:
          #  output += '|'
   # print(output)

def check_move(board, move):
    if board[move] == ' ':
        return True
    return False

def winner(board, player):
    if board[0] == player and board[1] == player and board[2] == player:
        if player == 'X':
            return True
        return False
    if board[0] == player and board[3] == player and board[6] == player:
        if player == 'X':
            return True
        return False
    if board[0] == player and board[4] == player and board[8] == player:
        if player == 'X':
            return True
        return False
    if board[1] == player and board[4] == player and board[7] == player:
        if player == 'X':
            return True
        return False
    if board[2] == player and board[5] == player and board[8] == player:
        if player == 'X':
            return True
        return False
    if board[2] == player and board[4] == player and board[6] == player:
        if player == 'X':
            return True
        return False
    if board[3] == player and board[4] == player and board[5] == player:
        if player == 'X':
            return True
        return False
    if board[6] == player and board[7] == player and board[8] == player:
        if player == 'X':
            return True
        return False

def new_board(board, move, chariter='X'):
    output = []
    output_send = {'board': '', 'move': move, 'WL': None}
    output_str = ''
    for x in range(len(board)):
        if x != move:
            output.append(board[x])
            output_str += board[x]
        else:
            output.append(chariter)
            if chariter is 'X':
                output_str += chariter
            else:
                output_str += board[x]
    output_send['board'] = output_str
    output_send['WL'] = winner(output, chariter)
    return output_send

def directory(board, move):
    board_list = []
    for x in board:
        board_list.append(x)
    board_dic = new_board(board_list, move)
    if board_dic['WL'] is not None:
        return board_dic
    board_list = []
    for x in board_dic['board']:
        board_list.append(x)
    greedy_move = greedy_bot(board_list)
    board_dic = new_board(board_list, greedy_move ,'O')
    return board_dic

def new_board_bot(board, move, chariter='X'):
    output = []
    for x in board:
        output.append(x)
    output[move] = chariter
    return output

def greedy_bot(board):
    wins = []
    count = 0
    for a in range(len(board)):
        count += 1
        wins.append(-1)
        if board[a] == ' ':
            wins[a] = 0
            newboard_0 = new_board_bot(board, a, 'O')
            if not winner(newboard_0, 'O'):
                return a
            for s in range(len(board)):
                count += 1
                if newboard_0[s] == ' ':
                    newboard_1 = new_board_bot(newboard_0, s, 'X')
                    if winner(newboard_1, 'X'):
                        return s
                    for d in range(len(board)):
                        count += 1
                        if newboard_1[d] == ' ':
                            newboard_2 = new_board_bot(newboard_1, d, 'O')
                            if winner(newboard_2, 'O') is False:
                                wins[a] += 1
                            else:
                                for f in range(len(board)):
                                    count += 1
                                    if newboard_2[f] == ' ':
                                        newboard_3 = new_board_bot(newboard_2, f, 'X')
                                        if winner(newboard_3, 'X'):
                                            wins[a] -= 1
                                        else:
                                            for g in range(len(board)):
                                                count += 1
                                                if newboard_3[g] == ' ':
                                                    newboard_4 = new_board_bot(newboard_3, g, 'O')
                                                    if winner(newboard_4, 'O') is False:
                                                        wins[a] += 1
                                                    else:
                                                        for q in range(len(board)):
                                                            if newboard_4[q] == ' ':
                                                                newboard_5 = new_board_bot(newboard_4, q, 'X')
                                                                if winner(newboard_5, 'X'):
                                                                    wins[a] -= 1
                                                                else:
                                                                    for w in range(len(board)):
                                                                        if newboard_5[w] == ' ':
                                                                            newboard_6 = new_board_bot(newboard_5, w, 'O')
                                                                            if winner(newboard_6, 'O') is False:
                                                                                wins[a] += 1
                                                                            else:
                                                                                for e in range(len(board)):
                                                                                if newboard_6[e] == ' ':
                                                                                newboard_7 = new_board_bot(newboard_6, e, 'X')
                                                                                if winner(newboard_7, 'X'):
                                                                                wins[a] -= 1
                                                                                else:
                                                                                for r in range(len(board)):
                                                                                if newboard_7[r] ==  ' ':
                                                                                newboard_8 = new_board_bot(newboard_7, r, 'O')
                                                                                if winner(newboard_8, 'O') is False:
                                                                                wins[a] += 1
    the_max = float('-inf')
    total = 0
    for x in range(len(wins)):
        if the_max <= wins[x] and check_move(board, x):
            the_max = wins[x]
            total = x
    return total

def dumb_bot(board):
    while True:
        move = randint(0, 8)
        if board[move] == ' ':
            break
    return move

#def play_game():
    #board = [' ', ' ', ' ', ' ', ' ',  ' ', ' ',  ' ',  ' ']
    #player = randint(1, 2)
    #want_bot = 'Y'
    #move = ''
    #if want_bot == 'Y':
        #want_bot = True
    #else:
        #want_bot = False
    #turn = 0
    #while True:
        #turn += 1
        #if want_bot and player == 1:
            #while True:
                #output = 'It is player ' + str(player) + ' turn enter a move'
                #move = input(output)
                #if check_move(board, int(move)):
                    #break
                #print('Move is not vaild, please enter a vaild move ')
        #else:
            #move = greedy_bot(board)
        #if player == 1:
            #board[int(move)] = 'X'
        #else:
            #board[int(move)] = 'O'
        #if player == 1:
            #if winner(board, 'X'):
                #make_board(board)
                #output = 'player ' + str(player) + ' has won'
                #print(output)
                #break
        #else:
            #if winner(board, 'O'):
                #make_board(board)
                #output = 'player ' + str(player) + ' has won'
                #print(output)
                #break

        #make_board(board)
        #print('\n')
        #if player == 1:
            #player = 2
        #else:
            #player = 1
        #if turn == 9:
            #print('It is a tie, nobody wins')
            #break

<commit_msg>bug in bot fixed<commit_after>#from random import randint

#def make_board(board):
  #  mid_line = '------\n'
    #output = ''
   # for x in range(9):
     #   if board[x] not in ['X', 'O']:
        #    output += ' '
        #else:
          #  output += board[x]
        #if x in [2, 5]:
          #  output += '\n'
            #output += mid_line
        #elif x != 8:
          #  output += '|'
   # print(output)

def check_move(board, move):
    if board[move] == ' ':
        return True
    return False

def winner(board, player):
    if board[0] == player and board[1] == player and board[2] == player:
        if player == 'X':
            return True
        return False
    if board[0] == player and board[3] == player and board[6] == player:
        if player == 'X':
            return True
        return False
    if board[0] == player and board[4] == player and board[8] == player:
        if player == 'X':
            return True
        return False
    if board[1] == player and board[4] == player and board[7] == player:
        if player == 'X':
            return True
        return False
    if board[2] == player and board[5] == player and board[8] == player:
        if player == 'X':
            return True
        return False
    if board[2] == player and board[4] == player and board[6] == player:
        if player == 'X':
            return True
        return False
    if board[3] == player and board[4] == player and board[5] == player:
        if player == 'X':
            return True
        return False
    if board[6] == player and board[7] == player and board[8] == player:
        if player == 'X':
            return True
        return False

def new_board(board, move, chariter='X'):
    output = []
    output_send = {'board': '', 'move': move, 'WL': None}
    output_str = ''
    for x in range(len(board)):
        if x != move:
            output.append(board[x])
            output_str += board[x]
        else:
            output.append(chariter)
            if chariter is 'X':
                output_str += chariter
            else:
                output_str += board[x]
    output_send['board'] = output_str
    output_send['WL'] = winner(output, chariter)
    return output_send

def directory(board, move):
    board_list = []
    for x in board:
        board_list.append(x)
    board_dic = new_board(board_list, move)
    if board_dic['WL'] is not None:
        return board_dic
    board_list = []
    for x in board_dic['board']:
        board_list.append(x)
    greedy_move = greedy_bot(board_list)
    board_dic = new_board(board_list, greedy_move ,'O')
    return board_dic

def new_board_bot(board, move, chariter='X'):
    output = []
    for x in board:
        output.append(x)
    output[move] = chariter
    return output

def greedy_bot(board):
    wins = []
    count = 0
    for a in range(len(board)):
        count += 1
        wins.append(-1)
        if board[a] == ' ':
            wins[a] = 0
            newboard_0 = new_board_bot(board, a, 'O')
            if winner(newboard_0, 'O') is False:
                return a
            for s in range(len(board)):
                count += 1
                if newboard_0[s] == ' ':
                    newboard_1 = new_board_bot(newboard_0, s, 'X')
                    if winner(newboard_1, 'X'):
                        return s
                    for d in range(len(board)):
                        count += 1
                        if newboard_1[d] == ' ':
                            newboard_2 = new_board_bot(newboard_1, d, 'O')
                            if winner(newboard_2, 'O') is False:
                                wins[a] += 1
                            else:
                                for f in range(len(board)):
                                    count += 1
                                    if newboard_2[f] == ' ':
                                        newboard_3 = new_board_bot(newboard_2, f, 'X')
                                        if winner(newboard_3, 'X'):
                                            wins[a] -= 1
                                        else:
                                            for g in range(len(board)):
                                                count += 1
                                                if newboard_3[g] == ' ':
                                                    newboard_4 = new_board_bot(newboard_3, g, 'O')
                                                    if winner(newboard_4, 'O') is False:
                                                        wins[a] += 1
                                                    else:
                                                        for q in range(len(board)):
                                                            if newboard_4[q] == ' ':
                                                                newboard_5 = new_board_bot(newboard_4, q, 'X')
                                                                if winner(newboard_5, 'X'):
                                                                    wins[a] -= 1
                                                                else:
                                                                    for w in range(len(board)):
                                                                        if newboard_5[w] == ' ':
                                                                            newboard_6 = new_board_bot(newboard_5, w, 'O')
                                                                            if winner(newboard_6, 'O') is False:
                                                                                wins[a] += 1
                                                                            else:
                                                                                for e in range(len(board)):
                                                                                if newboard_6[e] == ' ':
                                                                                newboard_7 = new_board_bot(newboard_6, e, 'X')
                                                                                if winner(newboard_7, 'X'):
                                                                                wins[a] -= 1
                                                                                else:
                                                                                for r in range(len(board)):
                                                                                if newboard_7[r] ==  ' ':
                                                                                newboard_8 = new_board_bot(newboard_7, r, 'O')
                                                                                if winner(newboard_8, 'O') is False:
                                                                                wins[a] += 1
    the_max = float('-inf')
    total = 0
    for x in range(len(wins)):
        if the_max <= wins[x] and check_move(board, x):
            the_max = wins[x]
            total = x
    return total

def dumb_bot(board):
    while True:
        move = randint(0, 8)
        if board[move] == ' ':
            break
    return move

#def play_game():
    #board = [' ', ' ', ' ', ' ', ' ',  ' ', ' ',  ' ',  ' ']
    #player = randint(1, 2)
    #want_bot = 'Y'
    #move = ''
    #if want_bot == 'Y':
        #want_bot = True
    #else:
        #want_bot = False
    #turn = 0
    #while True:
        #turn += 1
        #if want_bot and player == 1:
            #while True:
                #output = 'It is player ' + str(player) + ' turn enter a move'
                #move = input(output)
                #if check_move(board, int(move)):
                    #break
                #print('Move is not vaild, please enter a vaild move ')
        #else:
            #move = greedy_bot(board)
        #if player == 1:
            #board[int(move)] = 'X'
        #else:
            #board[int(move)] = 'O'
        #if player == 1:
            #if winner(board, 'X'):
                #make_board(board)
                #output = 'player ' + str(player) + ' has won'
                #print(output)
                #break
        #else:
            #if winner(board, 'O'):
                #make_board(board)
                #output = 'player ' + str(player) + ' has won'
                #print(output)
                #break

        #make_board(board)
        #print('\n')
        #if player == 1:
            #player = 2
        #else:
            #player = 1
        #if turn == 9:
            #print('It is a tie, nobody wins')
            #break

<|endoftext|>


<commit_before>
from hdl_toolkit.serializer.formater import formatVhdl
from hdl_toolkit.synthetisator.rtlLevel.netlist import RtlNetlist
from hdl_toolkit.hdlObjects.typeShortcuts import vecT
from hdl_toolkit.synthetisator.codeOps import connect, If

w = connect

def LeadingZero():
    t = vecT(64)
    resT = vecT(8)
    n = RtlNetlist("LeadingZero")

    s_in = n.sig("s_in", t)
    index = n.sig("s_indexOfFirstZero", resT)

    leadingZeroTop = None  # index is index of first empty record or last one
    for i in reversed(range(8)):
        connections = w(resT.fromPy(i), index)
        if leadingZeroTop is None:
            leadingZeroTop = connections
        else:
            leadingZeroTop = If(s_in[i]._eq(0),
               connections
            ).Else(
               leadingZeroTop
            )

    interf = [s_in, index]

    return n, interf

if __name__ == "__main__":
    n, interf = LeadingZero()

    for o in n.synthetize(interf):
            print(formatVhdl(str(o)))

<commit_msg>new: leading zero detector<commit_after>
from hdl_toolkit.serializer.formater import formatVhdl
from hdl_toolkit.synthetisator.rtlLevel.netlist import RtlNetlist
from hdl_toolkit.hdlObjects.typeShortcuts import vecT
from hdl_toolkit.synthetisator.codeOps import connect, If

w = connect

def LeadingZero():
    t = vecT(64)
    resT = vecT(8)
    n = RtlNetlist("LeadingZero")

    s_in = n.sig("s_in", t)
    index = n.sig("s_indexOfFirstZero", resT)

    leadingZeroTop = None  # index is index of first empty record or last one
    for i in reversed(range(8)):
        connections = w(i, index)
        if leadingZeroTop is None:
            leadingZeroTop = connections
        else:
            leadingZeroTop = If(s_in[i]._eq(0),
               connections
            ).Else(
               leadingZeroTop
            )

    interf = [s_in, index]

    return n, interf

if __name__ == "__main__":
    n, interf = LeadingZero()

    for o in n.synthetize(interf):
            print(formatVhdl(str(o)))

<|endoftext|>


<commit_before>from easyquery import Query, QueryMaker

_list_by_pgc = {
    # fmt: off
    'hostlist_v1': (
        1952, 2052, 2081, 2437, 2778, 3089, 3743, 4948, 5139, 5619, 6110, 6656,
        6993, 8160, 8631, 9057, 9747, 9788, 9843, 9988, 10330, 10965, 771919,
        11819, 12007, 12431, 12626, 13130, 13166, 13602, 13881, 13999, 14084,
        16716, 16906, 16949, 17436, 18394, 18880, 20458, 22270, 23028, 23701,
        24464, 25069, 25248, 25886, 25955, 25999, 26246, 26483, 26649, 26939,
        27004, 27635, 27723, 27982, 28805, 28876, 29009, 30176, 31166, 31428,
        31636, 32533, 32854, 33371, 33623, 33860, 34561, 34731, 34908, 35088,
        35097, 35285, 35294, 35539, 35869, 36138, 36266, 36304, 36737, 36827,
        36914, 37178, 37366, 37483, 37845, 38031, 38049, 38555, 38588, 38802,
        38851, 39414, 40055, 40265, 40284, 41083, 41150, 42139, 44025, 44145,
        44600, 45269, 45795, 46304, 46938, 47394, 47721, 48258, 48307, 48767,
        48815, 48816, 49342, 49563, 49604, 49618, 50031, 50670, 50732, 51055,
        51340, 51471, 51564, 51620, 51787, 51932, 52116, 52266, 52365, 52411,
        52735, 52825, 53037, 53043, 53499, 53630, 53724, 53755, 54119, 54234,
        54348, 54364, 54429, 54761, 54849, 55081, 55281, 55588, 56337, 57345,
        57924, 58470, 59426, 61742, 61833, 61972, 63797, 64427, 64446, 64772,
        65436, 66318, 66566, 66648, 66812, 66934, 67018, 67146, 67546, 67663,
        67817, 67839, 67892, 67932, 68128, 68389, 68469, 68511, 68712, 68743,
        68826, 69094, 69253, 69349, 69521, 69661, 70025, 70094, 70128, 70419,
        70505, 70676, 70795, 71548, 71729, 71883, 72001, 73163,
    ),
    'paper1_complete': (9988, 23028, 52735, 55588, 58470, 68743, 70795, 71883),
    'paper1_incomplete': (279, 4948, 28805, 31166, 37845, 38031, 38802, 53499),
    # fmt: on
}

has_decals_image = Query("COVERAGE_DECALS_DR6 >= 0.95") | Query(
    "COVERAGE_DECALS_DR7 >= 0.95"
)
has_des_image = Query("COVERAGE_DES_DR1 >= 0.85")
has_sdss_image = Query("COVERAGE_SDSS >= 0.85")

has_image = Query("HAS_IMAGE > 0")
has_better_image = Query("HAS_IMAGE > 1")

potential_hosts = Query("HOST_SCORE > 0")
good_hosts = Query("HOST_SCORE >= 4")
preferred_hosts = Query("HOST_SCORE >= 4")
good = good_hosts & has_image
build_default = potential_hosts & has_image

hostlist_v1 = QueryMaker.in1d("PGC", _list_by_pgc["hostlist_v1"])
paper1_complete = QueryMaker.in1d("PGC", _list_by_pgc["paper1_complete"])
paper1_incomplete = QueryMaker.in1d("PGC", _list_by_pgc["paper1_incomplete"])
paper1 = QueryMaker.in1d(
    "PGC", _list_by_pgc["paper1_complete"] + _list_by_pgc["paper1_incomplete"]
)
paper1_observed = paper1

paper2_observed = Query("specs_ours_rvir >= 100", "HOST_SCORE >= 2")
paper2_bright_complete = Query(paper2_observed, "really_need_spec_bright < 2")
paper2_complete = Query(paper2_observed, "really_need_spec < 35.5", "sats_missed_approx < 0.32")
paper2_incomplete = Query(paper2_observed, ~paper2_complete)
paper2 = paper2_observed
<commit_msg>update host cuts<commit_after>from easyquery import Query, QueryMaker

_list_by_pgc = {
    # fmt: off
    'hostlist_v1': (
        1952, 2052, 2081, 2437, 2778, 3089, 3743, 4948, 5139, 5619, 6110, 6656,
        6993, 8160, 8631, 9057, 9747, 9788, 9843, 9988, 10330, 10965, 771919,
        11819, 12007, 12431, 12626, 13130, 13166, 13602, 13881, 13999, 14084,
        16716, 16906, 16949, 17436, 18394, 18880, 20458, 22270, 23028, 23701,
        24464, 25069, 25248, 25886, 25955, 25999, 26246, 26483, 26649, 26939,
        27004, 27635, 27723, 27982, 28805, 28876, 29009, 30176, 31166, 31428,
        31636, 32533, 32854, 33371, 33623, 33860, 34561, 34731, 34908, 35088,
        35097, 35285, 35294, 35539, 35869, 36138, 36266, 36304, 36737, 36827,
        36914, 37178, 37366, 37483, 37845, 38031, 38049, 38555, 38588, 38802,
        38851, 39414, 40055, 40265, 40284, 41083, 41150, 42139, 44025, 44145,
        44600, 45269, 45795, 46304, 46938, 47394, 47721, 48258, 48307, 48767,
        48815, 48816, 49342, 49563, 49604, 49618, 50031, 50670, 50732, 51055,
        51340, 51471, 51564, 51620, 51787, 51932, 52116, 52266, 52365, 52411,
        52735, 52825, 53037, 53043, 53499, 53630, 53724, 53755, 54119, 54234,
        54348, 54364, 54429, 54761, 54849, 55081, 55281, 55588, 56337, 57345,
        57924, 58470, 59426, 61742, 61833, 61972, 63797, 64427, 64446, 64772,
        65436, 66318, 66566, 66648, 66812, 66934, 67018, 67146, 67546, 67663,
        67817, 67839, 67892, 67932, 68128, 68389, 68469, 68511, 68712, 68743,
        68826, 69094, 69253, 69349, 69521, 69661, 70025, 70094, 70128, 70419,
        70505, 70676, 70795, 71548, 71729, 71883, 72001, 73163,
    ),
    'paper1_complete': (9988, 23028, 52735, 55588, 58470, 68743, 70795, 71883),
    'paper1_incomplete': (279, 4948, 28805, 31166, 37845, 38031, 38802, 53499),
    # fmt: on
}

has_decals_image = Query("COVERAGE_DECALS_DR6 >= 0.95") | Query(
    "COVERAGE_DECALS_DR7 >= 0.95"
)
has_des_image = Query("COVERAGE_DES_DR1 >= 0.85")
has_sdss_image = Query("COVERAGE_SDSS >= 0.85")

has_image = Query("HAS_IMAGE > 0")
has_better_image = Query("HAS_IMAGE > 1")

potential_hosts = Query("HOST_SCORE > 0")
good_exceptions = QueryMaker.in1d("HOSTID", ("nsa163956", "nsa135739", "pgc67817"))
good_hosts = Query("HOST_SCORE >= 4") | good_exceptions
good = good_hosts & has_image
build_default = potential_hosts & has_image

hostlist_v1 = QueryMaker.in1d("PGC", _list_by_pgc["hostlist_v1"])
paper1_complete = QueryMaker.in1d("PGC", _list_by_pgc["paper1_complete"])
paper1_incomplete = QueryMaker.in1d("PGC", _list_by_pgc["paper1_incomplete"])
paper1 = QueryMaker.in1d(
    "PGC", _list_by_pgc["paper1_complete"] + _list_by_pgc["paper1_incomplete"]
)
paper1_observed = paper1

paper2_observed = Query("specs_ours_rvir >= 100", good_hosts)
paper2_bright_complete = Query(paper2_observed, "really_need_spec_bright < 2")
paper2_complete_definition = Query("really_need_spec < 35.5", "sats_missed_approx < 0.32")
paper2_complete = Query(paper2_observed, paper2_complete_definition)
paper2_incomplete = Query(paper2_observed, ~paper2_complete)
paper2 = paper2_observed
<|endoftext|>


<commit_before>import datetime

from third import filetail

from __reader import Reader
from __message import Message

class AzionAnalytics(Reader):
    """Conf:
        - logpath (required): path of log file,
            e.g. /tmp/my.log
        - delimiter (required): character to split log lines,
            e.g. '\t'
        - columns (required): list of columns for each log line,
            e.g. ['date', 'hour', 'message']"""

    def setup(self):
        self.check_conf(['delimiter', 'columns', 'logpath'])
        self.tail = filetail.Tail(self.logpath, max_sleep=1)
        self.client = self.logpath.split('.')[1]
        self.time_format = "%d/%b/%Y:%H:%M:%S"
        self.start_counters()

    def start_counters(self):
        if self.count:
            self.agg_count = {}
            for key, value in self.count.items():
                d = {'value' : 0,
                     'start_time' : 0,
                     'content' : value[0],
                     'interval' : value[1]*60}
                self.agg_count[key] = d

    def check_conf(self, items):
        for item in items:
            if not hasattr(self, item):
                print '%s not defined in conf.yaml.' % item
                exit(-1)

    def dictify_line(self, line):
        return dict(zip(self.columns, line.strip().split(self.delimiter)))

    def get_datetime(self, date_format):
        return datetime.datetime.strptime(date_format, self.time_format)

    def get_minute(self, date):
        return date - datetime.timedelta(0, date.second)

    def add_interval(self, date, seconds):
        return date + datetime.timedelta(0, seconds)

    def get_empty_periods(self, beginning, end, period):
        empty_periods = []
        while beginning + period <= end:
            empty_periods.append(beginning)
            beginning = beginning + period
        return empty_periods

    def store_empty_periods(self, line, metadata, empty_periods):
        for empty_period in empty_periods:
            checkpoint = self.generate_checkpoint(line)
            content = {'count' : 0,
                       'client' : self.client,
                       'match' : metadata['content'],
                       'time' : empty_period}
            msg = Message(checkpoint=checkpoint,
                          content=content)
            self.store(msg)

    def set_first(self, metadata, time):
        metadata['value'] = 1
        metadata['start_time'] = time

    def generate_checkpoint(self, pos):
        checkpoint = {'pos' : pos}
        checkpoint.update({'count' : self.agg_count})
        return checkpoint

    def read(self):
        cur_time = 0
        if self.checkpoint_enabled and self.last_checkpoint:
            self.agg_count = self.last_checkpoint['count']
            self.tail.seek_bytes(self.last_checkpoint['pos'])
        while True:
            try:
                line = self.tail.nextline()
                cur_pos = self.tail.pos
                line_data = self.dictify_line(line)
                try:
                    cur_time = self.get_datetime(line_data['time_local'][1:21])
                    cur_minute = self.get_minute(cur_time)
                except ValueError:
                    print 'Cannot parse date, skipping line'
                    continue

                for column, metadata in self.agg_count.items():
                    #match occurred
                    if line_data[column] == metadata['content']:

                        #first occurrence
                        if metadata['start_time'] == 0:
                            self.set_first(metadata, cur_minute)
                            continue

                        time_passed = cur_time - metadata['start_time']

                        #in the interval
                        if time_passed.seconds <= metadata['interval']:
                            metadata['value'] += 1
                            continue

                        #closing interval
                        else:
                            time = metadata['start_time'].strftime(self.time_format)
                            checkpoint = self.generate_checkpoint(cur_pos)
                            content={'count_' + column : metadata['value'],
                                     'client' : self.client,
                                     'match' : metadata['content'],
                                     'time' : time}
                            msg = Message(checkpoint=checkpoint,
                                          content=content)
                            self.store(msg)

                            #empty periods
                            next_interval = self.add_interval(metadata['start_time'],
                                                              metadata['interval'])
                            empty_periods = self.get_empty_periods(next_interval,
                                                                   cur_minute,
                                                                   datetime.timedelta(0, metadata['interval']))
                            self.store_empty_periods(cur_pos, metadata, empty_periods)
                            self.set_first(metadata, cur_minute)

            except Exception, e:
                print 'error reading line: %s' % line
                print e
<commit_msg>message format changes<commit_after>import datetime

from third import filetail

from __reader import Reader
from __message import Message

class AzionAnalytics(Reader):
    """Conf:
        - logpath (required): path of log file,
            e.g. /tmp/my.log
        - delimiter (required): character to split log lines,
            e.g. '\t'
        - columns (required): list of columns for each log line,
            e.g. ['date', 'hour', 'message']"""

    def setup(self):
        self.check_conf(['delimiter', 'columns', 'logpath'])
        self.tail = filetail.Tail(self.logpath, max_sleep=1)
        self.client = self.logpath.split('.')[1]
        self.time_format = "%d/%b/%Y:%H:%M:%S"
        self.start_counters()

    def start_counters(self):
        if self.count:
            self.agg_count = {}
            for key, value in self.count.items():
                d = {'value' : 0,
                     'start_time' : 0,
                     'content' : value[0],
                     'interval' : value[1]*60}
                self.agg_count[key] = d

    def check_conf(self, items):
        for item in items:
            if not hasattr(self, item):
                print '%s not defined in conf.yaml.' % item
                exit(-1)

    def dictify_line(self, line):
        return dict(zip(self.columns, line.strip().split(self.delimiter)))

    def get_datetime(self, date_format):
        return datetime.datetime.strptime(date_format, self.time_format)

    def get_minute(self, date):
        return date - datetime.timedelta(0, date.second)

    def add_interval(self, date, seconds):
        return date + datetime.timedelta(0, seconds)

    def get_empty_periods(self, beginning, end, period):
        empty_periods = []
        while beginning + period <= end:
            empty_periods.append(beginning)
            beginning = beginning + period
        return empty_periods

    def store_empty_periods(self, pos, column, metadata, empty_periods):
        for empty_period in empty_periods:
            checkpoint = self.generate_checkpoint(pos)
            content = {'count' : 0,
                       'client' : self.client,
                       'interval_duration_sec' : metadata['interval'],
                       column : metadata['content'],
                       'interval_started_at' : empty_period}
            msg = Message(checkpoint=checkpoint,
                          content=content)
            self.store(msg)

    def set_first(self, metadata, time):
        metadata['value'] = 1
        metadata['start_time'] = time

    def generate_checkpoint(self, pos):
        checkpoint = {'pos' : pos}
        checkpoint.update({'count' : self.agg_count})
        return checkpoint

    def read(self):
        cur_time = 0
        if self.checkpoint_enabled and self.last_checkpoint:
            self.agg_count = self.last_checkpoint['count']
            self.tail.seek_bytes(self.last_checkpoint['pos'])
        while True:
            try:
                line = self.tail.nextline()
                cur_pos = self.tail.pos
                line_data = self.dictify_line(line)
                try:
                    cur_time = self.get_datetime(line_data['time_local'][1:21])
                    cur_minute = self.get_minute(cur_time)
                except ValueError:
                    print 'Cannot parse date, skipping line'
                    continue

                for column, metadata in self.agg_count.items():
                    #match occurred
                    if line_data[column] == metadata['content']:

                        #first occurrence
                        if metadata['start_time'] == 0:
                            self.set_first(metadata, cur_minute)
                            continue

                        time_passed = cur_time - metadata['start_time']

                        #in the interval
                        if time_passed.seconds <= metadata['interval']:
                            metadata['value'] += 1
                            continue

                        #closing interval
                        else:
                            time = metadata['start_time'].strftime(self.time_format)
                            checkpoint = self.generate_checkpoint(cur_pos)
                            content={'count_' + column : metadata['value'],
                                     'client' : self.client,
                                     'interval_duration_sec' : metadata['interval'],
                                      column : metadata['content'],
                                     'interval_started_at' : time}
                            msg = Message(checkpoint=checkpoint,
                                          content=content)
                            self.store(msg)

                            #empty periods
                            next_interval = self.add_interval(metadata['start_time'],
                                                              metadata['interval'])
                            empty_periods = self.get_empty_periods(next_interval,
                                                                   cur_minute,
                                                                   datetime.timedelta(0, metadata['interval']))
                            self.store_empty_periods(cur_pos, column, metadata, empty_periods)
                            self.set_first(metadata, cur_minute)

            except Exception, e:
                print 'error reading line: %s' % line
                print e
<|endoftext|>


<commit_before>import time as ttime  # tea time
from datetime import datetime
from ophyd import (ProsilicaDetector, SingleTrigger, TIFFPlugin,
                   ImagePlugin, StatsPlugin, DetectorBase, HDF5Plugin,
                   AreaDetector, EpicsSignal, EpicsSignalRO, ROIPlugin,
                   TransformPlugin, ProcessPlugin, Device)
from ophyd.areadetector.cam import AreaDetectorCam
from ophyd.areadetector.base import ADComponent, EpicsSignalWithRBV
from ophyd.areadetector.filestore_mixins import (FileStoreTIFFIterativeWrite,
                                                 FileStoreHDF5IterativeWrite,
                                                 FileStoreBase, new_short_uid,
                                                 FileStoreIterativeWrite)
from ophyd import Component as Cpt, Signal
from ophyd.utils import set_and_wait
import filestore.api as fs
from pathlib import PurePath
from bluesky.plans import stage, unstage, open_run, close_run, trigger_and_read, pause

class TIFFPluginWithFileStore(TIFFPlugin, FileStoreTIFFIterativeWrite):
    """Add this as a component to detectors that write TIFFs."""
    pass

class TIFFPluginEnsuredOff(TIFFPlugin):
    """Add this as a component to detectors that do not write TIFFs."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs.update([('auto_save', 'No')])

class StandardProsilica(SingleTrigger, ProsilicaDetector):
    image = Cpt(ImagePlugin, 'image1:')
    stats1 = Cpt(StatsPlugin, 'Stats1:')
    stats2 = Cpt(StatsPlugin, 'Stats2:')
    stats3 = Cpt(StatsPlugin, 'Stats3:')
    stats4 = Cpt(StatsPlugin, 'Stats4:')
    stats5 = Cpt(StatsPlugin, 'Stats5:')
    trans1 = Cpt(TransformPlugin, 'Trans1:')
    roi1 = Cpt(ROIPlugin, 'ROI1:')
    roi2 = Cpt(ROIPlugin, 'ROI2:')
    roi3 = Cpt(ROIPlugin, 'ROI3:')
    roi4 = Cpt(ROIPlugin, 'ROI4:')
    proc1 = Cpt(ProcessPlugin, 'Proc1:')

    # This class does not save TIFFs. We make it aware of the TIFF plugin
    # only so that it can ensure that the plugin is not auto-saving.
    tiff = Cpt(TIFFPluginEnsuredOff, suffix='TIFF1:')

class StandardProsilicaWithTIFF(StandardProsilica):
    tiff = Cpt(TIFFPluginWithFileStore,
               suffix='TIFF1:',
               write_path_template='/XF11ID/data/%Y/%m/%d/',
               root='/XF11ID/data',
               fs=db.reg)

class EigerSimulatedFilePlugin(Device, FileStoreBase):
    sequence_id = ADComponent(EpicsSignalRO, 'SequenceId')
    file_path = ADComponent(EpicsSignalWithRBV, 'FilePath', string=True)
    file_write_name_pattern = ADComponent(EpicsSignalWithRBV, 'FWNamePattern',
                                          string=True)
    file_write_images_per_file = ADComponent(EpicsSignalWithRBV,
                                             'FWNImagesPerFile')
    current_run_start_uid = Cpt(Signal, value='', add_prefix=())

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._datum_kwargs_map = dict()  # store kwargs for each uid

    def stage(self):
        res_uid = new_short_uid()
        write_path = datetime.now().strftime(self.write_path_template)
        set_and_wait(self.file_path, write_path)
        set_and_wait(self.file_write_name_pattern, '{}_$id'.format(res_uid))
        super().stage()
        fn = (PurePath(self.file_path.get()) / res_uid).relative_to(self.root)

        ipf = int(self.file_write_images_per_file.get())
        # logger.debug("Inserting resource with filename %s", fn)
        self._resource = fs.insert_resource('AD_EIGER2',
                                            fn,
                                            {'images_per_file': ipf},
                                            root=str(self.root))

    def generate_datum(self, key, timestamp):
        uid = super().generate_datum(key, timestamp)
        # The detector keeps its own counter which is uses label HDF5
        # sub-files.  We access that counter via the sequence_id
        # signal and stash it in the datum.
        seq_id = 1 + int(self.sequence_id.get())  # det writes to the NEXT one
        fs.insert_datum(self._resource, uid, {'seq_id': seq_id})
        return uid

class EigerBase(AreaDetector):
    """
    Eiger, sans any triggering behavior.

    Use EigerSingleTrigger or EigerFastTrigger below.
    """
    num_triggers = ADComponent(EpicsSignalWithRBV, 'cam1:NumTriggers')
    file = Cpt(EigerSimulatedFilePlugin, suffix='cam1:',
               write_path_template='/XF11ID/data/%Y/%m/%d/',
               root='/XF11ID/',
               fs=db.reg)
    beam_center_x = ADComponent(EpicsSignalWithRBV, 'cam1:BeamX')
    beam_center_y = ADComponent(EpicsSignalWithRBV, 'cam1:BeamY')
    wavelength = ADComponent(EpicsSignalWithRBV, 'cam1:Wavelength')
    det_distance = ADComponent(EpicsSignalWithRBV, 'cam1:DetDist')
    threshold_energy = ADComponent(EpicsSignalWithRBV, 'cam1:ThresholdEnergy')
    photon_energy = ADComponent(EpicsSignalWithRBV, 'cam1:PhotonEnergy')
    manual_trigger = ADComponent(EpicsSignalWithRBV, 'cam1:ManualTrigger')  # the checkbox
    special_trigger_button = ADComponent(EpicsSignal, 'cam1:Trigger')  # the button next to 'Start' and 'Stop'
    image = Cpt(ImagePlugin, 'image1:')
    stats1 = Cpt(StatsPlugin, 'Stats1:')
    stats2 = Cpt(StatsPlugin, 'Stats2:')
    stats3 = Cpt(StatsPlugin, 'Stats3:')
    stats4 = Cpt(StatsPlugin, 'Stats4:')
    stats5 = Cpt(StatsPlugin, 'Stats5:')
    roi1 = Cpt(ROIPlugin, 'ROI1:')
    roi2 = Cpt(ROIPlugin, 'ROI2:')
    roi3 = Cpt(ROIPlugin, 'ROI3:')
    roi4 = Cpt(ROIPlugin, 'ROI4:')
    proc1 = Cpt(ProcessPlugin, 'Proc1:')

    shutter_mode = ADComponent(EpicsSignalWithRBV, 'cam1:ShutterMode')

    # hotfix: shadow non-existant PV
    size_link = None

    def stage(self):
        # before parent
        super().stage()
        # after parent
        set_and_wait(self.manual_trigger, 1)

    def unstage(self):
        set_and_wait(self.manual_trigger, 0)
        super().unstage()

class EigerSingleTrigger(SingleTrigger, EigerBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs['cam.trigger_mode'] = 0
        self.stage_sigs['shutter_mode'] = 1  # 'EPICS PV'
        self.stage_sigs.update({'num_triggers': 1})

    def trigger(self):
        status = super().trigger()
        set_and_wait(self.special_trigger_button, 1)
        return status

class FastShutterTrigger(Device):
    """This represents the fast trigger *device*.

    See below, FastTriggerMixin, which defines the trigging logic.
    """
    auto_shutter_mode = Cpt(EpicsSignal, 'Mode-Sts', write_pv='Mode-Cmd')
    num_images = Cpt(EpicsSignal, 'NumImages-SP')
    exposure_time = Cpt(EpicsSignal, 'ExposureTime-SP')
    acquire_period = Cpt(EpicsSignal, 'AcquirePeriod-SP')
    acquire = Cpt(EpicsSignal, 'Acquire-Cmd', trigger_value=1)

class EigerFastTrigger(EigerBase):
    tr = Cpt(FastShutterTrigger, '')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs['cam.trigger_mode'] = 3  # 'External Enable' mode
        self.stage_sigs['shutter_mode'] = 0  # 'EPICS PV'
        self.stage_sigs['tr.auto_shutter_mode'] = 1  # 'Enable'

    def trigger(self):
        self.dispatch('image', ttime.time())
        return self.tr.trigger()

# test_trig4M = FastShutterTrigger('XF:11IDB-ES{Trigger:Eig4M}', name='test_trig4M')

## This renaming should be reversed: no correspondance between CSS screens, PV names and ophyd....
xray_eye1 = StandardProsilica('XF:11IDA-BI{Bpm:1-Cam:1}', name='xray_eye1')
xray_eye2 = StandardProsilica('XF:11IDB-BI{Mon:1-Cam:1}', name='xray_eye2')
xray_eye3 = StandardProsilica('XF:11IDB-BI{Cam:08}', name='xray_eye3')
xray_eye4 = StandardProsilica('XF:11IDB-BI{Cam:09}', name='xray_eye4')
xray_eye1_writing = StandardProsilicaWithTIFF('XF:11IDA-BI{Bpm:1-Cam:1}', name='xray_eye1')
xray_eye2_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Mon:1-Cam:1}', name='xray_eye2')
xray_eye3_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Cam:08}', name='xray_eye3')
xray_eye4_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Cam:09}', name='xray_eye4')
fs1 = StandardProsilica('XF:11IDA-BI{FS:1-Cam:1}', name='fs1')
fs2 = StandardProsilica('XF:11IDA-BI{FS:2-Cam:1}', name='fs2')
fs_wbs = StandardProsilica('XF:11IDA-BI{BS:WB-Cam:1}', name='fs_wbs')
# dcm_cam = StandardProsilica('XF:11IDA-BI{Mono:DCM-Cam:1}', name='dcm_cam')
fs_pbs = StandardProsilica('XF:11IDA-BI{BS:PB-Cam:1}', name='fs_pbs')
# elm = Elm('XF:11IDA-BI{AH401B}AH401B:',)

all_standard_pros = [xray_eye1, xray_eye2, xray_eye3, xray_eye4,
                     xray_eye1_writing, xray_eye2_writing,
                     xray_eye3_writing, xray_eye4_writing, fs1, fs2,
                     fs_wbs, fs_pbs]
#                     xray_eye3_writing, fs1, fs2, dcm_cam, fs_wbs, fs_pbs]
for camera in all_standard_pros:
    camera.read_attrs = ['stats1', 'stats2', 'stats3', 'stats4', 'stats5']
    # camera.tiff.read_attrs = []  # leaving just the 'image'
    for stats_name in ['stats1', 'stats2', 'stats3', 'stats4', 'stats5']:
        stats_plugin = getattr(camera, stats_name)
        stats_plugin.read_attrs = ['total']
        camera.stage_sigs['stats_plugin.blocking_callbacks'] = 1

    camera.stage_sigs['camera.roi1.blocking_callbacks'] = 1
    camera.stage_sigs['camera.trans1.blocking_callbacks'] = 1
    camera.stage_sigs['camera.cam.trigger_mode'] = 'Fixed Rate'

for camera in [xray_eye1_writing, xray_eye2_writing,
               xray_eye3_writing, xray_eye4_writing]:
    camera.read_attrs.append('tiff')
    camera.tiff.read_attrs = []

def set_eiger_defaults(eiger):
    """Choose which attributes to read per-step (read_attrs) or
    per-run (configuration attrs)."""

    eiger.file.read_attrs = []
    eiger.read_attrs = ['file', 'stats1', 'stats2',
                        'stats3', 'stats4', 'stats5']
    for stats in [eiger.stats1, eiger.stats2, eiger.stats3,
                  eiger.stats4, eiger.stats5]:
        stats.read_attrs = ['total']
    eiger.configuration_attrs = ['beam_center_x', 'beam_center_y',
                                 'wavelength', 'det_distance', 'cam',
                                 'threshold_energy', 'photon_energy']
    eiger.cam.read_attrs = []
    eiger.cam.configuration_attrs = ['acquire_time', 'acquire_period',
                                     'num_images']

# Eiger 500k using internal trigger
# eiger500K_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig500K}', name='eiger500K_single')
# set_eiger_defaults(eiger500K_single)

# Eiger 1M using internal trigger
eiger1m_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig1M}',
                                    name='eiger1m_single')
set_eiger_defaults(eiger1m_single)

# Eiger 4M using internal trigger
eiger4m_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig4M}',
                                    name='eiger4m_single')
set_eiger_defaults(eiger4m_single)

# Eiger 1M using fast trigger assembly
eiger1m = EigerFastTrigger('XF:11IDB-ES{Det:Eig1M}', name='eiger1m')
set_eiger_defaults(eiger1m)

# Eiger 4M using fast trigger assembly
eiger4m = EigerFastTrigger('XF:11IDB-ES{Det:Eig4M}', name='eiger4m')
set_eiger_defaults(eiger4m)

def manual_count(det=eiger4m_single):
    detectors = [det]
    for det in detectors:
        yield from stage(det)
        yield from open_run()
        print("All slow setup code has been run. "
              "Type RE.resume() when ready to acquire.")
        yield from pause()
        yield from trigger_and_read(detectors)
        yield from close_run()
        for det in detectors:
            yield from unstage(det)

# Comment this out to suppress deluge of logging messages.
# import logging
# logging.basicConfig(level=logging.DEBUG)
# import ophyd.areadetector.filestore_mixins
# ophyd.areadetector.filestore_mixins.logger.setLevel(logging.DEBUG)
<commit_msg>API: update Asset Registry usage<commit_after>import time as ttime  # tea time
from datetime import datetime
from ophyd import (ProsilicaDetector, SingleTrigger, TIFFPlugin,
                   ImagePlugin, StatsPlugin, DetectorBase, HDF5Plugin,
                   AreaDetector, EpicsSignal, EpicsSignalRO, ROIPlugin,
                   TransformPlugin, ProcessPlugin, Device)
from ophyd.areadetector.cam import AreaDetectorCam
from ophyd.areadetector.base import ADComponent, EpicsSignalWithRBV
from ophyd.areadetector.filestore_mixins import (FileStoreTIFFIterativeWrite,
                                                 FileStoreHDF5IterativeWrite,
                                                 FileStoreBase, new_short_uid,
                                                 FileStoreIterativeWrite)
from ophyd import Component as Cpt, Signal
from ophyd.utils import set_and_wait
from pathlib import PurePath
from bluesky.plans import stage, unstage, open_run, close_run, trigger_and_read, pause

class TIFFPluginWithFileStore(TIFFPlugin, FileStoreTIFFIterativeWrite):
    """Add this as a component to detectors that write TIFFs."""
    pass

class TIFFPluginEnsuredOff(TIFFPlugin):
    """Add this as a component to detectors that do not write TIFFs."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs.update([('auto_save', 'No')])

class StandardProsilica(SingleTrigger, ProsilicaDetector):
    image = Cpt(ImagePlugin, 'image1:')
    stats1 = Cpt(StatsPlugin, 'Stats1:')
    stats2 = Cpt(StatsPlugin, 'Stats2:')
    stats3 = Cpt(StatsPlugin, 'Stats3:')
    stats4 = Cpt(StatsPlugin, 'Stats4:')
    stats5 = Cpt(StatsPlugin, 'Stats5:')
    trans1 = Cpt(TransformPlugin, 'Trans1:')
    roi1 = Cpt(ROIPlugin, 'ROI1:')
    roi2 = Cpt(ROIPlugin, 'ROI2:')
    roi3 = Cpt(ROIPlugin, 'ROI3:')
    roi4 = Cpt(ROIPlugin, 'ROI4:')
    proc1 = Cpt(ProcessPlugin, 'Proc1:')

    # This class does not save TIFFs. We make it aware of the TIFF plugin
    # only so that it can ensure that the plugin is not auto-saving.
    tiff = Cpt(TIFFPluginEnsuredOff, suffix='TIFF1:')

class StandardProsilicaWithTIFF(StandardProsilica):
    tiff = Cpt(TIFFPluginWithFileStore,
               suffix='TIFF1:',
               write_path_template='/XF11ID/data/%Y/%m/%d/',
               root='/XF11ID/data',
               fs=db.reg)

class EigerSimulatedFilePlugin(Device, FileStoreBase):
    sequence_id = ADComponent(EpicsSignalRO, 'SequenceId')
    file_path = ADComponent(EpicsSignalWithRBV, 'FilePath', string=True)
    file_write_name_pattern = ADComponent(EpicsSignalWithRBV, 'FWNamePattern',
                                          string=True)
    file_write_images_per_file = ADComponent(EpicsSignalWithRBV,
                                             'FWNImagesPerFile')
    current_run_start_uid = Cpt(Signal, value='', add_prefix=())

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._datum_kwargs_map = dict()  # store kwargs for each uid

    def stage(self):
        res_uid = new_short_uid()
        write_path = datetime.now().strftime(self.write_path_template)
        set_and_wait(self.file_path, write_path)
        set_and_wait(self.file_write_name_pattern, '{}_$id'.format(res_uid))
        super().stage()
        fn = (PurePath(self.file_path.get()) / res_uid).relative_to(self.root)

        ipf = int(self.file_write_images_per_file.get())
        # logger.debug("Inserting resource with filename %s", fn)
        self._resource = self._reg.register_resource(
            'AD_EIGER2',
            str(self.root), fn,
            {'images_per_file': ipf})

    def generate_datum(self, key, timestamp, datum_kwargs):
        # The detector keeps its own counter which is uses label HDF5
        # sub-files.  We access that counter via the sequence_id
        # signal and stash it in the datum.
        seq_id = 1 + int(self.sequence_id.get())  # det writes to the NEXT one
        datum_kwargs.update({'seq_id': seq_id})
        return super().generate_datum(key, timestamp, datum_kwargs)

class EigerBase(AreaDetector):
    """
    Eiger, sans any triggering behavior.

    Use EigerSingleTrigger or EigerFastTrigger below.
    """
    num_triggers = ADComponent(EpicsSignalWithRBV, 'cam1:NumTriggers')
    file = Cpt(EigerSimulatedFilePlugin, suffix='cam1:',
               write_path_template='/XF11ID/data/%Y/%m/%d/',
               root='/XF11ID/',
               fs=db.reg)
    beam_center_x = ADComponent(EpicsSignalWithRBV, 'cam1:BeamX')
    beam_center_y = ADComponent(EpicsSignalWithRBV, 'cam1:BeamY')
    wavelength = ADComponent(EpicsSignalWithRBV, 'cam1:Wavelength')
    det_distance = ADComponent(EpicsSignalWithRBV, 'cam1:DetDist')
    threshold_energy = ADComponent(EpicsSignalWithRBV, 'cam1:ThresholdEnergy')
    photon_energy = ADComponent(EpicsSignalWithRBV, 'cam1:PhotonEnergy')
    manual_trigger = ADComponent(EpicsSignalWithRBV, 'cam1:ManualTrigger')  # the checkbox
    special_trigger_button = ADComponent(EpicsSignal, 'cam1:Trigger')  # the button next to 'Start' and 'Stop'
    image = Cpt(ImagePlugin, 'image1:')
    stats1 = Cpt(StatsPlugin, 'Stats1:')
    stats2 = Cpt(StatsPlugin, 'Stats2:')
    stats3 = Cpt(StatsPlugin, 'Stats3:')
    stats4 = Cpt(StatsPlugin, 'Stats4:')
    stats5 = Cpt(StatsPlugin, 'Stats5:')
    roi1 = Cpt(ROIPlugin, 'ROI1:')
    roi2 = Cpt(ROIPlugin, 'ROI2:')
    roi3 = Cpt(ROIPlugin, 'ROI3:')
    roi4 = Cpt(ROIPlugin, 'ROI4:')
    proc1 = Cpt(ProcessPlugin, 'Proc1:')

    shutter_mode = ADComponent(EpicsSignalWithRBV, 'cam1:ShutterMode')

    # hotfix: shadow non-existant PV
    size_link = None

    def stage(self):
        # before parent
        super().stage()
        # after parent
        set_and_wait(self.manual_trigger, 1)

    def unstage(self):
        set_and_wait(self.manual_trigger, 0)
        super().unstage()

class EigerSingleTrigger(SingleTrigger, EigerBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs['cam.trigger_mode'] = 0
        self.stage_sigs['shutter_mode'] = 1  # 'EPICS PV'
        self.stage_sigs.update({'num_triggers': 1})

    def trigger(self):
        status = super().trigger()
        set_and_wait(self.special_trigger_button, 1)
        return status

class FastShutterTrigger(Device):
    """This represents the fast trigger *device*.

    See below, FastTriggerMixin, which defines the trigging logic.
    """
    auto_shutter_mode = Cpt(EpicsSignal, 'Mode-Sts', write_pv='Mode-Cmd')
    num_images = Cpt(EpicsSignal, 'NumImages-SP')
    exposure_time = Cpt(EpicsSignal, 'ExposureTime-SP')
    acquire_period = Cpt(EpicsSignal, 'AcquirePeriod-SP')
    acquire = Cpt(EpicsSignal, 'Acquire-Cmd', trigger_value=1)

class EigerFastTrigger(EigerBase):
    tr = Cpt(FastShutterTrigger, '')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.stage_sigs['cam.trigger_mode'] = 3  # 'External Enable' mode
        self.stage_sigs['shutter_mode'] = 0  # 'EPICS PV'
        self.stage_sigs['tr.auto_shutter_mode'] = 1  # 'Enable'

    def trigger(self):
        self.dispatch('image', ttime.time())
        return self.tr.trigger()

# test_trig4M = FastShutterTrigger('XF:11IDB-ES{Trigger:Eig4M}', name='test_trig4M')

## This renaming should be reversed: no correspondance between CSS screens, PV names and ophyd....
xray_eye1 = StandardProsilica('XF:11IDA-BI{Bpm:1-Cam:1}', name='xray_eye1')
xray_eye2 = StandardProsilica('XF:11IDB-BI{Mon:1-Cam:1}', name='xray_eye2')
xray_eye3 = StandardProsilica('XF:11IDB-BI{Cam:08}', name='xray_eye3')
xray_eye4 = StandardProsilica('XF:11IDB-BI{Cam:09}', name='xray_eye4')
xray_eye1_writing = StandardProsilicaWithTIFF('XF:11IDA-BI{Bpm:1-Cam:1}', name='xray_eye1')
xray_eye2_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Mon:1-Cam:1}', name='xray_eye2')
xray_eye3_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Cam:08}', name='xray_eye3')
xray_eye4_writing = StandardProsilicaWithTIFF('XF:11IDB-BI{Cam:09}', name='xray_eye4')
fs1 = StandardProsilica('XF:11IDA-BI{FS:1-Cam:1}', name='fs1')
fs2 = StandardProsilica('XF:11IDA-BI{FS:2-Cam:1}', name='fs2')
fs_wbs = StandardProsilica('XF:11IDA-BI{BS:WB-Cam:1}', name='fs_wbs')
# dcm_cam = StandardProsilica('XF:11IDA-BI{Mono:DCM-Cam:1}', name='dcm_cam')
fs_pbs = StandardProsilica('XF:11IDA-BI{BS:PB-Cam:1}', name='fs_pbs')
# elm = Elm('XF:11IDA-BI{AH401B}AH401B:',)

all_standard_pros = [xray_eye1, xray_eye2, xray_eye3, xray_eye4,
                     xray_eye1_writing, xray_eye2_writing,
                     xray_eye3_writing, xray_eye4_writing, fs1, fs2,
                     fs_wbs, fs_pbs]
#                     xray_eye3_writing, fs1, fs2, dcm_cam, fs_wbs, fs_pbs]
for camera in all_standard_pros:
    camera.read_attrs = ['stats1', 'stats2', 'stats3', 'stats4', 'stats5']
    # camera.tiff.read_attrs = []  # leaving just the 'image'
    for stats_name in ['stats1', 'stats2', 'stats3', 'stats4', 'stats5']:
        stats_plugin = getattr(camera, stats_name)
        stats_plugin.read_attrs = ['total']
        camera.stage_sigs['stats_plugin.blocking_callbacks'] = 1

    camera.stage_sigs['camera.roi1.blocking_callbacks'] = 1
    camera.stage_sigs['camera.trans1.blocking_callbacks'] = 1
    camera.stage_sigs['camera.cam.trigger_mode'] = 'Fixed Rate'

for camera in [xray_eye1_writing, xray_eye2_writing,
               xray_eye3_writing, xray_eye4_writing]:
    camera.read_attrs.append('tiff')
    camera.tiff.read_attrs = []

def set_eiger_defaults(eiger):
    """Choose which attributes to read per-step (read_attrs) or
    per-run (configuration attrs)."""

    eiger.file.read_attrs = []
    eiger.read_attrs = ['file', 'stats1', 'stats2',
                        'stats3', 'stats4', 'stats5']
    for stats in [eiger.stats1, eiger.stats2, eiger.stats3,
                  eiger.stats4, eiger.stats5]:
        stats.read_attrs = ['total']
    eiger.configuration_attrs = ['beam_center_x', 'beam_center_y',
                                 'wavelength', 'det_distance', 'cam',
                                 'threshold_energy', 'photon_energy']
    eiger.cam.read_attrs = []
    eiger.cam.configuration_attrs = ['acquire_time', 'acquire_period',
                                     'num_images']

# Eiger 500k using internal trigger
# eiger500K_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig500K}', name='eiger500K_single')
# set_eiger_defaults(eiger500K_single)

# Eiger 1M using internal trigger
eiger1m_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig1M}',
                                    name='eiger1m_single')
set_eiger_defaults(eiger1m_single)

# Eiger 4M using internal trigger
eiger4m_single = EigerSingleTrigger('XF:11IDB-ES{Det:Eig4M}',
                                    name='eiger4m_single')
set_eiger_defaults(eiger4m_single)

# Eiger 1M using fast trigger assembly
eiger1m = EigerFastTrigger('XF:11IDB-ES{Det:Eig1M}', name='eiger1m')
set_eiger_defaults(eiger1m)

# Eiger 4M using fast trigger assembly
eiger4m = EigerFastTrigger('XF:11IDB-ES{Det:Eig4M}', name='eiger4m')
set_eiger_defaults(eiger4m)

def manual_count(det=eiger4m_single):
    detectors = [det]
    for det in detectors:
        yield from stage(det)
        yield from open_run()
        print("All slow setup code has been run. "
              "Type RE.resume() when ready to acquire.")
        yield from pause()
        yield from trigger_and_read(detectors)
        yield from close_run()
        for det in detectors:
            yield from unstage(det)

# Comment this out to suppress deluge of logging messages.
# import logging
# logging.basicConfig(level=logging.DEBUG)
# import ophyd.areadetector.filestore_mixins
# ophyd.areadetector.filestore_mixins.logger.setLevel(logging.DEBUG)
<|endoftext|>


<commit_before># Copyright 2019 Omar Castiñeira, Comunitea Servicios Tecnológicos S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api, _, exceptions
import numpy

class AccountMoveLine(models.Model):

    _inherit = 'account.move.line'

    scheme = fields.Selection([('CORE', 'Basic (CORE)'),
                               ('B2B', 'Enterprise (B2B)')], string='Scheme',
                              related="mandate_id.scheme", readonly=True)
    pner_vat = fields.Char("CIF/NIF/VAT", related="partner_id.vat",
                           readonly=True)

    last_rec_date = fields.Date(
        compute='_compute_last_rec_date',
        store=True,
        index=False,
        string='Last reconciliation date',
        help="The date of the last reconciliation (full) "
             "account move line."
    )

    @api.depends('full_reconcile_id.reconciled_line_ids.date')
    def _compute_last_rec_date(self):
        for line in self:
            if line.full_reconcile_id:
                move_lines = line.full_reconcile_id.reconciled_line_ids
                last_line = move_lines.sorted(lambda l: l.date)[-1]
                line.last_rec_date = last_line.date

class AccountInvoiceLine(models.Model):

    _inherit = 'account.invoice.line'

    purchase_supplier_reference = fields.Char(
        'Supplier reference', related='purchase_id.partner_ref',
        readonly=True)
    sale_order_id = fields.Many2one("sale.order", "Sale", readonly=True,
                                    related="sale_line_ids.order_id")
    cost_unit = fields.Float("Product cost price")

    invoice_line_tax_ids = fields.Many2many(required=True)

class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    @api.multi
    def _get_sale_order_ids(self):
        for invoice in self:
            invoice.sale_order_ids = invoice.invoice_line_ids.\
                mapped('sale_line_ids.order_id')

    attach_picking = fields.Boolean('Attach picking')
    sale_order_ids = fields.Many2many('sale.order', string='Sale Orders',
                                      compute="_get_sale_order_ids",
                                      readonly=True,
                                      help="This is the list of sale orders "
                                           "linked to this invoice. ")
    country_id = fields.Many2one('res.country', 'Country',
                                 related="commercial_partner_id.country_id",
                                 readonly=True, store=False)
    invoice_type_id = fields. \
        Many2one('res.partner.invoice.type', 'Invoice type', readonly=True,
                 related="commercial_partner_id.invoice_type_id")
    not_send_email = fields.Boolean("Not send email")
    total = fields.Float("Total Paid", compute="total_paid")
    last_payment = fields.Date("Last Payment", compute="last_payment_date")
    partner_commercial = fields.Many2one("res.users", String="Commercial",
                                         related="partner_id.user_id",
                                         readonly=True)

    date_due = fields.Date(states={'draft': [('readonly', False)],
                                   'open': [('readonly', False)]})
    state_web = fields.Char('State web', compute='_get_state_web', store=True)
    payment_mode_id = fields.Many2one(states={'draft': [('readonly', False)], 'open': [('readonly', False)]})

    name = fields.Char(string='Supplier Reference')
    reference = fields.Char(string='N. Supplier Invoice (SII)')

    @api.multi
    @api.depends('state', 'payment_mode_id', 'payment_move_line_ids')
    def _get_state_web(self):
        for invoice in self:
            res = ''
            invoice_state = invoice.state
            if invoice.payment_mode_id:
                if invoice.state == 'open' and invoice.returned_payment:
                    res = 'returned'
                elif invoice.state == 'paid' \
                        and invoice.payment_mode_id.transfer_account_id \
                        and invoice.payment_mode_id.payment_method_id.code == \
                        'sepa_direct_debit':
                    res = invoice._check_payments()
                else:
                    res = invoice_state
            else:
                res = invoice_state

            invoice.state_web = res

    @api.multi
    def _check_payments(self):
        self.ensure_one()
        res = ''
        for payment in self.payment_move_line_ids:
            for payment_account in payment.move_id.line_ids:
                if payment_account.account_id.id == \
                        self.payment_mode_id.transfer_account_id.id:
                    for reconcile_line in payment_account.full_reconcile_id.\
                            reconciled_line_ids:
                        if reconcile_line.move_id != payment.move_id and \
                                reconcile_line.credit != 0:
                            res = 'paid'
                            return res
                else:
                    res = 'remitted'
        return res

    @api.onchange('user_id')
    def onchage_user_id(self):
        if self.user_id:
            team_obj = self.env['crm.team']
            self.team_id = team_obj.search([('member_ids', 'in',
                                             [self.user_id.id])])

    @api.multi
    def total_paid(self):
        for invoice in self:
            if invoice.state in ('open', 'paid'):
                invoice.total = invoice.amount_total - invoice.residual
            else:
                invoice.total = 0.0

    @api.multi
    def last_payment_date(self):
        for invoice in self:
            if invoice.payment_move_line_ids:
                invoice.last_payment = \
                    invoice.payment_move_line_ids[0].date

    @api.model
    def create(self, vals):
        if vals.get('partner_id', False):
            partner = self.env["res.partner"].browse(vals["partner_id"])
            if partner.commercial_partner_id.attach_picking:
                vals["attach_picking"] = \
                    partner.commercial_partner_id.attach_picking
            vals['team_id'] = partner.commercial_partner_id.team_id.id
            if vals.get('type', False) == "out_refund":
                # vencimiento inmediato en rectificativas
                vals["payment_term_id"] = self.env.ref('account.account_payment_term_immediate').id
            invoice_type = (partner.invoice_type_id
                            or partner.commercial_partner_id.invoice_type_id)
            if invoice_type and invoice_type.journal_id:
                vals['journal_id'] = invoice_type.journal_id.id
        return super().create(vals)

    @api.onchange('partner_id', 'company_id')
    def _onchange_partner_id(self):
        result = super()._onchange_partner_id()
        company_id = self.company_id.id
        p = self.partner_id if not company_id else \
            self.partner_id.with_context(force_company=company_id)
        if p:
            self.attach_picking = p.commercial_partner_id.attach_picking
            self.team_id = p.commercial_partner_id.team_id.id
            self.user_id = p.commercial_partner_id.user_id.id
            if self.type != "in_invoice":
                self.currency_id = p.commercial_partner_id.\
                    property_purchase_currency_id.id or self.env.user.company_id.currency_id.id
            if self.type == "out_refund":
                self.payment_term_id = False
                self.date_due = fields.Date.today()
                self.payment_mode_id = self.partner_id.commercial_partner_id.customer_payment_mode_id
            invoice_type = (p.invoice_type_id or p.commercial_partner_id.invoice_type_id)
            if invoice_type and invoice_type.journal_id:
                self.journal_id = invoice_type.journal_id.id
            else:
                self.journal_id = self.with_context(type=self.type)._default_journal()
        return result

    @api.multi
    @api.returns('account.move.line')
    def _get_payment(self):
        self.ensure_one()
        payments = self.env['account.move.line']
        if self.type == "out_invoice" and self.sale_order_ids:
            payments = self.sale_order_ids.mapped('payment_line_ids').filtered(lambda l: not l.reconciled)
        return payments

    @api.multi
    def action_move_create(self):
        res = super().action_move_create()
        for inv in self:
            if inv.type == 'out_refund':
                inv.move_id.line_ids.\
                    write({'blocked': inv.payment_mode_id.blocked})
            else:
                inv.move_id.line_ids.\
                    write({'blocked': inv.payment_mode_id.blocked or
                                      inv.payment_term_id.blocked})
            payment_move_lines = inv._get_payment()
            for payment_line in payment_move_lines:
                inv.assign_outstanding_credit(payment_line.id)
        return res

    @api.multi
    def write(self, vals):
        res = super().write(vals)
        if vals.get('partner_id', False):
            partner = self.env["res.partner"].browse(vals["partner_id"])
            self.write({'team_id':partner.commercial_partner_id.team_id.id})
        if vals.get('payment_mode_id', False):
            for inv in self:
                if inv.move_id and inv.payment_mode_id.blocked:
                    inv.move_id.line_ids.\
                        write({'blocked': inv.payment_mode_id.blocked})
        elif vals.get('payment_term_id', False):
            for inv in self:
                if inv.move_id and inv.payment_term_id.blocked:
                    inv.move_id.line_ids.\
                        write({'blocked': inv.payment_term_id.blocked})
        return res

    @api.multi
    def invoice_validate(self):
        res = super().invoice_validate()
        for inv in self:
            if not inv.claim_id:
                for line in inv.invoice_line_ids:
                    cost = line.product_id.standard_price_2
                    if line.move_line_ids:
                        if line.product_id.bom_ids and line.product_id.bom_ids[0].type == 'phantom':
                            # We need to multiply by qty when te product is pack, because the product in the
                            # stock_move is just the component
                            cost = 0.0
                            for move in line.move_line_ids:
                                cost += move.price_unit * (move.product_qty/line.quantity) * -1
                        else:
                            cost = numpy.average(line.move_line_ids.mapped('price_unit')) * -1
                    line.write({'cost_unit': cost or line.product_id.standard_price_2})
            else:
                for line in inv.invoice_line_ids:
                    if not line.cost_unit:
                        # We choose the standard_price and not the 2 because is the one used in the picking
                        line.write({'cost_unit': line.product_id.standard_price})
        return res

    @api.model
    def _get_first_invoice_fields(self, invoice):
        res = super()._get_first_invoice_fields(invoice)
        res.update({'team_id': invoice.team_id.id})
        return res

    @api.multi
    @api.onchange('date_due')
    def onchange_date_due(self):
        if self.state == 'open':
            return {'warning': {
                'title': _('Warning'),
                'message':
                _('Remember to change due date in associated payment(s)')
            }}

    def _prepare_invoice_line_from_po_line(self, line):
        data = super()._prepare_invoice_line_from_po_line(line)
        if self.currency_id != line.order_id.currency_id:
            self.currency_id = line.order_id.currency_id
        return data

    @api.onchange('payment_mode_id')
    def _onchange_payment_mode_id(self):
        super()._onchange_payment_mode_id()
        self.move_id.line_ids.filtered(lambda l: l.account_id.code == '43000000').write({'payment_mode_id': self.payment_mode_id.id})

    scheme = fields.Selection(related="mandate_id.scheme")

class PaymentMode(models.Model):

    _inherit = 'account.payment.mode'

    blocked = fields.Boolean('No Follow-up')
    not_send_emails = fields.Boolean('Not send emails')

class AccountPaymentTerm(models.Model):

    _inherit = "account.payment.term"

    blocked = fields.Boolean('No Follow-up')

class AccountInvoiceRefund(models.TransientModel):

    _inherit = "account.invoice.refund"

    @api.multi
    def compute_refund(self, mode='refund'):
        res = super().compute_refund(mode=mode)
        if mode == "modify":
            new_invoices = []
            inv_obj = self.env['account.invoice']
            orig_invoice = inv_obj.browse(self.env.context['active_ids'][0])
            for tup in res['domain']:
                if tup[0] == "id":
                    new_invoices = inv_obj.browse(tup[2])
                    break
            for new_invoice in new_invoices:
                new_invoice.\
                    write({'user_id': orig_invoice.user_id.id or False,
                           'team_id': orig_invoice.team_id.id or False,
                           'partner_bank_id': orig_invoice.partner_bank_id.id
                           or False,
                           'mandate_id': orig_invoice.mandate_id.id or False,
                           'payment_mode_id': orig_invoice.payment_mode_id.id
                           or False})
        return res

class InvoiceMerge(models.TransientModel):
    _inherit = "invoice.merge"

    @api.model
    def _dirty_check(self):
        res = super()._dirty_check()
        if self.env.context.get('active_model', '') == 'account.invoice':
            invs = self.env['account.invoice'].\
                browse(self.env.context['active_ids'])
            for d in invs:
                if d['payment_term_id'] != invs[0]['payment_term_id']:
                    raise exceptions.Warning(
                        _('Not all invoices have the same payment term!'))
                if d['payment_mode_id'] != invs[0]['payment_mode_id']:
                    raise exceptions.Warning(
                        _('Not all invoices use the same payment mode!'))
                if d['user_id'] != invs[0]['user_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are at the same salesperson!'))
                if d['team_id'] != invs[0]['team_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are at the same sales team!'))
                if d['invoice_type_id'] != invs[0]['invoice_type_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are of the same invoice type!'))
        return res

class AccountPayment(models.Model):
    _inherit = "account.payment"

    def _create_payment_entry(self, amount):
        res = super()._create_payment_entry(amount)

        if 'RCONF' in self.journal_id.code or 'RPAG' in self.journal_id.code:
            lines = self.mapped('move_line_ids')
            for line in lines:
                if not line.full_reconcile_id:
                    line.write({'blocked': True})

        return res
<commit_msg>[FIX] custom_account: corregido actualización estado web<commit_after># Copyright 2019 Omar Castiñeira, Comunitea Servicios Tecnológicos S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api, _, exceptions
import numpy

class AccountMoveLine(models.Model):

    _inherit = 'account.move.line'

    scheme = fields.Selection([('CORE', 'Basic (CORE)'),
                               ('B2B', 'Enterprise (B2B)')], string='Scheme',
                              related="mandate_id.scheme", readonly=True)
    pner_vat = fields.Char("CIF/NIF/VAT", related="partner_id.vat",
                           readonly=True)

    last_rec_date = fields.Date(
        compute='_compute_last_rec_date',
        store=True,
        index=False,
        string='Last reconciliation date',
        help="The date of the last reconciliation (full) "
             "account move line."
    )

    @api.depends('full_reconcile_id.reconciled_line_ids.date')
    def _compute_last_rec_date(self):
        for line in self:
            if line.full_reconcile_id:
                move_lines = line.full_reconcile_id.reconciled_line_ids
                last_line = move_lines.sorted(lambda l: l.date)[-1]
                line.last_rec_date = last_line.date

class AccountInvoiceLine(models.Model):

    _inherit = 'account.invoice.line'

    purchase_supplier_reference = fields.Char(
        'Supplier reference', related='purchase_id.partner_ref',
        readonly=True)
    sale_order_id = fields.Many2one("sale.order", "Sale", readonly=True,
                                    related="sale_line_ids.order_id")
    cost_unit = fields.Float("Product cost price")

    invoice_line_tax_ids = fields.Many2many(required=True)

class AccountInvoice(models.Model):

    _inherit = 'account.invoice'

    @api.multi
    def _get_sale_order_ids(self):
        for invoice in self:
            invoice.sale_order_ids = invoice.invoice_line_ids.\
                mapped('sale_line_ids.order_id')

    attach_picking = fields.Boolean('Attach picking')
    sale_order_ids = fields.Many2many('sale.order', string='Sale Orders',
                                      compute="_get_sale_order_ids",
                                      readonly=True,
                                      help="This is the list of sale orders "
                                           "linked to this invoice. ")
    country_id = fields.Many2one('res.country', 'Country',
                                 related="commercial_partner_id.country_id",
                                 readonly=True, store=False)
    invoice_type_id = fields. \
        Many2one('res.partner.invoice.type', 'Invoice type', readonly=True,
                 related="commercial_partner_id.invoice_type_id")
    not_send_email = fields.Boolean("Not send email")
    total = fields.Float("Total Paid", compute="total_paid")
    last_payment = fields.Date("Last Payment", compute="last_payment_date")
    partner_commercial = fields.Many2one("res.users", String="Commercial",
                                         related="partner_id.user_id",
                                         readonly=True)

    date_due = fields.Date(states={'draft': [('readonly', False)],
                                   'open': [('readonly', False)]})
    state_web = fields.Char('State web', compute='_get_state_web', store=True)
    payment_mode_id = fields.Many2one(states={'draft': [('readonly', False)], 'open': [('readonly', False)]})

    name = fields.Char(string='Supplier Reference')
    reference = fields.Char(string='N. Supplier Invoice (SII)')

    @api.multi
    @api.depends('state', 'payment_mode_id', 'payment_move_line_ids','payment_move_line_ids.move_id.line_ids.full_reconcile_id')
    def _get_state_web(self):
        for invoice in self:
            res = ''
            invoice_state = invoice.state
            if invoice.payment_mode_id:
                if invoice.state == 'open' and invoice.returned_payment:
                    res = 'returned'
                elif invoice.state == 'paid' \
                        and invoice.payment_mode_id.transfer_account_id \
                        and invoice.payment_mode_id.payment_method_id.code == \
                        'sepa_direct_debit':
                    res = invoice._check_payments()
                else:
                    res = invoice_state
            else:
                res = invoice_state

            invoice.state_web = res

    @api.multi
    def _check_payments(self):
        self.ensure_one()
        res = ''
        for payment in self.payment_move_line_ids:
            for payment_account in payment.move_id.line_ids:
                if payment_account.account_id.id == \
                        self.payment_mode_id.transfer_account_id.id:
                    for reconcile_line in payment_account.full_reconcile_id.\
                            reconciled_line_ids:
                        if reconcile_line.move_id != payment.move_id and \
                                reconcile_line.credit != 0:
                            res = 'paid'
                            return res
                else:
                    res = 'remitted'
        return res

    @api.onchange('user_id')
    def onchage_user_id(self):
        if self.user_id:
            team_obj = self.env['crm.team']
            self.team_id = team_obj.search([('member_ids', 'in',
                                             [self.user_id.id])])

    @api.multi
    def total_paid(self):
        for invoice in self:
            if invoice.state in ('open', 'paid'):
                invoice.total = invoice.amount_total - invoice.residual
            else:
                invoice.total = 0.0

    @api.multi
    def last_payment_date(self):
        for invoice in self:
            if invoice.payment_move_line_ids:
                invoice.last_payment = \
                    invoice.payment_move_line_ids[0].date

    @api.model
    def create(self, vals):
        if vals.get('partner_id', False):
            partner = self.env["res.partner"].browse(vals["partner_id"])
            if partner.commercial_partner_id.attach_picking:
                vals["attach_picking"] = \
                    partner.commercial_partner_id.attach_picking
            vals['team_id'] = partner.commercial_partner_id.team_id.id
            if vals.get('type', False) == "out_refund":
                # vencimiento inmediato en rectificativas
                vals["payment_term_id"] = self.env.ref('account.account_payment_term_immediate').id
            invoice_type = (partner.invoice_type_id
                            or partner.commercial_partner_id.invoice_type_id)
            if invoice_type and invoice_type.journal_id:
                vals['journal_id'] = invoice_type.journal_id.id
        return super().create(vals)

    @api.onchange('partner_id', 'company_id')
    def _onchange_partner_id(self):
        result = super()._onchange_partner_id()
        company_id = self.company_id.id
        p = self.partner_id if not company_id else \
            self.partner_id.with_context(force_company=company_id)
        if p:
            self.attach_picking = p.commercial_partner_id.attach_picking
            self.team_id = p.commercial_partner_id.team_id.id
            self.user_id = p.commercial_partner_id.user_id.id
            if self.type != "in_invoice":
                self.currency_id = p.commercial_partner_id.\
                    property_purchase_currency_id.id or self.env.user.company_id.currency_id.id
            if self.type == "out_refund":
                self.payment_term_id = False
                self.date_due = fields.Date.today()
                self.payment_mode_id = self.partner_id.commercial_partner_id.customer_payment_mode_id
            invoice_type = (p.invoice_type_id or p.commercial_partner_id.invoice_type_id)
            if invoice_type and invoice_type.journal_id:
                self.journal_id = invoice_type.journal_id.id
            else:
                self.journal_id = self.with_context(type=self.type)._default_journal()
        return result

    @api.multi
    @api.returns('account.move.line')
    def _get_payment(self):
        self.ensure_one()
        payments = self.env['account.move.line']
        if self.type == "out_invoice" and self.sale_order_ids:
            payments = self.sale_order_ids.mapped('payment_line_ids').filtered(lambda l: not l.reconciled)
        return payments

    @api.multi
    def action_move_create(self):
        res = super().action_move_create()
        for inv in self:
            if inv.type == 'out_refund':
                inv.move_id.line_ids.\
                    write({'blocked': inv.payment_mode_id.blocked})
            else:
                inv.move_id.line_ids.\
                    write({'blocked': inv.payment_mode_id.blocked or
                                      inv.payment_term_id.blocked})
            payment_move_lines = inv._get_payment()
            for payment_line in payment_move_lines:
                inv.assign_outstanding_credit(payment_line.id)
        return res

    @api.multi
    def write(self, vals):
        res = super().write(vals)
        if vals.get('partner_id', False):
            partner = self.env["res.partner"].browse(vals["partner_id"])
            self.write({'team_id':partner.commercial_partner_id.team_id.id})
        if vals.get('payment_mode_id', False):
            for inv in self:
                if inv.move_id and inv.payment_mode_id.blocked:
                    inv.move_id.line_ids.\
                        write({'blocked': inv.payment_mode_id.blocked})
        elif vals.get('payment_term_id', False):
            for inv in self:
                if inv.move_id and inv.payment_term_id.blocked:
                    inv.move_id.line_ids.\
                        write({'blocked': inv.payment_term_id.blocked})
        return res

    @api.multi
    def invoice_validate(self):
        res = super().invoice_validate()
        for inv in self:
            if not inv.claim_id:
                for line in inv.invoice_line_ids:
                    cost = line.product_id.standard_price_2
                    if line.move_line_ids:
                        if line.product_id.bom_ids and line.product_id.bom_ids[0].type == 'phantom':
                            # We need to multiply by qty when te product is pack, because the product in the
                            # stock_move is just the component
                            cost = 0.0
                            for move in line.move_line_ids:
                                cost += move.price_unit * (move.product_qty/line.quantity) * -1
                        else:
                            cost = numpy.average(line.move_line_ids.mapped('price_unit')) * -1
                    line.write({'cost_unit': cost or line.product_id.standard_price_2})
            else:
                for line in inv.invoice_line_ids:
                    if not line.cost_unit:
                        # We choose the standard_price and not the 2 because is the one used in the picking
                        line.write({'cost_unit': line.product_id.standard_price})
        return res

    @api.model
    def _get_first_invoice_fields(self, invoice):
        res = super()._get_first_invoice_fields(invoice)
        res.update({'team_id': invoice.team_id.id})
        return res

    @api.multi
    @api.onchange('date_due')
    def onchange_date_due(self):
        if self.state == 'open':
            return {'warning': {
                'title': _('Warning'),
                'message':
                _('Remember to change due date in associated payment(s)')
            }}

    def _prepare_invoice_line_from_po_line(self, line):
        data = super()._prepare_invoice_line_from_po_line(line)
        if self.currency_id != line.order_id.currency_id:
            self.currency_id = line.order_id.currency_id
        return data

    @api.onchange('payment_mode_id')
    def _onchange_payment_mode_id(self):
        super()._onchange_payment_mode_id()
        self.move_id.line_ids.filtered(lambda l: l.account_id.code == '43000000').write({'payment_mode_id': self.payment_mode_id.id})

    scheme = fields.Selection(related="mandate_id.scheme")

class PaymentMode(models.Model):

    _inherit = 'account.payment.mode'

    blocked = fields.Boolean('No Follow-up')
    not_send_emails = fields.Boolean('Not send emails')

class AccountPaymentTerm(models.Model):

    _inherit = "account.payment.term"

    blocked = fields.Boolean('No Follow-up')

class AccountInvoiceRefund(models.TransientModel):

    _inherit = "account.invoice.refund"

    @api.multi
    def compute_refund(self, mode='refund'):
        res = super().compute_refund(mode=mode)
        if mode == "modify":
            new_invoices = []
            inv_obj = self.env['account.invoice']
            orig_invoice = inv_obj.browse(self.env.context['active_ids'][0])
            for tup in res['domain']:
                if tup[0] == "id":
                    new_invoices = inv_obj.browse(tup[2])
                    break
            for new_invoice in new_invoices:
                new_invoice.\
                    write({'user_id': orig_invoice.user_id.id or False,
                           'team_id': orig_invoice.team_id.id or False,
                           'partner_bank_id': orig_invoice.partner_bank_id.id
                           or False,
                           'mandate_id': orig_invoice.mandate_id.id or False,
                           'payment_mode_id': orig_invoice.payment_mode_id.id
                           or False})
        return res

class InvoiceMerge(models.TransientModel):
    _inherit = "invoice.merge"

    @api.model
    def _dirty_check(self):
        res = super()._dirty_check()
        if self.env.context.get('active_model', '') == 'account.invoice':
            invs = self.env['account.invoice'].\
                browse(self.env.context['active_ids'])
            for d in invs:
                if d['payment_term_id'] != invs[0]['payment_term_id']:
                    raise exceptions.Warning(
                        _('Not all invoices have the same payment term!'))
                if d['payment_mode_id'] != invs[0]['payment_mode_id']:
                    raise exceptions.Warning(
                        _('Not all invoices use the same payment mode!'))
                if d['user_id'] != invs[0]['user_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are at the same salesperson!'))
                if d['team_id'] != invs[0]['team_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are at the same sales team!'))
                if d['invoice_type_id'] != invs[0]['invoice_type_id']:
                    raise exceptions.Warning(
                        _('Not all invoices are of the same invoice type!'))
        return res

class AccountPayment(models.Model):
    _inherit = "account.payment"

    def _create_payment_entry(self, amount):
        res = super()._create_payment_entry(amount)

        if 'RCONF' in self.journal_id.code or 'RPAG' in self.journal_id.code:
            lines = self.mapped('move_line_ids')
            for line in lines:
                if not line.full_reconcile_id:
                    line.write({'blocked': True})

        return res
<|endoftext|>


<commit_before>from django import template

register = template.Library()

from django.utils.html import format_html

@register.simple_tag(name='percent', takes_context=True)
def percent(context, arg1, arg2):
    if ( arg1 == 0 ) or ( arg2 == 0 ):
        return 0
    else:
        return int( 100*arg1/arg2 )<commit_msg>#31 templatetag without context<commit_after>from django import template

register = template.Library()

from django.utils.html import format_html

@register.simple_tag(name='percent', takes_context=False)
def percent(arg1, arg2):
    if ( arg1 == 0 ) or ( arg2 == 0 ):
        return 0
    else:
        return int( 100*arg1/arg2 )<|endoftext|>