Source code for core.utils.other
# core/utils/other.py
#
#
""" utils package. """
__copyright__ = "Copyright 2015, B.H.J Thate"
## IMPORTS
from core.defines import YELLOW, ENDC
import logging
import _thread
import sys
import re
import os
run_thr = _thread.start_new_thread
## LOCATING
[docs]def get_source(mod, package):
import pkg_resources as p
source = os.path.abspath(p.resource_filename(mod, package))
logging.warn("# source %s" % source)
return source
[docs]def stripbadchar(s): return "".join([c for c in s if ord(c) > 31 or c in allowedchars])
[docs]def enc_char(s):
result = []
for c in s:
if c in allowedchars: result.append(c)
else: result.append(enc_name(c))
return "".join(result)
[docs]def enc_needed(s): return [c for c in s if c not in allowedchars]
[docs]def enc_name(input): return str(base64.urlsafe_b64encode(bytes(input, "utf-8")), "utf-8")
[docs]def split_txt(what, l=375):
txtlist = []
start = 0
end = l
length = len(what)
for i in range(int(length/end+1)):
endword = what.find(' ', end)
if endword == -1: endword = length
res = what[start:endword]
if res: txtlist.append(res)
start = endword
end = start + l
return txtlist
[docs]def copyright(): return "Copyright 2015, B.H.J Thate"
[docs]def hello(*args, **kwargs):
from core import __version__
version = kwargs.get("version", __version__)
if "colors" in kwargs and kwargs["colors"]: print("%s%s #%s%s\n" % (YELLOW, args[0], version, ENDC))
else: print("%s %s\n" % (args[0], version))
[docs]def list_eggs(filter="core"):
for f in sys.path:
if ".egg" not in f: continue
if filter and filter not in f: continue
yield f
[docs]def show_eggs(filter="core"):
path = list(list_eggs(filter))[0]
logging.warn("# egg %s" % path)
[docs]def stripped(input):
try: return input.split("/")[0]
except: return input
## HEADER
headertxt = '''# this is an CORE file, %s
#
# this file can be edited !!
'''
## FEEDER
[docs]def feed(text):
from core.ding import Thing
result = []
chunks = text.split("\r\n")
for chunk in chunks:
obj = Thing().feed(chunk)
result.append(obj)
return result
## SED
[docs]def run_sed(filename, sedstring):
""" replace oldcore strings with the new one. """
logging.warn("# %s verwisseld" % filename)
f = open(filename, 'r')
tmp = filename + '.tmp'
fout = open(tmp, 'w')
if sedstring:
char = "#"
seds = sedstring.split(char)
fr = seds[1]
to = seds[2]
for line in f:
l = re.sub(fr, to, line)
fout.write(l)
else:
for line in f:
l = re.sub("\t", " ", line.rstrip() + "\n")
fout.write(l)
fout.flush()
fout.close()
try: os.rename(tmp, filename)
except WindowsError: os.remove(filename) ; os.rename(tmp, filename)