How to use the portalocker.utils.Lock function in portalocker

To help you get started, we’ve selected a few portalocker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / azure-cli / src / azure-cli-telemetry / azure / cli / telemetry / components / telemetry_note.py View on Github external
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import time
import datetime

import portalocker.utils


class TelemetryNote(portalocker.utils.Lock):
    def __init__(self, config_dir):
        from azure.cli.telemetry.components.telemetry_logging import get_logger

        self._path = self.get_file_path(config_dir)
        self._logger = get_logger('note')

        if not os.path.exists(self._path):
            super(TelemetryNote, self).__init__(self._path, mode='w', timeout=0.1, fail_when_locked=True)
        else:
            super(TelemetryNote, self).__init__(self._path, mode='r+', timeout=1, fail_when_locked=True)

    @staticmethod
    def get_file_path(config_dir):
        from azure.cli.telemetry.const import TELEMETRY_NOTE_NAME
        return os.path.join(config_dir, TELEMETRY_NOTE_NAME)