def handle_json_file(cls, file: PathStr, encoding: str = "utf-8", encoders: Iterable[EncoderType] = ALL_ENCODERS, **kwargs) -> None: """Handles a json-file""" file = pstr(file) with file.open("r", encoding=encoding) as file: packages = json.load(file) found = [] for package in packages: # Get values data, information, encoder = itemgetter("data", "information", "encoder")(package) encoder = cls._find_encoder(encoder, encoders=encoders) found.append({ "data": data, "information": information, "encoder": encoder }) cls.handle_packed_data(found, **kwargs)
def __init__(self, host, port='8021', auth='ClueCon', locked=True, lock=None): """ Parameters ----------- host : string host name or ip address for server hosting an esl connection. port : string port where esl connection socket is being offered. auth : string authentication password for esl connection. locked : bool indicates whether to return a thread safe derivative of the default ESLConnection class. lock : instance of mp.Lock a lock implementation which the connection will utilize when serializing accesses from multiple threads (requires locked=True) """ self.host = host self.port = port self.auth = auth self.log = utils.get_logger(utils.pstr(self)) self._sub = () # events subscription if locked: self._mutex = lock or mp.Lock() # don't connect by default self._con = False
def encode_file(cls, file: PathStr, output: Optional[PathStr] = None, **kwargs) -> str: # Constrain values file = pstr(file) data = cls.get_encoded_data(file, **kwargs) cls.create_video(data, output) return data
def encode_text_from_file(cls, path: str, output: Optional[PathStr] = None, *, encoding: str = "utf-8", **kwargs) -> str: # Constrain values path = pstr(path) with path.open("r", encoding=encoding) as file: data = file.read() return cls.encode_text(data, output, **kwargs)
def encode_folder( cls, folder: PathStr, output: Optional[PathStr] = None, *, folder_glob: str = "*", **kwargs, ) -> str: # Constrain values folder = pstr(folder) output = pstrnone(output) information_opts = kwargs.pop("information_opts", {}) if (key := "relative_to") not in information_opts: information_opts[key] = folder.parent.absolute()
def decode_video(cls, path: PathStr) -> str: """Decodes a video and returns it`s data""" # Constrain values path = pstr(path) # Video cap = VideoCapture(str(path)) frames = int(cap.get(CAP_PROP_FRAME_COUNT)) found: str = "" for frame in tqdm(cls._get_video_frames(cap), desc="Reading video", total=frames): data = cls.decode_qr(frame) found += data return found
def encode_multiple(cls, targets: Iterator[PathStr], output: Optional[PathStr] = None, **kwargs) -> str: # Constrain values targets: List[Path] = list(map(lambda x: pstr(x), targets)) # Collect data data: str = "" for target in tqdm(targets, desc="Collecting data", total=len(targets)): if target.is_dir(): files = {x for x in target.rglob("*/*") if x.is_file()} data += "".join( list(cls.collect_data_from_files(files, **kwargs))) else: data += cls.get_encoded_data(target, **kwargs) cls.create_video(data, output) return data
def __init__(self, host='127.0.0.1', port='8021', auth='ClueCon', listener=None, logger=None): self.host = self.server = host self.port = port self.auth = auth self._id = utils.uuid() self._orig_cmd = None self.log = logger or utils.get_logger(utils.pstr(self)) # generally speaking clients should only host one call app self._apps = {} self.apps = type('apps', (), {})() self.apps.__dict__ = self._apps # dot-access to `_apps` from `apps` self.client = self # for app funcarg insertion # WARNING: order of these next steps matters! # create a local connection for sending commands self._con = Connection(self.server, self.port, self.auth) # if the listener is provided it is expected that the # user will run the set up methods (i.e. connect, start, etc..) self.listener = listener
def __init__(self, host='127.0.0.1', port='8021', auth='ClueCon', listener=None, logger=None): self.host = self.server = host self.port = port self.auth = auth self._id = utils.uuid() self._orig_cmd = None self.log = logger or utils.get_logger(utils.pstr(self)) # generally speaking clients should only host one call app self._apps = {} self.apps = type('apps', (), {})() self.apps.__dict__ = self._apps # dot-access to apps from 'apps' attr self.client = self # for app funcarg insertion # WARNING: order of these next steps matters! # create a local connection for sending commands self._con = Connection(self.server, self.port, self.auth) # if the listener is provided it is expected that the # user will run the set up methods (i.e. connect, start, etc..) self.listener = listener
def __init__(self, host=HOST, port=PORT, auth=AUTH, session_map=None, bg_jobs=None, rx_con=None, call_corr_var_name='variable_call_uuid', call_corr_xheader_name='originating_session_uuid', autorecon=30, max_limit=float('inf'), # proxy_mng=None, _tx_lock=None): ''' Parameters ---------- host : string Hostname or ip of the FS engine server to listen to port : string Port on which the FS server is offering an esl connection auth : string Authentication password for connecting via esl call_corr_var_name : string Name of the freeswitch variable (without the 'variable_' prefix) to use for associating sessions into calls (see _handle_create). call_corr_xheader_name : string Name of an Xheader which can be used to associate sessions into calls. This is useful if an intermediary device such as a B2BUA is being tested and is the first hop receiving requests. Note in order for this association mechanism to work the intermediary device must be configured to forward the Xheaders it recieves. (see `self._handle_create` for more details) autorecon : int, bool Enable reconnection attempts on server disconnect. An integer value specifies the of number seconds to spend re-trying the connection before bailing. A bool of 'True' will poll indefinitely and 'False' will not poll at all. ''' self.server = host self.port = port self.auth = auth self._sessions = session_map or OrderedDict() self._bg_jobs = bg_jobs or OrderedDict() self._calls = OrderedDict() # maps aleg uuids to Sessions instances self.hangup_causes = Counter() # record of causes by category self.failed_sessions = OrderedDict() self._handlers = self.default_handlers # active handler set self._unsub = () self.consumers = {} # callback chains, one for each event type self._waiters = {} # holds events being waited on self._blockers = [] # holds cached events for reuse # store up to the last 10k of each event type self.events = defaultdict(functools.partial(deque, maxlen=1e3)) # constants self.autorecon = autorecon self._call_var = None self.call_corr_var = call_corr_var_name self.set_xheader_var(call_corr_xheader_name) self.max_limit = max_limit self._id = utils.uuid() # if a mng is provided then assume this listener will # be instantiated as a shared object and thus will require # at least one shared lock for the tx connection (since # it is used by internally by Session instances) # if proxy_mng: # self._tx_lock = proxy_mng.Lock() if proxy_mng else None # self._tx_lock = _tx_lock self._shared = False # sync self._exit = mp.Event() # indicate when event loop should terminate self._lookup_blocker = mp.Event() # used block event loop temporarily self._lookup_blocker.set() self.log = utils.get_logger(utils.pstr(self)) self._epoch = self._fs_time = 0.0 # set up contained connections self._rx_con = rx_con or Connection(self.server, self.port, self.auth) self._tx_con = Connection(self.server, self.port, self.auth) # mockup thread self._thread = None self.reset()