Example #1
0
File: peer.py Project: qkniep/naxos
    def __init__(self, first=True, addr=None):
        """"""
        super().__init__()  # Thread constructor

        self.queue = util.PollableQueue()
        self.selector = selectors.DefaultSelector()
        self.cache = Cache()
        self.index = Index()
        self.network = NetworkNode(self.selector, self.cache)
        if first:
            self.paxos = PaxosNode(self.network)
            paxos_header = '- This peer is now operating Paxos node %s' % self.paxos.node_id(
            )
        else:
            self.paxos = None
            self.connect_to_paxos(addr)
            self.queue.put(('first_connection', {}))
            paxos_header = '- This peer is not yet operating as a Paxos node.'
        self.last_keepalive = time.time()
        self.peer_keepalives = {}
        self._running = True
        self.periodic_runner = PeriodicRunner()
        self.periodic_runner.start()

        naxos_header = '- Running Naxos v%s' % self.VERSION
        net_header = '- This peer is listening for incoming connections on: %s:%s' % self.network.listen_addr
        mid = '=' * max(len(naxos_header), len(net_header), len(paxos_header))

        print('\n'.join((mid, naxos_header, net_header, paxos_header, mid)))
        self.selector.register(self.queue, selectors.EVENT_READ)
Example #2
0
def test_build_account_path(cls):
    cls.client = Mock()
    cache = Cache()
    cls.client.list_parents.return_value = stub_organizations.list_parents_root
    cls.client.describe_organizational_unit.return_value = stub_organizations.describe_organizational_unit

    assert cls.build_account_path('some_ou_id', [], cache) == 'some_ou_name'
Example #3
0
    def test_cacheRefreshOverOneDay(self):
        cache = Cache()
        cache.put('k', 'v')
        self.assertEqual(cache.get('k'), 'v')

        fakedate = datetime.now() + timedelta(days=2)
        self.assertEqual(cache.get('k', fakedate), None)
 def testCacheWithPrefix(self):
     s = Storage({'application': 'admin', 'folder': 'applications/admin'})
     cache = Cache(s)
     prefix = cache.with_prefix(cache.ram, 'prefix')
     self.assertEqual(prefix('a', lambda: 1, 0), 1)
     self.assertEqual(prefix('a', lambda: 2, 100), 1)
     self.assertEqual(cache.ram('prefixa', lambda: 2, 100), 1)
Example #5
0
def main():
    args = parse_args()

    with Cache(args.cache_file) as cache, \
            Client(cache) as client, \
            Server(client) as server:
        server.run()
Example #6
0
def main():
    """ The main function. It creates VmaaS application, servers, run everything."""

    vmaas_app = Application()

    server = tornado.httpserver.HTTPServer(vmaas_app)
    server.bind(PUBLIC_API_PORT)
    num_servers = int(os.getenv("MAX_VMAAS_SERVERS", MAX_SERVERS))
    server.start(num_servers)  # start forking here
    init_logging(num_servers)
    LOGGER.info("Starting (version %s).", VMAAS_VERSION)

    # The rest stuff must be done only after forking
    BaseHandler.db_cache = Cache()
    BaseHandler.updates_api = UpdatesAPI(BaseHandler.db_cache)
    BaseHandler.repo_api = RepoAPI(BaseHandler.db_cache)
    BaseHandler.cve_api = CveAPI(BaseHandler.db_cache)
    BaseHandler.errata_api = ErrataAPI(BaseHandler.db_cache)
    BaseHandler.dbchange_api = DBChange(BaseHandler.db_cache)

    vmaas_app.websocket_reconnect()
    vmaas_app.reconnect_callback = PeriodicCallback(
        vmaas_app.websocket_reconnect, WEBSOCKET_RECONNECT_INTERVAL * 1000)
    vmaas_app.reconnect_callback.start()

    IOLoop.instance().start()
Example #7
0
 def __init__(self):
     self.port = 54321
     self.buffer_size = 8192
     self.conexoes = 10
     self.cacheSize = 999999999
     self.cache = Cache(self.cacheSize)
     self.host = "localhost"
