當前位置: 首頁>>代碼示例>>Python>>正文


Python Config.update方法代碼示例

本文整理匯總了Python中flask.config.Config.update方法的典型用法代碼示例。如果您正苦於以下問題:Python Config.update方法的具體用法?Python Config.update怎麽用?Python Config.update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask.config.Config的用法示例。


在下文中一共展示了Config.update方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: DatacatCore

# 需要導入模塊: from flask.config import Config [as 別名]
# 或者: from flask.config.Config import update [as 別名]
class DatacatCore(object):
    def __init__(self, config=None):
        self.config = Config()
        if config is not None:
            self.config.update(config)

    @property
    def db(self):
        if getattr(self, '_db', None) is None:
            self._db = connect(**self.config['DATABASE'])
            self._db.autocommit = False
        return self._db

    @property
    def admin_db(self):
        if getattr(self, '_admin_db', None) is None:
            self._admin_db = connect(**self.config['DATABASE'])
            self._admin_db.autocommit = True
        return self._admin_db

    def create_tables(self):
        create_tables(self.admin_db)

    def drop_tables(self):
        drop_tables(self.admin_db)

    # ------------------------------------------------------------
    # Resource data CRUD
    # ------------------------------------------------------------

    def resource_data_iter(self):
        """Iterate over resource attributes"""

        with self.db, self.db.cursor() as cur:
            cur.execute("SELECT * FROM resource_data")
            for row in cur.fetchall():
                yield row

    def resource_data_create(self, stream, metadata=None, mimetype=None):
        """Create resource data from a stream"""

        resource_hash = hashlib.sha1()
        with self.db, self.db.cursor() as cur:
            lobj = self.db.lobject(oid=0, mode='wb')
            oid = lobj.oid
            for chunk in file_read_chunks(stream):
                lobj.write(chunk)
                resource_hash.update(chunk)
            lobj.close()

            data = {
                'ctime': datetime.now(),
                'mtime': datetime.now(),
                'metadata': json.dumps(metadata),
                'mimetype': mimetype or 'application/octet-stream',
                'data_oid': oid,
                'hash': 'sha1:{0}'.format(resource_hash.hexdigest()),
            }

            query = querybuilder.insert('resource_data', data)
            cur.execute(query, data)
            resource_data_id = cur.fetchone()[0]

        return resource_data_id

    def resource_data_get_info(self, objid):
        query = querybuilder.select_pk('resource_data')
        with self.db, self.db.cursor() as cur:
            cur.execute(query, {'id': objid})
            return cur.fetchone()

    def resource_data_read(self, objid):
        resource = self.resource_data_get_info(objid)
        with self.db:
            lobj = self.db.lobject(oid=resource['data_oid'], mode='rb')
            return lobj.read()

    def resource_data_copy(self, objid, dest):
        resource = self.resource_data_get_info(objid)
        with self.db:
            lobj = self.db.lobject(oid=resource['data_oid'], mode='rb')
            for chunk in file_read_chunks(lobj):
                dest.write(chunk)

    def resource_data_update(self, objid, stream=None, metadata=None,
                             mimetype=None):

        # Get the original object, to check for its existence
        # and to get the oid of the lobject holding the data.
        original = self.resource_data_get_info(objid)

        data = {
            'id': objid,
            'mtime': datetime.now(),
        }

        if metadata is not None:
            data['metadata'] = json.dumps(metadata)
        if mimetype is not None:
            data['mimetype'] = mimetype
#.........這裏部分代碼省略.........
開發者ID:rshk-archive,項目名稱:datacat-poc-141007,代碼行數:103,代碼來源:core.py


注:本文中的flask.config.Config.update方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。