def test_changer(self):
      script_str = r'''
x = func1(a,b,c)
func2(1,2,3)'''
      modified_script_str = r'''
x = func1(00,11,22)
func2(1,2,3)'''

      script = Script(script_str)
      manager = ParameterChangerManager(script)
      changer = manager._create_changer_from_pos(0)
      changer.set_parameters(['00', '11', '22'])
      self.assertEqual(script.get_code_as_str(), modified_script_str)
      params = changer.get_parameters()
      self.assertEqual(params[0], '00')
      self.assertEqual(params[1], '11')
      self.assertEqual(params[2], '22')
      

      changer.set_parameters(['a','b', 'c'])
      self.assertEqual(script.get_code_as_str(), script_str)
      params = changer.get_parameters()
      self.assertEqual(params[0], 'a')
      self.assertEqual(params[1], 'b')
      self.assertEqual(params[2], 'c')
Exemplo n.º 2
0
def main():
    print 'Cargando... rate = %r...' % get_ratio()
    with DAOokwDataContext(_CS_OKW) as okw:
        okw.CommandTimeout = _DEFAULT_COMMAND_TIMEOUT
        with DAOSomatomDataContext(_CS_OKW) as somatom:
            somatom.CommandTimeout = _DEFAULT_COMMAND_TIMEOUT

            script = Script(okw, somatom, get_ratio())
            script.procesa()
    print 'listo...'
Exemplo n.º 3
0
 def birthday(self):
     now = datetime.datetime.now()
     today = datetime.datetime(2000, now.month, now.day)
     bday_users = session.query(Follower).filter_by(birthday=today)
     subtable = session.query(ConditionScript).filter_by(is_blind=0).filter_by(keyword='bday')
     for bday_user in bday_users:
         script = Script(NixUtil.get_random_from_subtable(subtable).contents)
         script.replace(u'%{아이디}', bday_user.mention_id)
         script.replace(u'%{이름}', bday_user.screen_name)
         script.translate()
         self.do_tweet(script)
     return
Exemplo n.º 4
0
    def validate_forms(self):
        """Validate form."""
        root_pass = self.ids.pr2.text
        username = self.ids.us1.text
        user_pass = self.ids.us3.text
        home = self.ids.us4.text
        shell = self.ids.us5.text

        pre = self.ids.pre.text
        pos = self.ids.pos.text

        if self.source:
            s = self.source
        else:
            s = 'cdrom'

        folder = ''
        server = ''
        partition = self.ids.partition.text
        ftp_user = self.ids.ftp_user.text
        ftp_pass = self.ids.ftp_pass.text

        print('SOURCE:' + self.source)
        if s == 'Hard drive':
            folder = self.ids.hh_folder.text
        elif s == 'NFS':
            folder = self.ids.nfs_folder.text
            server = self.ids.nfs_server.text
        elif s == 'HTTP':
            folder = self.ids.http_folder.text
            server = self.ids.http_server.text
        elif s == 'FTP':
            folder = self.ids.ftp_folder.text
            server = self.ids.ftp_server.text

        source = Source()
        source.save_source(s, partition, folder, server, ftp_user, ftp_pass)

        # if self.active is True and self.ids.pr1.text
        # is not self.ids.pr2.text:
        # print(self.ids.pr1.focus)
        # popup = InfoPopup()
        # popup.set_info('Root passwords do not match')
        # popup.open()
        user = User()
        user.save_user(root_pass, username, user_pass, home, shell)
        script = Script()
        script.save_script(pre, pos)
        section = Section()
        section.create_file()
Exemplo n.º 5
0
    def active(self):
        log.debug('process_active_mention called')
        if self.config['active_mention'] == 'false':
            log.info('nixiko active mention off')
            return

        active_subtable = session.query(ActiveScript).filter_by(is_blind=0)
        active = NixUtil.get_random_from_subtable(active_subtable)
        script = Script(active.contents, active.image_keyword)
        followers = session.query(Follower).filter_by(is_blocked=0)
        while True:
            target_candidate = NixUtil.get_random_from_subtable(followers)
            try:
                target = self.api.get_user(target_candidate.mention_id)
                break
            except tweepy.error.TweepError:
                log.error(u'user not exist: {}({})'.format(target_candidate.screen_name, target_candidate.mention_id))

        script.replace(u'%{이름}', target.name)
        script.replace(u'%{아이디}', target.screen_name)
        script.translate()

        mention_id = target.screen_name
        log.info(u'active mention to {}: {}'.format(mention_id, script.script))

        self.do_tweet(script)
        return
Exemplo n.º 6
0
    def process_toggle_block_mention(self, minfo, keyword):
        log.debug(u'process_toggle_block_mention called with keyword: {}'.format(keyword.encode('utf8')))
        user = session.query(Follower).filter_by(uniqueid = minfo['id'])

        try: user = user.one()
        except NoResultFound: user = session.query(Follower).filter_by(mention_id = minfo['screen_name'])
        try: user = user.one()
        except: user = Follower(minfo['screen_name'], minfo['name'])

        SCRIPT_STOP = u'알았다요. 이제 먼저 말 걸지 않겠다요.. '
        SCRIPT_RESUME = u'헤헤 알았다요! 이제 닉시코가 먼저 말을 걸 수도 있다요!'

        if keyword.split()[1] in (u'중지', u'그만', u'중단'): # if suspend
            user.is_blocked = 1
            log.info(u'사용자 블록 전환: '.format(minfo['screen_name']))
            script = Script(SCRIPT_STOP)
            script.mention(minfo['screen_name'])
            self.do_tweet(script, minfo['id'])
        else: # if resume
            user.is_blocked = 0
            log.info(u'사용자 블록 해제: '.format(minfo['screen_name']))
            script = Script(SCRIPT_RESUME)
            script.mention(minfo['screen_name'])
            self.do_tweet(script, minfo['id'])

        session.add(user)
        session.commit()
        log.debug('toggle_block_mention done')
Exemplo n.º 7
0
class StageManager:
    def __init__(self, actors, topics, pool):
        self.characters = self.generateCharacters(actors)
        self.topics = topics
        self.pool = pool
        self.script = Script("TEST SCRIPT")

    def produceScript(self, topic):
        for character in self.characters:
            line = Line(random.choice(self.pool), character)
            self.addLine(character, line)
        return self.script

    def generateCharacters(self, actors):
        characters = []
        for ii in range(0, actors):
            name = "Actor " + str(ii+1)
            characters.append(Character(name))
        return characters

    def addLine(self, character, line):
    	character.addLine(line)
    	self.script.addLine(line)
Exemplo n.º 8
0
def installParcel(parcel, oldVersion=None):

    scripts = pim.KindCollection.update(parcel, 'scripts',
        kind=Script.getKind(parcel.itsView)
    )

    scriptsCollection = \
        pim.SmartCollection.update(parcel, 'scriptsCollection',
            renameable = False,
            private = False,
            source=scripts,
            displayName='Scripts'
            )
    userScripts = UserCollection(scriptsCollection)
    userScripts.dontDisplayAsCalendar = True


    from blocks import installBlocks
    installBlocks(parcel, oldVersion)
Exemplo n.º 9
0
 def __init__(self, amount, script_pubkey):
     self.amount = amount
     self.script_pubkey = Script.parse(script_pubkey)
Exemplo n.º 10
0
def save_genesis_block():

    script_sig = Script(
        [bytes.fromhex("ffff001d"),
         bytes.fromhex("04"), GENESIS_THE_TIMES])

    script_pubkey = Script([GENESIS_SEC_PUBKEY, 0xac])

    tx_in = TxIn(prev_tx=b"\x00" * 32,
                 prev_index=0xffffffff,
                 script_sig=script_sig,
                 sequence=0xffffffff)

    tx_out = TxOut(amount=GENESIS_BLOCK_REWARD, script_pubkey=script_pubkey)

    tx = Tx(version=1, tx_ins=[tx_in], tx_outs=[tx_out], locktime=0)

    block = Block_Full(version=1,
                       prev_block=b"\x00" * 32,
                       merkle_root=GENESIS_BLOCK_MERKLE_ROOT,
                       timestamp=GENESIS_BLOCK_TIMESTAMP,
                       bits=GENESIS_BLOCK_BITS,
                       nonce=GENESIS_BLOCK_NONCE,
                       txs=[tx])

    tx = block.txs[0]
    script_sig_cmds = tx.tx_ins[0].script_sig.cmds
    script_pubkey_cmds = tx.tx_outs[0].script_pubkey.cmds
    GENESIS_SCRIPT_SIG_DB = Script_db(cmds=script_sig_cmds)
    GENESIS_SCRIPT_PUBKEY_DB = Script_db(cmds=script_pubkey_cmds)
    tx_in = tx.tx_ins[0]
    GENESIS_TX_IN_DB = TxIn_db(prev_tx=tx_in.prev_tx,
                               prev_index=tx_in.prev_index,
                               script_sig=GENESIS_SCRIPT_SIG_DB,
                               sequence=tx_in.sequence)

    tx_out = tx.tx_outs[0]
    GENESIS_TX_OUT_DB = TxOut_db(amount=tx_out.amount,
                                 script_pubkey=GENESIS_SCRIPT_PUBKEY_DB)

    tx_size = len(tx.serialize())

    GENESIS_TX_DB = Tx_db(segwit=False,
                          tx_hash=tx.hash(),
                          version=tx.version,
                          tx_ins=[GENESIS_TX_IN_DB],
                          tx_outs=[GENESIS_TX_OUT_DB],
                          locktime=tx.locktime,
                          size=tx_size)

    block_size = block.size()
    try:
        GENESIS_BLOCK_DB = Block_db(block_hash=GENESIS_BLOCK_HASH,
                                    block_height=0,
                                    block_reward=GENESIS_BLOCK_REWARD,
                                    version=1,
                                    prev_block=b"\x00" * 32,
                                    merkle_root=GENESIS_BLOCK_MERKLE_ROOT,
                                    timestamp=GENESIS_BLOCK_TIMESTAMP,
                                    bits=GENESIS_BLOCK_BITS,
                                    nonce=GENESIS_BLOCK_NONCE,
                                    txs=[GENESIS_TX_DB],
                                    size=block_size,
                                    mined_btc=GENESIS_BLOCK_REWARD,
                                    total_size=block_size).save()
    except:
        pass
Exemplo n.º 11
0
prev_tx2 = bytes.fromhex(
    '47c0095c4e40c79128037c3861445c7f7d93e6ff9b55c10274395798f3503da8')
prev_index2 = 1

tx_in1 = TxIn(prev_tx1, prev_index1)
tx_in2 = TxIn(prev_tx2, prev_index2)

# where we send our sats

tx_outs = []
target_amount = 200000
target_h160 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1')
target_script = p2pkh_script(target_h160)
target_output = TxOut(amount=target_amount, script_pubkey=target_script)
tx_obj = Tx(1, [tx_in1, tx_in2], [target_output], 0, True)

# we sign

z1 = tx_obj.sig_hash(0)
z2 = tx_obj.sig_hash(1)
der1 = private_key.sign(z1).der()
der2 = private_key.sign(z2).der()
sig1 = der1 + SIGHASH_ALL.to_bytes(1, 'big')
sig2 = der2 + SIGHASH_ALL.to_bytes(1, 'big')
sec = private_key.point.sec()
script_sig1 = Script([sig1, sec])
script_sig2 = Script([sig2, sec])
tx_obj.tx_ins[0].script_sig = script_sig1
tx_obj.tx_ins[1].script_sig = script_sig2
print(tx_obj.serialize().hex())
Exemplo n.º 12
0
class TxIn:

    def __init__(self, prev_tx, prev_index, script_sig=None, sequence=0xffffffff):
        self.prev_tx = prev_tx
        self.prev_index = prev_index
        if script_sig is None:
            self.script_sig = Script()
        else:
            self.script_sig = script_sig
        self.sequence = sequence

    def __repr__(self):
        return '{}:{}'.format(
            self.prev_tx.hex(),
            self.prev_index,
        )

    @classmethod
    def parse(cls, s):
        '''Takes a byte stream and parses the tx_input at the start
        return a TxIn object
        '''
        # s.read(n) will return n bytes
        # prev_tx is 32 bytes, little endian
        prev_tx = s.read(32)[::-1]
        # prev_index is 4 bytes, little endian, interpret as int
        prev_index = little_endian_to_int(s.read(4))
        # script_sig is a variable field (length followed by the data)
        # you can use Script.parse to get the actual script
        script_sig = Script.parse(s)
        # sequence is 4 bytes, little-endian, interpret as int
        sequence = little_endian_to_int(s.read(4))
        # return an instance of the class (cls(...))
        return cls(prev_tx, prev_index, script_sig, sequence)

    def serialize(self):
        '''Returns the byte serialization of the transaction input'''
        # serialize prev_tx, little endian
        result = self.prev_tx[::-1]
        # serialize prev_index, 4 bytes, little endian
        result += int_to_little_endian(self.prev_index, 4)
        # serialize the script_sig
        result += self.script_sig.serialize()
        # serialize sequence, 4 bytes, little endian
        result += int_to_little_endian(self.sequence, 4)
        return result

    def fetch_tx(self, testnet=False):
        return TxFetcher.fetch(self.prev_tx.hex(), testnet=testnet)

    def value(self, testnet=False):
        '''Get the outpoint value by looking up the tx hash
        Returns the amount in satoshi
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the amount property
        return tx.tx_outs[self.prev_index].amount

    def script_pubkey(self, testnet=False):
        '''Get the ScriptPubKey by looking up the tx hash
        Returns a Script object
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the script_pubkey property
        return tx.tx_outs[self.prev_index].script_pubkey
Exemplo n.º 13
0
private_key2 = PrivateKey(secret2)

print(private_key1.point.address(testnet=True))

print(private_key2.point.address(testnet=True))

## first we create the scriptPubKey of the redeemscript or redeem script

# we decode our previous pubkey to put them in script commands

public_key1 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1')
public_key2 = decode_base58('mwjg9Y1jeFdNu7DQBAW7DUbJqj6jMmhd74')

#dat_redeem_script_t1 = 0x52, public_key1, public_key2, 0x52, 0xae

dat_redeem_script_op = Script([0x52, public_key1, public_key2, 0x52, 0xae])

dat_redeem_script_op_serialized = dat_redeem_script_op.serialize()

#dat_redeem_script = dat_redeem_script.raw_serialize()

dat_redeem_script_h160 = hash160(dat_redeem_script_op_serialized)

#dat_redeem_script_address = h160_to_p2sh_address(dat_redeem_script_op, testnet = True)

#print(dat_redeem_script_address)

# create a simple address to send to the p2sh address that will receive the fund

secret = little_endian_to_int(hash256(b'dat_test_private_key3'))
Exemplo n.º 14
0
 def load(cls, text):
     script = Script("", text, "{}=")
     script.error = cfg_error.__get__(script, Script)
     node = ConfigNode()
     ConfigNode.ParseNode(node, script, True)
     return node