Example #8
0
def drexel_scraper(name):
    print('Retrieving ' + name + "'s email...")
    query_link = DIRECTORIES.get('drexel')
    name = name.replace(" ", "%20")
    cache = Cache()
    try:
        email = cache[name]
        return email
    except KeyError:
        pass
    query_link = query_link.format(name)
    driver = get_driver()
    driver.get(query_link)
    driver.implicitly_wait(5)
    time.sleep(3)
    tree = fromstring(driver.page_source)
    email = tree.xpath(
        '//tr[@class="result-row"]//span[@class="email-address"]//a[contains(@href, "mailto")]/text()'
    )
    print(email)
    driver.quit()
    email = email[0] if email else None
    if email is not None:
        cache[name] = email
    return email
Example #9
0
class Connector(AbstractToken):

    _cache = Cache()

    # Static method to build and return a new Connector with the given type
    @staticmethod
    def build(token_type):
        return Connector._cache.get(token_type, Connector)

    def __init__(self, token_type):
        # Guard against initialization without a representation argument
        if token_type is None:
            raise ValueError("New Connectors must have a token_type argument")
            
        if token_type is TerminalSymbol.VARIABLE:
            raise ValueError("New Connectors cannot be of Variable type")

        # Assign internal _type field
        self._type = token_type

    # Override __str__ to return this Connector's type
    def __str__(self):
        if self._type.get_type() in SymbolTranslations:
            return SymbolTranslations[self._type.get_type()]
        else:
            raise ValueError("Invalid Symbol Type")

    def __eq__(self, obj):
        return type(obj) is Connector and str(obj) == str(self)

    def __ne__(self, obj):
        return not (self == obj)
Example #10
0
def test_should_evict_least_accessed():
    with DockerCompose(
        os.getcwd() + "/test",
        compose_file_name="docker-compose.yml",
        pull=True
    ) as compose:
        (host, port) = assert_and_get_host_port(compose, REDIS_PORT)

        cache = Cache(host, port)

        namespace = "evict-least-accessed"

        @cache(namespace=namespace, limit = 3)
        def test_func(test_input: int):
            return test_input

        func_input = range(3)
        keys = []

        # Create three keys
        [test_func(i) for i in func_input]
        [keys.append(get_cache_key(namespace, [i])) for i in func_input]
        keys.append(f'rc:{namespace}:keys')  # base function

        assert set(cache.get_all_keys()) == set(keys)

        # Access all except first input
        [test_func(i) for i in func_input[1:]]

        # Add new input
        test_func(3)
        keys.append(get_cache_key(namespace, [3]))

        diff = set(keys) - set(cache.get_all_keys())
        assert diff == set([keys[0]])
Example #11
0
def test_should_respect_key_limit():
    with DockerCompose(
        os.getcwd() + "/test",
        compose_file_name="docker-compose.yml",
        pull=True
    ) as compose:
        (host, port) = assert_and_get_host_port(compose, REDIS_PORT)

        cache = Cache(host, port)

        namespace = "respect_key_limit"

        @cache(namespace=namespace, limit = 1)
        def test_func(test_input: int):
            return test_input

        assert cache.get_key_count() == 0

        test_func(1)

        # First call gives two keys. One for the base function and one for the inputs
        assert cache.get_key_count() == 2

        [test_func(i) for i in range(50)]

        assert cache.get_key_count() == 2
Example #12
0
    def __init__(self, inidir, inifile, amount):
        """Commence download operation.
        
        Arguments
        inidir -- working directory
        inifile -- config file
        amount -- amount of items to download

        """
        print('Download data for display 3...')
        self._data = []

        #Create dummy GUI
        root = tki.Tk()
        settings = Settings3(inidir, inifile)
        dsdblog = InifileDataSourceDescription(sBlog, inidir, inifile)

        itemarg = (dsdblog.cachedir, (settings.previewx, settings.previewy),
                   (settings.smallpreviewx,
                    settings.smallpreviewy), settings.library,
                   settings.booksearchprefix, settings.booksearchsuffix)
        cache = Cache(dsdblog.cachedir, BlogspotItemWithIsbn, itemarg)
        harvester = BlogspotHarvester(dsdblog, self._addandcheck,
                                      BlogspotItemWithIsbn)
        harvester.itemarg = itemarg
        harvester.newestId = ''

        harvester.update(amount)
        cache.updateContents(self._data, harvester.newestId)
        print('Done!')
