def get_src_path(obj, src_root='tefla', append_base=True):
"""Creates a src path string with line info for use as markdown link.
"""
path = getsourcefile(obj)
if not src_root in path:
# this can happen with e.g.
# inlinefunc-wrapped functions
if hasattr(obj, "__module__"):
path = "%s.%s" % (obj.__module__, obj.__name__)
else:
path = obj.__name__
path = path.replace(".", "/")
try:
pre, post = path.rsplit(src_root + "/", 1)
except:
pre, post = '', ''
lineno = get_line_no(obj)
lineno = "" if lineno is None else "#L{}".format(lineno)
path = src_root + "/" + post + lineno
if append_base:
path = os.path.join(
'https://github.com/openagi/tefla/blob/master', path)
return path
python类rsplit()的实例源码
def gen_package_pickled_dic(path, module_name):
modules_dic={}
start_path=module_name.replace(".", "/")
search_path=os.path.dirname(path)
logging.info("embedding %s ..."%os.path.join(search_path, start_path))
#TODO: remove comments from python files || compile to .pyc to make payloads lighter
if os.path.isdir(path):
for root, dirs, files in os.walk(os.path.join(search_path, start_path)):
for f in files:
module_code=""
with open(os.path.join(root,f),'rb') as fd:
module_code=fd.read()
modprefix = root[len(search_path.rstrip(os.sep))+1:]
modpath = os.path.join(modprefix,f).replace("\\","/")
modules_dic[modpath]=module_code
elif os.path.isfile(path):
ext=path.rsplit(".",1)[1]
module_code=""
with open(path,'rb') as f:
module_code=f.read()
cur=""
for rep in start_path.split("/")[:-1]:
if not cur+rep+"/__init__.py" in modules_dic:
modules_dic[rep+"/__init__.py"]=""
cur+=rep+"/"
modules_dic[start_path+"."+ext]=module_code
if not modules_dic:
raise NameError("path %s not found"%path)
return modules_dic
def gen_package_pickled_dic(path, module_name):
modules_dic={}
start_path=module_name.replace(".", "/")
search_path=os.path.dirname(path)
logging.info("embedding %s ..."%os.path.join(search_path, start_path))
#TODO: remove comments from python files || compile to .pyc to make payloads lighter
if os.path.isdir(path):
for root, dirs, files in os.walk(os.path.join(search_path, start_path)):
for f in files:
module_code=""
with open(os.path.join(root,f),'rb') as fd:
module_code=fd.read()
modprefix = root[len(search_path.rstrip(os.sep))+1:]
modpath = os.path.join(modprefix,f).replace("\\","/")
modules_dic[modpath]=module_code
elif os.path.isfile(path):
ext=path.rsplit(".",1)[1]
module_code=""
with open(path,'rb') as f:
module_code=f.read()
cur=""
for rep in start_path.split("/")[:-1]:
if not cur+rep+"/__init__.py" in modules_dic:
modules_dic[rep+"/__init__.py"]=""
cur+=rep+"/"
modules_dic[start_path+"."+ext]=module_code
if not modules_dic:
raise NameError("path %s not found"%path)
return modules_dic
def can_resolve(self, uri_path, from_resource=None):
uri_path = Resource.normalize(uri_path)
fragment = uri_path.rsplit('#', maxsplit=1)
if len(fragment) == 2:
uri_str, fragment = fragment
else:
return False
if uri_str in self.resources:
return True
start = from_resource.uri.normalize() if from_resource else '.'
apath = path.dirname(start)
uri = URI(path.join(apath, uri_str))
return uri.normalize() in self.resources
def resolve(self, uri, from_resource=None):
upath = Resource.normalize(uri)
uri_str, fragment = upath.rsplit('#', maxsplit=1)
if uri_str in self.resources:
return Resource._navigate_from(fragment, self.resources[uri_str])
start = from_resource.uri.normalize() if from_resource else '.'
apath = path.dirname(start)
uri = URI(path.join(apath, uri_str))
epackage = self.resources[uri.normalize()]
if isinstance(epackage, Resource):
epackage = epackage.contents[0]
return Resource._navigate_from(fragment, epackage)
def split_path(path):
path = Resource.normalize(path)
fragment = path.rsplit('#', maxsplit=1)
if len(fragment) == 2:
uri, fragment = fragment
else:
uri = None
return uri, fragment
def resolve(path, registry):
path = Resource.normalize(path)
uri, fragment = path.rsplit('#', maxsplit=1)
epackage = registry[uri]
return Resource._navigate_from(fragment, epackage)
def _is_external(self, path):
path = self.normalize(path)
uri, fragment = (path.rsplit('#', maxsplit=1)
if '#' in path else (None, path))
return uri, fragment
def namespace_path_split(path):
'''Split the namespace path into a pair (head, tail).
Tail will be the last namespace path component and head will
be everything leading up to that in the path. This is similar to
os.path.split.
:param path: (String) A namespace path.
:returns: (String, String) A tuple where the first component is the base
path and the second is the last path component.
'''
return tuple(path.rsplit('.', 1))