File: cache.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (43 lines) | stat: -rw-r--r-- 1,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
# License: https://www.pysnmp.com/pysnmp/license.html
#
from typing import Any, Dict


from pysnmp import nextid
from pysnmp.proto import error


class Cache:
    """SNMP securityData cache."""

    __state_reference = nextid.Integer(0xFFFFFF)
    __cache_entries: Dict[int, Any]

    def __init__(self):
        """Create a cache object."""
        self.__cache_entries = {}

    def push(self, **securityData):
        """Push securityData into cache."""
        stateReference = self.__state_reference()
        self.__cache_entries[stateReference] = securityData
        return stateReference

    def pop(self, stateReference):
        """Pop securityData from cache."""
        if stateReference in self.__cache_entries:
            securityData = self.__cache_entries[stateReference]
        else:
            raise error.ProtocolError(
                f"Cache miss for stateReference={stateReference} at {self}"
            )
        del self.__cache_entries[stateReference]
        return securityData

    def is_empty(self):
        """Return True if cache is empty."""
        return not bool(self.__cache_entries)