Example #13
0
def magic_algorithm(file_path):
    input = read(file_path)
    conf = input[0]
    video_sizes = input[1]
    endpoints = input[2]
    caches = []

    threshold = get_request_threshold(endpoints)

    for i in range(0, conf[INDEX_NUMBER_OF_CACHES]):
        caches.append(Cache(conf[INDEX_CACHE_SIZE], video_sizes))

    for endpoint_id in get_rank_endpoint_latency(endpoints):
        for video_id in endpoints[endpoint_id].get_rank_requests():
            if endpoints[endpoint_id].requests[video_id] > threshold[0]:
                for cache_id in endpoints[endpoint_id].get_rank_caches():
                    if endpoints[endpoint_id].caches[cache_id] < endpoints[endpoint_id].latency and \
                            caches[cache_id].add_video(video_id):
                        break

    for endpoint_id in get_rank_endpoint_latency(endpoints):
        for video_id in endpoints[endpoint_id].get_rank_requests():
            if endpoints[endpoint_id].requests[video_id] <= threshold[0]:
                for cache_id in endpoints[endpoint_id].get_rank_caches():
                    if endpoints[endpoint_id].caches[cache_id] < endpoints[endpoint_id].latency and \
                            caches[cache_id].add_video(video_id):
                        break

    return caches
Example #14
0
def oak_ridge_scraper(name):
    print('Retrieving ' + name + "'s email...")
    query_link = DIRECTORIES.get('oak ridge')
    name = name.replace(" ", "+")
    cache = Cache()
    try:
        email = cache[name]
        return email
    except KeyError:
        pass
    query_link = query_link.format(name)
    driver = get_driver()
    driver.get(query_link)
    driver.implicitly_wait(5)
    driver.find_element_by_css_selector('td.views-field-nothing a:nth-child(1)').click()
    driver.implicitly_wait(5)
    try:
        email = driver.find_element_by_xpath('//div[contains(@class, "staff-profile-contact-info")]//a[contains(@href, "mailto")]')
        email = email.text
        print(email)
    except NoSuchElementException:
        email = None
    driver.quit()
    if email is not None:
        cache[name] = email
    return email
Example #15
0
def test_lru():
    algorithm = LRUCacheAlgorithm(max_item=3)
    cache = Cache(algorithm=algorithm)

    cache.put(1, 0)  # 1
    cache.get(1)
    cache.put(2, 0)  # 1 2
    cache.put(3, 0)  # 1 2 3
    algorithm.index_list.log()
    assert len(cache.data) == 3

    cache.put(4, 0)  # 2 3 4
    algorithm.index_list.log()
    assert len(cache.data) == 3
    assert cache.get(1) is None

    cache.get(2)  # 3 4 2
    cache.put(5, 0)  # 4 2 5
    algorithm.index_list.log()
    assert cache.get(3) is None

    cache.put(4, 0)  # 2 5 4
    cache.put(3, 0)  # 5 4 3
    algorithm.index_list.log()
    assert cache.get(2) is None
Example #16
0
    def __init__(self, competition):
        self.competition = competition

        self._htmlscraper = HTMLScraper()
        self._cache = Cache()
        self._wca_event_dict = {
            "2x2x2": "222",
            "3x3x3": "333",
            "3x3x3 One-Handed": "333oh",
            "3x3x3 Blindfolded": "333bf",
            "4x4x4 Blindfolded": "444bf",
            "5x5x5 Blindfolded": "555bf",
            "3x3x3 Fewest Moves": "333fm",
            "3x3x3 With Feet": "333wf",
            "3x3x3 Multi-Blind": "333mbd",
            "4x4x4": "444",
            "5x5x5": "555",
            "6x6x6": "666",
            "7x7x7": "777",
            "Pyraminx": "pyram",
            "Skewb": "skewb",
            "Megaminx": "minx",
            "Square-1": "sq1",
            "Clock": "clock"
        }
