Beispiel #1
0
 def get_resource_as_stream(resource: str, mode='r', **args):
     AssertUtils.assert_not_null("get_resource_as_stream", resource)
     try:
         return open(
             resource,
             mode=mode, **args)
     except FileNotFoundError as e:
         raise UtilityException(BasicCodeEnum.MSG_0003, "unable to read file {} because: {}".format(resource, e))
Beispiel #2
0
    def delete(path: str, deep=True):
        AssertUtils.assert_not_null("delete", path)

        if os.path.isdir(path):
            if deep:
                PathUtils._delete_directory(path)
                PathUtils._delete(path, True)
        else:
            PathUtils._delete(path, False)
Beispiel #3
0
 def to_byte_array(path: str):
     AssertUtils.assert_not_null("to_byte_array", path)
     try:
         with open(
                 file=path,
                 mode='rb') as f:
             return f.read()
     except Exception as e:
         msg = "unable to read file {} because: {}".format(path, e)
         raise UtilityException(BasicCodeEnum.MSG_0003, msg)
Beispiel #4
0
    def cast(target, set_type):
        if not set_type:
            return None
        AssertUtils.assert_not_null("cast", target)

        if not isinstance(target, set_type):
            msg = "class {} needs to implement class or interface {}".format(
                target, set_type)
            raise UtilityException(BasicCodeEnum.MSG_0004, msg)

        return target
Beispiel #5
0
    def copy_to_temp(data: bytes, prefix: str, call_fn=None):
        AssertUtils.assert_not_null("copy_to_temp", data)
        delete = True if call_fn else False

        with tempfile.NamedTemporaryFile(
                prefix=prefix,
                suffix=".tmp",
                delete=delete) as temp:
            temp.write(data)

            if call_fn:
                call_fn(temp.name, data)
            return temp.name
Beispiel #6
0
    def to_uri(set_uri: str, scheme=None):
        AssertUtils.assert_not_null("to_uri", set_uri)

        if scheme:
            if not set_uri.startswith("/"):
                set_uri = "/" + set_uri
            uri_p = URI(
                scheme=scheme,
                path=set_uri.replace(" ", "%20"))
        else:
            uri_p = URI(set_uri.replace(" ", "%20"))
        return uri_p if uri_p.scheme else URI(
            scheme="string",
            path=set_uri.replace(" ", "%20"))
Beispiel #7
0
    def copy_to_path(path: str, set_in, mode="wb", replace=True):
        AssertUtils.assert_not_null("copy_to_path", path, set_in)

        if replace and os.path.exists(path):
            PathUtils.delete(path, False)
        try:
            with open(path, mode) as out:
                data = set_in.read()
                out.write(data)
                return len(data)
        except Exception as e:
            msg = "unable to create file {} because: {}".format(path, e)
            raise UtilityException(BasicCodeEnum.MSG_0003, msg)
        finally:
            set_in.close()
Beispiel #8
0
    def load_class(classpath: str, name=None):
        AssertUtils.assert_not_null("load_class", classpath)
        pkg = None
        m_name = classpath
        c_name = name

        if not name:
            path = str(classpath).rsplit(".", 1)
            c_name = path[-1]
            m_name = path[0]
        # path = str(classpath).rsplit(".", 1 if name else 2)
        #
        # if not name and len(path) < 2:
        #     msg = "{} is not a valid path(e.g. [pkg.]module.Class)".format(classpath)
        #     raise AssertException(BasicCodeEnum.MSG_0005, msg)
        # c_name = name if name else path[-1]
        # m_name = path[-1] if name else path[-2]
        # pkg = path[0] if (name and len(path) > 1) or (not name and len(path) > 2) else None
        module = importlib.import_module(m_name, package=pkg)
        return getattr(module, c_name)
Beispiel #9
0
    def list_paths(path: str, set_filter=None, include_dir=False):
        AssertUtils.assert_not_null("list_paths", path)

        def def_filter_fn(target):
            return True if target else False

        set_filter = def_filter_fn if not set_filter else set_filter

        def loop_fn(arr, set_root, args):
            for i in args:
                target = os.path.join(set_root, i)

                if set_filter(target):
                    arr.append(target)
        tmp = []

        for root, dirs, files in os.walk(path):
            if include_dir:
                loop_fn(tmp, root, dirs)
            loop_fn(tmp, root, files)

        return tmp
Beispiel #10
0
 def cast(cls, plugin: IPlugin, clazz):
     AssertUtils.assert_not_null("cast", plugin, clazz)
     return ClassUtils.cast(plugin, clazz)
Beispiel #11
0
 def new_and_cast(clazz, set_type):
     if not set_type:
         return None
     AssertUtils.assert_not_null("new_and_cast", clazz)
     return ClassUtils.cast(clazz(), set_type)