def _upgrade(): global _upgraded _upgraded = True try: import sys sqlite_dll = File.new_instance(sys.exec_prefix, "dlls/sqlite3.dll") python_dll = File("pyLibrary/vendor/sqlite/sqlite3.dll") if python_dll.read_bytes() != sqlite_dll.read_bytes(): backup = sqlite_dll.backup() File.copy(python_dll, sqlite_dll) except Exception, e: Log.warning("could not upgrade python's sqlite", cause=e)
def _read_file(filename): try: file = File.new_instance(STATIC_DIRECTORY, filename) if not file.abspath.startswith(STATIC_DIRECTORY.abspath): return "", 404, "text/html" Log.note("Read {{file}}", file=file.abspath) mimetype, encoding = mimetypes.guess_type(file.extension) if not mimetype: mimetype = "text/html" return file.read_bytes(), 200, mimetype except Exception: return "", 404, "text/html"
def _get_attr(obj, path): if not path: return obj attr_name = path[0] if isinstance(obj, ModuleType): if attr_name in obj.__dict__: return _get_attr(obj.__dict__[attr_name], path[1:]) elif attr_name in dir(obj): return _get_attr(obj[attr_name], path[1:]) # TRY FILESYSTEM from pyLibrary.env.files import File possible_error = None if File.new_instance(File(obj.__file__).parent, attr_name).set_extension("py").exists: try: # THIS CASE IS WHEN THE __init__.py DOES NOT IMPORT THE SUBDIR FILE # WE CAN STILL PUT THE PATH TO THE FILE IN THE from CLAUSE if len(path) == 1: # GET MODULE OBJECT output = __import__(obj.__name__ + "." + attr_name, globals(), locals(), [path[0]], 0) return output else: # GET VARIABLE IN MODULE output = __import__(obj.__name__ + "." + attr_name, globals(), locals(), [path[1]], 0) return _get_attr(output, path[1:]) except Exception, e: from pyLibrary.debugs.exceptions import Except possible_error = Except.wrap(e) # TRY A CASE-INSENSITIVE MATCH attr_name = lower_match(attr_name, dir(obj)) if not attr_name: from pyLibrary.debugs.logs import Log Log.warning(PATH_NOT_FOUND + ". Returning None.", cause=possible_error) elif len(attr_name) > 1: from pyLibrary.debugs.logs import Log Log.error(AMBIGUOUS_PATH_FOUND + " {{paths}}", paths=attr_name) else: return _get_attr(obj[attr_name[0]], path[1:])
def __init__(self, _file): """ file - USES FILE FOR PERSISTENCE """ self.file = File.new_instance(_file) self.lock = Lock("lock for persistent queue using file " + self.file.name) self.please_stop = Signal() self.db = Dict() self.pending = [] if self.file.exists: for line in self.file: try: delta = convert.json2value(line) apply_delta(self.db, delta) except: pass if self.db.status.start == None: # HAPPENS WHEN ONLY ADDED TO QUEUE, THEN CRASH self.db.status.start = 0 self.start = self.db.status.start # SCRUB LOST VALUES lost = 0 for k in self.db.keys(): try: if k != "status" and int(k) < self.start: self.db[k] = None lost += 1 except Exception: pass # HAPPENS FOR self.db.status, BUT MAYBE OTHER PROPERTIES TOO if lost: Log.warning("queue file had {{num}} items lost", num=lost) if DEBUG: Log.note("Persistent queue {{name}} found with {{num}} items", name=self.file.abspath, num=len(self)) else: self.db.status = Dict(start=0, end=0) self.start = self.db.status.start if DEBUG: Log.note("New persistent queue {{name}}", name=self.file.abspath)
def __init__(self, _file): """ file - USES FILE FOR PERSISTENCE """ self.file = File.new_instance(_file) self.lock = Lock("lock for persistent queue using file " + self.file.name) self.please_stop = Signal() self.db = Dict() self.pending = [] if self.file.exists: for line in self.file: try: delta = convert.json2value(line) apply_delta(self.db, delta) except: pass if self.db.status.start == None: # HAPPENS WHEN ONLY ADDED TO QUEUE, THEN CRASH self.db.status.start = 0 self.start = self.db.status.start # SCRUB LOST VALUES lost = 0 for k in self.db.keys(): try: if k!="status" and int(k) < self.start: self.db[k] = None lost += 1 except Exception: pass # HAPPENS FOR self.db.status, BUT MAYBE OTHER PROPERTIES TOO if lost: Log.warning("queue file had {{num}} items lost", num= lost) if DEBUG: Log.note("Persistent queue {{name}} found with {{num}} items", name=self.file.abspath, num=len(self)) else: self.db.status = Dict( start=0, end=0 ) self.start = self.db.status.start if DEBUG: Log.note("New persistent queue {{name}}", name=self.file.abspath)
def _get_attr(obj, path): if not path: return obj attr_name = path[0] if isinstance(obj, ModuleType): if attr_name in obj.__dict__: return _get_attr(obj.__dict__[attr_name], path[1:]) elif attr_name in dir(obj): return _get_attr(obj[attr_name], path[1:]) # TRY FILESYSTEM from pyLibrary.env.files import File if File.new_instance(File(obj.__file__).parent, attr_name).set_extension("py").exists: try: # THIS CASE IS WHEN THE __init__.py DOES NOT IMPORT THE SUBDIR FILE # WE CAN STILL PUT THE PATH TO THE FILE IN THE from CLAUSE if len(path) == 1: # GET MODULE OBJECT output = __import__(obj.__name__ + "." + attr_name, globals(), locals(), [path[0]], 0) return output else: # GET VARIABLE IN MODULE output = __import__(obj.__name__ + "." + attr_name, globals(), locals(), [path[1]], 0) return _get_attr(output, path[1:]) except Exception, e: pass # TRY A CASE-INSENSITIVE MATCH attr_name = lower_match(attr_name, dir(obj)) if not attr_name: from pyLibrary.debugs.logs import Log Log.error(PATH_NOT_FOUND) elif len(attr_name) > 1: from pyLibrary.debugs.logs import Log Log.error(AMBIGUOUS_PATH_FOUND + " {{paths}}", paths=attr_name) else: return _get_attr(obj[attr_name[0]], path[1:])
finally: signal.go() else: try: self.db.execute(command) except Exception, e: e = Except.wrap(e) e.cause = Except( type=ERROR, template="Bad call to Sqlite", trace=trace ) Log.warning("Failure to execute", cause=e) except Exception, e: Log.error("Problem with sql thread", e) finally: self.db.close() try: import sys sqlite_dll = File.new_instance(sys.exec_prefix, "dlls/sqlite3.dll") python_dll = File("pyLibrary/vendor/sqlite/sqlite3.dll") if python_dll.read_bytes() != sqlite_dll.read_bytes(): backup = sqlite_dll.backup() File.copy(python_dll, sqlite_dll) except Exception, e: Log.warning("could not upgrade python's sqlite", cause=e)
from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals import mimetypes import flask from werkzeug.wrappers import Response from active_data import record_request, cors_wrapper from pyLibrary.debugs.logs import Log from pyLibrary.env.files import File from pyLibrary.meta import cache from pyLibrary.times.durations import DAY STATIC_DIRECTORY = File.new_instance("active_data/public") @cors_wrapper def download(filename): """ DOWNLOAD FILE CONTENTS :param filename: URL PATH :return: Response OBJECT WITH FILE CONTENT """ try: record_request(flask.request, None, flask.request.get_data(), None) content, status, mimetype = _read_file(filename) return Response(content, status=status, headers={"Content-Type": mimetype})