Example #17
0
 def __init__(self):
     reload(sys)
     sys.setdefaultencoding('UTF-8')
     self.config = Config()
     self.datatype = 'main'
     self.title = '网易云音乐'
     self.datalist = [
         '排行榜', '艺术家', '新碟上架', '精选歌单', '我的歌单', 'DJ节目', '每日推荐', '私人FM', '搜索',
         '帮助'
     ]
     self.offset = 0
     self.index = 0
     self.storage = Storage()
     self.storage.load()
     self.collection = self.storage.database['collections'][0]
     self.player = Player()
     self.player.playing_song_changed_callback = self.song_changed_callback
     self.cache = Cache()
     self.ui = Ui()
     self.netease = NetEase()
     self.screen = curses.initscr()
     self.screen.keypad(1)
     self.step = 10
     self.stack = []
     self.djstack = []
     self.userid = self.storage.database["user"]["user_id"]
     self.username = self.storage.database["user"]["nickname"]
     self.resume_play = True
     self.at_playing_list = False
     signal.signal(signal.SIGWINCH, self.change_term)
     signal.signal(signal.SIGINT, self.send_kill)
     self.START = time.time()
def blocking_generator_function(resource, layer, schedule=None ,verbose=False):

    '''
    Generate all possible loop tilings for each loop,
    store them in one list. 
    '''

    hint = schedule.schedule_hint if schedule != None else None

    num_levels = resource.buffer_levels()

    all_tile_permutations = []
    for i in xrange(le.NUM):
        loop_hint = hint[i] if hint and i in hint else None
        all_tile_permutations.append(loop_tile(layer.sizes[i], num_levels, loop_hint))

    '''
    Generate all possible loop tilings for all loops,
    then transform the data organizations to match with loop_blocking_list 
    Use cache to buffer the valid status of blocking for the first level
    '''
    blocking_cache = Cache(1, 100)
    for tile in itertools.product(*all_tile_permutations):
        #TODO here the generated is a list of lists, not a list of tuples
        if opt_valid_blocking(blocking_cache, resource, layer, tile):
            yield list(tile)
Example #19
0
def delaware_scraper(name):
    print('Retrieving ' + name + "'s email...")
    first_name, last_name = split_name(name)
    cache = Cache()
    try:
        email = cache[name]
        return email
    except KeyError:
        pass
    query_link = DIRECTORIES.get('delaware')
    driver = get_driver()
    driver.get(query_link)
    driver.implicitly_wait(5)
    driver.find_element_by_id('lastName').send_keys(
        last_name)
    driver.find_element_by_id('firstName').send_keys(
        first_name)
    driver.find_element_by_css_selector('form button[type="submit"]').click()
    driver.implicitly_wait(5)
    time.sleep(3)
    try:
        email = driver.find_element_by_xpath('//div[contains(@role, "main")]//a[contains(@href, "mailto")]')
        email = email.text
        print(email)
    except selenium.common.exceptions.NoSuchElementException:
        email = None
    driver.quit()
    email = email if email else None
    cache[name] = email
    return email
Example #20
0
    def __init__(self, reader, writer, token):
        super().__init__(Platform.Steam, __version__, reader, writer, token)
        self._steam_id = None
        self._miniprofile_id = None
        self._level_db_parser = None
        self._regmon = get_steam_registry_monitor()
        self._local_games_cache: Optional[List[LocalGame]] = None
        self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
        self._ssl_context.load_verify_locations(certifi.where())
        self._http_client = AuthenticatedHttpClient()
        self._client = SteamHttpClient(self._http_client)
        self._persistent_storage_state = PersistentCacheState()
        self._servers_cache = ServersCache(self._client, self._ssl_context,
                                           self.persistent_cache,
                                           self._persistent_storage_state)
        self._friends_cache = FriendsCache()
        self._steam_client = WebSocketClient(self._client, self._ssl_context,
                                             self._servers_cache,
                                             self._friends_cache)
        self._achievements_cache = Cache()
        self._achievements_cache_updated = False

        self._achievements_semaphore = asyncio.Semaphore(20)
        self._tags_semaphore = asyncio.Semaphore(5)

        self._library_settings_import_iterator = 0
        self._game_tags_cache = {}

        self._update_task = None

        def user_presence_update_handler(user_id: str, user_info: UserInfo):
            self.update_user_presence(user_id, from_user_info(user_info))

        self._friends_cache.updated_handler = user_presence_update_handler
