def read_batchfile(pythonpath, file_ending='.py'): """ This reads the contents of a batch-file. Filename is considered to be the name of the batch file relative the directory specified in settings.py. file_ending specify which batchfile ending should be assumed (.ev or .py). """ # open the file if pythonpath and not (pythonpath.startswith('src.') or pythonpath.startswith('game.') or pythonpath.startswith('contrib.')): abspaths = [] for basepath in settings.BASE_BATCHPROCESS_PATHS: abspaths.append(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending)) else: abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)] fobj, lines, err = None, [], None for file_encoding in ENCODINGS: # try different encodings, in order load_errors = [] for abspath in abspaths: # try different paths, until we get a match try: # we read the file directly into unicode. fobj = codecs.open(abspath, 'r', encoding=file_encoding) except IOError: load_errors.append("Could not open batchfile '%s'." % abspath) continue break if not fobj: continue load_errors = [] err =None # We have successfully found and opened the file. Now actually # try to decode it using the given protocol. try: lines = fobj.readlines() except UnicodeDecodeError: # give the line of failure fobj.seek(0) try: lnum = 0 for lnum, line in enumerate(fobj): pass except UnicodeDecodeError, err: # lnum starts from 0, so we add +1 line, # besides the faulty line is never read # so we add another 1 (thus +2) to get # the actual line number seen in an editor. err.linenum = lnum + 2 fobj.close() # possibly try another encoding continue # if we get here, the encoding worked. Stop iteration. break
def read_batchfile(pythonpath, file_ending='.py'): """ This reads the contents of a batch-file. Filename is considered to be a python path to a batch file relative the directory specified in settings.py. file_ending specify which batchfile ending should be assumed (.ev or .py). The ending should not be included in the python path. """ # open the file if pythonpath and not (pythonpath.startswith('src.') or pythonpath.startswith('game.') or pythonpath.startswith('contrib.')): abspaths = [] for basepath in settings.BASE_BATCHPROCESS_PATHS: abspaths.append( utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending)) else: abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)] text, fobj = None, None fileerr, decoderr = [], [] for abspath in abspaths: # try different paths, until we get a match # we read the file directly into unicode. for file_encoding in ENCODINGS: # try different encodings, in order try: fobj = codecs.open(abspath, 'r', encoding=file_encoding) text = fobj.read() except IOError, e: # could not find the file fileerr.append(str(e)) break except (ValueError, UnicodeDecodeError), e: # this means an encoding error; try another encoding decoderr.append(str(e)) continue break
def _cache_lockfuncs(): "Updates the cache." global _LOCKFUNCS _LOCKFUNCS = {} for modulepath in settings.LOCK_FUNC_MODULES: modulepath = utils.pypath_to_realpath(modulepath) mod = utils.mod_import(modulepath) if mod: for tup in (tup for tup in inspect.getmembers(mod) if callable(tup[1])): _LOCKFUNCS[tup[0]] = tup[1] else: logger.log_errmsg("Couldn't load %s from PERMISSION_FUNC_MODULES." % modulepath)
def read_batchfile(pythonpath, file_ending='.py'): """ This reads the contents of a batch-file. Filename is considered to be a python path to a batch file relative the directory specified in settings.py. file_ending specify which batchfile ending should be assumed (.ev or .py). The ending should not be included in the python path. """ # open the file if pythonpath and not (pythonpath.startswith('src.') or pythonpath.startswith('game.') or pythonpath.startswith('contrib.')): abspaths = [] for basepath in settings.BASE_BATCHPROCESS_PATHS: abspaths.append(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending)) else: abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)] text, fobj = None, None fileerr, decoderr = [], [] for abspath in abspaths: # try different paths, until we get a match # we read the file directly into unicode. for file_encoding in ENCODINGS: # try different encodings, in order try: fobj = codecs.open(abspath, 'r', encoding=file_encoding) text = fobj.read() except IOError, e: # could not find the file fileerr.append(str(e)) break except (ValueError, UnicodeDecodeError), e: # this means an encoding error; try another encoding decoderr.append(str(e)) continue break