コード例 #1
0
ファイル: thread_lock.py プロジェクト: dNG-git/dpt_threading
    def acquire(self):
        """
Acquire a lock.

:since: v1.0.0
        """

        # pylint: disable=unexpected-keyword-arg

        try:
            if (not self.lock.acquire(timeout=self.timeout)):
                raise IOException("Timeout occurred while acquiring lock")
        except TypeError:
            if (self.event is None):
                self.event = Event()
                self.event.set()
            #

            if (self.lock.acquire(False)): self.event.clear()
            else:
                timeout = self.timeout

                while (timeout > 0):
                    _time = time()
                    self.event.wait(timeout)

                    if (self.lock.acquire(False)):
                        self.event.clear()
                        break
                    else:
                        timeout -= (time() - _time)
                #

                if (timeout <= 0):
                    raise IOException("Timeout occurred while acquiring lock")
コード例 #2
0
    def scan(self):
        """
Scan over objects of a collection like a directory.

:return: (list) Child VFS objects
:since:  v1.0.0
        """

        if (self.file_path_name is not None): raise OperationNotSupportedException("VFS object can not be scanned")
        if (self.dir_path_name is None): raise IOException("VFS object not opened")

        _return = [ ]

        entry_list = os.listdir(self.dir_path_name)
        entry_list.sort()

        dir_path_url = self.url

        for entry in entry_list:
            if (entry[0] != "."):
                vfs_child_object = Object()

                try:
                    vfs_child_object.open("{0}/{1}".format(dir_path_url, entry), self.object_readonly)
                    _return.append(vfs_child_object)
                except IOException as handled_exception: LogLine.error(handled_exception, context = "dpt_vfs")
            #
        #

        return _return
コード例 #3
0
    def get_class(scheme):
        """
Returns an VFS watcher class for the given scheme.

:return: (object) VFS watcher class
:since:  v1.0.0
        """

        with WatcherImplementation._lock:
            if (WatcherImplementation._is_disabled):
                raise OperationNotSupportedException(
                    "VFS watcher creation has been disabled")

            _return = WatcherImplementation._classes.get(scheme)

            if (_return is None):
                _return = NamedClassLoader.get_class_in_namespace(
                    "dpt_vfs", "{0}.Watcher".format(scheme.replace("-", "_")))

                if (issubclass(_return, AbstractWatcher)):
                    WatcherImplementation._classes[scheme] = _return
                else:
                    _return = None
            #
        #

        if (_return is None):
            raise IOException(
                "VFS watcher not defined for URL scheme '{0}'".format(scheme))

        return _return
コード例 #4
0
    def _read_file(file_path_name, required):
        """
Read data from the given file or from cache.

:param file_path_name: File path and name
:param required: True if missing files should throw an exception

:return: (mixed) File data; None on error
:since:  v1.0.0
        """

        _return = None

        file_object = File()

        if (file_object.open(file_path_name, True, "r")):
            _return = file_object.read()
            file_object.close()

            if (_return is not None): _return = _return.replace("\r", "")
        elif (required):
            raise IOException("{0} not found".format(file_path_name))
        else:
            LogLine.debug("{0} not found", file_path_name, context="dpt_cache")

        return _return
コード例 #5
0
    def __getattribute__(self, name):
        """
python.org: Called unconditionally to implement attribute accesses for
instances of the class.

:param name: Attribute name

:return: (mixed) Instance attribute
:since:  v1.0.0
        """

        # pylint: disable=protected-access

        if (name == "__class__"
                or name not in self.__class__._FILE_WRAPPED_METHODS):
            _return = object.__getattribute__(self, name)
        else:
            if (self._wrapped_resource is None): self._open_wrapped_resource()
            if (self._wrapped_resource is None):
                raise IOException("'{0}' not available for {1!r}".format(
                    name, self))

            _return = getattr(self._wrapped_resource, name)
        #

        return _return
コード例 #6
0
    def result(self):
        """
Returns the result being set previously.

:return: (mixed) Result set
:since:  v1.0.0
        """

        if (not self.result_set):
            raise IOException("No result has been set for this ResultEvent.")
        return self._result
コード例 #7
0
ファイル: abstract.py プロジェクト: dNG-git/dpt_vfs
    def mimetype(self):
        """
Returns the mime type of this VFS object.

:return: (str) VFS object mime type
:since:  v1.0.0
        """

        if (not self.is_valid): raise IOException("VFS object not opened")
        return ("text/directory" if
                (self.is_directory) else "application/octet-stream")