Example #21
0
def main():
    """ Main Routine """

    print("\n[.] Initializing parameters/settings for simulator...")
    print("[.] Values in brackets represent reccommended/tested values.")
    print("[.] Using untested values may result in unstable behavior.\n")
    # Ask for parameters
    user_debug = input("[?] Enable debugging information [No]: ")
    debug = (user_debug == "Yes")
    memory_size = input("[?] Size of main memory (bytes) [100]: ")
    virtual_memory_size = input("[?] Size of virtual memory (bytes) [8000]: ")
    cache_size = input("[?] Size of cache (bytes)[40]: ")
    block_size = input("[?] Size of cache blocks (bytes)[4]: ")
    page_size = input("[?] Size of disk pages (bytes)[32]: ")
    table_size = input("[?] Number of TLB table entries (bytes)[10]: ")

    # Initialize components with bus and debug flag
    bus = Bus(debug)
    cpu = CPU(debug)
    cache = Cache(int(cache_size), int(block_size), debug)
    tlb = TLB(int(table_size), debug)
    memory = Memory(int(memory_size), int(virtual_memory_size), debug)
    disk = Disk(int(page_size), debug)

    # Initialize GUI
    menu = GUI(bus, cpu, cache, tlb, memory, disk, debug)
    menu.menu_loop()
