atw.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:aboutthiswebsite 作者: a3n 项目源码 文件源码
def upsertfile(dbName, clctName, fileName):
    '''atw.py upsertfile dbName clctName fileName

    Upsert (insert, or replace the existing document) the data in fileName
    to dbName.clctName.

    If clctName is "meta", fileName's contents must be valid json. Note
    that data is then stored as individual document/json objects found
    in fileName, and is not retrievable as the same file.

    clctName can be:

    - meta: Site-wide metadata.
      - In json format, conforming to meta's validation rules.
      - Name(s) of upserted document(s) is in the data.
        - See atwMeta.json for example.
      - Relationship of stored documents to fileName is not stored.

    (The name of the upserted document for any of the below is fileName,
    without any leading path.)

    - template: fileName contains one html template with template tags.
      - A jinja2 html template file.

    - content: fileName contains content for one page.
      - .md = treat as Markdown at page generation.
      - .rst = treat as reStructuredText at page generation.
      - .txt, .html, or any other suffix = treat as plain text.
          - .html should be only what goes inside a <body>, as it will
            be placed inside the template's <body> tag as-is.
      - Any content document has access to jinja2 meta variables.

    - code: Intended for plain text storage of program files.

    - bin: fileName contains binary data like an image or audio.
      - fileName suffix does not influence treatment; it's just bits.
    '''

    db, client = _dbAndClient(dbName)
    clct = _collection(db, clctName) # Will raise if clctName is not of the body.

    baseName, prefix, suffix = _basenamePrefixSuffix(fileName)

    if suffix == 'pyc':
        return 'Ignore compiled python file %s.'%(fileName)

    if clctName == 'meta':
        return _upsertJson(dbName, clctName, fileName).strip()
    elif clctName == 'bin':
        with open(fileName, 'rb') as binFile:
            binData = Binary(binFile.read())
        return _upsertData(
            db, clct, {'name': baseName, 'data': binData}).strip()
    else:
        with open(fileName, 'r') as dataFile:
            fileData = dataFile.read()
        return _upsertData(
            db, clct, {'name': baseName, 'data': fileData}).strip()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号