forked from 0rpc/zerorpc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
35 lines (27 loc) · 884 Bytes
/
utils.py
File metadata and controls
35 lines (27 loc) · 884 Bytes
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
# -*- coding: utf-8 -*-
from importlib import import_module
__author__ = 'nemo'
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls, *args, **kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance
def get_mod_func(path):
try:
dot = path.rindex(':')
except ValueError:
try:
dot = path.rindex('.')
except ValueError:
return path, ''
return path[:dot], path[dot+1:]
def load_by_path(path):
mod_name, func_name = get_mod_func(path)
if func_name != '':
package = None
if path.startswith('.'):
package = __package__
return getattr(import_module(mod_name, package), func_name)