Example #22
0
class MEMtest:
    R = [0, 1, 2, 104, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    opcodeStr = ["STUR", "LDUR"]
    destReg = [4, -5]
    dataval = [0, 1, 2, 3, 4, 5, 6, 7]
    Src1Reg = [3, 3]
    Src2Reg = [0, 0]
    address = [96, 100, 104, 108, 112]
    numInstructs = 2
    preALUbuff = [-1, -1]
    postALUbuff = [-1, -1]
    preMEMbuff = [0, 1]
    postMEMbuff = [-1, -1]
    instructions = [11111000010000000001000010000001, 11111000000000000001000010000010]
    c = Cache(numInstructs, instructions, dataval, address)

    test = mem.MEM(c, R, opcodeStr, destReg, dataval, Src1Reg, Src2Reg, address, numInstructs, preALUbuff, postALUbuff, preMEMbuff,
                   postMEMbuff, instructions)
    print("MEMTEST INSTRUCTION INDEX 0: ", str(preMEMbuff[0]))
    print("MEMTEST INSTRUCTION INDEX 1: ", str(preMEMbuff[1]))
    test.run()
    print("MEMTEST RESULT:              ", str(postMEMbuff[1]))
    print("MEMTEST INSTRUCTION INDEX 0: ", str(preMEMbuff[0]))
    print("MEMTEST INSTRUCTION INDEX 1: ", str(preMEMbuff[1]))
    test.run()
    print("MEMTEST RESULT:              ", str(postMEMbuff[1]))
    print("MEMTEST INSTRUCTION INDEX 0: ", str(preMEMbuff[0]))
    print("MEMTEST INSTRUCTION INDEX 1: ", str(preMEMbuff[1]))
    test.run()
    print("MEMTEST RESULT:              ", str(postMEMbuff[1]))
Example #23
0
def get_html(url):
    # type: (str) -> Optional[BeautifulSoup]
    """Gets cached or live HTML from the url"""
    headers = {"Accept": "text/html", "Accept-encoding": "gzip"}
    with Cache() as c:
        cached = c.get(url)
        is_info = url.startswith(MIA_INFO_URI)
        if cached:
            # Always return cached info pages...
            if cached["fresh"] or is_info:
                return BeautifulSoup(cached["blob"], "html.parser")
            headers.update(conditional_headers(cached))
        r = requests.get(url, headers=headers, timeout=SEARCH_TIMEOUT)
        if 200 == r.status_code:
            # Always cache info pages without any query string
            headers = None if is_info else r.headers
            url = url.split("?")[0] if is_info else url
            soup = BeautifulSoup(r.content, "html.parser")
            c.set(url, r.content, headers)
            return soup
        if 304 == r.status_code:
            c.touch(url, r.headers)
            return BeautifulSoup(cached["blob"], "html.parser")
        logger.debug("get_html error: {} {}".format(r.status_code, url))
        return None
Example #24
0
def unlock(target):
    redis = Cache()
    if target in DEVICE_SET:
        manual_lock = LOCK_FORMAT % (target)
        redis.set(manual_lock, 0)
        return jsonify({'status_code': 200, 'data': True})
    return jsonify({'status_code': 400, 'error': 'Unsupported Target'})
 def __init__(self, parameter_store, stage_parameters,
              comparison_parameters):
     self.parameter_store = parameter_store
     self.stage_parameters = stage_parameters
     self.comparison_parameters = comparison_parameters
     self.sts = STS()
     self.cache = Cache()
Example #26
0
 def __init__(self, reader, writer, token):
     super().__init__(Platform.Psn, __version__, reader, writer, token)
     self._http_client = AuthenticatedHttpClient(self.lost_authentication,
                                                 self.store_credentials)
     self._psn_client = PSNClient(self._http_client)
     self._trophies_cache = Cache()
     logging.getLogger("urllib3").setLevel(logging.FATAL)
Example #27
0
    def __init__(self, args):
        """Intialize WebDeface."""

        if int(platform.sys.version_info[0]) < 3:  # if Python 2.X.X
            self.url = raw_input(">> Enter the URL of the website: ")
            self.thread = int(raw_input(">> Enter the number of threads: "))
        else:
            self.url = input(">> Enter the URL of the website: ")
            self.thread = int(input(">> Enter the number of threads: "))

        if (self.url is not None and
            deface_utils.verify_url(self.url)):
            # Create crawler object
            self.crawler_obj = Crawler(url=self.url,
                                       threads=self.thread)
            self.crawler_obj.threading_crawl()

        # Create a cache object
        self.cache_obj = Cache()
        self.cache_obj.generate_cache()

        # Arguments
        self.args = args

        # Initialize empty objects
        self.twitter_obj = None
        self.slack_obj = None
        self.telegram_obj = None
        self.twilio_sms_obj = None
Example #28
0
def test_lfu():
    algorithm = LFUCacheAlgorithm(max_item=3)
    cache = Cache(algorithm=algorithm)

    cache.put(1, 0)  # 1(0)
    cache.get(1)  # 1(1)
    algorithm.log()
    cache.put(2, 0)  # 1(1) 2(0)
    cache.put(3, 0)  # 1(1) 2(0) 3(0)
    algorithm.log()
    assert len(cache.data) == 3

    cache.put(4, 0)  # 1(1) 3(0) 4(0)
    algorithm.log()
    assert cache.get(2) is None

    for _ in range(0, 3):
        cache.get(4)  # 1(1) 3(0) 4(3)
    for _ in range(0, 2):
        cache.get(3)  # 1(1) 3(2) 4(3)

    for _ in range(0, 2):
        cache.put(5, 0)  # 3(2) 4(3) 5(1)
    algorithm.log()
    assert cache.get(1) is None
Example #29
0
    def test_cacheDoesNotRefreshUnderOneDay(self):
        cache = Cache()
        cache.put('k', 'v')
        self.assertEqual(cache.get('k'), 'v')

        fakedate = datetime.now() + timedelta(hours=23)
        self.assertEqual(cache.get('k', fakedate), 'v')
Example #30
0
def test_get_single(mock_cs, mock_redis):
    mock_cs.return_value.get = mock.Mock(return_value='bar')
    mock_redis.return_value.get = mock.Mock()
    cache = Cache('fake-host', 1234, None, 1, 3)
    assert cache.get('foo') == 'bar'
    mock_cs.return_value.get.assert_called_once_with('foo')
    mock_redis.return_value.get.assert_not_called()