Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def findT(path, language='en-us'):
"""
must be run by the admin app
"""
filename = os.path.join(path, 'languages', '%s.py' % language)
sentences = read_dict(filename)
mp = os.path.join(path, 'models')
cp = os.path.join(path, 'controllers')
vp = os.path.join(path, 'views')
for file in listdir(mp, '.+\.py', 0) + listdir(cp, '.+\.py', 0)\
+ listdir(vp, '.+\.html', 0):
fp = open(file, 'r')
portalocker.lock(fp, portalocker.LOCK_SH)
data = fp.read()
portalocker.unlock(fp)
fp.close()
items = regex_translate.findall(data)
for item in items:
try:
message = eval(item)
if not message.startswith('#') and not '\n' in message:
tokens = message.rsplit('##', 1)
else:
# this allows markmin syntax in translations
tokens = [message]
if len(tokens) == 2:
message = tokens[0].strip() + '##' + tokens[1].strip()
if message and not message in sentences:
sentences[message] = message
def get_created():
with portalocker.Lock(CREATED_FILE, mode='r', truncate=None,
flags=portalocker.LOCK_SH, timeout=1) as f:
return json.load(f)
def _get_flags(self):
if self.exclusive:
return portalocker.LOCK_NB | portalocker.LOCK_EX
else:
return portalocker.LOCK_NB | portalocker.LOCK_SH
def load_json_or_empty(path, default=None, kind=None, lock=False):
"""
Load the contents of the given file as a JSON and return it's value,
or default if the file can't be loaded.
"""
ret = default
try:
with io.open(path, 'r') as handle:
if lock:
portalocker.lock(handle, portalocker.LOCK_SH)
ret = json.loads(handle.read())
if lock:
portalocker.unlock(handle)
except IOError as ex:
LOG.warning("Failed to open %s file: %s",
kind if kind else 'json',
path)
LOG.warning(ex)
except OSError as ex:
LOG.warning("Failed to open %s file: %s",
kind if kind else 'json',
path)
LOG.warning(ex)
except ValueError as ex:
def __init__(self, filename):
self.lock_file = filename + ".lock"
self.filename = filename
self.f = None
self.lock = portalocker.Lock(
self.lock_file, "r", flags=portalocker.LOCK_SH, timeout=1
)
if not os.path.exists(self.lock_file):
f = open(self.lock_file, "w+")
f.close()
def load_precompiled(self, file):
""" Load precompiled template from disk.
Remove the precompiled template file and recompile it
if the file contains corrupted or unpicklable data.
@hidden
"""
filename = file + "c" # "template.tmplc"
self.DEB("LOADING PRECOMPILED")
try:
remove_bad = 0
file = None
try:
file = open(filename, "rb")
portalocker.lock(file, portalocker.LOCK_SH)
precompiled = cPickle.load(file)
except IOError, (errno, errstr):
raise TemplateError, "IO error in load precompiled "\
"template '%s': (%d) %s"\
% (filename, errno, errstr)
except cPickle.UnpicklingError:
remove_bad = 1
raise PrecompiledError, filename
except:
remove_bad = 1
raise
else:
return precompiled
finally:
if file:
portalocker.unlock(file)