コード例 #8
0
    def _ensure_directory_writable(self, vfs_url, dir_path_name):
        """
Ensures that the given directory path writable.

:param vfs_url: VFS URL
:param dir_path_name: Directory path and name

:since: v1.0.0
        """

        if (not os.access(dir_path_name, os.X_OK)): raise IOException("VFS URL '{0}' is invalid".format(vfs_url))
コード例 #9
0
    def clear(self):
        """
python.org: Reset the internal flag to false.

:since: v1.0.0
        """

        if (self.result_set):
            raise IOException(
                "A ResultEvent can not be cleared after a result was set.")
        Event.clear(self)
コード例 #10
0
ファイル: abstract.py プロジェクト: dNG-git/dpt_vfs
    def implementing_instance(self):
        """
Returns the implementing instance.

:return: (mixed) Implementing instance or "None"
:since:  v1.0.0
        """

        # pylint: disable=bad-option-value,useless-return

        if (not self.is_valid): raise IOException("VFS object not opened")
        return None
コード例 #11
0
    def _import_with_imp(package, module):
        """
Imports the Python module with "imp".

:param package: Package name
:param module: Module name

:return: (object) Python module; None if unknown
:since:  v1.0.0
        """

        _return = None

        base_path = None
        base_path_entry = ""
        package_directory = None
        package_module_name = module

        if (package is None):
            base_path_entry = "{0}.py".format(module)
        else:
            base_path_entry = package.split(".", 1)[0]
            package_directory = package.replace(".", path.sep)
            package_module_name = "{0}.{1}".format(package, module)
        #

        if (base_path_entry == ""):
            raise IOException(
                "Import base path '{0}' is empty".format(base_path_entry))

        for _path in Loader.get_base_dirs():
            if (os.access(path.join(_path, base_path_entry), os.R_OK)):
                base_path = _path
                break
            #
        #

        if (base_path is not None and package_directory is not None):
            base_path = path.join(base_path, package_directory)
        #

        if (base_path is not None):
            with _imp_lock():
                (file_obj, file_path,
                 description) = imp.find_module(module, [base_path])
                _return = imp.load_module(package_module_name, file_obj,
                                          file_path, description)
                if (file_obj is not None): file_obj.close()
            #
        #

        return _return
コード例 #12
0
    def _open_wrapped_resource(self):
        """
Opens the wrapped resource once needed for an file IO request.

:since: v1.0.0
        """

        if (self.file_path_name is None): raise IOException("VFS object not opened")

        file_mode = ("rb" if (self.object_readonly) else "r+b")

        _file = File()
        if (_file.open(self.file_path_name, self.object_readonly, file_mode)): self._set_wrapped_resource(_file)
コード例 #13
0
    def filesystem_path_name(self):
        """
Returns the path and name for the VFS object in the system filesystem.

:return: (str) System filesystem path and name of the VFS object
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = self.dir_path_name
        elif (self.file_path_name is not None): _return = self.file_path_name
        else: raise IOException("VFS object not opened")

        return _return
コード例 #14
0
    def type(self):
        """
Returns the type of this object.

:return: (int) Object type
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = Object.TYPE_DIRECTORY
        elif (self.file_path_name is not None): _return = Object.TYPE_FILE
        else: raise IOException("VFS object not opened")

        return _return
コード例 #15
0
    def size(self):
        """
Returns the size in bytes.

:return: (int) Size in bytes
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = 0
        elif (self.file_path_name is not None): _return = os.stat(self.file_path_name).st_size
        else: raise IOException("VFS object not opened")

        return _return
コード例 #16
0
    def time_updated(self):
        """
Returns the UNIX timestamp this object was updated.

:return: (int) UNIX timestamp this object was updated
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = os.stat(self.dir_path_name).st_mtime
        elif (self.file_path_name is not None): _return = os.stat(self.file_path_name).st_mtime
        else: raise IOException("VFS object not opened")

        return _return
コード例 #17
0
    def name(self):
        """
Returns the name of this VFS object.

:return: (str) VFS object name
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = path.basename(self.dir_path_name)
        elif (self.file_path_name is not None): _return = path.basename(self.file_path_name)
        else: raise IOException("VFS object not opened")

        return _return
コード例 #18
0
    def get_class(scheme):
        """
Returns an VFS object class for the given scheme.

:return: (object) VFS object class
:since:  v1.0.0
        """

        _return = NamedClassLoader.get_class_in_namespace(
            "dpt_vfs", "{0}.Object".format(scheme.replace("-", "_")))

        if (_return is None or (not issubclass(_return, Abstract))):
            raise IOException(
                "VFS object not defined for URL scheme '{0}'".format(scheme))

        return _return
コード例 #19
0
    def url(self):
        """
Returns the URL of this VFS object.