Exemplo n.º 15
0
 def compute_type(self):
     self.validate()
     if self.word == 'pk_k':
         self.type = ExpressionType('Konudemsx')
         self.size = 34
     elif self.word == 'pk_h':
         self.type = ExpressionType('Knudemsx')
         self.size = 24
     elif self.word == 'pk':
         self.type = ExpressionType('Bdemnosu')
         self.size = 35
     elif self.word == 'pkh':
         self.type = ExpressionType('Bdemnsu')
         self.size = 25
     elif self.word in ('older', 'after'):
         self.type = ExpressionType('Bzfmx')
         script_num = encode_minimal_num(int(self.children[0].word))
         self.size = len(Script([script_num]).raw_serialize()) + 1
     elif self.word in ('sha256', 'hash256'):
         self.type = ExpressionType('Bonudm')
         self.size = 6 + 33
     elif self.word in ('ripemd160', 'hash160'):
         self.type = ExpressionType('Bonudm')
         self.size = 6 + 21
     elif self.word == 'just_1':
         self.type = ExpressionType('Bzufmx')
         self.size = 1
     elif self.word == 'just_0':
         self.type = ExpressionType('Bzudemsx')
         self.size = 1
     elif self.word == 'multi':
         self.type = ExpressionType('Bnudems')
         self.size = 3 + 34 * (len(self.children) - 1)
     elif self.word == 'and_v':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> <y>
             (x.V * y.B).B  # B=V_x*B_y
             + (x.V * y.V).V  # V=V_x*V_y
             + (x.V * y.K).K  # K=V_x*K_y
             + (x.n + x.z * y.n).n  # n=n_x+z_x*n_y
             + (x.z * y.o + x.o * y.z).o  # o=o_x*z_y+z_x*o_y
             + (x.d * y.d).d  # d=d_x*d_y
             + (x.m * y.m).m  # m=m_x*m_y
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.s + y.s).s  # s=s_x+s_y
             + (x.s + y.f).f  # f=f_y+s_x
             + y.u.u  # u=u_y
             + y.x.x  # x=x_y
         )
         self.size = self.children[0].size + \
             self.children[1].size
     elif self.word == 'and_b':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> <y> BOOLAND
             'ux' + (y.W * x.B).B  # B=B_x*W_y
             + (x.n + x.z * y.n).n  # n=n_x+z_x*n_y
             + (x.z * y.o + x.o * y.z).o  # o=o_x*z_y+z_x*o_y
             + (x.d * y.d).d  # d=d_x*d_y
             + (x.m * y.m).m  # m=m_x*m_y
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.s + y.s).s  # s=s_x+s_y
             + (x.es * y.es).e  # e=e_x*e_y*s_x*s_y
             + (x.f * y.f + x.fs + y.fs).f  # f=f_x*f_y + f_x*s_x + f_y*s_y
         )
         self.size = self.children[0].size + \
             self.children[1].size + 1
     elif self.word == 'and_n':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> NOTIF 0 ELSE <y> ENDIF
             'x' + (x.Bdu * y.B).B  # B=B_x*d_x*u_x*B_y
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.o * y.z).o  # o=o_x*z_y
             + y.u.u  # u=u_y
             + x.d.d  # d=d_x
             + (x.s + y.s).s  # s=s_x+s_y
             + (x.e * (x.s + y.s)).e  # e=e_x*(s_x+s_y)
             + (x.em * y.m).m  # m=m_x*m_y*e_x
         )
         self.size = self.children[0].size + \
             self.children[1].size + 4
     elif self.word == 'or_b':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> <y> BOOLOR
             'dux' + (x.Bd * y.Wd).B  # B=B_x*d_x*W_x*d_y
             + (x.z * y.o + x.o * y.z).o  # o=o_x*z_y+z_x*o_y
             + (x.em * y.em * (x.s + y.s)).m  # m=m_x*m_y*e_x*e_y*(s_x+s_y)
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.s * y.s).s  # s=s_x*s_y
             + (x.e * y.e).e  # e=e_x*e_y
         )
         self.size = self.children[0].size + \
             self.children[1].size + 1
     elif self.word == 'or_c':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> NOTIF <y> ENDIF
             'fx' + (x.Bud * y.V).V  # V=V_y*B_x*u_x*d_x
             + (x.o * y.z).o  # o=o_x*z_y
             + (x.me * y.m * (x.s + y.s)).m  # m=m_x*m_y*e_x*(s_x+s_y)
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.s * y.s).s  # s=s_x*s_y
         )
         self.size = self.children[0].size + \
             self.children[1].size + 2
     elif self.word == 'or_d':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # <x> IFDUP NOTiF <y> ENDIF
             'x' + (x.Bdu * y.B).B  # B=B_y*B_x*d_x*u_x
             + (x.o * y.z).o  # o=o_x*z_y
             + (x.me * y.m * (x.s + y.s)).m  # m=m_x*m_y*e_x*(s_x+s_y)
             + (x.z * y.z).z  # z=z_x*z_y
             + (x.s * y.s).s  # s=s_x*s_y
             + (x.e * y.e).e  # e=e_x*e_y
             + y.u.u  # u=u_y
             + y.d.d  # d=d_y
             + y.f.f  # f=f_y
         )
         self.size = self.children[0].size + \
             self.children[1].size + 3
     elif self.word == 'or_i':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         self.type = ExpressionType(
             # IF <x> ELSE <y> ENDIF
             'x' + (x.V * y.V).V  # V=V_x*V_y
             + (x.B * y.B).B  # B=B_x*B_y
             + (x.K * y.K).K  # K=K_x*K_y
             + (x.u * y.u).u  # u=u_x*u_y
             + (x.f * y.f).f  # f=f_x*f_y
             + (x.s * y.s).s  # s=s_x*s_y
             + (x.z * y.z).o  # o=z_x*z_y
             + (x.e * y.f + x.f * y.e).e  # e=e_x*f_y+f_x*e_y
             + (x.m * y.m * (x.s + y.s)).m  # m=m_x*m_y*(s_x+s_y)
             + (x.d + y.d).d  # d=d_x+d_y
         )
         self.size = self.children[0].size + \
             self.children[1].size + 3
     elif self.word == 'andor':
         x = self.children[0].compute_type()
         y = self.children[1].compute_type()
         z = self.children[2].compute_type()
         self.type = ExpressionType(
             # <x> NOTIF <z> ELSE <y> ENDIF
             'x' + (x.Bdu * y.B * z.B).B  # B=B_x*d_x*u_x*B_y*B_z
             + (x.Bdu * y.K * z.K).K  # K=B_x*d_x*u_x*K_y*K_z
             + (x.Bdu * y.V * z.V).V  # V=B_x*d_x*u_x*V_y*V_z
             + (x.z * y.z * z.z).z  # z=z_x*z_y*z_z
             + (x.z * y.o * z.o +
                x.o * y.z * z.z).o  # o=o_x*z_y*z_z+z_x*o_y*o_z
             + (y.u * z.u).u  # u=u_y*u_z
             + ((x.s + y.f) * z.f).f  # f=(s_x+f_y)*f_z
             + z.d.d  # d=d_z
             + ((x.s + y.f) * x.e * z.e).e  # e=e_x*e_z*(s_x+s_y)
             + (x.em * y.m * z.m *
                (x.s + y.s + z.s)).m  # m=m_x*m_y*m_z*e_x*(s_x+s_y+s_z)
             + (z.s * (x.s + y.s)).s  # s=s_z*(s_x+s_y)
         )
         self.size = self.children[0].size + \
             self.children[1].size + \
             self.children[2].size + 3
     elif self.word == 'thresh':
         all_e, all_m = BetterBool(True), BetterBool(True)
         num_s, args = 0, 0
         k = int(self.children[0].word)
         for i, child in enumerate(self.children[1:]):
             t = child.compute_type()
             if i == 0 and not t.Bdu:
                 raise ValueError(
                     'First expression in thresh should be Bdu')
             elif i > 0 and not t.Wdu:
                 raise ValueError(
                     f'non-first expression in thresh should be Wdu {t} {child}'
                 )
             all_e *= t.e
             all_m *= t.m
             if t.s:
                 num_s += 1
             if t.o:
                 args += 1
             elif not t.z:
                 args += 2
         self.type = ExpressionType(
             'Bdu'
             # all z's to get z
             + BetterBool(args == 0).z
             # one o, all z's to get o
             + BetterBool(args == 1).o
             # all s's and all e's
             + (all_e * (num_s == len(self.children) - 1)).e
             # all e's and all m's and some s's
             + (all_e * all_m * (num_s >= len(self.children) - 1 - k)).m
             # some s's
             + BetterBool(num_s >= len(self.children) - k).s)
         self.size = len(encode_minimal_num(int(self.children[0].word)))
         for child in self.children[1:]:
             self.size += child.size + 1
     else:
         raise SyntaxError(f'bad word {self.word}')
     assert self.type, f'{self.word} has no type'
     # take care of modifiers
     for m in reversed(self.modifiers):
         if m == 'a':
             self.type = ExpressionType(
                 # TOALTSTACK <x> FROMALTSTACK
                 'x' + self.type.B.W  # W=B_x
                 + self.type.u.u  # u=u_x
                 + self.type.d.d  # d=d_x
                 + self.type.f.f  # f=f_x
                 + self.type.e.e  # e=e_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
             self.size += 2
         elif m == 's':
             self.type = ExpressionType(
                 # SWAP <x>
                 (self.type.Bo).W  # W=B_x*o_x
                 + self.type.u.u  # u=u_x
                 + self.type.d.d  # d=d_x
                 + self.type.f.f  # f=f_x
                 + self.type.e.e  # e=e_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
                 + self.type.x.x  # x=x_x
             )
             self.size += 1
         elif m == 'c':
             self.type = ExpressionType(
                 # <x> CHECKSIG
                 'us' + self.type.K.B  # B=K_x
                 + self.type.o.o  # o=o_x
                 + self.type.n.n  # n=n_x
                 + self.type.d.d  # d=d_x
                 + self.type.f.f  # f=f_x
                 + self.type.e.e  # e=e_x
                 + self.type.m.m  # m=m_x
             )
             self.size += 1
         elif m == 'd':
             self.type = ExpressionType(
                 # DUP IF <x> ENDIF
                 'nudx' + (self.type.Vz).B  # B=V_x*z_x
                 + self.type.z.o  # o=z_x
                 + self.type.f.e  # e=f_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
             self.size += 3
         elif m == 'v':
             if self.type.x:
                 self.size += 1
             self.type = ExpressionType(
                 # <x> VERIFY
                 'fx' + self.type.B.V  # V=B_x
                 + self.type.z.z  # z=z_x
                 + self.type.o.o  # o=o_x
                 + self.type.n.n  # n=n_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
         elif m == 'j':
             self.type = ExpressionType(
                 # SIZE 0NOTEQUAL IF <x> ENDIF
                 'ndx' + (self.type.Bn).B  # B=B_x*n_x
                 + self.type.f.e  # e=f_x
                 + self.type.o.o  # o=o_x
                 + self.type.u.u  # u=u_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
             self.size += 4
         elif m == 'n':
             self.type = ExpressionType(
                 # <x> 0NOTEQUAL
                 'ux' + self.type.B.B  # B=B_x
                 + self.type.z.z  # z=z_x
                 + self.type.o.o  # o=o_x
                 + self.type.n.n  # n=n_x
                 + self.type.d.d  # d=d_x
                 + self.type.f.f  # f=f_x
                 + self.type.e.e  # e=e_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
             self.size += 1
         elif m in ('l', 'u'):
             self.type = ExpressionType(
                 # IF 0 ELSE <x> ENDIF/IF <x> ELSE 0 ENDIF
                 'dx' + self.type.B.B  # B=B_x
                 + self.type.u.u  # u=u_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
                 + self.type.z.o  # o=z_x
                 + self.type.f.e  # e=f_x
             )
             self.size += 4
         elif m == 't':
             self.type = ExpressionType(
                 # <x> 1
                 'f*x' + self.type.V.B  # B=V_x
                 + self.type.z.z  # z=z_x
                 + self.type.o.o  # o=o_x
                 + self.type.d.d  # d=d_x
                 + self.type.m.m  # m=m_x
                 + self.type.s.s  # s=s_x
             )
             self.size += 1
     return self.type
Exemplo n.º 16
0
from markov_generator import MarkovGenerator
from script import Script


def read_script_config(path):
    with open(path, 'r') as inf:
        return yaml.load(inf)

if __name__ == '__main__':
    print '\t--> Reading settings file: "config.yml"'
    configs = read_script_config('config.yml')

    print '\t--> Configuring script object'
    script_configs = configs['script_configs']
    movie_script = Script(**script_configs)

    print '\t\t--> Loading script from file "{}"'.format(configs['run_configs']['script_path'])
    movie_script.load_from_file(configs['run_configs']['script_path'])

    print '\t\t--> Parsing script text'
    movie_script.parse()

    print '\t--> Setting up Markov generator for script notes'
    note_generator = MarkovGenerator(movie_script.notes)

    print '\t--> Setting up Markov generator for scene settings'
    setting_generator = MarkovGenerator(movie_script.scenes)

    print '\t--> Setting up Markov generators for character dialog'
    character_generators = {}
Exemplo n.º 17
0
 def script(self):
     if self.word == 'pk':
         sec = bytes.fromhex(self.children[0].word)
         script = Script([sec, OP_CHECKSIG])
     elif self.word == 'pk_k':
         # sometimes the checksig can sit outside an IF
         # to save a byte, which motivates this script
         # IF <x> ELSE <y> ENDIF CHECKSIG
         sec = bytes.fromhex(self.children[0].word)
         script = Script([sec])
     elif self.word == 'pkh':
         h160 = bytes.fromhex(self.children[0].word)
         script = Script(
             [OP_DUP, OP_HASH160, h160, OP_EQUALVERIFY, OP_CHECKSIG])
     elif self.word == 'pk_h':
         # sometimes the checksig can sit outside an IF
         # to save a byte, which motivates this script
         # IF <x> ELSE <y> ENDIF CHECKSIG
         h160 = bytes.fromhex(self.children[0].word)
         script = Script([OP_DUP, OP_HASH160, h160, OP_EQUALVERIFY])
     elif self.word == 'older':
         amount = encode_minimal_num(int(self.children[0].word))
         script = Script([amount, OP_CHECKSEQUENCEVERIFY])
     elif self.word == 'after':
         amount = encode_minimal_num(int(self.children[0].word))
         script = Script([amount, OP_CHECKLOCKTIMEVERIFY])
     elif self.word in ('sha256', 'ripemd160', 'hash256', 'hash160'):
         # hashlocks
         if self.word == 'sha256':
             op = OP_SHA256
         elif self.word == 'ripemd160':
             op = OP_RIPEMD160
         elif self.word == 'hash256':
             op = OP_HASH256
         else:
             op = OP_HASH160
         h = bytes.fromhex(self.children[0].word)
         script = Script(
             [OP_SIZE,
              encode_num(32), OP_EQUALVERIFY, op, h, OP_EQUAL])
     elif self.word == 'just_1':
         script = Script([OP_1])
     elif self.word == 'just_0':
         script = Script([OP_0])
     elif self.word == 'and_v':
         # "and" with a V expression and B, V or K
         # outputs whatever the second one is
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + y
     elif self.word == 'and_b':
         # "and" with a B expression and a W expression
         # outputs a B
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + y + [OP_BOOLAND]
     elif self.word == 'and_n':
         # "and" with two B expressions, the first being du
         # outputs a B
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + [OP_NOTIF, OP_0, OP_ELSE] + y + [OP_ENDIF]
     elif self.word == 'or_b':
         # "or" with a B expression and a W expression, both being d
         # outputs a B
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + y + [OP_BOOLOR]
     elif self.word == 'or_c':
         # "or" with a B expression and a V expression, first being du
         # outputs a V
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + [OP_NOTIF] + y + [OP_ENDIF]
     elif self.word == 'or_d':
         # "or" with two B expressions, first being du
         # outputs a B
         x = self.children[0].script()
         y = self.children[1].script()
         script = x + [OP_IFDUP, OP_NOTIF] + y + [OP_ENDIF]
     elif self.word == 'or_i':
         # "or" with two B, V or K expressions
         # outputs whatever the two expressions are
         x = self.children[0].script()
         y = self.children[1].script()
         script = [OP_IF] + x + [OP_ELSE] + y + [OP_ENDIF]
     elif self.word == 'andor':
         #
         x = self.children[0].script()
         y = self.children[1].script()
         z = self.children[2].script()
         script = x + [OP_NOTIF] + z + [OP_ELSE] + y + [OP_ENDIF]
     elif self.word == 'multi':
         # for multisig
         keys = [bytes.fromhex(c.word) for c in self.children[1:]]
         k = encode_minimal_num(int(self.children[0].word))
         n = encode_minimal_num(len(keys))
         script = Script([k, *keys, n, OP_CHECKMULTISIG])
     elif self.word == 'thresh':
         # useful meta condition, first has to be Bdu, rest Wdu
         script = self.children[1].script()
         for sub in self.children[2:]:
             script += sub.script() + [OP_ADD]
         k = encode_minimal_num(int(self.children[0].word))
         script += [k, OP_EQUAL]
     else:
         raise SyntaxError(f'unknown word: {self.word}')
     if self.modifiers:
         for c in reversed(self.modifiers):
             if c == 'a':
                 # Turn B into W
                 script = [OP_TOALTSTACK] + script + [OP_FROMALTSTACK]
             elif c == 's':
                 # Turn Bo into W
                 script = [OP_SWAP] + script
             elif c == 'c':
                 # Turn K into B
                 script += [OP_CHECKSIG]
             elif c == 'd':
                 # Turn Vz into B
                 script = [OP_DUP, OP_IF] + script + [OP_ENDIF]
             elif c == 'v':
                 # Turn B into V
                 if script[-1] == OP_EQUAL:
                     script[-1] = OP_EQUALVERIFY
                 elif script[-1] == OP_CHECKSIG:
                     script[-1] = OP_CHECKSIGVERIFY
                 elif script[-1] == OP_CHECKMULTISIG:
                     script[-1] = OP_CHECKMULTISIGVERIFY
                 else:
                     script += [OP_VERIFY]
             elif c == 'j':
                 # bypass if the top stack element is 0
                 script = [OP_SIZE, OP_0NOTEQUAL, OP_IF] \
                     + script + [OP_ENDIF]
             elif c == 'n':
                 # turn B into a Bu (for feeding into IF)
                 script += [OP_0NOTEQUAL]
             elif c == 't':
                 # turn V into B
                 script += [OP_1]
             elif c == 'u':
                 # turn z into o (consume one)
                 script = [OP_IF] + script + [OP_ELSE, OP_0, OP_ENDIF]
             elif c == 'l':
                 # turn z into o (consume one)
                 script = [OP_IF, OP_0, OP_ELSE] + script + [OP_ENDIF]
     return script
Exemplo n.º 18
0
 def from_script(cls, items, parent=None):
     '''The array items will be used destructively'''
     node = None
     last_length = len(items)
     if len(items) > 4 and is_number_op_code(items[0]) \
        and len(items[1]) == 33:
         node = cls('multi', parent, None)
         cls(str(op_code_to_number(items[0])), node, None)
         items.pop(0)
         while len(items[0]) == 33:
             cls(items[0].hex(), node, None)
             items.pop(0)
         n = len(node.children) - 1
         if number_to_op_code(n) != items[0]:
             raise SyntaxError(f'not the expected pattern')
         items.pop(0)
         if items[0] not in (OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY):
             raise SyntaxError(f'not the expected pattern')
         if items[0] == OP_CHECKMULTISIGVERIFY:
             node.modifiers = 'v'
         items.pop(0)
     elif len(items) > 1 and items[1] == OP_CHECKSEQUENCEVERIFY:
         num = items.pop(0)
         items.pop(0)
         node = cls('older', parent, None)
         cls(str(decode_minimal_num(num)), node, None)
     elif len(items) > 1 and items[1] == OP_CHECKLOCKTIMEVERIFY:
         num = items.pop(0)
         items.pop(0)
         node = cls('after', parent, None)
         cls(str(decode_minimal_num(num)), node, None)
     elif items[0] == OP_1:
         items.pop(0)
         node = cls('just_1', parent, None)
     elif items[0] == OP_0:
         items.pop(0)
         node = cls('just_0', parent, None)
     elif len(items[0]) == 33:
         sec = items.pop(0)
         word = 'pk_k'
         modifiers = None
         if items[0] in (OP_CHECKSIG, OP_CHECKSIGVERIFY):
             op = items.pop(0)
             word = 'pk'
             if op == OP_CHECKSIGVERIFY:
                 modifiers = 'v'
         node = cls(word, parent, modifiers)
         cls(sec.hex(), node, None)
     elif len(items) > 4 and items[0] == OP_DUP \
          and items[1] == OP_HASH160 \
          and len(items[2]) == 20 \
          and items[3] == OP_EQUALVERIFY:
         h160 = items[2]
         for _ in range(4):
             items.pop(0)
         modifiers = None
         word = 'pk_h'
         if items and items[0] in (OP_CHECKSIG, OP_CHECKSIGVERIFY):
             word = 'pkh'
             if items[0] == OP_CHECKSIGVERIFY:
                 modifiers = 'v'
             items.pop(0)
         node = cls(word, parent, modifiers)
         cls(h160.hex(), node, None)
     elif len(items) > 5 and items[0] == OP_SIZE \
          and decode_num(items[1]) == 32 \
          and items[2] == OP_EQUALVERIFY \
          and items[3] in (OP_SHA256, OP_RIPEMD160, OP_HASH160, OP_HASH256) \
          and len(items[4]) in (20, 32) \
          and items[5] in (OP_EQUAL, OP_EQUALVERIFY):
         if items[3] == OP_SHA256:
             word = 'sha256'
         elif items[3] == OP_RIPEMD160:
             word = 'ripemd160'
         elif items[3] == OP_HASH160:
             word = 'hash160'
         else:
             word = 'hash256'
         if items[5] == OP_EQUALVERIFY:
             modifiers = 'v'
         else:
             modifiers = None
         hash_value = items[4]
         for _ in range(6):
             items.pop(0)
         node = cls(word, parent, modifiers)
         cls(hash_value.hex(), node, None)
     elif items[0] == OP_IF:
         items.pop(0)
         child_1 = cls.from_script(items)
         if items[0] != OP_ELSE:
             raise SyntaxError('not the expected pattern')
         items.pop(0)
         child_2 = cls.from_script(items)
         if items[0] != OP_ENDIF:
             raise SyntaxError(f'not the expected pattern {items}')
         items.pop(0)
         if child_1.word == 'just_0':
             node = child_2
             node.modifiers = 'l' + node.modifiers
         elif child_2.word == 'just_0':
             node = child_1
             node.modifiers = 'u' + node.modifiers
         else:
             node = cls('or_i', parent, None, [child_1, child_2])
     elif items[0] == OP_TOALTSTACK:
         items.pop(0)
         node = cls.from_script(items, parent)
         if items[0] != OP_FROMALTSTACK:
             raise SyntaxError('not the expected pattern')
         items.pop(0)
         node.modifiers = 'a' + node.modifiers
     elif items[0] == OP_SWAP:
         items.pop(0)
         node = cls.from_script(items)
         node.modifiers = 's' + node.modifiers
     elif len(items) > 2 and items[0] == OP_DUP and items[1] == OP_IF:
         items.pop(0)
         items.pop(0)
         node = cls.from_script(items, parent)
         if items[0] != OP_ENDIF:
             raise SyntaxError('not the expected pattern')
         items.pop(0)
         node.modifiers = 'd' + node.modifiers
     elif len(items) > 3 and items[0] == OP_SIZE and \
          items[1] == OP_0NOTEQUAL and items[2] == OP_IF:
         for _ in range(3):
             items.pop(0)
         node = cls.from_script(items, parent)
         if items[0] != OP_ENDIF:
             raise SyntaxError('not the expected pattern')
         items.pop(0)
         node.modifiers = 'j' + node.modifiers
     else:
         raise SyntaxError(f'script is not a miniscript {items}')
     while len(items) < last_length:
         if not items:
             return node
         last_length = len(items)
         if items[0] == OP_CHECKSIG:
             node.modifiers = 'c' + node.modifiers
             items.pop(0)
         elif items[0] == OP_CHECKSIGVERIFY:
             node.modifiers = 'vc' + node.modifiers
             items.pop(0)
         elif items[0] == OP_VERIFY:
             node.modifiers = 'v' + node.modifiers
             items.pop(0)
         elif items[0] == OP_0NOTEQUAL:
             node.modifiers = 'n' + node.modifiers
             items.pop(0)
         elif items[0] == OP_NOTIF:
             items.pop(0)
             child = cls.from_script(items)
             if items[0] == OP_ELSE:
                 # andor
                 items.pop(0)
                 child_2 = cls.from_script(items)
                 if items[0] != OP_ENDIF:
                     raise SyntaxError('not the expected pattern')
                 items.pop(0)
                 if child.word == 'just_0':
                     node = cls('and_n', parent, None, [node, child_2])
                 else:
                     node = cls('andor', parent, None,
                                [node, child_2, child])
             elif items[0] == OP_ENDIF:
                 items.pop(0)
                 # or_c
                 node = cls('or_c', parent, None, [node, child])
             else:
                 raise SyntaxError('not the expected pattern')
         elif len(items) > 1 and items[0] == OP_IFDUP \
              and items[1] == OP_NOTIF:
             items.pop(0)
             items.pop(0)
             child = cls.from_script(items)
             if items[0] != OP_ENDIF:
                 raise SyntaxError('not the expected pattern')
             items.pop(0)
             node = cls('or_d', parent, None, [node, child])
         elif items[0] == OP_1:
             items.pop(0)
             node.modifiers = 't' + node.modifiers
         else:
             # attempt to look ahead
             try:
                 items_copy = Script(items[:])
                 node2 = cls.from_script(items_copy)
             except SyntaxError as e:
                 continue
             if items_copy and items_copy[0] == OP_BOOLOR:
                 # or_b
                 for _ in range(len(items) - len(items_copy) + 1):
                     items.pop(0)
                 node = cls('or_b', parent, None, [node, node2])
             elif items_copy and items_copy[0] == OP_BOOLAND:
                 # and_b
                 for _ in range(len(items) - len(items_copy) + 1):
                     items.pop(0)
                 node = cls('and_b', parent, None, [node, node2])
             elif items_copy and items_copy[0] == OP_ADD:
                 # thresh
                 for _ in range(len(items) - len(items_copy) + 1):
                     items.pop(0)
                 node = cls('thresh', parent, None, [node, node2])
                 while True:
                     if is_number_op_code(items[0]) \
                        and items[1] in (OP_EQUAL, OP_EQUALVERIFY):
                         break
                     cls.from_script(items, node)
                     if items[0] != OP_ADD:
                         raise SyntaxError('not the expected pattern')
                     items.pop(0)
                 k = op_code_to_number(items.pop(0))
                 first_child = cls(str(k), node, None)
                 node.children.pop()
                 node.children.insert(0, first_child)
                 if items[0] == OP_EQUALVERIFY:
                     node.modifiers = 'v' + node.modifiers
                 items.pop(0)
             else:
                 # and_v
                 for _ in range(len(items) - len(items_copy)):
                     items.pop(0)
                 node = cls('and_v', parent, None, [node, node2])
     return node
Exemplo n.º 19
0
class Engine:
    def __init__(self, args):

        print("Engine INIT")

        self.state = "WAITING"

        self.args = args

        self.script = Script()

        self.send_noise = False

        self.args.stop = False
        self.args.restart = False

        self.args.callback = self.emotion_update

        #self.live_ser = LiveSer()
        #self.live_ser.run(self.args)

        self.mid_text = None
        self.last_react = 0
        self.last_speech = 0
        self.mid_match = False
        self.beating = False
        self.matched_to_word = 0

        self.osc_commands = {}
        self.func_sched = {}

        #self.lock = asyncio.Lock()

        self.t2i_client = udp_client.SimpleUDPClient("127.0.0.1", 3838)
        #self.pix2pix_client = udp_client.SimpleUDPClient("127.0.0.1", 8383)
        self.voice_client = udp_client.SimpleUDPClient("127.0.0.1", 57120)

        self.mental_state = MentalState()

        #google_speech = GoogleSpeech()
        #google_speech.say("Hi")
        #asyncio.get_event_loop().run_until_complete(ms_speech.say("Pewdiepie"))

        self.time_check()

        self.main_loop = asyncio.get_event_loop()

        self.queue = janus.Queue(loop=self.main_loop)

        self.recognizer = Recognizer(self.queue.sync_q, self.args)
        #fut = self.main_loop.run_in_executor(None, self.recognizer.start)

        self.server = Server(self.gain_update, self.queue.async_q,
                             self.control, self.mood_update,
                             self.pix2pix_update)

        print("Starting server")
        self.main_loop.run_until_complete(self.server.start())

        print("Consuming speech")
        self.main_loop.run_until_complete(self.consume_speech())

    def schedule_osc(self, timeout, client, command, args):
        osc_command = ScheduleOSC(timeout, client, command, args, self.del_osc)
        self.osc_commands[command + str(timeout)] = osc_command

    def schedule_function(self, timeout, callback):
        func = ScheduleFunction(timeout, callback, self.del_func)
        self.func_sched[func._uuid] = func

    def del_osc(self, command, timeout):
        #print("del {}".format(command + str(timeout)))
        del self.osc_commands[command + str(timeout)]

    def del_func(self, uid):
        #print("del {}".format(command + str(timeout)))
        del self.func_sched[uid]

    def purge_osc(self):
        for command in self.osc_commands:
            self.osc_commands[command].cancel()
        self.osc_commands.clear()

    def purge_func(self):
        for uid in self.func_sched:
            self.func_sched[uid].cancel()
        self.func_sched.clear()

    def start_google(self):
        print("Resume listening")
        fut = self.main_loop.run_in_executor(None, self.recognizer.start)

    async def consume_speech(self):
        while True:
            item = await self.queue.async_q.get()
            if item["action"] == "speech":
                self.speech_text(item["text"])
            else:
                self.mid_speech_text(item["text"])

    def time_check(self):
        # recognition timeout
        now = time.time()
        if self.state == "SCRIPT":
            if (self.script.awaiting_index > -1 and
                ((now - self.last_react) > self.script.awaiting_global_timeout
                 or (now - self.last_speech) >
                 self.script.awaiting_nospeech_timeout)):
                print("Script TIMEOUT! {}, {}, {}".format(
                    now, self.last_react, self.last_speech))
                asyncio.set_event_loop(self.main_loop)
                self.last_react = self.last_speech = now
                if self.timeout_response():
                    self.last_react = self.last_speech = time.time(
                    ) + self.speech_duration
                    # say it
                    self.say(delay_sec=1, callback=self.next_variation)
                elif self.script.next_variation():
                    self.show_next_line()
                else:
                    self.react("")

        if not self.args.stop:
            threading.Timer(0.1, self.time_check).start()

    def next_variation(self):
        if self.script.next_variation():
            self.show_next_line()

    def timeout_response(self):
        if "timeout-response" in self.script.awaiting:
            del self.script.awaiting["timeout-response"]
            self.preload_speech("gan_responses/timeout{}.wav".format(
                self.script.awaiting_index))
            return True
        else:
            return False

    def emotion_update(self, data):
        print("Emotion update! {}".format(data))
        if (data["status"] == "silence"):
            self.mental_state.update_silence()
        else:
            self.mental_state.update_emotion(data["analysis"])
            #self.mental_state_updated()

        conc = asyncio.run_coroutine_threadsafe(
            self.server.emotion_update(data, self.mental_state.value),
            self.main_loop)

    def gain_update(self, min, max):
        print("Update gain! {} : {}".format(min, max))
        self.live_ser.feat_ext.min = float(min)
        self.live_ser.feat_ext.max = float(max)

    def speech_text(self, text):
        #print("<{}>".format(text))
        self.t2i_client.send_message("/speech", text)
        if self.state == "SCRIPT":
            if self.mid_text is not None:
                #print("Looking up {}".format(text))
                self.lookup(text)

            self.mid_match = False
            self.matched_to_word = 0

        elif self.state == "QUESTION":
            self.question_answer = text
            self.pre_script()

    def mid_speech_text(self, text):
        self.last_speech = time.time()
        if self.state == "SCRIPT":
            self.mid_text = text
            #print("({})".format(text))
            self.t2i_client.send_message("/speech", text)
            self.lookup(text)
        elif self.state == "QUESTION":
            self.question_answer = text

    def lookup(self, text):
        # print("REACT: {}".format(text))
        # update last speech time
        # First get the top line matches
        tries = []
        # We have to try all combinations
        words = text.split()
        for i in range(len(words) - 2, self.matched_to_word - 1, -1):
            tries.append(" ".join(words[i:]))

        self.matches_cache = []

        try_i = self.lookup_all(tries)

        if try_i != -1:
            self.react(text)
            return True


#
#        # try up to 2 lines aheead
#        for i in range(self.script.awaiting_index + 1, self.script.awaiting_index + 4):
#            try_i = self.match_cache(i)
#            if try_i != -1:
#                self.react(text, i)
#                return True
#
# If it has been too long accept whatever
#now =  int(round(time.time() * 1000))
#if self.last_react > 0 and now - self.last_react > 1000  * 20:
#    (index, try_i) = self.match_cache_any()
#    if try_i != -1:
#        self.react(text, index)
#        return True

    def lookup_all(self, tries):
        for i in range(0, len(tries)):
            s = tries[i]
            try_i = self.match(s, i)
            if try_i != -1:
                return try_i

        return -1

    def lookup_index_space(self, tries, index):
        for i in range(0, len(tries)):
            s = tries[i]
            try_i = self.match(s, index, i)
            if try_i != -1:
                return try_i

        return -1

    def match_cache_any(self):
        for matches in self.matches_cache:
            for match in matches["data"]:
                if match["distance"] < DISTANCE_THRESHOLD:
                    print("EMERGECNY BOOM")
                    return (matches["try_index"], match["index"])

        return (-1, -1)

    def match_cache(self, index):
        for matches in self.matches_cache:
            for match in matches["data"]:
                if match["distance"] < DISTANCE_THRESHOLD and match[
                        "index"] == index:
                    print("CACHE BOOM")
                    return matches["try_index"]
        return -1

    def match_space(self, text, index, try_index):
        #print("[{}]".format(text))
        matches = self.script.match_space(text)
        if matches:
            self.matches_cache.append({
                "data": matches,
                "try_index": try_index
            })
            #print(matches)
            for match in matches:
                if match["distance"] < DISTANCE_THRESHOLD and match[
                        "index"] == index:
                    print("BOOM")
                    return try_index
        return -1

    def match(self, text, try_index):
        match = self.script.match(text)
        if match >= DISTANCE_THRESHOLD:
            return try_index
        else:
            return -1

    def react(self, matched_utterance):

        # restart google
        self.args.restart = True

        # Which word was it?
        self.last_matched_word = self.matched_to_word
        self.matched_to_word = len(matched_utterance.split())
        script_text = self.script.awaiting_text
        words_ahead = max(
            0,
            len(script_text.split()) -
            (len(matched_utterance.split()) - self.last_matched_word))
        print("Said {} ({}) Matched: {}. Words ahead {}".format(
            self.script.awaiting_index, script_text, matched_utterance,
            words_ahead))

        line = self.script.awaiting

        # Send a pause the average person speaks at somewhere between 125 and 150 words per minute (2-2.5 per sec)
        delay = words_ahead / 2.8

        if "triggers-end" in line:
            self.schedule_osc(delay, self.voice_client, "/control/musicbox",
                              [0.0, 0.0, 0.0, 0.0])
            self.schedule_osc(delay, self.voice_client, "/control/synthbass",
                              [0.0, 0.0, 0.0])
            self.schedule_function(delay, self.stop_noise)
            self.schedule_function(delay, self.hide)

        if "triggers-transition" in line:
            # Transition sequence
            self.schedule_osc(delay, self.voice_client, "/control/musicbox",
                              [0.0, 0.5, 0.8, 0.0])
            self.schedule_osc(delay + 1, self.voice_client, "/control/beacon",
                              [0.9, 0.0])
            self.schedule_osc(delay + 1, self.voice_client,
                              "/control/bassheart", [0.9, 0.9])
            self.schedule_osc(delay + 1, self.voice_client,
                              "/control/membrane", [0.9, 0.45, 0.0])
            self.schedule_osc(delay + 4, self.voice_client,
                              "/control/membrane", [0.9, 0.45, 0.2])
            self.schedule_osc(delay + 5, self.voice_client,
                              "/control/musicbox", [0.7, 0.0, 0.8, 0.0])

        if "triggers-gan" in line:
            print("Say response!")
            self.last_react = self.last_speech = time.time(
            ) + delay + self.speech_duration
            self.state = "GAN"
            self.matched_to_word = 0
            echo = None
            if "triggers-echo" in line:
                echo = line["triggers-echo"]
            distort = None
            if "triggers-distort" in line:
                distort = line["triggers-distort"]

            if self.script.awaiting_index == self.script.length - 1:
                print("Ending sequence!!")
                self.state = "END"
                self.schedule_osc(delay, self.t2i_client, "/table/fadeout", 1)
                self.schedule_osc(delay, self.voice_client,
                                  "/control/musicbox", [0.0, 0.0, 0.0, 0.5])
                self.schedule_osc(delay, self.voice_client, "/control/beacon",
                                  [0.0, 0.0])
                self.schedule_osc(delay, self.voice_client, "/control/strings",
                                  [0.0, 0.0])
                self.schedule_osc(delay, self.voice_client, "/control/bells",
                                  [0.0, 0.0])
                self.schedule_osc(delay, self.voice_client,
                                  "/control/synthbass", [0.0, 0.0, 0.0])

                self.schedule_osc(delay + 2, self.voice_client,
                                  "/control/stop", 1)

                self.schedule_osc(delay + 5.5, self.voice_client,
                                  "/strings/effect", [3, 0.0])
                self.schedule_osc(delay + 5.5, self.voice_client,
                                  "/control/strings", [0.0, 1.0])
                #self.schedule_osc(delay + 5.5, self.voice_client, "/control/bells", [0.0, 0.2])
                #self.schedule_osc(delay + 5.5, self.voice_client, "/control/synthbass", [0.0, 0.0, 0.2])

                self.say(delay + 2,
                         callback=self.next_line,
                         echos=echo,
                         distorts=distort)
                self.schedule_osc(delay + 12, self.voice_client,
                                  "/control/start", 1)
                self.schedule_osc(delay + 19, self.t2i_client, "/table/titles",
                                  1)
            else:
                self.say(delay,
                         callback=self.next_line,
                         echos=echo,
                         distorts=distort)
            if "triggers-effect" in line:
                self.load_effect(line["triggers-effect"])
                self.schedule_function(delay + line["triggers-effect"]["time"],
                                       self.play_effect)

        else:
            self.next_line(delay)

    # if "triggers-beat" in line:
    #    self.voice_client.send_message("/gan/beat",0.0)

    def play_effect(self):
        self.voice_client.send_message("/effect/play", 1)

    def next_line(self, delay=0):
        self.last_react = self.last_speech = time.time() + delay
        if self.script.next_line():
            self.state = "SCRIPT"
            print("Preload {}/{}?".format(self.script.awaiting_index + 1,
                                          self.script.length))
            if (self.script.awaiting_index + 1 <= self.script.length - 1
                    and "triggers-gan" in self.script.data["script-lines"][
                        self.script.awaiting_index + 1]):
                self.preload_speech(
                    "gan_responses/{}.wav".format(self.script.awaiting_index +
                                                  1))

            self.schedule_function(delay, self.show_next_line)
        else:
            self.end()

    def end(self):
        self.state = "END"
        print("END")

        self.schedule_osc(4, self.voice_client, "/control/strings", [0.0, 0.0])
        self.schedule_osc(4, self.voice_client, "/control/bells", [0.0, 0.0])
        self.schedule_osc(4, self.voice_client, "/control/synthbass",
                          [0.0, 0.0, 0.0])

        self.schedule_function(10, self.stop)
        #self.pix2pix_client.send_message("/gan/end",1)

    def say(self,
            delay_sec=0,
            delay_effect=False,
            callback=None,
            echos=None,
            distorts=None):

        if self.speech_duration:

            print("Saying line with {} delay".format(delay_sec))

            self.pause_listening(math.ceil(self.speech_duration + delay_sec))

            effect_time = 0.05

            self.schedule_osc(delay_sec, self.voice_client, "/speech/play", 1)
            self.schedule_osc(delay_sec + self.speech_duration + 0.2,
                              self.voice_client, "/speech/stop", 1)
            self.schedule_osc(delay_sec, self.t2i_client, "/gan/speaks", 1)

            if echos:
                # one or many?
                if isinstance(echos[0], numbers.Number):
                    echos = [echos]

                for echo in echos:
                    self.schedule_osc(delay_sec + echo[0], self.voice_client,
                                      "/gan/echo", 3)
                    self.schedule_osc(delay_sec + echo[1], self.voice_client,
                                      "/gan/echo", 2)

            if distorts:
                # one or many?
                if isinstance(distorts[0], numbers.Number):
                    distorts = [distorts]

                for distort in distorts:
                    self.schedule_osc(delay_sec + distort[0],
                                      self.voice_client, "/gan/distort", 1.0)
                    self.schedule_osc(delay_sec + distort[1],
                                      self.voice_client, "/gan/distort", 0.0)

            if self.state == "GAN":

                # coming back
                self.schedule_osc(delay_sec + self.speech_duration,
                                  self.voice_client, "/control/bassheart",
                                  [0.65, 0.0])
                self.schedule_osc(delay_sec + self.speech_duration,
                                  self.voice_client, "/control/musicbox",
                                  [0.65, 0.5, 0.65, 0.5])
                self.schedule_osc(delay_sec + self.speech_duration,
                                  self.voice_client, "/control/membrane",
                                  [0.6, 0.0, 0.0])
                self.schedule_osc(delay_sec + self.speech_duration,
                                  self.voice_client, "/control/beacon",
                                  [0.8, 0.26])
            #self.schedule_osc(self.speech_duration + delay_sec, self.voice_client, "/gan/heartbeat", 0)
            #self.schedule_osc(self.speech_duration + delay_sec, self.voice_client, "/gan/bassheart", [1.0, 0.0])

            self.schedule_osc(self.speech_duration + delay_sec,
                              self.t2i_client, "/gan/speaks", 0)

            if callback:
                self.schedule_function(self.speech_duration + delay_sec,
                                       callback)

        else:
            print("Nothing to say!")

    def preload_speech(self, file_name):
        print("Preload speech {}", file_name)
        with contextlib.closing(wave.open(file_name, 'r')) as f:
            frames = f.getnframes()
            rate = f.getframerate()
            self.speech_duration = frames / float(rate)

        #shutil.copyfile(
        #        file_name,
        #        "tmp/gan.wav"
        #)
        #print("Copied")
        #self.voice_client.send_message("/speech/reload",1)
        absPath = os.path.abspath(file_name)
        self.voice_client.send_message("/speech/load", absPath)

    def pause_listening(self, duration=0):
        #asyncio.ensure_future(self.server.pause_listening(duration))
        print("Pause listening for {}".format(duration))
        self.recognizer.stop()
        if self.state != "INTRO" and duration >= 1:
            # Minus 1 for the time it takes to start listening
            self.schedule_function(duration - 0.5, self.start_google)

    def control(self, data):
        print("Control command! {}".format(data))
        command = data["command"]
        if command == 'start':
            self.start_intro()
        elif command == 'stop':
            self.stop()
        elif command == 'skip-intro':
            self.purge_osc()
        elif command == 'stop-ser':
            self.ser_stop()
        elif command == 'start-ser':
            self.ser_start()
        elif command == 'start-question':
            self.preload_speech("gan_question/line.wav")
            self.schedule_function(0.5, self.start_question)
        elif command == 'start-script':
            self.start_script()
        elif command == 'hide':
            self.hide()

    def ser_stop(self):
        self.args.stop = True

    def ser_start(self):
        self.args.stop = False
        self.live_ser.listen(self.args)

    def stop(self):
        print("Stopping experience")
        self.script.reset()
        self.voice_client.send_message("/control/membrane", [0.0, 0.0, 0.0])
        self.voice_client.send_message("/control/stop", 1)
        self.t2i_client.send_message("/table/fadeout", 1)
        self.t2i_client.send_message("/control/stop", 1)
        self.voice_client.send_message("/speech/stop", 1)
        self.send_noise = False
        asyncio.ensure_future(self.server.control("stop"))
        self.pause_listening()
        self.state = "WAITING"
        self.purge_osc()
        self.purge_func()
        #self.pix2pix_client.send_message("/control/stop",1)

    def start_intro(self):
        if self.state != "WAITING":
            print("Not starting intro twice!")
            return

        print("Start intro!")
        self.script.reset()

        self.state = "INTRO"
        self.send_noise = False
        self.voice_client.send_message("/control/stop", 1)
        self.pause_listening()

        self.voice_client.send_message("/control/bells", [0.0, 0.26])
        self.voice_client.send_message("/control/musicbox",
                                       [0.0, 0.0, 0.0, 0.5])
        self.voice_client.send_message("/control/strings", [0.0, 0.0])
        self.voice_client.send_message("/strings/effect", [2, 0.0])
        self.voice_client.send_message("/control/bassheart", [0.0, 0.0])
        self.voice_client.send_message("/control/membrane", [0.0, 0.0, 0.0])
        self.voice_client.send_message("/control/beacon", [0.0, 0.0])
        self.voice_client.send_message("/control/synthbass", [0.0, 0.0, 0.0])
        self.voice_client.send_message("/gan/distort", 0.0)
        self.voice_client.send_message("/gan/echo", 2)

        self.t2i_client.send_message("/control/start", 1)
        asyncio.ensure_future(self.server.control("start"))
        self.preload_speech("gan_intro/intro.wav")
        #self.load_effect(self.script.data["intro-effect"])
        #self.schedule_function(0.5, self.play_effect)
        self.say(delay_sec=0.5, callback=self.pre_question)
        self.schedule_osc(13.4, self.voice_client, "/control/start", 1)
        self.schedule_osc(31.51, self.voice_client, "/control/strings",
                          [0.0, 0.95])
        self.schedule_osc(61.51, self.voice_client, "/control/synthbass",
                          [0.0, 0.0, 0.4])
        self.schedule_function(61.51, self.start_noise)

    def start_noise(self):
        self.send_noise = True

    def stop_noise(self):
        self.send_noise = False

    def hide(self):
        asyncio.ensure_future(self.server.control("hide"))

    ########### QUESTION ###############

    def pre_question(self):
        self.preload_speech("gan_question/line.wav")
        self.schedule_function(6, self.start_question)
        self.schedule_osc(8.5, self.voice_client, "/control/strings",
                          [0.5, 0.0])
        self.schedule_osc(8.5, self.voice_client, "/control/bells", [0.7, 0.0])
        self.schedule_osc(8.5, self.voice_client, "/control/synthbass",
                          [0.8, 0.0, 0.0])

    def start_question(self):
        print("Start question")
        self.send_noise = True
        self.current_question_timeout = None
        self.last_asked = time.time() + self.speech_duration
        self.question_answer = None
        self.state = "QUESTION"
        self.question_timeout_index = 0
        self.say()
        self.schedule_function(self.speech_duration - 0.5, self.table_fadein)
        self.schedule_function(self.speech_duration + 1,
                               self.load_next_question_timeout)

    def table_fadein(self):
        self.t2i_client.send_message("/table/fadein", 1)

    def check_question(self):
        if self.state != "QUESTION":
            return

        if self.question_answer is not None:
            self.pre_script()
        else:
            self.question_timed_out()

    def load_next_question_timeout(self):
        print("Load question timeout")
        if self.question_timeout_index < len(
                self.script.data["question"]["timeouts"]):
            self.current_question_timeout = self.script.data["question"][
                "timeouts"][self.question_timeout_index]
            self.preload_speech("gan_question/timeout{}.wav".format(
                self.question_timeout_index))
            self.schedule_function(self.current_question_timeout["after"],
                                   self.check_question)
        else:
            self.current_question_timeout = None
            self.pre_script()

    def question_timed_out(self):
        if self.question_answer == None:
            print("Question timed out!")
            self.question_timeout_index += 1
            self.last_asked = time.time() + self.speech_duration
            self.say(callback=self.load_next_question_timeout)

    ######### QUESTION ################

    def pre_script(self):
        self.state = "PRE-SCRIPT"
        self.preload_speech("gan_intro/pre_script.wav")
        affects = self.script.data["question"]["affects"]
        target = self.script.data["script-lines"][affects["line"]]
        if not self.question_answer:
            self.question_answer = affects["default"]
        print("PRE SCRIPT!! Chosen food: {}".format(self.question_answer))
        self.t2i_client.send_message("/table/dinner", self.question_answer)
        self.voice_client.send_message("/control/synthbass", [0.0, 0.4, 0.0])
        target["text"] = target["text"].replace("%ANSWER%",
                                                self.question_answer)
        self.schedule_function(7, self.say_pre_script)
        self.schedule_function(13, self.show_plates)

    def say_pre_script(self):
        self.say(callback=self.spotlight_mom)

    def show_plates(self):
        print("Show plates")
        self.t2i_client.send_message("/table/showplates", 1)

        # Main theme
        self.voice_client.send_message("/control/musicbox",
                                       [0.7, 0.5, 0.0, 0.5])
        self.voice_client.send_message("/control/beacon", [0.8, 0.26])

    def spotlight_mom(self):
        print("Spotlight on mom")
        self.t2i_client.send_message("/spotlight", "mom")
        self.schedule_function(2, self.start_script)

    def start_script(self):
        print("Start script")
        self.last_react = self.last_speech = time.time()
        self.state = "SCRIPT"
        self.show_next_line()

    def show_next_line(self):
        self.t2i_client.send_message(
            "/script",
            [self.script.awaiting["speaker"], self.script.awaiting_text])

        print("{}, PLEASE SAY: {}".format(self.script.awaiting["speaker"],
                                          self.script.awaiting_text))

    def load_effect(self, data):
        print("Load effect {}".format(data["effect"]))
        self.voice_client.send_message("/effect/fades", data["fades"])
        absPath = os.path.abspath("effects/{}.wav".format(data["effect"]))
        self.voice_client.send_message("/effect/load", absPath)

    def mood_update(self, data):
        self.mental_state.value = float(data["value"])
        self.mental_state_updated()

    def pix2pix_update(self, loss=None):
        if self.send_noise:
            self.voice_client.send_message("/noise/trigger", 1)
        if loss:
            mapped = interp(loss, [0.016, 0.03], [0, 1])
            print("Sending loss function update. {} mapped to {} ".format(
                loss, mapped))
            self.voice_client.send_message("/synthbass/effect", mapped)
Exemplo n.º 20
0
@author: quentinpeter
"""
import numpy as np
from script import Script

fn = '20180309_chip.txt'

write_large = {'power': 4, 'speed': 400, 'offset': -.8}
write_small = {'power': 2.6943, 'speed': 400, 'offset': -0.95}

off_speed = 1500
focus_int = 0.5

script = Script(focus_int=focus_int,
                off_speed=off_speed,
                write_speed=write_large['speed'],
                write_power=write_large['power'])

for x_origin in np.arange(10) * 1000:

    origin = [x_origin, 0]

    script.write_power = write_large['power']
    script.write_speed = write_large['speed']
    # Write legs
    z = write_large['offset']

    Ymotor_start = origin[1]
    for Xmotor in origin[0] + np.arange(2) * 100:

        for Ymotor in np.arange(0, 95 * 15, 95) + Ymotor_start:
Exemplo n.º 21
0
 def test_marks(self):
   script = Script('helllllo')
   script.add_mark(1, gravity='left', name='left_mark')
   script.add_mark(1, gravity='right', name='right_mark')
   self.assertEqual(script.mark_position('left_mark'), 1)
   self.assertEqual(script.mark_position('right_mark'), 1)
   script.insert_text(0, '1234567890')
   self.assertEqual(script.mark_position('left_mark'), 11)
   self.assertEqual(script.mark_position('right_mark'), 11)    
   script.insert_text(12, '1234567890')
   self.assertEqual(script.mark_position('left_mark'), 11)
   self.assertEqual(script.mark_position('right_mark'), 11)    
   script.insert_text(11, '1234567890') 
   self.assertEqual(script.mark_position('left_mark'), 11)
   self.assertEqual(script.mark_position('right_mark'), 21)    
   script.delete_range(11, 13) 
   self.assertEqual(script.mark_position('left_mark'), 11)
   self.assertEqual(script.mark_position('right_mark'), 19)    
   script.delete_range(5) 
   self.assertEqual(script.mark_position('left_mark'), 5)
   self.assertEqual(script.mark_position('right_mark'), 5)    
Exemplo n.º 22
0
class Gui(object):
  def __init__(self):
    builder = gtk.Builder()
    builder.add_from_file('gui.glade')
    builder.connect_signals(self)
    
    self.window = builder.get_object('window')
    self.toolbutton_run = builder.get_object('toolbutton_run')
    self.toolbutton_abort = builder.get_object('toolbutton_abort')
    self.main_notebook = builder.get_object('main_notebook')
    self.left_notebook = builder.get_object('left_notebook')
    self.templates_menu_item = builder.get_object('imagemenuitem_new_template')
    
    submenu = self.create_templates_menu()
    self.templates_menu_item.set_submenu(submenu)

    
    self.textbuffer_script = gtksourceview2.Buffer()
    lm = gtksourceview2.LanguageManager()
    lang = lm.get_language('python')
    self.textbuffer_script.set_highlight_syntax(True)
    self.textbuffer_script.set_language(lang)    
    #syntax_loader = SyntaxLoader('python')
    #self.textbuffer_script = CodeBuffer(None, syntax_loader)
    self.textbuffer_script.connect('delete-range', 
        self.on_textbuffer_script_delete_range)
    self.textbuffer_script.connect('insert-text', 
        self.on_textbuffer_script_insert_text)
    self.source_view = gtksourceview2.View(self.textbuffer_script)
    self.source_view.set_auto_indent(True)
    self.source_view.set_indent_on_tab(True)
    self.source_view.set_indent_width(2)
    self.source_view.set_insert_spaces_instead_of_tabs(True)
    self.source_view.set_show_line_numbers(True)
    self.source_view.set_smart_home_end(True)
    self.source_view.set_show_line_marks(True)
    scheme = gtksourceview2.style_scheme_manager_get_default().get_scheme(
        'tango')
    self.textbuffer_script.set_style_scheme(scheme)
    self.source_view.set_show_line_marks(True)
    
    
    font_desc = pango.FontDescription('monospace 10')
    if font_desc:
        self.source_view.modify_font(font_desc)
    #self.source_view = gtk.TextView(self.textbuffer_script)
    self.source_view.set_size_request(500, -1)
    
    
    scrolled_win = gtk.ScrolledWindow()
    scrolled_win.show()
    scrolled_win.add(self.source_view)
    self.source_view.show()
    self.left_notebook.append_page(scrolled_win, gtk.Label('Code'))
    
    scrolled_win_control_box = gtk.ScrolledWindow()
    scrolled_win_control_box.show()
    self.left_notebook.append_page(scrolled_win_control_box, gtk.Label('Controls'))
    self.left_notebook.show_all()
    
    self.control_box_manager = ControlBoxManager(scrolled_win_control_box)
    self.control_box_manager.add_widget('Welcome! Use File->New from template to start a new project.', gtk.Label())
    
    self.left_notebook.set_current_page(1)
  
    self.script = Script()
    self.script.text_inserted += self.on_script_text_inserted
    self.script.range_deleted += self.on_script_range_deleted


    # configuer logging for the application:
    logging.getLogger('').setLevel(logging.DEBUG)
    self.textview_log = builder.get_object('textview_log')
    self.gui_handler = TextViewHandler(self.textview_log)
    stream_handler = logging.StreamHandler()
    formatter = logging.Formatter(
        "%(asctime)s - %(levelname)s - %(message)s")
    self.gui_handler.setFormatter(formatter)
    stream_handler.setFormatter(formatter)
    # Not adding by default (see view->show logs)
    #logging.getLogger('').addHandler(gui_handler)
    logging.getLogger('').addHandler(stream_handler)
    
    
    self.file_manager = FileManager(self.script)
    self.file_manager.current_file_changed += self.on_current_file_changed
    self.file_manager.need_save_changed += self.on_need_save_changed
    
    # TODO(daniv): These are ugly hacks to pervent raising events when the
    # is not the user.
    self.ignore_next_on_textbuffer_script_delete_range = False
    self.ignore_next_on_textbuffer_script_insert_text = False
    
    self.space_manager = SpaceManager(self.main_notebook)
    self.changer_manager = ParameterChangerManager(self.script)
    self.script_server = ScriptServer(self.changer_manager)
    self.script_server.script_started += self.on_script_started
    self.script_server.script_ended += self.on_script_ended
    
    
    
    
    global services
    services.init(self.space_manager, self.changer_manager, self.script_server, self.window.get_toplevel(), self.control_box_manager, self.textview_log)
    
    
    
    self._updateWindowTitle()

  def template_menu_activate(self, menu_item, template_name, template_path):
     with open(template_path, 'rU') as f:
       template = f.read()
     print template
     if self.on_imagemenuitem_new_activate(None):
       self.script.insert_text(0, template, None)
       self.on_toolbutton_run_clicked(None)
     
  def create_templates_menu(self):
    current_filename = os.path.realpath(__file__)
    templates_path = os.path.dirname(current_filename) + '/../templates'
    templates_path = os.path.realpath(templates_path)
    templates = [
        ('Progress over event number', 'qc.py'),
        ('Gate and switch dimensions', 'gate_switch.py'),
        ('PCA components', 'pca.py'),
        ('Histograms', 'hists.py'),
        ('Mutual Information', 'mutual.py')]
    templates_menu = gtk.Menu()
    templates_menu.show()
    for t in templates:
      t_path = os.path.join(templates_path, t[1])
      item = gtk.MenuItem(label = t[0])
      item.show()
      item.connect('activate', self.template_menu_activate, t[0], t_path)
      templates_menu.append(item)
    return templates_menu
  
  def on_toolbutton_run_clicked(self, toolbutton):
    global services
    services._cache = {}
    buff = self.textview_log.get_buffer()
    buff.delete(buff.get_start_iter(), buff.get_end_iter())
    self.space_manager.clear_spaces()
    self.control_box_manager.clear()
    script_name = self.file_manager.current_file
    if not script_name:
      script_name = 'Untitled'
    script_name +='_%.2f' % time.time()
    self.script_server.add_to_queue(script_name)
    self.toolbutton_run.set_sensitive(False)

  def on_checkmenuitem_show_log_messages_toggled(self, menu_item):
    print 'adfasdasdfasdffs'
    if menu_item.get_active():
      logging.getLogger('').addHandler(self.gui_handler)
    else:
      logging.getLogger('').removeHandler(self.gui_handler)
      
  def on_toolbutton_abort_clicked(self, toolbutoon):
    try:
      self.script_server.terminate_script()
      self.toolbutton_abort.set_sensitive(False)    
    except:
      logging.exception('could not terminate script')

  def on_window_destroy(self, widget, data=None):
    logging.info('closing script server')
    #while not self.script_server.end_server(1):
    #  self.script_server.terminate_script()
    gtk.main_quit()

  # Script textview events (from GUI to DOM)
  def on_textbuffer_script_delete_range(self, txtbuffer, start, end):
    if not self.ignore_next_on_textbuffer_script_delete_range:
      self.script.delete_range(start.get_offset(), end.get_offset(), self)
    self.ignore_next_on_textbuffer_script_delete_range = False

  def on_textbuffer_script_insert_text(self, txtbuffer, iter, text, length):
    if not self.ignore_next_on_textbuffer_script_insert_text:
      self.script.insert_text(iter.get_offset(), text, self)
    self.ignore_next_on_textbuffer_script_insert_text = False

  # Internal script events (from DOM to GUI)
  # The _gui versions run on the gui thread (and must return false).
  def on_script_text_inserted(self, position, text, sender):
    def on_script_text_inserted_gui(position, text):
      pos_iter =  self.textbuffer_script.get_iter_at_offset(position)
      # TODO(daniv): maybe count here, or think of a less ugly solution
      if text:
        self.ignore_next_on_textbuffer_script_insert_text = True
        self.textbuffer_script.insert(pos_iter, text)
    if sender != self:
      gobject.idle_add(on_script_text_inserted_gui, position, text)

  def on_script_range_deleted(self, start, end, sender):
    def on_script_range_deleted_gui(start, end):
      start_iter =  self.textbuffer_script.get_iter_at_offset(start)
      end_iter =  self.textbuffer_script.get_iter_at_offset(end)
      if start_iter.get_offset() != end_iter.get_offset():
        self.ignore_next_on_textbuffer_script_delete_range = True
        self.textbuffer_script.delete(start_iter, end_iter)
    if sender != self:
      gobject.idle_add(on_script_range_deleted_gui, start, end)

  # Script server events
  def on_script_started(self, script_name):
    def on_script_started_gui(self, script_name):
      self.toolbutton_run.set_visible_horizontal(False)
      self.toolbutton_run.set_visible_vertical(False)
      self.toolbutton_abort.set_visible_horizontal(True)
      self.toolbutton_abort.set_visible_vertical(True)
      self.toolbutton_abort.set_sensitive(True)
    gobject.idle_add(on_script_started_gui, self, script_name)

  def on_script_ended(self, script_name):
    def on_script_ended_gui(self, script_name):
      self.toolbutton_run.set_visible_horizontal(True)
      self.toolbutton_run.set_visible_vertical(True)
      self.toolbutton_abort.set_visible_horizontal(False)
      self.toolbutton_abort.set_visible_vertical(False)
      self.toolbutton_run.set_sensitive(True)
    gobject.idle_add(on_script_ended_gui, self, script_name)

  # Menu events
  
  def _show_save_changes_dialog(self):
    """Returns false if we need to cancel"""
    dia = gtk.Dialog(
        'Save File?',
        self.window.get_toplevel(),
        gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
        ('Yes', 1, 
            'No', 2, 
            'Cancel', 3))
    dia.vbox.pack_start(gtk.Label('Do you want to play a game?'))
    dia.show()
    result = dia.run()
    dia.destroy()
    if result == 1:
       self.on_imagemenuitem_save_activate(None)
    return result == 1 or result == 2
#   code for when the users closes the dialog: gtk.RESPONSE_CLOSE
    
    
  def on_imagemenuitem_new_activate(self, menuitem):
    if self.file_manager.need_save and not self._show_save_changes_dialog():
      return False
    self.file_manager.new()
    return True

  def on_imagemenuitem_open_activate(self, menuitem):
    if self.file_manager.need_save and not self._show_save_changes_dialog():
      return
    dialog = gtk.FileChooserDialog(
        title='Open',
        action=gtk.FILE_CHOOSER_ACTION_OPEN,
        buttons=(
            gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
            gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)
    filter = gtk.FileFilter()
    filter.set_name("Python Scripts")
    filter.add_pattern("*.py")
    dialog.add_filter(filter)
    filter = gtk.FileFilter()
    filter.set_name("All files")
    filter.add_pattern("*")
    dialog.add_filter(filter)
    if dialog.run() == gtk.RESPONSE_OK:
      filename = dialog.get_filename()
      self.file_manager.open(filename)
    dialog.destroy()

  def on_imagemenuitem_quit_activate(self, menuitem):
    pass
  
  def on_imagemenuitem_save_activate(self, menuitem):
    if not self.file_manager.current_file:
      return self.on_imagemenuitem_save_as_activate(menuitem)
    self.file_manager.save()

  def on_imagemenuitem_save_as_activate(self, menuitem):
    dialog = gtk.FileChooserDialog(
        title='Save as',
        action=gtk.FILE_CHOOSER_ACTION_SAVE,
        buttons=(
            gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
            gtk.STOCK_SAVE,gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)
    dialog.set_do_overwrite_confirmation(True)
    py_filter = gtk.FileFilter()
    py_filter.set_name("Python Scripts")
    py_filter.add_pattern("*.py")
    dialog.add_filter(py_filter)
    filter = gtk.FileFilter()
    filter.set_name("All files")
    filter.add_pattern("*")
    dialog.add_filter(filter)
    if dialog.run() == gtk.RESPONSE_OK:
      filename = dialog.get_filename()
      #if dialog.get_filter() == py_filter and filename.find('.') == -1:
      #  filename += '.py'
      self.file_manager.save_as(filename)
    dialog.destroy()

  # File manager events  
  def _updateWindowTitle(self):
    program_name = 'editor'
    filename = self.file_manager.current_file
    if filename == None:
      filename = 'Untitled'
    save_str = ''
    if self.file_manager.need_save:
      save_str='*'
    title = '%s%s - %s' % (save_str, filename, program_name)
    #logging.debug('updating window title to %s', title)
    self.window.set_title(title)

  def on_need_save_changed(self):
    def on_need_save_changed_gui(self):
      self._updateWindowTitle()
    gobject.idle_add(on_need_save_changed_gui, self)

  def on_current_file_changed(self):
    def on_current_file_changed(self):
      self._updateWindowTitle()
    gobject.idle_add(on_current_file_changed, self)
Exemplo n.º 23
0
 def tx_out_parse(cls, s):
     amount = little_endian_to_int(s.read(8))
     script_pubkey = Script.parse(s)
     return cls(amount, script_pubkey)
Exemplo n.º 24
0

def dump_parse_expr(s):

    tree = ast.parse(s, mode='eval')
    return ast.dump(tree.body)


if __name__ == '__main__':

    # Boilerplate.
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, 'some_tests.py')

    with open(filename) as f:
        script = Script(f.read())

    # Create the globals_dict we will test.
    globals_dict = {}

    # Create the globals_dict we will test.
    ast_names = 'Attribute Call Compare Eq Load Lt Name Num'.split()
    globals_dict.update(
        (k, getattr(ast,k))
        for k in ast_names
        )

    globals_dict.update(
        ast = ast,
        dpe = dump_parse_expr,
        )
Exemplo n.º 25
0
 def parseScript(self):
     part = self.getPartWithName('py_script')
     headerAndScript = part.split('\n', 1)
     return Script(headerAndScript[1])
Exemplo n.º 26
0
# ############## PLEASE RUN THIS CELL FIRST! ###################

# # import everything and define a test runner function
# from importlib import reload
# from helper import run
# import ecc
# import helper
# import op
# import script
# import tx

# Exercise 4

from io import BytesIO
from ecc import S256Point, Signature
from helper import encode_varint, hash256, int_to_little_endian
from script import Script
from tx import Tx, SIGHASH_ALL

hex_tx = '0100000001868278ed6ddfb6c1ed3ad5f8181eb0c7a385aa0836f01d5e4789e6bd304d87221a000000db00483045022100dc92655fe37036f47756db8102e0d7d5e28b3beb83a8fef4f5dc0559bddfb94e02205a36d4e4e6c7fcd16658c50783e00c341609977aed3ad00937bf4ee942a8993701483045022100da6bee3c93766232079a01639d07fa869598749729ae323eab8eef53577d611b02207bef15429dcadce2121ea07f233115c6f09034c0be68db99980b9a6c5e75402201475221022626e955ea6ea6d98850c994f9107b036b1334f18ca8830bfff1295d21cfdb702103b287eaf122eea69030a0e9feed096bed8045c8b98bec453e1ffac7fbdbd4bb7152aeffffffff04d3b11400000000001976a914904a49878c0adfc3aa05de7afad2cc15f483a56a88ac7f400900000000001976a914418327e3f3dda4cf5b9089325a4b95abdfa0334088ac722c0c00000000001976a914ba35042cfe9fc66fd35ac2224eebdafd1028ad2788acdc4ace020000000017a91474d691da1574e6b3c192ecfb52cc8984ee7b6c568700000000'
hex_sec = '03b287eaf122eea69030a0e9feed096bed8045c8b98bec453e1ffac7fbdbd4bb71'
hex_der = '3045022100da6bee3c93766232079a01639d07fa869598749729ae323eab8eef53577d611b02207bef15429dcadce2121ea07f233115c6f09034c0be68db99980b9a6c5e754022'
hex_redeem_script = '475221022626e955ea6ea6d98850c994f9107b036b1334f18ca8830bfff1295d21cfdb702103b287eaf122eea69030a0e9feed096bed8045c8b98bec453e1ffac7fbdbd4bb7152ae'
sec = bytes.fromhex(hex_sec)
der = bytes.fromhex(hex_der)
redeem_script = Script.parse(BytesIO(bytes.fromhex(hex_redeem_script)))
stream = BytesIO(bytes.fromhex(hex_tx))

dat_tx = Tx.parse(stream)
z = Tx.verify_input(dat_tx, 0)
Exemplo n.º 27
0
 def __init__(self, actors, topics, pool):
     self.characters = self.generateCharacters(actors)
     self.topics = topics
     self.pool = pool
     self.script = Script("TEST SCRIPT")
Exemplo n.º 28
0
class TxIn:
    def __init__(self,
                 prev_tx,
                 prev_index,
                 script_sig=None,
                 sequence=0xffffffff):
        self.prev_tx = prev_tx
        self.prev_index = prev_index
        if script_sig is None:
            self.script_sig = Script()
        else:
            self.script_sig = script_sig
        self.sequence = sequence

    def __repr__(self):
        return '{}:{}'.format(
            self.prev_tx.hex(),
            self.prev_index,
        )

    @classmethod
    def parse(cls, s):
        '''Takes a byte stream and parses the tx_input at the start
        return a TxIn object
        '''
        raise NotImplementedError

    def serialize(self):
        '''Returns the byte serialization of the transaction input'''
        # serialize prev_tx, little endian
        result = self.prev_tx[::-1]
        # serialize prev_index, 4 bytes, little endian
        result += int_to_little_endian(self.prev_index, 4)
        # serialize the script_sig
        result += self.script_sig.serialize()
        # serialize sequence, 4 bytes, little endian
        result += int_to_little_endian(self.sequence, 4)
        return result

    def fetch_tx(self, testnet=False):
        return TxFetcher.fetch(self.prev_tx.hex(), testnet=testnet)

    def value(self, testnet=False):
        '''Get the outpoint value by looking up the tx hash
        Returns the amount in satoshi
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the amount property
        return tx.tx_outs[self.prev_index].amount

    def script_pubkey(self, testnet=False):
        '''Get the ScriptPubKey by looking up the tx hash
        Returns a Script object
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the script_pubkey property
        return tx.tx_outs[self.prev_index].script_pubkey
Exemplo n.º 29
0
 def runScript(self, evt):
     #        with Script() as script:
     script = Script()
     yield deferToThread(script.run)
Exemplo n.º 30
0
 def get_custom_scripts(self):
     all_scripts = []
     for script in self._manifest_custom_scripts:
         custom_script_context = {'script_location': script}
         all_scripts.append(Script(custom_script_context))
     return all_scripts
Exemplo n.º 31
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from script import Script

if __name__ == '__main__':
    st = Script()
    st.test01()
Exemplo n.º 32
0
 async def do_scan(self, module: Script,target: Union[dict]) -> Iterable[dict]:
     records = []
     func_name = self.pm.func_name
     parameter = self.pm.parameter
     flag = -1
     try:
         poc = module.POC()
         poc.initialize(target['host'], target['port'], target['url'], parameter)
         func = getattr(poc, func_name)
         logger.debug(
             "Running %s:%s for %s:%s" % (module.__name__, func_name, poc.target_host, poc.target_port))
         async with async_timeout.timeout(timeout=int(conf['basic']['timeout'])):
             await func()
             flag = poc.flag
             if poc.url != None:
                 target['url'] = poc.url
     except AttributeError as e:
         if 'has no attribute \'POC\'' in get_safe_ex_string(e):
             logger.error('Invalid POC script, Please check the script: %s' % module.__name__, )
         elif '\'POC\' object has no attribute' in get_safe_ex_string(e):
             logger.error('%s, Please check it in the script: %s' % (e, module.__name__))
         elif 'Function is not exist.' in get_safe_ex_string(e):
             logger.error(
                 'Function is not exist, Please check \'%s\' in the script: %s' % (
                     func_name, module.__name__,))
         else:
             self.errmsg = traceback.format_exc()
             logger.error(self.errmsg)
             logger.error("%s %s:%s for %s:%d" % (e, module.__name__, func_name, target['host'], target['port']))
         self._error_task_count += 1
     except KeyError as e:
         logger.error("Missing parameters: %s, please load parameters by -p. For example. -p %s=value" % (
             e, str(e).replace('\'', '')))
         self._error_task_count += 1
     except (ConnectionResetError, ConnectionAbortedError, TimeoutError):
         flag = poc.flag
     except (CancelledError, ConnectionRefusedError, OSError):
         if target['status'] != None:
             target['status'] -= 1
         else:
             target['status'] = -1
     except Exception:
         self._error_task_count += 1
         errmsg = traceback.format_exc()
         logger.error("Error for " + target['host'] + ":" + str(target['port']) + "\r\n"+ errmsg)
     finally:
         if conf.VERBOSE or flag >= 0:
             if poc.flag >= 0:
                 self._find_task_count += 1
                 if module.__name__ == 'script.info.port_scan':
                     target['status'] = 5
                     if  len(poc.res) == 0 :
                         poc.res = [{"info": None , "key": "port scan"}]
                     for res in poc.res:
                         target['service'] = res['info']
                         await self.vul_targets.put(target)
                 else:
                     target['status'] = 3
             data = {
                 "id": target['id'],
                 "flag": poc.flag,
                 'module_name': module.__name__,
                 'func_name': func_name,
                 'target_host': poc.target_host,
                 'target_port': poc.target_port,
                 'url': poc.url,
                 'base_url': poc.base_url,
                 "req": poc.req,
                 "res": poc.res,
                 "other": poc.other,
             }
             self.hashdb.insert(data)
             self.hashdb.flush()
             print_dic(data)
             records.append(data)
         logger.debug("Ending  %s:%s for %s:%s" % (module.__name__, func_name, poc.target_host, poc.target_port))
     return records
Exemplo n.º 33
0
Arquivo: main.py Projeto: duy/dispersy
    def start():
        # start Dispersy
        dispersy = Dispersy.get_instance(callback, unicode(opt.statedir))

        if opt.swiftproc:
            # start swift
            from Tribler.Core.Swift.SwiftProcessMgr import SwiftProcessMgr
            sesslock = threading.Lock()
            spm = SwiftProcessMgr(opt.swiftpath, opt.swiftcmdlistenport, opt.swiftdlsperproc, sesslock)
            swift_process = spm.get_or_create_sp(opt.statedir)
            dispersy.endpoint = TunnelEndpoint(swift_process, dispersy)
            swift_process.add_download(dispersy.endpoint)

        else:
            def exception(exception, fatal):
                if fatal:
                    dispersy.endpoint.stop()
            dispersy.endpoint = StandaloneEndpoint(dispersy, opt.port, opt.ip)
            dispersy.endpoint.start()
            dispersy.callback.attach_exception_handler(exception)

        # load the script parser
        if opt.script:
            from script import Script
            script = Script.get_instance(callback)

            script_kargs = {}
            if opt.script_args:
                for arg in opt.script_args.split(','):
                    key, value = arg.split('=')
                    script_kargs[key] = value

            if opt.enable_dispersy_script:
                from script import DispersyClassificationScript, DispersyTimelineScript, DispersyDestroyCommunityScript, DispersyBatchScript, DispersySyncScript, DispersyIdenticalPayloadScript, DispersySubjectiveSetScript, DispersySignatureScript, DispersyMemberTagScript, DispersyMissingMessageScript, DispersyUndoScript, DispersyCryptoScript, DispersyDynamicSettings, DispersyBootstrapServers, DispersyBootstrapServersStresstest
                script.add("dispersy-batch", DispersyBatchScript)
                script.add("dispersy-classification", DispersyClassificationScript)
                script.add("dispersy-crypto", DispersyCryptoScript)
                script.add("dispersy-destroy-community", DispersyDestroyCommunityScript)
                script.add("dispersy-dynamic-settings", DispersyDynamicSettings)
                script.add("dispersy-identical-payload", DispersyIdenticalPayloadScript)
                script.add("dispersy-member-tag", DispersyMemberTagScript)
                script.add("dispersy-missing-message", DispersyMissingMessageScript)
                script.add("dispersy-signature", DispersySignatureScript)
                script.add("dispersy-subjective-set", DispersySubjectiveSetScript)
                script.add("dispersy-sync", DispersySyncScript)
                script.add("dispersy-timeline", DispersyTimelineScript)
                script.add("dispersy-undo", DispersyUndoScript)
                script.add("dispersy-bootstrap-servers", DispersyBootstrapServers)
                script.add("dispersy-bootstrap-servers-stresstest", DispersyBootstrapServersStresstest)

                from callbackscript import DispersyCallbackScript
                script.add("dispersy-callback", DispersyCallbackScript)

            if opt.enable_allchannel_script:
                # from Tribler.Community.allchannel.script import AllChannelScript
                # script.add("allchannel", AllChannelScript, include_with_all=False)

                from Tribler.community.allchannel.script import AllChannelScenarioScript
                script.add("allchannel-scenario", AllChannelScenarioScript, script_kargs, include_with_all=False)

            if opt.enable_walktest_script:
                from Tribler.community.walktest.script import ScenarioScript
                script.add("walktest-scenario", ScenarioScript, script_kargs, include_with_all=False)

            if opt.enable_ycsb_script:
                from Tribler.community.ycsb.script import YCSBScript
                script.add("ycsb-scenario", YCSBScript, script_kargs, include_with_all=False)

            if opt.enable_demers_script:
                from Tribler.community.demerstest.script import DemersScript
                script.add("demers-scenario", DemersScript, script_kargs, include_with_all=False)

            if opt.enable_udp_script:
                from script import DispersyUDPScript
                script.add("udp-scenario", DispersyUDPScript, script_kargs, include_with_all=False)

            if opt.enable_effort_script:
                from Tribler.community.effort.script import ScenarioScript
                script.add("effort-scenario", ScenarioScript, script_kargs, include_with_all=False)

            # if opt.enable_barter_script:
            #     from Tribler.Community.barter.script import BarterScript, BarterScenarioScript
            #     script.add("barter", BarterScript)
            #     script.add("barter-scenario", BarterScenarioScript, script_kargs, include_with_all=False)

            # # bump the rawserver, or it will delay everything... since it sucks.
            # def bump():
            #     pass
            # rawserver.add_task(bump)

            script.load(opt.script)
Exemplo n.º 34
0
change_script = p2pkh_script(change_h160)
change_output = TxOut(amount=change_amount, script_pubkey=change_script)

# output address that will receive our sat

target_amount = int(0.00006 * 100000000)
target_h160 = decode_base58('mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv')
print()
print(target_h160)

target_script = p2pkh_script(target_h160)
print()
print(target_script)
target_output = TxOut(amount=target_amount, script_pubkey=target_script)
tx_obj = Tx(1, [tx_in], [change_output, target_output], 0, True)
#print(tx_obj).hex()

# now we sign the transaction

z = tx_obj.sig_hash(0)
private_key = PrivateKey(little_endian_to_int(
    hash256(b'dat_test_private_key')))
der = private_key.sign(z).der()
sig = der + SIGHASH_ALL.to_bytes(1, 'big')
sec = private_key.point.sec()
script_sig = Script([sig, sec])
tx_obj.tx_ins[0].script_sig = script_sig
print(tx_obj.serialize().hex())

# end
Exemplo n.º 35
0
# make a tx from a p2sh address

secret1 = little_endian_to_int(hash256(b'dat_test_private_key_p2sh1'))

secret2 = little_endian_to_int(hash256(b'dat_test_private_key2_p2sh2'))

private_key1 = PrivateKey(secret1)
private_key2 = PrivateKey(secret2)

public_key1 = decode_base58('mfchv5Xq63UuQTAVwzqhWVHAZVkiZrhDKh')
public_key2 = decode_base58('ms4kdy8Hn4WfxK2J2VpbonTit5NeNy6vJS')

#redeem script as op code

dat_redeem_script_op = Script([0x52, public_key1, public_key2, 0x52, 0xae])

# we serialize it in binary

dat_redeem_script_serialized = dat_redeem_script_op.serialize()

prev_tx = bytes.fromhex(
    'f387eba7393e07b4c2134cd29551b7294a992c9bae129b347b64ba6650522acb')
prev_index = 0

target_h160 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1')
target_script = p2pkh_script(target_h160)

target_amount = int(0.00008 * 100000000)
target_output = TxOut(amount=target_amount, script_pubkey=target_script)
Exemplo n.º 36
0
                    color = '#303030'
                strings.append('<table><tr style="border-bottom: 1px solid #666;"><td width="170px";>'+\
                 '<img src="'+str(row[1])+'" style="opacity:'+opacity+';""></td>')
                strings[-1] += '<td><h2><a href="/document/'+str(uid)+'/'+str(did)+\
                 '" style="color:'+color+';">' + str(row[2]) + '</a></h2>\n'
                strings[
                    -1] += '<p style="color:#606060; font-size:15"><span style="color:#A0A0A0">'
                strings[-1] += parser.parse(str(
                    row[0])).strftime('%d/%m/%Y') + '</span>'
                if str(row[3]) != '':
                    strings[-1] += ' - ' + str(row[3])
                strings[-1] += '</p></td></tr></table>\n'
                #strings[-1] += '\n<hr class="dots"/>\n'

        return ''.join(strings)


if __name__ == '__main__':
    with open('../TextMining/ac.loc', 'rb') as f:
        ac = load(f)
    root = Home(ac)
    root.topic = Topic(ac)
    root.document = Document(ac)
    root.tag = Tag(ac)
    root.recommend = Recommend(ac)
    root.search = Search(ac)
    root.script = Script()
    #cherrypy.server.socket_host = '192.168.1.95'
    #cherrypy.engine.start()
    cherrypy.quickstart(root)
Exemplo n.º 37
0
    def verify_input(self, input_index):
        '''Returns whether the input has a valid signature'''
        # get the relevant transaction input
        txi = self.tx_ins[input_index]

        # grab the previous (utxo) ScriptPubKey--"fetch's the utxo's script_pubkey"
        prev_script_pubkey = txi.script_pubkey(testnet=self.testnet)
        # is the script_pubkey a p2sh?
        if prev_script_pubkey.is_p2sh_script_pubkey():
            print("p2sh")
            # get the redeem script--the last element in the script_sig cmds list
            redeem_script_command = txi.script_sig.cmds[-1]
            # prepend the redeem_script_bytes with its length
            redeem_script_serialized = encode_varint(
                len(redeem_script_command)) + redeem_script_command

            # create the redeem_script as a script object
            redeem_script = Script.parse(BytesIO(redeem_script_serialized))
            if redeem_script.is_p2wpkh_script_pubkey():
                z = self.sig_hash_bip143(input_index, redeem_script)
                witness = txi.witness
            elif redeem_script.is_p2wsh_script_pubkey():
                cmd = txi.witness[-1]
                raw_witness = encode_varint(len(cmd)) + cmd
                witness_script = Script.parse(BytesIO(raw_witness))
                z = self.sig_hash_bip143(input_index,
                                         witness_script=witness_script)
                witness = txi.witness
            else:
                z = self.sig_hash(input_index=input_index,
                                  redeem_script=redeem_script)
                witness = None

        else:
            if prev_script_pubkey.is_p2wpkh_script_pubkey():
                print("p2wpkh")
                z = self.sig_hash_bip143(input_index)
                witness = txi.witness
                print("witness: ")
                print(txi.witness)
            elif prev_script_pubkey.is_p2wsh_script_pubkey():
                print("p2wsh")
                cmd = txi.witness[-1]
                raw_witness = encode_varint(len(cmd)) + cmd
                witness_script = Script.parse(BytesIO(raw_witness))
                z = self.sig_hash_bip143(input_index,
                                         witness_script=witness_script)
                witness = txi.witness
            else:
                z = self.sig_hash(input_index)
                print("else")
                witness = None

        # combine the current ScriptSig and the previous ScriptPubKey
        combined_script = txi.script_sig + prev_script_pubkey
        # evaluate the combined script
        print("in verify_input:  ")
        print(witness)
        verified = combined_script.evaluate(z, witness)
        print("in verify_input:  ")
        print(verified)
        return verified
Exemplo n.º 38
0
class TxIn:
    def __init__(self, prev_tx, prev_index, script_sig=None, sequence=0xffffffff):
        self.prev_tx = prev_tx
        self.prev_index = prev_index
        if script_sig is None:  # <1>
            self.script_sig = Script()
        else:
            self.script_sig = script_sig
        self.sequence = sequence

    def __repr__(self):
        return '{}:{}'.format(
            self.prev_tx.hex(),
            self.prev_index,
        )
    # end::source2[]

    @classmethod
    def parse(cls, s):
        '''Takes a byte stream and parses the tx_input at the start
        return a TxIn object
        '''
        # prev_tx is 32 bytes, little endian
        prev_tx = s.read(32)[::-1]
        
        # prev_index is an integer in 4 bytes, little endian
        prev_index = little_endian_to_int(s.read(4))
        
        # use Script.parse to get the ScriptSig
        script_sig = Script.parse(s)
        
        # sequence is an integer in 4 bytes, little-endian
        sequence = little_endian_to_int(s.read(4))
        
        # return an instance of the class (see __init__ for args)
        return TxIn(prev_tx, prev_index, script_sig, sequence)
        
    # tag::source5[]
    def serialize(self):
        '''Returns the byte serialization of the transaction input'''
        result = self.prev_tx[::-1]
        result += int_to_little_endian(self.prev_index, 4)
        result += self.script_sig.serialize()
        result += int_to_little_endian(self.sequence, 4)
        return result
    # end::source5[]

    # tag::source8[]
    def fetch_tx(self, testnet=False):
        return TxFetcher.fetch(self.prev_tx.hex(), testnet=testnet)

    def value(self, testnet=False):
        '''Get the output value by looking up the tx hash.
        Returns the amount in satoshi.
        '''
        tx = self.fetch_tx(testnet=testnet)
        return tx.tx_outs[self.prev_index].amount

    def script_pubkey(self, testnet=False):
        '''Get the ScriptPubKey by looking up the tx hash.
        Returns a Script object.
        '''
        tx = self.fetch_tx(testnet=testnet)
        return tx.tx_outs[self.prev_index].script_pubkey
Exemplo n.º 39
0
class TxIn:
    # prev_tx in bytes
    # prev_index as integer
    def __init__(self,
                 prev_tx,
                 prev_index,
                 script_sig=None,
                 sequence=0xffffffff,
                 witness=None):
        self.prev_tx = prev_tx
        self.prev_index = prev_index
        if script_sig is None:
            self.script_sig = Script()
        else:
            self.script_sig = script_sig
        self.sequence = sequence

        # Allow for the witness field
        self.witness = witness

    def __repr__(self):
        return '{}:{}:{}:{}:{}'.format(self.prev_tx.hex(), self.prev_index,
                                       self.script_sig, self.sequence,
                                       self.witness)

    @classmethod
    def parse(cls, s):
        '''Takes a byte stream and parses the tx_input at the start
        return a TxIn object
        '''
        # prev_tx is 32 bytes, little endian
        prev_tx = s.read(32)[::-1]
        # prev_index is an integer in 4 bytes, little endian
        prev_index = little_endian_to_int(s.read(4))
        # use Script.parse to get the ScriptSig
        script_sig = Script.parse(s)
        # sequence is an integer in 4 bytes, little-endian
        sequence = little_endian_to_int(s.read(4))
        # return an instance of the class (see __init__ for args)
        return cls(prev_tx, prev_index, script_sig, sequence)

    def serialize(self):
        '''Returns the byte serialization of the transaction input'''
        # serialize prev_tx, little endian
        result = self.prev_tx[::-1]
        # serialize prev_index, 4 bytes, little endian
        result += int_to_little_endian(self.prev_index, 4)
        # serialize the script_sig
        result += self.script_sig.serialize()
        # serialize sequence, 4 bytes, little endian
        result += int_to_little_endian(self.sequence, 4)
        return result

    def fetch_tx(self, testnet=False):
        return TxFetcher.fetch(self.prev_tx.hex(), testnet=testnet)

    def value(self, testnet=False):
        '''Get the outpoint value by looking up the tx hash
        Returns the amount in satoshi
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the amount property
        return tx.tx_outs[self.prev_index].amount

    def script_pubkey(self, testnet=False):
        '''Get the ScriptPubKey by looking up the tx hash
        Returns a Script object
        '''
        # use self.fetch_tx to get the transaction
        tx = self.fetch_tx(testnet=testnet)
        # get the output at self.prev_index
        # return the script_pubkey property
        return tx.tx_outs[self.prev_index].script_pubkey
 def __init__(self, prev_tx, prev_index, script_sig, sequence):
     self.prev_tx = prev_tx
     self.prev_index = prev_index
     self.script_sig = Script.parse(script_sig)
     self.sequence = sequence
Exemplo n.º 41
0
    def process_birthday(self, minfo, keyword):
        log.debug('insert_birthday called')
        
        user = session.query(Follower).filter_by(uniqueid = minfo['id']).one()

        SCRIPT_UNKNOWN = [
                u'%(감탄사) 생일이 언제라는건지 잘 모르겠다요.. 대충 %(랜덤1,12)월 %(랜덤1,28)일로 해두면 되는거냐요?',
                u'생일이 그러니까 언제라는 거냐요! 모른다 닉시코는! %(해시)',
                u'닉시코는 모르겠다요.. %(사람)(이)랑 같은 생일...이다요?']
        SCRIPT_INPUTOK = [
                u'알았다요! %d월 %d일이라는거다요?',
                u'알았다요! 기억해두겠다요! %d월 %d일이다요?']
        SCRIPT_MODIFYOK = [
                u'에엣 %d월 %d일인줄 알았는데.. 알겠다요 %d월 %d일! %s의 생일!',
                u'알겠다요 %d월 %d일이 아니라 %d월 %d일... %s의 생일은 그렇게 기억해두면 되는거다요?']
        SCRIPT_DELETEOK = [
                u'알았다요. 닉시코는 이제 %s의 생일 같은거 모른다요?',
                u'알았다요. 닉시코는 기억에서 %s의 생일 지.웠.다.요.']
        SCRIPT_DUPLICATE = [
                u'알고 있다요! 생일이 그 날이라는거! 이미 알려줬잖다요? 잊지 않는다요!',
                u'에에 이미 알고 있다요? 닉시코를 무시하면 무참하게 당하게 될거다요!']

        KEYWORD_FORGET = ['잊어', '지워', '삭제']
        
        mention = minfo['text'].encode('utf8') if type(minfo['text']) == unicode else minfo['text']
        mention_back = '@%s ' % minfo['screen_name']

        for keyword in KEYWORD_FORGET:
            if keyword in mention:
                user.birthday = None
                script = Script(random.choice(SCRIPT_DELETEOK) % minfo['name'])
                script.translate()
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
                break
        else:

            regex_bday = '(?P<month>\d{1,2})월.*?(?P<day>\d{1,2})일'
            bday = re.search(regex_bday, mention)

            if not bday:
                script = Script(random.choice(SCRIPT_UNKNOWN))
                script.translate()
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
                return
            b_month = int(bday.group('month'))
            b_day = int(bday.group('day'))

            try:
                birthday_in = datetime.datetime(2000, b_month, b_day)
            except ValueError:
                script = Script(random.choice(SCRIPT_UNKNOWN))
                script.translate()
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
                return

            if user.birthday is not None:
                if user.birthday.day == b_day and user.birthday.month == b_month:
                    script = Script(random.choice(SCRIPT_DUPLICATE))
                    script.translate()
                    script.mention(minfo['screen_name'])
                    self.do_tweet(script, minfo['id'])
                else:
                    script = Script(random.choice(SCRIPT_MODIFYOK) %
                        (user.birthday.month, user.birthday.day, b_month, b_day, minfo['name']))
                    script.mention(minfo['screen_name'])
                    self.do_tweet(script, minfo['id'])

            else:
                script = Script(random.choice(SCRIPT_INPUTOK) % (b_month, b_day))
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
            user.birthday = birthday_in

        session.add(user)
        session.commit()
        return
Exemplo n.º 42
0
  def __init__(self):
    builder = gtk.Builder()
    builder.add_from_file('gui.glade')
    builder.connect_signals(self)
    
    self.window = builder.get_object('window')
    self.toolbutton_run = builder.get_object('toolbutton_run')
    self.toolbutton_abort = builder.get_object('toolbutton_abort')
    self.main_notebook = builder.get_object('main_notebook')
    self.left_notebook = builder.get_object('left_notebook')
    self.templates_menu_item = builder.get_object('imagemenuitem_new_template')
    
    submenu = self.create_templates_menu()
    self.templates_menu_item.set_submenu(submenu)

    
    self.textbuffer_script = gtksourceview2.Buffer()
    lm = gtksourceview2.LanguageManager()
    lang = lm.get_language('python')
    self.textbuffer_script.set_highlight_syntax(True)
    self.textbuffer_script.set_language(lang)    
    #syntax_loader = SyntaxLoader('python')
    #self.textbuffer_script = CodeBuffer(None, syntax_loader)
    self.textbuffer_script.connect('delete-range', 
        self.on_textbuffer_script_delete_range)
    self.textbuffer_script.connect('insert-text', 
        self.on_textbuffer_script_insert_text)
    self.source_view = gtksourceview2.View(self.textbuffer_script)
    self.source_view.set_auto_indent(True)
    self.source_view.set_indent_on_tab(True)
    self.source_view.set_indent_width(2)
    self.source_view.set_insert_spaces_instead_of_tabs(True)
    self.source_view.set_show_line_numbers(True)
    self.source_view.set_smart_home_end(True)
    self.source_view.set_show_line_marks(True)
    scheme = gtksourceview2.style_scheme_manager_get_default().get_scheme(
        'tango')
    self.textbuffer_script.set_style_scheme(scheme)
    self.source_view.set_show_line_marks(True)
    
    
    font_desc = pango.FontDescription('monospace 10')
    if font_desc:
        self.source_view.modify_font(font_desc)
    #self.source_view = gtk.TextView(self.textbuffer_script)
    self.source_view.set_size_request(500, -1)
    
    
    scrolled_win = gtk.ScrolledWindow()
    scrolled_win.show()
    scrolled_win.add(self.source_view)
    self.source_view.show()
    self.left_notebook.append_page(scrolled_win, gtk.Label('Code'))
    
    scrolled_win_control_box = gtk.ScrolledWindow()
    scrolled_win_control_box.show()
    self.left_notebook.append_page(scrolled_win_control_box, gtk.Label('Controls'))
    self.left_notebook.show_all()
    
    self.control_box_manager = ControlBoxManager(scrolled_win_control_box)
    self.control_box_manager.add_widget('Welcome! Use File->New from template to start a new project.', gtk.Label())
    
    self.left_notebook.set_current_page(1)
  
    self.script = Script()
    self.script.text_inserted += self.on_script_text_inserted
    self.script.range_deleted += self.on_script_range_deleted


    # configuer logging for the application:
    logging.getLogger('').setLevel(logging.DEBUG)
    self.textview_log = builder.get_object('textview_log')
    self.gui_handler = TextViewHandler(self.textview_log)
    stream_handler = logging.StreamHandler()
    formatter = logging.Formatter(
        "%(asctime)s - %(levelname)s - %(message)s")
    self.gui_handler.setFormatter(formatter)
    stream_handler.setFormatter(formatter)
    # Not adding by default (see view->show logs)
    #logging.getLogger('').addHandler(gui_handler)
    logging.getLogger('').addHandler(stream_handler)
    
    
    self.file_manager = FileManager(self.script)
    self.file_manager.current_file_changed += self.on_current_file_changed
    self.file_manager.need_save_changed += self.on_need_save_changed
    
    # TODO(daniv): These are ugly hacks to pervent raising events when the
    # is not the user.
    self.ignore_next_on_textbuffer_script_delete_range = False
    self.ignore_next_on_textbuffer_script_insert_text = False
    
    self.space_manager = SpaceManager(self.main_notebook)
    self.changer_manager = ParameterChangerManager(self.script)
    self.script_server = ScriptServer(self.changer_manager)
    self.script_server.script_started += self.on_script_started
    self.script_server.script_ended += self.on_script_ended
    
    
    
    
    global services
    services.init(self.space_manager, self.changer_manager, self.script_server, self.window.get_toplevel(), self.control_box_manager, self.textview_log)
    
    
    
    self._updateWindowTitle()
Exemplo n.º 43
0
                        help="a device id for microphone",
                        default=None)

    #automatic gain normalisation
    parser.add_argument("-g_min",
                        "--gain_min",
                        dest='g_min',
                        type=float,
                        help="the min value of automatic gain normalisation")
    parser.add_argument("-g_max",
                        "--gain_max",
                        dest='g_max',
                        type=float,
                        help="the max value of automatic gain normalisation")

    script = Script()

    args = parser.parse_args()
    args.stop = False

    args.callback = emotion_update

    #live_ser = LiveSer()
    #live_ser.run(args)

    ms_speech = MSSpeech()

    t2i_client = udp_client.SimpleUDPClient("192.168.1.13", 3838)

    # google_speech = GoogleSpeech()
Exemplo n.º 44
0
 def __init__(self, cantidad, script_pubkey):
     self.cantidad = cantidad
     self.script_pubkey = Script.parsear(script_pubkey)
Exemplo n.º 45
0
 def tx_in_parse(cls, s):
     prev_tx = s.read(32)[::-1]
     prev_index = little_endian_to_int(s.read(4))
     script_sig = Script.parse(s)
     sequence = little_endian_to_int(s.read(4))
     return cls(prev_tx, prev_index, script_sig, sequence)
Exemplo n.º 46
0
 def test_exercise_3(self):
     hex_script_pubkey = '1976a914338c84849423992471bffb1a54a8d9b1d69dc28a88ac'
     stream = BytesIO(bytes.fromhex(hex_script_pubkey))
     script_pubkey = Script.parse(stream)
     self.assertEqual(script_pubkey.address(), '15hZo812Lx266Dot6T52krxpnhrNiaqHya')
Exemplo n.º 47
0
    def mention(self, input_mentions=[]):
        assert(isinstance(input_mentions, list))

        log.debug('process_mention called')

        if self.config['auto_mention'] != 'true':
            log.info('nixiko auto mention is off')
            return

        scripts = []
        if not input_mentions:
            since_id = self.config['last_mention_id']
            received_mentions = self.api.mentions_timeline(since_id=since_id, count=5)
            # since_id 옵션으로 멘션들을 불러오므로, 처리한 마지막 트윗의 아이디를 기록
            if received_mentions:
                # tweepy에서 self.api.mentions를 호출할 경우 시간 역순으로 나오기 때문에, 시간 순으로 처리하기 위해선
                # 한번 뒤집어주는 것이 필요함.
                received_mentions.reverse()
        else:
            received_mentions = input_mentions

        log.debug(u'received_mention #: {}'.format(len(received_mentions)))
        for mention in received_mentions: # for each mentions
            minfo = self.fetch_mention_info(mention)

            log.info(u'받은 멘션: {} (from {})'.format(minfo['text'], minfo['screen_name']))
            # 셀프 멘션에 대한 재멘션을 방지. 셀프멘션은 무시한다.
            if minfo['screen_name'] == 'nixieko': continue

            minfo['text'] = minfo['text'].replace(self.config['bot_name'], '').strip()

            # follower 추가를 먼저 한다.
            self.process_follower(minfo)

            # screen_name 제거
            minfo['text'] = self.remove_screen_name(minfo['text'])

            # 특수기능 처리
            if self.process_special_mention(minfo): break

            # 학습 처리
            if ':' in minfo['text'] and len(minfo['text'].split(':'))>=2 and 'RT' not in minfo['text'] and 'http' not in minfo['text']:
                log.debug(u'학습멘션: {}'.format(minfo['text']))
                split_words = minfo['text'].split(':')
                new_category = split_words[0].strip()
                new_word = ' '.join(split_words[1:]).strip() # escape!

                if ',' in new_word: # multi learn
                    new_words = map(lambda x: x.strip(), new_word.split(','))
                    n_known_word = 0
                    for new_word in new_words:
                        existing_word = self.get_words(contents=new_word).first()
                        if existing_word:
                            n_known_word += 1
                            log.info(u'중복 단어: {} {} by {}'.format(new_category, new_word, minfo['screen_name']))
                        else: # 학습성공
                            log.info(u'학습 성공: {} {} by {}'.format(new_category, new_word, minfo['screen_name']))
                            self.process_learn(new_category, new_word, minfo['screen_name'])

                    if n_known_word == 0:
                        subtable = session.query(ConditionScript).filter_by(keyword='multilearn')
                        condition_script = NixUtil.get_random_from_subtable(subtable)
                        script = Script(condition_script.contents, condition_script.image_keyword)
                        script.replace(u'%{대표단어}', new_words[0])
                        script.replace(u'%{배운수}', str(len(new_words)))
                    elif n_known_word == len(new_words):
                        subtable = session.query(ConditionScript).filter_by(keyword='multilearn_allknown')
                        condition_script = NixUtil.get_random_from_subtable(subtable)
                        script = Script(condition_script.contents, condition_script.image_keyword)
                    else:
                        subtable = session.query(ConditionScript).filter_by(keyword='multilearn_partlyknown')
                        condition_script = NixUtil.get_random_from_subtable(subtable)
                        script = Script(condition_script.contents, condition_script.image_keyword)
                        script.replace(u'%{대표단어}', new_words[0])
                        script.replace(u'%{배운수}', str(len(new_words) - n_known_word))
                        script.replace(u'%{중복수}', str(n_known_word))

                    script.translate()
                    script.mention(minfo['screen_name'])
                    self.do_tweet(script, minfo['id'])
                    scripts += [script]
                    continue
                        

                log.debug('existing word')
                existing_word = self.get_words(contents=new_word).first()
                if existing_word:
                    log.info(u'이미 알고 있는 단어: {} in {}'.format(new_word, existing_word.keyword))

                    subtable = session.query(ConditionScript).filter_by(keyword='duplicate')
                    condition_script = NixUtil.get_random_from_subtable(subtable)
                    script = Script(condition_script.contents, condition_script.image_keyword)

                    script.replace_numbered_literals([minfo['name'], new_word, new_category])
                    script.translate()
                    script.mention(minfo['screen_name'])
                    self.do_tweet(script, minfo['id'])
                    scripts += [script]
                    break
                else: # 학습 성공
                    log.info(u'학습 성공: {} {} by {}'.format(new_category, new_word, minfo['screen_name']))
                    subtable = session.query(ConditionScript).filter_by(keyword='learn')
                    condition_script = NixUtil.get_random_from_subtable(subtable)
                    script = Script(condition_script.contents, condition_script.image_keyword)
                    script.replace_numbered_literals([minfo['name'], new_word, new_category]);
                    script.translate()
                    scripts += [script.mention(minfo['screen_name'])]
                    self.do_tweet(script, minfo['id'])
                    self.process_learn(new_category, new_word, minfo['screen_name'])
                continue

            # 키워드 찾기
            log.debug('finding keyword...')
            script_candidate = []
            numbered_literals = []
            keywords_found = []
            max_match = 0
            keyword_literal = session.query(ResponseScript).filter_by(is_blind=0)
            for response in filter(lambda x:not x.keyword.startswith('%'), keyword_literal):
                keywords = response.keyword
                for keyword in keywords.split(','):
                    partial_keywords = map(unicode.strip, keyword.split('&&'))
                    for partial_keyword in partial_keywords:
                        if minfo['text'].find(partial_keyword.strip()) < 0: # if not found
                            break
                    else: # all match!
                        keywords_found.append(partial_keyword)
                        if max_match > len(partial_keywords):
                            continue
                        elif max_match < len(partial_keywords):
                            script_candidate = []
                            max_match = len(partial_keywords)
                        try:
                            script = Script(response.contents, response.image_keyword)
                        except:
                            log.exception(u'category error: '.format(contents))
                        script.replace(u'%{상대}', minfo['name'])
                        script.replace(u'%{키워드}', keyword.strip())
                        script_candidate.append(script)
            if script_candidate: # if there was a match(es)
                for keyword in keywords_found:
                    log.debug(u'{} keyword found: {}'.format(len(keywords_found), keyword))
                script = random.choice(script_candidate)
                script.translate()
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
                scripts += [script]
                continue
            log.debug('keyword not found')

            # 카테고리 찾기
            log.debug('finding category...')
            script_candidate = []
            keywords_found = []
            for keyword in self.get_words(is_blind=0):
                if keyword.contents in minfo['text']:
                    keywords_found.append(keyword.contents)
                    # category found
                    script = ''
                    category = keyword.keyword
                    try:
                        subtable = session.query(ResponseScript)\
                                    .filter_by(is_blind=0)\
                                    .filter(ResponseScript.keyword.like('\%('+ category +')'))
                        response_script = NixUtil.get_random_from_subtable(subtable)
                        script = Script(response_script.contents, response_script.image_keyword)
                    except:
                        log.exception(u'Script category error: '.format(category))
                        continue
                    try:
                        if not script: continue
                        script.replace(u'%{상대}', minfo['name'])
                        script.replace(u'%{키워드}', keyword.contents)
                        script_candidate += [script]
                    except:
                        log.exception(u'Error: category: {}, opponent: {}, keyword: {}, script: {}' \
                                .format(category, minfo['name'], keyword.contents.strip(), script))
                        raise

            if script_candidate:
                for keyword in keywords_found:
                    log.debug(u'{} keyword found: {}'.format(len(keywords_found), keyword))
                script = random.choice(script_candidate)
                script.translate()
                script.mention(minfo['screen_name'])
                self.do_tweet(script, minfo['id'])
                scripts += [script.mention(minfo['screen_name'])]
                continue

            # 해당사항 없음
            log.debug('받은 멘션이 어떤 카테고리에도 들어가지 않습니다. 랜덤한 스크립트를 선택합니다.')
            subtable = session.query(ConditionScript).filter_by(is_blind=0).filter_by(keyword='unknown')
            condition_script = NixUtil.get_random_from_subtable(subtable)
            script = Script(condition_script.contents, condition_script.image_keyword)
            script.replace(u'%{상대}', minfo['name'])
            script.translate()
            script.mention(minfo['screen_name'])
            self.do_tweet(script, minfo['id'])
            scripts += [script]

        if not input_mentions: # only if processed w/ timeline
            if received_mentions:
                try:
                    log.debug(u'now update received mention id: {}'.format(str(received_mentions[-1].id)))
                    self.config['last_mention_id'] = str(received_mentions[-1].id)
                    #if not DEBUG:
                    NixConf.update_config(self.config)
                except:
                    log.exception('id fetching failed')

        return scripts