<commit_before>__author__ = 'rahul'
import data
import ta
import datetime

def main():

    current_time = datetime.datetime.now().time()
    print 'Start time:' + str(current_time)

    # Refresh Watchlist
    watch_list = data.WatchlistDb()
    updated_dict = watch_list.get_watchlists_csv('../symbols/watchlistdb.csv')

    conn = watch_list.open_watchlistdb('data/watchlistdb.db')
    watch_list.insert_multiple_watchlists(conn, updated_dict)

    # Return watchlist
    ibd50 = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'IBD50')
    biotech = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'Biotech')
    ETFOptions = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'ETFOptions')

    # Technology Stocks
    yahoo_stocks = data.SymbolDb()
    tech = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Volume='1000000', Sector='Technology')
    biotech1 = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Volume='1000000', Industry='Bio')
    marketcap = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Mcap=[500,5000])

    wlist = list(set(ibd50 + biotech + ETFOptions + tech + biotech1 + marketcap))
    #wlist = list(set(ibd50 + biotech + ETFOptions + tech))

    # Get data for watchlist

    current_time = datetime.datetime.now().time()
    print 'Data download start time:' + str(current_time)

    dat = data.MarketData()
    test_data = dat.get_yahoo_data(wlist, '02/01/2014', '07/24/2015')
    test_data['Close'] = test_data['Close'].fillna(method='ffill')
    test_data['Open'] = test_data['Open'].fillna(method='ffill')
    test_data['High'] = test_data['High'].fillna(method='ffill')
    test_data['Low'] = test_data['Low'].fillna(method='ffill')

    #test_data['Close'].ffill()
    #df_test = test_data['Close'].drop(test_data['Close'].index[79])
    #test_data['Close'].to_csv('test_data.csv')
    #df_test.to_csv('df_test.csv')

    current_time = datetime.datetime.now().time()
    print 'Data download end time:' + str(current_time)

    ind = ta.Indicators()

    # Bollinger Bands
    df_bb_u, df_bb_m, df_bb_l = ind.bb(wlist, test_data['Close'], 20, 2, 2)

    # Fibonacci EMA's and Waves
    df_ema8 = ind.ema(wlist, test_data['Close'], 8)
    df_ema21 = ind.ema(wlist, test_data['Close'], 21)
    df_ema34 = ind.ema(wlist, test_data['Close'], 34)
    df_ema55 = ind.ema(wlist, test_data['Close'], 55)
    df_ema89 = ind.ema(wlist, test_data['Close'], 89)

    df_waveaa, df_waveab = ind.wavea(wlist, df_ema8, df_ema21, df_ema34)
    df_waveba, df_wavebb = ind.wavea(wlist, df_ema21, df_ema34, df_ema55)
    df_waveca, df_wavecb = ind.wavea(wlist, df_ema34, df_ema55, df_ema89)

    df_ema40 = ind.ema(wlist, test_data['Close'], 40)
    df_ema105 = ind.ema(wlist, test_data['Close'], 105)
    df_ema170 = ind.ema(wlist, test_data['Close'], 170)
    df_ema275 = ind.ema(wlist, test_data['Close'], 275)
    df_ema445 = ind.ema(wlist, test_data['Close'], 445)

    # Keltner Channels
    df_kelt_u, df_kelt_m, df_kelt_l = ind.keltner(wlist, test_data['High'], test_data['Low'], test_data['Close'], 20, 20, 2)

    # TTM Squeeze Test
    df_bb_ma, df_bb_u, df_bb_l, df_kch_m, df_kch_u, df_kch_l = ind.ttm_squeeze(wlist, test_data['High'], test_data['Low'], test_data['Close'], 21, 2, 21, 21, 1.5)

    # Ichimoku Test
    df_ichi_tenkan, df_ichi_kijun = ind.ichimoku(wlist, test_data['High'], test_data['Low'])

    # Momentum
    df_mom = ind.mom(wlist, test_data['Close'], 12)

    # ADX
    df_adx = ind.adx(wlist, test_data['High'], test_data['Low'], test_data['Close'], 14)

    #MACD
    df_macd, df_macdsig, df_macdhist = ind.macd(wlist, test_data['Close'], 12, 26, 9)

    #Stochastic RSI

    df_fastk, df_fastd = ind.stochastic_rsi(wlist, test_data['Close'], 5, 3)
    df_fastd.to_csv('df_fastd.csv')

    # Open Text File
    file_tmstmp = str(datetime.datetime.now().month) + str(datetime.datetime.now().day) + str(datetime.datetime.now().year)
    file_name = 'options_scan_results_' + file_tmstmp + '.txt'
    f = open(file_name, 'w+')

    # Event Scanner
    event = ta.Events()
    cross_sym = event.crossabove_scan(df_ema8, df_ema21)
    f.write('EMA8 X EMA21:'+ str(cross_sym)+'\n')
    f.write('----------------------------------------------\n')

    # Ichimoku Cross
    cross_ichimoku = event.crossabove_scan(df_ichi_tenkan, df_ichi_kijun)
    f.write('Tenkan X Kijun(ETF):' + str(cross_ichimoku) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses over Kijun Sen
    cross_kijun = event.crossabove_scan(test_data['Close'], df_ichi_kijun)
    f.write('Price X Kijun:(ETF)' + str(cross_kijun) + '\n')
    f.write('----------------------------------------------\n')

    # TTM squeeze firing
    ttm_cross_u = event.crossabove_scan(df_bb_u, df_kch_u)
    f.write('TTM Upper Cross: ' + str(ttm_cross_u) + '\n')
    f.write('----------------------------------------------\n')

    # MACD Histogram Rising
    macd_rising = event.rising_scan(df_macdhist)
    f.write('MACD Histogram Rising:' + str(macd_rising) + '\n')
    f.write('----------------------------------------------\n')

    # Explosive EMA power
    # EMA 21 is below EMA 89
    # Price crosses above EMA 8

    ema21b89 = event.is_below_scan(df_ema21, df_ema89, ETFOptions)
    pricex8 = event.crossabove_scan(test_data['Close'], df_ema8, ema21b89)

    f.write('Explosive Power: ' + str(pricex8) + '\n')
    f.write('----------------------------------------------\n')

    # Turning up scan
    macd_turn_up = event.turningup_scan(df_macdhist)
    f.write('MACD Histogram Turn Up:' + str(macd_turn_up) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses EMA 8
    price_ema8 = event.crossabove_scan(test_data['Close'], df_ema8)
    f.write('Price crosses EMA 8:' + str(price_ema8) + '\n')

    # Price crosses EMA 21
    price_ema21 = event.crossabove_scan(test_data['Close'], df_ema21)
    f.write('Price crosses EMA 21:' + str(price_ema21) + '\n')

    # Price crosses EMA 34
    price_ema34 = event.crossabove_scan(test_data['Close'], df_ema34)
    f.write('Price crosses EMA 34:' + str(price_ema34) + '\n')

    # Price crosses EMA 55
    price_ema55 = event.crossabove_scan(test_data['Close'], df_ema55)
    f.write('Price crosses EMA 55:' + str(price_ema55) + '\n')

    # Price crosses EMA 89
    price_ema89 = event.crossabove_scan(test_data['Close'], df_ema89)
    f.write('Price crosses EMA 89:' + str(price_ema89) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses EMA 40
    price_ema40 = event.crossabove_scan(test_data['Close'], df_ema40)
    f.write('Price crosses EMA 40:' + str(price_ema40) + '\n')

    # Price crosses EMA 105
    price_ema105 = event.crossabove_scan(test_data['Close'], df_ema105)
    f.write('Price crosses EMA 105:' + str(price_ema105) + '\n')

    # Price crosses EMA 170
    price_ema170 = event.crossabove_scan(test_data['Close'], df_ema170)
    f.write('Price crosses EMA 170:' + str(price_ema170) + '\n')

    # Price crosses EMA 275
    price_ema275 = event.crossabove_scan(test_data['Close'], df_ema275)
    f.write('Price crosses EMA 275:' + str(price_ema275) + '\n')

    # Price crosses EMA 445
    price_ema445 = event.crossabove_scan(test_data['Close'], df_ema445)
    f.write('Price crosses EMA 445:' + str(price_ema445) + '\n')
    f.write('----------------------------------------------\n')

    # Wave A Rising
    waveaa_rising = event.rising_scan(df_waveaa)
    f.write(('Wave Aa Rising:') + str(waveaa_rising) + '\n')

    waveab_rising = event.rising_scan(df_waveab)
    f.write(('Wave Ab Rising:') + str(waveab_rising) + '\n')

    # Wave A Turning Up
    waveaa_turnup = event.turningup_scan(df_waveaa)
    f.write(('Wave Aa Turn Up:') + str(waveaa_turnup) + '\n')

    waveab_turnup = event.turningup_scan(df_waveab)
    f.write(('Wave Ab Turn Up:') + str(waveab_turnup) + '\n')
    f.write('----------------------------------------------\n')

    # Oversold Stochastic
    stoch_sold = event.is_below_N_scan(df_fastk, 20)
    f.write(('Stochastic below 20:') + str(stoch_sold) + '\n')
    f.write('----------------------------------------------\n')

    f.close()

    '''
    Blueprint Scanning
    '''
    current_time = datetime.datetime.now().time()
    print 'Blueprint processing start time:' + str(current_time)

    sma8 = ind.sma(marketcap, test_data['Close'], 8)
    sma50 = ind.sma(marketcap, test_data['Close'], 50)

    sma_cross = event.crossabove_scan(sma8, sma50)

    bp_file_name = 'blueprint_scan_results_' + file_tmstmp + '.txt'
    f = open(bp_file_name, 'w+')

    f.write('SMA 8 X SMA 50:\n')
    f.write(str(sma_cross))

    f.close()

    current_time = datetime.datetime.now().time()
    print 'End time:' + str(current_time)

if __name__ == '__main__':
    main()
<commit_msg>Separated options and blueprint data<commit_after>__author__ = 'rahul'
import data
import ta
import datetime

def main():

    current_time = datetime.datetime.now().time()
    print 'Start time:' + str(current_time)

    # Refresh Watchlist
    watch_list = data.WatchlistDb()
    updated_dict = watch_list.get_watchlists_csv('../symbols/watchlistdb.csv')

    conn = watch_list.open_watchlistdb('data/watchlistdb.db')
    watch_list.insert_multiple_watchlists(conn, updated_dict)

    # Return watchlist
    ibd50 = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'IBD50')
    biotech = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'Biotech')
    ETFOptions = watch_list.get_watchlist_by_name('data/watchlistdb.db', 'ETFOptions')

    # Technology Stocks
    yahoo_stocks = data.SymbolDb()
    tech = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Volume='1000000', Sector='Technology')
    biotech1 = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Volume='1000000', Industry='Bio')
    marketcap = yahoo_stocks.get_symbols(source='Yahoo', Country='USA', Mcap=[500,5000])

    options = list(set(ibd50 + biotech + ETFOptions + tech + biotech1))

    blueprint = marketcap

    # Get data for watchlist

    current_time = datetime.datetime.now().time()
    print 'Data download start time:' + str(current_time)

    dat = data.MarketData()
    test_data = dat.get_yahoo_data(options, '02/01/2014', '07/24/2015')
    blueprint_data = dat.get_yahoo_data(blueprint, '02/01/2014', '07/24/2015')
    test_data['Close'] = test_data['Close'].fillna(method='ffill')
    test_data['Open'] = test_data['Open'].fillna(method='ffill')
    test_data['High'] = test_data['High'].fillna(method='ffill')
    test_data['Low'] = test_data['Low'].fillna(method='ffill')

    #test_data['Close'].ffill()
    #df_test = test_data['Close'].drop(test_data['Close'].index[79])
    #test_data['Close'].to_csv('test_data.csv')
    #df_test.to_csv('df_test.csv')

    current_time = datetime.datetime.now().time()
    print 'Data download end time:' + str(current_time)

    ind = ta.Indicators()

    # Bollinger Bands
    df_bb_u, df_bb_m, df_bb_l = ind.bb(options, test_data['Close'], 20, 2, 2)

    # Fibonacci EMA's and Waves
    df_ema8 = ind.ema(options, test_data['Close'], 8)
    df_ema21 = ind.ema(options, test_data['Close'], 21)
    df_ema34 = ind.ema(options, test_data['Close'], 34)
    df_ema55 = ind.ema(options, test_data['Close'], 55)
    df_ema89 = ind.ema(options, test_data['Close'], 89)

    df_waveaa, df_waveab = ind.wavea(options, df_ema8, df_ema21, df_ema34)
    df_waveba, df_wavebb = ind.wavea(options, df_ema21, df_ema34, df_ema55)
    df_waveca, df_wavecb = ind.wavea(options, df_ema34, df_ema55, df_ema89)

    df_ema40 = ind.ema(options, test_data['Close'], 40)
    df_ema105 = ind.ema(options, test_data['Close'], 105)
    df_ema170 = ind.ema(options, test_data['Close'], 170)
    df_ema275 = ind.ema(options, test_data['Close'], 275)
    df_ema445 = ind.ema(options, test_data['Close'], 445)

    # Keltner Channels
    df_kelt_u, df_kelt_m, df_kelt_l = ind.keltner(options, test_data['High'], test_data['Low'], test_data['Close'], 20, 20, 2)

    # TTM Squeeze Test
    df_bb_ma, df_bb_u, df_bb_l, df_kch_m, df_kch_u, df_kch_l = ind.ttm_squeeze(options, test_data['High'], test_data['Low'], test_data['Close'], 21, 2, 21, 21, 1.5)

    # Ichimoku Test
    df_ichi_tenkan, df_ichi_kijun = ind.ichimoku(options, test_data['High'], test_data['Low'])

    # Momentum
    df_mom = ind.mom(options, test_data['Close'], 12)

    # ADX
    df_adx = ind.adx(options, test_data['High'], test_data['Low'], test_data['Close'], 14)

    #MACD
    df_macd, df_macdsig, df_macdhist = ind.macd(options, test_data['Close'], 12, 26, 9)

    #Stochastic RSI

    df_fastk, df_fastd = ind.stochastic_rsi(options, test_data['Close'], 5, 3)
    df_fastd.to_csv('df_fastd.csv')

    # Open Text File
    file_tmstmp = str(datetime.datetime.now().month) + str(datetime.datetime.now().day) + str(datetime.datetime.now().year)
    file_name = 'options_scan_results_' + file_tmstmp + '.txt'
    f = open(file_name, 'w+')

    # Event Scanner
    event = ta.Events()
    cross_sym = event.crossabove_scan(df_ema8, df_ema21)
    f.write('EMA8 X EMA21:'+ str(cross_sym)+'\n')
    f.write('----------------------------------------------\n')

    # Ichimoku Cross
    cross_ichimoku = event.crossabove_scan(df_ichi_tenkan, df_ichi_kijun)
    f.write('Tenkan X Kijun(ETF):' + str(cross_ichimoku) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses over Kijun Sen
    cross_kijun = event.crossabove_scan(test_data['Close'], df_ichi_kijun)
    f.write('Price X Kijun:(ETF)' + str(cross_kijun) + '\n')
    f.write('----------------------------------------------\n')

    # TTM squeeze firing
    ttm_cross_u = event.crossabove_scan(df_bb_u, df_kch_u)
    f.write('TTM Upper Cross: ' + str(ttm_cross_u) + '\n')
    f.write('----------------------------------------------\n')

    # MACD Histogram Rising
    macd_rising = event.rising_scan(df_macdhist)
    f.write('MACD Histogram Rising:' + str(macd_rising) + '\n')
    f.write('----------------------------------------------\n')

    # Explosive EMA power
    # EMA 21 is below EMA 89
    # Price crosses above EMA 8

    ema21b89 = event.is_below_scan(df_ema21, df_ema89, ETFOptions)
    pricex8 = event.crossabove_scan(test_data['Close'], df_ema8, ema21b89)

    f.write('Explosive Power: ' + str(pricex8) + '\n')
    f.write('----------------------------------------------\n')

    # Turning up scan
    macd_turn_up = event.turningup_scan(df_macdhist)
    f.write('MACD Histogram Turn Up:' + str(macd_turn_up) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses EMA 8
    price_ema8 = event.crossabove_scan(test_data['Close'], df_ema8)
    f.write('Price crosses EMA 8:' + str(price_ema8) + '\n')

    # Price crosses EMA 21
    price_ema21 = event.crossabove_scan(test_data['Close'], df_ema21)
    f.write('Price crosses EMA 21:' + str(price_ema21) + '\n')

    # Price crosses EMA 34
    price_ema34 = event.crossabove_scan(test_data['Close'], df_ema34)
    f.write('Price crosses EMA 34:' + str(price_ema34) + '\n')

    # Price crosses EMA 55
    price_ema55 = event.crossabove_scan(test_data['Close'], df_ema55)
    f.write('Price crosses EMA 55:' + str(price_ema55) + '\n')

    # Price crosses EMA 89
    price_ema89 = event.crossabove_scan(test_data['Close'], df_ema89)
    f.write('Price crosses EMA 89:' + str(price_ema89) + '\n')
    f.write('----------------------------------------------\n')

    # Price crosses EMA 40
    price_ema40 = event.crossabove_scan(test_data['Close'], df_ema40)
    f.write('Price crosses EMA 40:' + str(price_ema40) + '\n')

    # Price crosses EMA 105
    price_ema105 = event.crossabove_scan(test_data['Close'], df_ema105)
    f.write('Price crosses EMA 105:' + str(price_ema105) + '\n')

    # Price crosses EMA 170
    price_ema170 = event.crossabove_scan(test_data['Close'], df_ema170)
    f.write('Price crosses EMA 170:' + str(price_ema170) + '\n')

    # Price crosses EMA 275
    price_ema275 = event.crossabove_scan(test_data['Close'], df_ema275)
    f.write('Price crosses EMA 275:' + str(price_ema275) + '\n')

    # Price crosses EMA 445
    price_ema445 = event.crossabove_scan(test_data['Close'], df_ema445)
    f.write('Price crosses EMA 445:' + str(price_ema445) + '\n')
    f.write('----------------------------------------------\n')

    # Wave A Rising
    waveaa_rising = event.rising_scan(df_waveaa)
    f.write(('Wave Aa Rising:') + str(waveaa_rising) + '\n')

    waveab_rising = event.rising_scan(df_waveab)
    f.write(('Wave Ab Rising:') + str(waveab_rising) + '\n')

    # Wave A Turning Up
    waveaa_turnup = event.turningup_scan(df_waveaa)
    f.write(('Wave Aa Turn Up:') + str(waveaa_turnup) + '\n')

    waveab_turnup = event.turningup_scan(df_waveab)
    f.write(('Wave Ab Turn Up:') + str(waveab_turnup) + '\n')
    f.write('----------------------------------------------\n')

    # Oversold Stochastic
    stoch_sold = event.is_below_N_scan(df_fastk, 20)
    f.write(('Stochastic below 20:') + str(stoch_sold) + '\n')
    f.write('----------------------------------------------\n')

    f.close()

    '''
    Blueprint Scanning
    '''
    current_time = datetime.datetime.now().time()
    print 'Blueprint processing start time:' + str(current_time)

    sma8 = ind.sma(blueprint, blueprint_data['Close'], 8)
    sma50 = ind.sma(blueprint, blueprint_data['Close'], 50)

    sma_cross = event.crossabove_scan(sma8, sma50)

    bp_file_name = 'blueprint_scan_results_' + file_tmstmp + '.txt'
    f = open(bp_file_name, 'w+')

    f.write('SMA 8 X SMA 50:\n')
    f.write(str(sma_cross))

    f.close()

    current_time = datetime.datetime.now().time()
    print 'End time:' + str(current_time)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before># Copyright 2013 IBM Corp.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from tempest.common.utils import data_utils
from tempest import config
from tempest.openstack.common import log as logging
from tempest.scenario import manager
from tempest import test

CONF = config.CONF

LOG = logging.getLogger(__name__)

class TestSwiftBasicOps(manager.ScenarioTest):
    """
    Test swift with the follow operations:
     * get swift stat.
     * create container.
     * upload a file to the created container.
     * list container's objects and assure that the uploaded file is present.
     * delete object from container.
     * list container's objects and assure that the deleted file is gone.
     * delete a container.
     * list containers and assure that the deleted container is gone.
    """

    @classmethod
    def setUpClass(cls):
        cls.set_network_resources()
        super(TestSwiftBasicOps, cls).setUpClass()
        if not CONF.service_available.swift:
            skip_msg = ("%s skipped as swift is not available" %
                        cls.__name__)
            raise cls.skipException(skip_msg)
        # Clients for Swift
        cls.account_client = cls.manager.account_client
        cls.container_client = cls.manager.container_client
        cls.object_client = cls.manager.object_client

    def _get_swift_stat(self):
        """get swift status for our user account."""
        self.account_client.list_account_containers()
        LOG.debug('Swift status information obtained successfully')

    def _create_container(self, container_name=None):
        name = container_name or data_utils.rand_name(
            'swift-scenario-container')
        self.container_client.create_container(name)
        # look for the container to assure it is created
        self._list_and_check_container_objects(name)
        LOG.debug('Container %s created' % (name))
        return name

    def _delete_container(self, container_name):
        self.container_client.delete_container(container_name)
        LOG.debug('Container %s deleted' % (container_name))

    def _upload_object_to_container(self, container_name, obj_name=None):
        obj_name = obj_name or data_utils.rand_name('swift-scenario-object')
        self.object_client.create_object(container_name, obj_name,
                                         data_utils.arbitrary_string())
        return obj_name

    def _delete_object(self, container_name, filename):
        self.object_client.delete_object(container_name, filename)
        self._list_and_check_container_objects(container_name,
                                               not_present_obj=[filename])

    def _list_and_check_container_objects(self, container_name, present_obj=[],
                                          not_present_obj=[]):
        """
        List objects for a given container and assert which are present and
        which are not.
        """
        _, object_list = self.container_client.list_container_contents(
            container_name)
        if present_obj:
            for obj in present_obj:
                self.assertIn(obj, object_list)
        if not_present_obj:
            for obj in not_present_obj:
                self.assertNotIn(obj, object_list)

    @test.services('object_storage')
    def test_swift_basic_ops(self):
        self._get_swift_stat()
        container_name = self._create_container()
        obj_name = self._upload_object_to_container(container_name)
        self._list_and_check_container_objects(container_name, [obj_name])
        self._delete_object(container_name, obj_name)
        self._delete_container(container_name)
<commit_msg>Add cases for Swift scenario test<commit_after># Copyright 2013 IBM Corp.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from tempest.common import http
from tempest.common.utils import data_utils
from tempest import config
from tempest.openstack.common import log as logging
from tempest.scenario import manager
from tempest import test

CONF = config.CONF

LOG = logging.getLogger(__name__)

class TestSwiftBasicOps(manager.ScenarioTest):
    """
    Test swift with the follow operations:
     * get swift stat.
     * create container.
     * upload a file to the created container.
     * list container's objects and assure that the uploaded file is present.
     * download the object and check the content
     * delete object from container.
     * list container's objects and assure that the deleted file is gone.
     * delete a container.
     * list containers and assure that the deleted container is gone.
     * change ACL of the container and make sure it works successfully
    """

    @classmethod
    def setUpClass(cls):
        cls.set_network_resources()
        super(TestSwiftBasicOps, cls).setUpClass()
        if not CONF.service_available.swift:
            skip_msg = ("%s skipped as swift is not available" %
                        cls.__name__)
            raise cls.skipException(skip_msg)
        # Clients for Swift
        cls.account_client = cls.manager.account_client
        cls.container_client = cls.manager.container_client
        cls.object_client = cls.manager.object_client

    def _get_swift_stat(self):
        """get swift status for our user account."""
        self.account_client.list_account_containers()
        LOG.debug('Swift status information obtained successfully')

    def _create_container(self, container_name=None):
        name = container_name or data_utils.rand_name(
            'swift-scenario-container')
        self.container_client.create_container(name)
        # look for the container to assure it is created
        self._list_and_check_container_objects(name)
        LOG.debug('Container %s created' % (name))
        return name

    def _delete_container(self, container_name):
        self.container_client.delete_container(container_name)
        LOG.debug('Container %s deleted' % (container_name))

    def _upload_object_to_container(self, container_name, obj_name=None):
        obj_name = obj_name or data_utils.rand_name('swift-scenario-object')
        obj_data = data_utils.arbitrary_string()
        self.object_client.create_object(container_name, obj_name, obj_data)
        return obj_name, obj_data

    def _delete_object(self, container_name, filename):
        self.object_client.delete_object(container_name, filename)
        self._list_and_check_container_objects(container_name,
                                               not_present_obj=[filename])

    def _list_and_check_container_objects(self, container_name, present_obj=[],
                                          not_present_obj=[]):
        """
        List objects for a given container and assert which are present and
        which are not.
        """
        _, object_list = self.container_client.list_container_contents(
            container_name)
        if present_obj:
            for obj in present_obj:
                self.assertIn(obj, object_list)
        if not_present_obj:
            for obj in not_present_obj:
                self.assertNotIn(obj, object_list)

    def _change_container_acl(self, container_name, acl):
        metadata_param = {'metadata_prefix': 'x-container-',
                          'metadata': {'read': acl}}
        self.container_client.update_container_metadata(container_name,
                                                        **metadata_param)
        resp, _ = self.container_client.list_container_metadata(container_name)
        self.assertEqual(resp['x-container-read'], acl)

    def _download_and_verify(self, container_name, obj_name, expected_data):
        _, obj = self.object_client.get_object(container_name, obj_name)
        self.assertEqual(obj, expected_data)

    @test.services('object_storage')
    def test_swift_basic_ops(self):
        self._get_swift_stat()
        container_name = self._create_container()
        obj_name, obj_data = self._upload_object_to_container(container_name)
        self._list_and_check_container_objects(container_name, [obj_name])
        self._download_and_verify(container_name, obj_name, obj_data)
        self._delete_object(container_name, obj_name)
        self._delete_container(container_name)

    @test.services('object_storage')
    def test_swift_acl_anonymous_download(self):
        """This test will cover below steps:
        1. Create container
        2. Upload object to the new container
        3. Change the ACL of the container
        4. Check if the object can be download by anonymous user
        5. Delete the object and container
        """
        container_name = self._create_container()
        obj_name, _ = self._upload_object_to_container(container_name)
        obj_url = '%s/%s/%s' % (self.object_client.base_url,
                                container_name, obj_name)
        http_client = http.ClosingHttp()
        resp, _ = http_client.request(obj_url, 'GET')
        self.assertEqual(resp.status, 401)
        self._change_container_acl(container_name, '.r:*')
        resp, _ = http_client.request(obj_url, 'GET')
        self.assertEqual(resp.status, 200)
        self._delete_object(container_name, obj_name)
        self._delete_container(container_name)
<|endoftext|>


<commit_before># Orca
#
# Copyright 2005-2009 Sun Microsystems Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

"""Custom script for Evolution."""

__id__        = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc."
__license__   = "LGPL"

import pyatspi

import orca.speechgenerator as speechgenerator

class SpeechGenerator(speechgenerator.SpeechGenerator):
    """Overrides _getSpeechForTableCell so that, if this is an expanded
       table cell,  we can strip off the "0 items".
    """

    def __init__(self, script):
        speechgenerator.SpeechGenerator.__init__(self, script)

    def _getRealTableCell(self, obj, **args):
        # pylint: disable-msg=W0142
        # Check that we are in a table cell in the mail message header list.
        # If we are and this table cell has an expanded state, then
        # dont speak the number of items.
        # See bug #432308 for more details.
        #
        rolesList = [pyatspi.ROLE_TABLE_CELL, \
                     pyatspi.ROLE_TREE_TABLE, \
                     pyatspi.ROLE_UNKNOWN]
        if self._script.isDesiredFocusedItem(obj, rolesList):
            state = obj.getState()
            if state and state.contains(pyatspi.STATE_EXPANDABLE):
                if state.contains(pyatspi.STATE_EXPANDED):
                    oldRole = self._overrideRole(
                        'ALTERNATIVE_REAL_ROLE_TABLE_CELL', args)
                    result = self.getSpeech(obj, **args)
                    self._restoreRole(oldRole, args)
                    return result
        return speechgenerator.SpeechGenerator._getRealTableCell(
            self, obj, **args)
<commit_msg>Pylinting<commit_after># Orca
#
# Copyright 2005-2009 Sun Microsystems Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

"""Custom script for Evolution."""

__id__        = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc."
__license__   = "LGPL"

import pyatspi

import orca.speechgenerator as speechgenerator

class SpeechGenerator(speechgenerator.SpeechGenerator):
    """Overrides _getSpeechForTableCell so that, if this is an expanded
       table cell,  we can strip off the "0 items".
    """

    # pylint: disable-msg=W0142

    def __init__(self, script):
        speechgenerator.SpeechGenerator.__init__(self, script)

    def _getRealTableCell(self, obj, **args):
        # Check that we are in a table cell in the mail message header list.
        # If we are and this table cell has an expanded state, then
        # dont speak the number of items.
        # See bug #432308 for more details.
        #
        rolesList = [pyatspi.ROLE_TABLE_CELL, \
                     pyatspi.ROLE_TREE_TABLE, \
                     pyatspi.ROLE_UNKNOWN]
        if self._script.isDesiredFocusedItem(obj, rolesList):
            state = obj.getState()
            if state and state.contains(pyatspi.STATE_EXPANDABLE):
                if state.contains(pyatspi.STATE_EXPANDED):
                    oldRole = self._overrideRole(
                        'ALTERNATIVE_REAL_ROLE_TABLE_CELL', args)
                    result = self.getSpeech(obj, **args)
                    self._restoreRole(oldRole, args)
                    return result
        return speechgenerator.SpeechGenerator._getRealTableCell(
            self, obj, **args)
<|endoftext|>


<commit_before>#ipc_lista2.04
#Professor: Jucimar Junior
#Any Mnedes Carvalho - 1615310044
#
#
#
#
#Faça um programa que verifique se uma letra digitada é vogal ou consoante.

letra = raw_input("")
<commit_msg>Update ipc_lista2.04.py<commit_after>#ipc_lista2.04
#Professor: Jucimar Junior
#Any Mnedes Carvalho - 1615310044
#
#
#
#
#Faça um programa que verifique se uma letra digitada é vogal ou consoante.

letra = raw_input("  ")
<|endoftext|>


<commit_before>from __future__ import absolute_import

from typing import Any, Dict, Optional, List, Text
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _

from zerver.decorator import require_realm_admin, to_non_negative_int, to_not_negative_int_or_none
from zerver.lib.actions import (
    do_set_realm_message_editing,
    do_set_realm_authentication_methods,
    do_set_realm_property,
)
from zerver.lib.i18n import get_available_language_codes
from zerver.lib.request import has_request_variables, REQ, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.validator import check_string, check_dict, check_bool
from zerver.models import UserProfile

@require_realm_admin
@has_request_variables
def update_realm(request, user_profile, name=REQ(validator=check_string, default=None),
                 description=REQ(validator=check_string, default=None),
                 restricted_to_domain=REQ(validator=check_bool, default=None),
                 invite_required=REQ(validator=check_bool, default=None),
                 invite_by_admins_only=REQ(validator=check_bool, default=None),
                 name_changes_disabled=REQ(validator=check_bool, default=None),
                 email_changes_disabled=REQ(validator=check_bool, default=None),
                 inline_image_preview=REQ(validator=check_bool, default=None),
                 inline_url_embed_preview=REQ(validator=check_bool, default=None),
                 create_stream_by_admins_only=REQ(validator=check_bool, default=None),
                 add_emoji_by_admins_only=REQ(validator=check_bool, default=None),
                 allow_message_editing=REQ(validator=check_bool, default=None),
                 message_content_edit_limit_seconds=REQ(converter=to_non_negative_int, default=None),
                 default_language=REQ(validator=check_string, default=None),
                 waiting_period_threshold=REQ(converter=to_non_negative_int, default=None),
                 authentication_methods=REQ(validator=check_dict([]), default=None),
                 message_retention_days=REQ(converter=to_not_negative_int_or_none, default=None)):
    # type: (HttpRequest, UserProfile, Optional[str], Optional[str], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[int], Optional[str], Optional[int], Optional[dict], Optional[int]) -> HttpResponse
    realm = user_profile.realm

    # Additional validation/error checking beyond types go here, so
    # the entire request can succeed or fail atomically.
    if default_language is not None and default_language not in get_available_language_codes():
        raise JsonableError(_("Invalid language '%s'" % (default_language,)))
    if description is not None and len(description) > 1000:
        return json_error(_("Realm description is too long."))
    if authentication_methods is not None and True not in list(authentication_methods.values()):
        return json_error(_("At least one authentication method must be enabled."),
                          data={"reason": "no authentication"},
                          status=403)

    # The user of `locals()` here is a bit of a code smell, but it's
    # restricted to the elements present in realm.property_types.
    #
    # TODO: It should be possible to deduplicate this function up
    # further by some more advanced usage of the
    # `REQ/has_request_variables` extraction.
    req_vars = {k: v for k, v in list(locals().items()) if k in realm.property_types}
    data = {} # type: Dict[str, Any]

    for k, v in list(req_vars.items()):
        if v is not None and getattr(realm, k) != v:
            do_set_realm_property(realm, k, v)
            if isinstance(v, Text):
                data[k] = 'updated'
            else:
                data[k] = v

    # The following realm properties do not fit the pattern above
    # authentication_methods is not supported by the do_set_realm_property
    # framework because of its bitfield.
    if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
        do_set_realm_authentication_methods(realm, authentication_methods)
        data['authentication_methods'] = authentication_methods
    # The message_editing settings are coupled to each other, and thus don't fit
    # into the do_set_realm_property framework.
    if (allow_message_editing is not None and realm.allow_message_editing != allow_message_editing) or \
       (message_content_edit_limit_seconds is not None and
            realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds):
        if allow_message_editing is None:
            allow_message_editing = realm.allow_message_editing
        if message_content_edit_limit_seconds is None:
            message_content_edit_limit_seconds = realm.message_content_edit_limit_seconds
        do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds)
        data['allow_message_editing'] = allow_message_editing
        data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
    return json_success(data)
<commit_msg>pep8: Add compliance with rule E261 to zerver/views/realm.py.<commit_after>from __future__ import absolute_import

from typing import Any, Dict, Optional, List, Text
from django.http import HttpRequest, HttpResponse
from django.utils.translation import ugettext as _

from zerver.decorator import require_realm_admin, to_non_negative_int, to_not_negative_int_or_none
from zerver.lib.actions import (
    do_set_realm_message_editing,
    do_set_realm_authentication_methods,
    do_set_realm_property,
)
from zerver.lib.i18n import get_available_language_codes
from zerver.lib.request import has_request_variables, REQ, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.validator import check_string, check_dict, check_bool
from zerver.models import UserProfile

@require_realm_admin
@has_request_variables
def update_realm(request, user_profile, name=REQ(validator=check_string, default=None),
                 description=REQ(validator=check_string, default=None),
                 restricted_to_domain=REQ(validator=check_bool, default=None),
                 invite_required=REQ(validator=check_bool, default=None),
                 invite_by_admins_only=REQ(validator=check_bool, default=None),
                 name_changes_disabled=REQ(validator=check_bool, default=None),
                 email_changes_disabled=REQ(validator=check_bool, default=None),
                 inline_image_preview=REQ(validator=check_bool, default=None),
                 inline_url_embed_preview=REQ(validator=check_bool, default=None),
                 create_stream_by_admins_only=REQ(validator=check_bool, default=None),
                 add_emoji_by_admins_only=REQ(validator=check_bool, default=None),
                 allow_message_editing=REQ(validator=check_bool, default=None),
                 message_content_edit_limit_seconds=REQ(converter=to_non_negative_int, default=None),
                 default_language=REQ(validator=check_string, default=None),
                 waiting_period_threshold=REQ(converter=to_non_negative_int, default=None),
                 authentication_methods=REQ(validator=check_dict([]), default=None),
                 message_retention_days=REQ(converter=to_not_negative_int_or_none, default=None)):
    # type: (HttpRequest, UserProfile, Optional[str], Optional[str], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[bool], Optional[int], Optional[str], Optional[int], Optional[dict], Optional[int]) -> HttpResponse
    realm = user_profile.realm

    # Additional validation/error checking beyond types go here, so
    # the entire request can succeed or fail atomically.
    if default_language is not None and default_language not in get_available_language_codes():
        raise JsonableError(_("Invalid language '%s'" % (default_language,)))
    if description is not None and len(description) > 1000:
        return json_error(_("Realm description is too long."))
    if authentication_methods is not None and True not in list(authentication_methods.values()):
        return json_error(_("At least one authentication method must be enabled."),
                          data={"reason": "no authentication"},
                          status=403)

    # The user of `locals()` here is a bit of a code smell, but it's
    # restricted to the elements present in realm.property_types.
    #
    # TODO: It should be possible to deduplicate this function up
    # further by some more advanced usage of the
    # `REQ/has_request_variables` extraction.
    req_vars = {k: v for k, v in list(locals().items()) if k in realm.property_types}
    data = {}  # type: Dict[str, Any]

    for k, v in list(req_vars.items()):
        if v is not None and getattr(realm, k) != v:
            do_set_realm_property(realm, k, v)
            if isinstance(v, Text):
                data[k] = 'updated'
            else:
                data[k] = v

    # The following realm properties do not fit the pattern above
    # authentication_methods is not supported by the do_set_realm_property
    # framework because of its bitfield.
    if authentication_methods is not None and realm.authentication_methods_dict() != authentication_methods:
        do_set_realm_authentication_methods(realm, authentication_methods)
        data['authentication_methods'] = authentication_methods
    # The message_editing settings are coupled to each other, and thus don't fit
    # into the do_set_realm_property framework.
    if (allow_message_editing is not None and realm.allow_message_editing != allow_message_editing) or \
       (message_content_edit_limit_seconds is not None and
            realm.message_content_edit_limit_seconds != message_content_edit_limit_seconds):
        if allow_message_editing is None:
            allow_message_editing = realm.allow_message_editing
        if message_content_edit_limit_seconds is None:
            message_content_edit_limit_seconds = realm.message_content_edit_limit_seconds
        do_set_realm_message_editing(realm, allow_message_editing, message_content_edit_limit_seconds)
        data['allow_message_editing'] = allow_message_editing
        data['message_content_edit_limit_seconds'] = message_content_edit_limit_seconds
    return json_success(data)
<|endoftext|>


<commit_before># Orca
#
# Copyright (C) 2010 Joanmarie Diggs
# Copyright (C) 2011 Igalia, S.L.
#
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

__id__ = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2010 Joanmarie Diggs." \
                "Copyright (c) 2011 Igalia, S.L."
__license__   = "LGPL"

import pyatspi
import re

import orca.script_utilities as script_utilities
import orca.settings as settings

#############################################################################
#                                                                           #
# Utilities                                                                 #
#                                                                           #
#############################################################################

class Utilities(script_utilities.Utilities):

    def __init__(self, script):
        """Creates an instance of the Utilities class.

        Arguments:
        - script: the script with which this instance is associated.
        """

        script_utilities.Utilities.__init__(self, script)

    def headingLevel(self, obj):
        """Determines the heading level of the given object.  A value
        of 0 means there is no heading level."""

        level = 0

        if obj is None:
            return level

        if obj.getRole() == pyatspi.ROLE_HEADING:
            attributes = obj.getAttributes()
            if attributes is None:
                return level
            for attribute in attributes:
                if attribute.startswith("level:"):
                    level = int(attribute.split(":")[1])
                    break

        return level

    def isWebKitGtk(self, obj):
        """Returns True if this object is a WebKitGtk object."""

        if not obj:
            return False

        attrs = dict([attr.split(':', 1) for attr in obj.getAttributes()])
        return attrs.get('toolkit', '') == 'WebKitGtk'

    def isReadOnlyTextArea(self, obj):
        """Returns True if obj is a text entry area that is read only."""

        if not obj.getRole() == pyatspi.ROLE_ENTRY:
            return False

        state = obj.getState()
        readOnly = state.contains(pyatspi.STATE_FOCUSABLE) \
                   and not state.contains(pyatspi.STATE_EDITABLE)

        return readOnly

    def displayedText(self, obj):
        """Returns the text being displayed for an object.

        Arguments:
        - obj: the object

        Returns the text being displayed for an object or None if there isn't
        any text being shown.
        """

        text = script_utilities.Utilities.displayedText(self, obj)
        if text and text != self.EMBEDDED_OBJECT_CHARACTER:
            return text

        if obj.getRole() in [pyatspi.ROLE_LINK, pyatspi.ROLE_LIST_ITEM]:
            text = ' '.join(map(self.displayedText, (x for x in obj)))
            if not text:
                text = self.linkBasename(obj)

        return text

    def getObjectsFromEOCs(self, obj, boundary=None, offset=None):
        """Breaks the string containing a mixture of text and embedded object
        characters into a list of (obj, startOffset, endOffset, string) tuples.

        Arguments
        - obj: the object whose EOCs we need to expand into tuples
        - boundary: the pyatspi text boundary type. If None, get all text.
        - offset: the character offset. If None, use the current offset.

        Returns a list of (obj, startOffset, endOffset, string) tuples.
        """

        try:
            text = obj.queryText()
            htext = obj.queryHypertext()
        except (AttributeError, NotImplementedError):
            return [(obj, 0, 1, '')]

        if offset == None:
            offset = text.caretOffset
        if boundary == None:
            start = offset
            end = text.characterCount
            string = text.getText(start, end)
        else:
            if boundary == pyatspi.TEXT_BOUNDARY_CHAR:
                key, mods = self.lastKeyAndModifiers()
                if (mods & settings.SHIFT_MODIFIER_MASK) and key == 'Right':
                    offset -= 1
            string, start, end = text.getTextAtOffset(offset, boundary)

        if not string:
            return [(obj, 0, 1, '')]

        string = string.decode('UTF-8')
        pattern = re.compile(self.EMBEDDED_OBJECT_CHARACTER)
        offsets = [m.start(0) for m in re.finditer(pattern, string)]
        objects = \
            [(o, obj[htext.getLinkIndex(o + start)], 0, 1, '') for o in offsets]

        pattern = re.compile('[^%s]+' % self.EMBEDDED_OBJECT_CHARACTER)
        spans = [m.span() for m in re.finditer(pattern, string)]
        for (s1, s2) in spans:
            objects.append([s1, obj, s1 + start, s2 + start, string[s1:s2]])

        objects = sorted(objects, key=lambda x: x[0])
        return map(lambda o: o[1:5], objects)
<commit_msg>Adjust getObjectsFromEOCs to deal with unexpected accessible hyperlink indices<commit_after># Orca
#
# Copyright (C) 2010 Joanmarie Diggs
# Copyright (C) 2011 Igalia, S.L.
#
# Author: Joanmarie Diggs <jdiggs@igalia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.

__id__ = "$Id$"
__version__   = "$Revision$"
__date__      = "$Date$"
__copyright__ = "Copyright (c) 2010 Joanmarie Diggs." \
                "Copyright (c) 2011 Igalia, S.L."
__license__   = "LGPL"

import pyatspi
import re

import orca.script_utilities as script_utilities
import orca.settings as settings

#############################################################################
#                                                                           #
# Utilities                                                                 #
#                                                                           #
#############################################################################

class Utilities(script_utilities.Utilities):

    def __init__(self, script):
        """Creates an instance of the Utilities class.

        Arguments:
        - script: the script with which this instance is associated.
        """

        script_utilities.Utilities.__init__(self, script)

    def headingLevel(self, obj):
        """Determines the heading level of the given object.  A value
        of 0 means there is no heading level."""

        level = 0

        if obj is None:
            return level

        if obj.getRole() == pyatspi.ROLE_HEADING:
            attributes = obj.getAttributes()
            if attributes is None:
                return level
            for attribute in attributes:
                if attribute.startswith("level:"):
                    level = int(attribute.split(":")[1])
                    break

        return level

    def isWebKitGtk(self, obj):
        """Returns True if this object is a WebKitGtk object."""

        if not obj:
            return False

        attrs = dict([attr.split(':', 1) for attr in obj.getAttributes()])
        return attrs.get('toolkit', '') == 'WebKitGtk'

    def isReadOnlyTextArea(self, obj):
        """Returns True if obj is a text entry area that is read only."""

        if not obj.getRole() == pyatspi.ROLE_ENTRY:
            return False

        state = obj.getState()
        readOnly = state.contains(pyatspi.STATE_FOCUSABLE) \
                   and not state.contains(pyatspi.STATE_EDITABLE)

        return readOnly

    def displayedText(self, obj):
        """Returns the text being displayed for an object.

        Arguments:
        - obj: the object

        Returns the text being displayed for an object or None if there isn't
        any text being shown.
        """

        text = script_utilities.Utilities.displayedText(self, obj)
        if text and text != self.EMBEDDED_OBJECT_CHARACTER:
            return text

        if obj.getRole() in [pyatspi.ROLE_LINK, pyatspi.ROLE_LIST_ITEM]:
            text = ' '.join(map(self.displayedText, (x for x in obj)))
            if not text:
                text = self.linkBasename(obj)

        return text

    @staticmethod
    def getHyperlinkRange(obj):
        """Returns the start and end indices associated with the embedded
        object, obj."""

        try:
            hyperlink = obj.queryHyperlink()
        except NotImplementedError:
            return 0, 0

        return hyperlink.startIndex, hyperlink.endIndex

    def getObjectsFromEOCs(self, obj, boundary=None, offset=None):
        """Breaks the string containing a mixture of text and embedded object
        characters into a list of (obj, startOffset, endOffset, string) tuples.

        Arguments
        - obj: the object whose EOCs we need to expand into tuples
        - boundary: the pyatspi text boundary type. If None, get all text.
        - offset: the character offset. If None, use the current offset.

        Returns a list of (obj, startOffset, endOffset, string) tuples.
        """

        try:
            text = obj.queryText()
            htext = obj.queryHypertext()
        except (AttributeError, NotImplementedError):
            return [(obj, 0, 1, '')]

        string = text.getText(0, -1).decode('UTF-8')
        if not string:
            return [(obj, 0, 1, '')]

        if offset == None:
            offset = text.caretOffset
        if boundary == None:
            start = offset
            end = text.characterCount
        else:
            if boundary == pyatspi.TEXT_BOUNDARY_CHAR:
                key, mods = self.lastKeyAndModifiers()
                if (mods & settings.SHIFT_MODIFIER_MASK) and key == 'Right':
                    offset -= 1
            segment, start, end = text.getTextAtOffset(offset, boundary)

        pattern = re.compile(self.EMBEDDED_OBJECT_CHARACTER)
        offsets = [m.start(0) for m in re.finditer(pattern, string)]
        offsets = filter(lambda x: start <= x < end, offsets)

        objects = []
        objs = [obj[htext.getLinkIndex(offset)] for offset in offsets]
        ranges = [self.getHyperlinkRange(x) for x in objs]
        for i, (first, last) in enumerate(ranges):
            objects.append((obj, start, first, string[start:first]))
            objects.append((objs[i], first, last, ''))
            start = last
        objects.append((obj, start, end, string[start:end]))

        return objects
<|endoftext|>


<commit_before>import logging
import re
from collections import defaultdict
from itertools import permutations
from copy import deepcopy as copy

from fst import FSA, PrintnameMatcher
from fst import PosControlMatcher as PosMatcher
from machine import Machine
from monoid import Monoid
from control import PosControl, ElviraPluginControl
from constants import deep_cases

class Construction(object):
    SEMANTIC, CHUNK, AVM = xrange(3)  # types
    def __init__(self, name, control, type_=SEMANTIC):
        """
        @param type_ the type of the construction -- SEMANTIC, CHUNK or AVM.
        """
        self.name = name

        if not isinstance(control, FSA):
            raise TypeError("control has to be an FSA instance")
        self.control = control

        self.type_ = type_

    def check(self, seq):
        logging.debug((u"Checking {0} construction for matching with " +
                      u"{1} machines").format(self.name,
                      u" ".join(unicode(m) for m in seq)).encode("utf-8"))
        self.control.reset()
        for machine in seq:
            self.control.read(machine)
        return self.control.in_final()

    def run(self, seq):
        """Shorthand for if check: act."""
        # read the sequence first, and give it to the control
        self.check(seq)

        # if control got into acceptor state, do something
        if self.control.in_final():
            return self.act(seq)
        else:
            return None

    def last_check(self, seq):
        """last_check() is called after construction is activated by the
        spreading activation. Can be used for order checking for example"""
        return True

    def act(self, seq):
        """@return a sequence of machines, or @c None, if last_check() failed.
        """
        logging.debug("Construction matched, running action")
        # arbitrary python code, now every construction will have it
        # hardcoded into the code, later it will be done by Machine objects

class VerbConstruction(Construction):
    """A default construction for verbs. It reads definitions, discovers
    cases, and builds a control from it. After that, the act() will do the
    linking process, eg. link the verb with other words, object, subject, etc.
    """
    def __init__(self, name, machine, supp_dict):
        self.name = name
        self.machine = machine
        self.supp_dict = supp_dict
        self.arg_locations = self.discover_arguments()
        self.generate_phi()
        control = self.generate_control()
        self.case_pattern = re.compile("N(OUN|P)[^C]*CAS<([^>]*)>")
        Construction.__init__(self, name, control)
        self.activated = False

    def generate_phi(self):
        arguments = self.arg_locations.keys()
        # creating Matcher objects from arguments
        self.transitions = {}
        self.phi = {}

        # Verb transition will imply no change, we put it into phi
        # to implement act() easier
        vt = PosMatcher("^VERB.*")
        self.transitions["VERB"] = vt
        self.phi[vt] = None

        # normal arguments
        for arg in arguments:
            if arg.startswith("@"):
                pt = PosMatcher(
                    "({0})".format("|".join(self.supp_dict[arg[1:]])))
                self.transitions[arg] = pt
                self.phi[pt] = self.arg_locations[arg]

            # NOM case is implicit, that is why we need a distinction here
            elif arg == "NOM":
                pt = PosMatcher("NOUN(?!.*CAS)".format(arg))
                self.transitions[arg] = pt
                self.phi[pt] = self.arg_locations[arg]

            else:
                pt = PosMatcher("CAS<{0}>".format(arg))
                self.transitions[arg] = pt
                self.phi[pt] = self.arg_locations[arg]

    def generate_control(self):
        arguments = self.transitions.keys()
        arguments.remove("VERB")

        # this will be a hypercube
        control = FSA()

        # zero state is for verb
        control.add_state("0", is_init=True, is_final=False)

        # inside states for the cube, except the last, accepting state
        for i in xrange(1, pow(2, len(arguments))):
            control.add_state(str(i), is_init=False, is_final=False)

        # last node of the hypercube
        control.add_state(str(int(pow(2, len(arguments)))),
                              is_init=False, is_final=True)

        # first transition
        control.add_transition(self.transitions["VERB"], "0", "1")

        # count every transition as an increase in number of state
        for path in permutations(arguments):
            actual_state = 1
            for arg in path:
                increase = pow(2, arguments.index(arg))
                new_state = actual_state + increase
                control.add_transition(self.transitions[arg],
                        str(actual_state), str(new_state))

                actual_state = new_state
        return control

    def discover_arguments(self, machine=None, d=None):
        if machine is None:
            machine = self.machine
        if d is None:
            d = defaultdict(list)

        for pi, p in enumerate(machine.base.partitions[1:]):
            pi += 1
            to_remove = None
            for mi, part_machine in enumerate(p):
                pn = part_machine.printname()
                # we are interested in deep cases and
                # supplementary regexps
                if pn in deep_cases or pn.startswith("@"):
                    d[pn].append((machine, pi))
                    to_remove = mi

                # recursive call
                d.update(self.discover_arguments(part_machine, d))

            if to_remove is not None:
                p = p[:to_remove] + p[to_remove+1:]
                machine.base.partitions[pi] = p

        return d

    def check(self, seq):
        if self.activated:
            return False
        else:
            return Construction.check(self, seq)

    def act(self, seq):
        result = []

        # put a clear machine into self.machine while verb_machine will be
        # the old self.machine, and the references in self.arg_locations
        # will point at good locations in verb_machine
        clear_machine = copy(self.machine)
        verb_machine = self.machine
        self.machine = clear_machine
        result.append(verb_machine)

        for m in seq:
            if m.printname() == self.machine.printname():
                # this is the verb machine
                # copy the (referenceof) PosControl of the verb in the sentence
                # to the control of the new machine
                verb_machine.control = m.control
            else:
                matcher = self.case_pattern.match(m.control.pos)
                if matcher is not None or re.match("^NOUN", m.control.pos):
                    case = (matcher.group(2) if matcher is not None else "NOM")
                    if case not in self.case_locations:
                        raise Exception("""Got a NOUN with a useless case""")

                    for m_to, m_p_i in self.case_locations[case]:
                        m_to.append(m, m_p_i)
                else:
                    raise Exception("""Every machine at this point of the code
                                    has to match a case pattern""")

        for m in seq:
            for transition in self.phi:
                # skip None transitions (VERB)
                if self.phi[transition] is None:
                    continue

                if transition.match(m):
                    # possible multiple places for one machine
                    for m_to, m_p_i in self.phi[transition]:
                        m_to.append(m, m_p_i)
                    break

        self.activated = True
        return result

class TheConstruction(Construction):
    """NOUN<DET> -> The NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=True)
        control.add_transition(PrintnameMatcher("^az?$"), "0", "1")
        control.add_transition(PosMatcher("^NOUN.*"), "1", "2")

        Construction.__init__(self, "TheConstruction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("TheConstruction matched, running action")
        seq[1].control.pos += "<DET>"
        return seq[1]

class DummyNPConstruction(Construction):
    """NP construction. NP -> Adj* NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^ADJ.*"), "0", "0")
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")

        Construction.__init__(self, "DummyNPConstruction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("DummyNPConstruction matched, running action")
        noun = seq[-1]
        adjs = seq[:-1]
        for adj in adjs:
            noun.append(adj)
        return noun

class MaxNP_InBetweenPostP_Construction(Construction):
    """NP -> NOUN POSTP[ATTRIB]|ADJ NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=False)
        control.add_state("3", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")
        control.add_transition(PosMatcher("POSTP\[ATTRIB\]\|ADJ", exact=True), "1", "2")
        control.add_transition(PosMatcher("^NOUN.*"), "2", "3")
        Construction.__init__(self, "MaxNP_InBetweenPostP_Construction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("MaxNP_InBetweenPostP_Construction matched, running action")
        noun1 = seq[0]
        postp = seq[1]
        noun2 = seq[2]
        postp.append(noun1, 2)
        postp.append(noun2, 1)
        return noun2

class PostPConstruction(Construction):
    """PP -> NOUN POSTP"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")
        control.add_transition(PosMatcher("POSTP", exact=True), "1", "2")
        Construction.__init__(self, "PostPConstruction", control, type_=
                              Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("PostPConstruction matched, running action")
        noun1 = seq[0]
        postp = seq[1]
        noun2 = seq[2]
        postp.append(noun1, 2)
        postp.append(noun2, 1)
        return noun2

class ElviraConstruction(Construction):
    def __init__(self):
        control = FSA()
        # TODO: to hypercube
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=False)
        control.add_state("3", is_init=False, is_final=False)
        control.add_state("4", is_init=False, is_final=True)
        control.add_transition(PrintnameMatcher("vonat"), "0", "1")
        control.add_transition(PrintnameMatcher("menetrend"), "1", "2")
        control.add_transition(PrintnameMatcher("BEFORE_AT"), "2", "3")
        control.add_transition(PrintnameMatcher("AFTER_AT"), "3", "4")

        Construction.__init__(self, self.__class__.__name__, control)

    def last_check(self, seq):
        try:
            if len(seq[2].base.partitions[2]) > 0 and len(seq[3].base.partitions[2]) > 0:
                return True
        except:
            pass
        return False

    def act(self, seq):
        #TODO: implement
        if not self.last_check(seq):
            return None

        elvira_machine = Machine(Monoid("elvira"), ElviraPluginControl())
        for m in seq:
            elvira_machine.append(m)

        return [elvira_machine]

def test():
    a = Machine(Monoid("the"), PosControl("DET"))
    kek = Machine(Monoid("kek"), PosControl("ADJ"))
    kockat = Machine(Monoid("kockat"), PosControl("NOUN<CAS<ACC>>"))
    m = Machine(Monoid("vonat"))
    m2 = Machine(Monoid("tb"))
    m.append(m2)
    m2.append(m)
    m3 = copy(m)

    npc = DummyNPConstruction()
    thec = TheConstruction()

    res = npc.run([kek, kockat])
    res = thec.run([a] + res)
    print res[0]
    print res[0].control
    print res[0].base.partitions[1][0]

if __name__ == "__main__":
    test()

<commit_msg>modified:   construction.py initial AVMConstruction implementation, not done, not tested<commit_after>import logging
import re
from collections import defaultdict
from itertools import permutations
from copy import deepcopy as copy

from fst import FSA, PrintnameMatcher
from fst import PosControlMatcher as PosMatcher
from machine import Machine
from monoid import Monoid
from control import PosControl, ElviraPluginControl
from constants import deep_cases

class Construction(object):
    SEMANTIC, CHUNK, AVM = xrange(3)  # types
    def __init__(self, name, control, type_=SEMANTIC):
        """
        @param type_ the type of the construction -- SEMANTIC, CHUNK or AVM.
        """
        self.name = name

        if not isinstance(control, FSA):
            raise TypeError("control has to be an FSA instance")
        self.control = control

        self.type_ = type_

    def check(self, seq):
        logging.debug((u"Checking {0} construction for matching with " +
                      u"{1} machines").format(self.name,
                      u" ".join(unicode(m) for m in seq)).encode("utf-8"))
        self.control.reset()
        for machine in seq:
            self.control.read(machine)
        return self.control.in_final()

    def run(self, seq):
        """Shorthand for if check: act."""
        # read the sequence first, and give it to the control
        self.check(seq)

        # if control got into acceptor state, do something
        if self.control.in_final():
            return self.act(seq)
        else:
            return None

    def last_check(self, seq):
        """last_check() is called after construction is activated by the
        spreading activation. Can be used for order checking for example"""
        return True

    def act(self, seq):
        """@return a sequence of machines, or @c None, if last_check() failed.
        """
        logging.debug("Construction matched, running action")
        # arbitrary python code, now every construction will have it
        # hardcoded into the code, later it will be done by Machine objects

class VerbConstruction(Construction):
    """A default construction for verbs. It reads definitions, discovers
    cases, and builds a control from it. After that, the act() will do the
    linking process, eg. link the verb with other words, object, subject, etc.
    """
    def __init__(self, name, machine, supp_dict):
        self.name = name
        self.machine = machine
        self.supp_dict = supp_dict
        self.arg_locations = self.discover_arguments()
        self.phi = self.generate_phi()
        control = self.generate_control()
        self.case_pattern = re.compile("N(OUN|P)[^C]*CAS<([^>]*)>")
        Construction.__init__(self, name, control)
        self.activated = False

    def generate_phi(self):
        arguments = self.arg_locations.keys()
        # creating Matcher objects from arguments
        self.matchers = {}
        phi = {}

        # Verb transition will imply no change, we put it into phi
        # to implement act() easier
        vm = PosMatcher("^VERB.*")
        self.matchers["VERB"] = vm
        phi[vm] = None

        # normal arguments
        for arg in arguments:
            if arg.startswith("@"):
                pm = PosMatcher(
                    "({0})".format("|".join(self.supp_dict[arg[1:]])))
                self.matchers[arg] = pm
                phi[pm] = self.arg_locations[arg]

            # NOM case is implicit, that is why we need a distinction here
            elif arg == "NOM":
                pm = PosMatcher("NOUN(?!.*CAS)".format(arg))
                self.matchers[arg] = pm
                phi[pm] = self.arg_locations[arg]

            else:
                pm = PosMatcher("CAS<{0}>".format(arg))
                self.matchers[arg] = pm
                phi[pm] = self.arg_locations[arg]
        return phi

    def generate_control(self):
        arguments = self.matchers.keys()
        arguments.remove("VERB")

        # this will be a hypercube
        control = FSA()

        # zero state is for verb
        control.add_state("0", is_init=True, is_final=False)

        # inside states for the cube, except the last, accepting state
        for i in xrange(1, pow(2, len(arguments))):
            control.add_state(str(i), is_init=False, is_final=False)

        # last node of the hypercube
        control.add_state(str(int(pow(2, len(arguments)))),
                              is_init=False, is_final=True)

        # first transition
        control.add_transition(self.matchers["VERB"], "0", "1")

        # count every transition as an increase in number of state
        for path in permutations(arguments):
            actual_state = 1
            for arg in path:
                increase = pow(2, arguments.index(arg))
                new_state = actual_state + increase
                control.add_transition(self.matchers[arg],
                        str(actual_state), str(new_state))

                actual_state = new_state
        return control

    def discover_arguments(self, machine=None, d=None):
        if machine is None:
            machine = self.machine
        if d is None:
            d = defaultdict(list)

        for pi, p in enumerate(machine.base.partitions[1:]):
            pi += 1
            to_remove = None
            for mi, part_machine in enumerate(p):
                pn = part_machine.printname()
                # we are interested in deep cases and
                # supplementary regexps
                if pn in deep_cases or pn.startswith("@"):
                    d[pn].append((machine, pi))
                    to_remove = mi

                # recursive call
                d.update(self.discover_arguments(part_machine, d))

            if to_remove is not None:
                p = p[:to_remove] + p[to_remove+1:]
                machine.base.partitions[pi] = p

        return d

    def check(self, seq):
        if self.activated:
            return False
        else:
            return Construction.check(self, seq)

    def act(self, seq):
        result = []

        # put a clear machine into self.machine while verb_machine will be
        # the old self.machine, and the references in self.arg_locations
        # will point at good locations in verb_machine
        clear_machine = copy(self.machine)
        verb_machine = self.machine
        self.machine = clear_machine
        result.append(verb_machine)

        for m in seq:
            if m.printname() == self.machine.printname():
                # this is the verb machine
                # copy the (referenceof) PosControl of the verb in the sentence
                # to the control of the new machine
                verb_machine.control = m.control
            else:
                matcher = self.case_pattern.match(m.control.pos)
                if matcher is not None or re.match("^NOUN", m.control.pos):
                    case = (matcher.group(2) if matcher is not None else "NOM")
                    if case not in self.case_locations:
                        raise Exception("""Got a NOUN with a useless case""")

                    for m_to, m_p_i in self.case_locations[case]:
                        m_to.append(m, m_p_i)
                else:
                    raise Exception("""Every machine at this point of the code
                                    has to match a case pattern""")

        for m in seq:
            for transition in self.phi:
                # skip None transitions (VERB)
                if self.phi[transition] is None:
                    continue

                if transition.match(m):
                    # possible multiple places for one machine
                    for m_to, m_p_i in self.phi[transition]:
                        m_to.append(m, m_p_i)
                    break

        self.activated = True
        return result

class AVMConstruction(Construction):
    """this class will fill the slots in the AVM"""
    def __init__(self, avm, name):
        self.avm = avm
        self.phi = self.generate_phi()
        control = self.generate_control()
        Construction.__init__(self, name, control, type_=Construction.AVM)

    def generate_phi(self):
        phi = {}
        for key in self.avm:
            matcher, _, _, _ = self.avm[key]
            phi[matcher] = key
        return phi

    def generate_control(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)

        state_num = 1
        for key in self.avm:
            state_name = str(state_num)
            matcher, _, _, _ = self.avm[key]
            control.add_state(state_name, is_init=False, is_final=True)
            control.add_transition(matcher, "0", state_name)
            state_num += 1
        return control

    def act(self, seq):
        for machine in seq:
            for matcher in self.phi:
                if matcher.match(machine):
                    self.avm[self.phi[matcher]] = machine

class TheConstruction(Construction):
    """NOUN<DET> -> The NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=True)
        control.add_transition(PrintnameMatcher("^az?$"), "0", "1")
        control.add_transition(PosMatcher("^NOUN.*"), "1", "2")

        Construction.__init__(self, "TheConstruction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("TheConstruction matched, running action")
        seq[1].control.pos += "<DET>"
        return seq[1]

class DummyNPConstruction(Construction):
    """NP construction. NP -> Adj* NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^ADJ.*"), "0", "0")
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")

        Construction.__init__(self, "DummyNPConstruction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("DummyNPConstruction matched, running action")
        noun = seq[-1]
        adjs = seq[:-1]
        for adj in adjs:
            noun.append(adj)
        return noun

class MaxNP_InBetweenPostP_Construction(Construction):
    """NP -> NOUN POSTP[ATTRIB]|ADJ NOUN"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=False)
        control.add_state("3", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")
        control.add_transition(PosMatcher("POSTP\[ATTRIB\]\|ADJ", exact=True), "1", "2")
        control.add_transition(PosMatcher("^NOUN.*"), "2", "3")
        Construction.__init__(self, "MaxNP_InBetweenPostP_Construction", control,
                              type_=Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("MaxNP_InBetweenPostP_Construction matched, running action")
        noun1 = seq[0]
        postp = seq[1]
        noun2 = seq[2]
        postp.append(noun1, 2)
        postp.append(noun2, 1)
        return noun2

class PostPConstruction(Construction):
    """PP -> NOUN POSTP"""
    def __init__(self):
        control = FSA()
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=True)
        control.add_transition(PosMatcher("^NOUN.*"), "0", "1")
        control.add_transition(PosMatcher("POSTP", exact=True), "1", "2")
        Construction.__init__(self, "PostPConstruction", control, type_=
                              Construction.CHUNK)

    def act(self, seq):
        logging.debug("Construction matched, running last check")
        self.last_check(seq)
        logging.debug("PostPConstruction matched, running action")
        noun1 = seq[0]
        postp = seq[1]
        noun2 = seq[2]
        postp.append(noun1, 2)
        postp.append(noun2, 1)
        return noun2

class ElviraConstruction(Construction):
    def __init__(self):
        control = FSA()
        # TODO: to hypercube
        control.add_state("0", is_init=True, is_final=False)
        control.add_state("1", is_init=False, is_final=False)
        control.add_state("2", is_init=False, is_final=False)
        control.add_state("3", is_init=False, is_final=False)
        control.add_state("4", is_init=False, is_final=True)
        control.add_transition(PrintnameMatcher("vonat"), "0", "1")
        control.add_transition(PrintnameMatcher("menetrend"), "1", "2")
        control.add_transition(PrintnameMatcher("BEFORE_AT"), "2", "3")
        control.add_transition(PrintnameMatcher("AFTER_AT"), "3", "4")

        Construction.__init__(self, self.__class__.__name__, control)

    def last_check(self, seq):
        try:
            if len(seq[2].base.partitions[2]) > 0 and len(seq[3].base.partitions[2]) > 0:
                return True
        except:
            pass
        return False

    def act(self, seq):
        #TODO: implement
        if not self.last_check(seq):
            return None

        elvira_machine = Machine(Monoid("elvira"), ElviraPluginControl())
        for m in seq:
            elvira_machine.append(m)

        return [elvira_machine]

def test():
    a = Machine(Monoid("the"), PosControl("DET"))
    kek = Machine(Monoid("kek"), PosControl("ADJ"))
    kockat = Machine(Monoid("kockat"), PosControl("NOUN<CAS<ACC>>"))
    m = Machine(Monoid("vonat"))
    m2 = Machine(Monoid("tb"))
    m.append(m2)
    m2.append(m)
    m3 = copy(m)

    npc = DummyNPConstruction()
    thec = TheConstruction()

    res = npc.run([kek, kockat])
    res = thec.run([a] + res)
    print res[0]
    print res[0].control
    print res[0].base.partitions[1][0]

if __name__ == "__main__":
    test()

<|endoftext|>