:return: (str) VFS URL
:since:  v1.0.0
        """

        object_id = None

        if (self.dir_path_name is not None): object_id = quote_plus(self.dir_path_name, "/")
        elif (self.file_path_name is not None): object_id = quote_plus(self.file_path_name, "/")

        if (object_id is None): raise IOException("VFS object not opened")

        return "file:///{0}".format(object_id)
コード例 #20
0
    def open(self, vfs_url, readonly = False):
        """
Opens a VFS object. The handle is set at the beginning of the object.

:param vfs_url: VFS URL
:param readonly: Open object in readonly mode

:since: v1.0.0
        """

        if (self.dir_path_name is not None
            or self.file_path_name is not None
           ): raise IOException("Can't create new VFS object on already opened instance")

        object_path_name = unquote_plus(Abstract._get_id_from_vfs_url(vfs_url))

        if (path.isdir(object_path_name)): self._open_directory(vfs_url, object_path_name, readonly)
        else: self._open_file(vfs_url, object_path_name, readonly)
コード例 #21
0
    def implementing_instance(self):
        """
Returns the implementing instance.

:return: (mixed) Implementing instance
:since:  v1.0.0
        """

        _return = None

        if (self._wrapped_resource is None
            and self.file_path_name is not None
            ): self._open_wrapped_resource()

        if (self._wrapped_resource is not None): _return = self._wrapped_resource
        elif (self.dir_path_name is None): raise IOException("VFS object not opened")

        return _return
コード例 #22
0
    def mimetype(self):
        """
Returns the mime type of this VFS object.

:return: (str) VFS object mime type
:since:  v1.0.0
        """

        _return = None

        if (self.dir_path_name is not None): _return = "text/directory"
        elif (self.file_path_name is not None):
            file_data = path.splitext(self.file_path_name)
            mimetype_definition = MimeType.get_instance().get(file_data[1][1:])

            _return = ("application/octet-stream" if (mimetype_definition is None) else mimetype_definition['type'])
        else: raise IOException("VFS object not opened")

        return _return
コード例 #23
0
    def get_class_in_namespace(namespace_package, _class, autoload = True):
        """
Get the class located in the specified namespace, package and module name.

:param namespace_package: Namespace package name
:param _class: Package, module and class name
:param autoload: True to load the class module automatically if not done
                 already.

:return: (object) Loaded class; None on error
:since:  v1.0.0
        """

        class_data = _class.rsplit(":", 1)
        if (len(class_data) < 2): raise IOException("Class '{0}' given is invalid".format(_class))

        class_name = class_data[1]
        module = Loader.get_module_in_namespace(namespace_package, class_data[0], autoload)

        return (None if (module is None) else getattr(module, class_name, None))
コード例 #24
0
    def get_instance(_class, required = True, **kwargs):
        """
Returns a new instance based on the specified package, module and class
name.

:param _class: Package, module and class name
:param required: True if exceptions should be thrown if the class is not
                 defined.

:return: (object) Requested object on success
:since:  v1.0.0
        """

        _return = None

        instance_class = ClassLoader.get_class(_class)

        if (instance_class is not None and issubclass(instance_class, object)): _return = instance_class(**kwargs)
        if (_return is None and required): raise IOException("Class '{0}' is not defined".format(_class))

        return _return
コード例 #25
0
    def get_class(_class, autoload = True):
        """
Get the class for the specified package, module and class name.

:param _class: Package, module and class name
:param autoload: True to load the class module automatically if not done
                 already.

:return: (object) Loaded class; None on error
:since:  v1.0.0
        """

        loader = ClassLoader.get_class_loader_instance()
        if (loader.is_registered(_class)): _class = loader.get(_class)

        class_data = _class.rsplit(":", 1)
        if (len(class_data) < 2): raise IOException("Class '{0}' given is invalid".format(_class))

        class_name = class_data[1]
        module = Loader.get_module(class_data[0], autoload)

        return (None if (module is None) else getattr(module, class_name, None))
コード例 #26
0
    def get_singleton(_class, required = True, **kwargs):
        """
Returns a singleton based on the specified package, module and class name.

:param _class: Package, module and class name
:param required: True if exceptions should be thrown if the class is not
                 defined.

:return: (object) Requested object on success
:since:  v1.0.0
        """

        _return = None

        singleton_class = ClassLoader.get_class(_class)

        if (singleton_class is None and required): raise IOException("Class '{0}' is not defined".format(_class))

        if (hasattr(singleton_class, "get_instance")
            and callable(singleton_class.get_instance)
           ): _return = singleton_class.get_instance(**kwargs)
        elif (required): raise TypeException("{0} has not defined a singleton".format(_class))

        return _return