Flask 自建扩展

Python56

开源发布准备

*
1. 添加文档字符串与注释后的完整代码

"""
    Flask-Share
    # ~~~~~~~~~~~~~~
    Create social share component in Jinja2 tempalte based on share.js.

    :copyright: (c) 2017 by Gavin Li.

    :license: MIT, see LICENSE for more details.

"""

import re
from flask import current_app, url_for, Markup, Blueprint, request

class Share(object):

  @staticmethod
  def load(css_url=None, js_url=None):
    """ Load share.js resourse.

    :param css_url: if set, will be used as css url
    :param js_url: if set, will be used as js url
    :param serve_local: if set to True, the local resource will be used
"""

  @staticmethod
  def create( title='', sites=None, mobile_sites=None,align='left',addtion_class=''):
    """ Create a share component.

    :param title: the prompt displayed on the left of the share component.

    :param sites: a string that consist of sites, separate by comma.

    :param mobile_sites: a string that consist of sites, separate by comma.

        supported site name: weibo, wechat, douban, facebook, twitter, google, linkedin, qq, qzone."
        for example: weibo,wechat, qq.

    :param mobile_sites: the sites displayed on mobile.

    :param align: the align of the share component,default to '`left`'.

    :param addition_class: the style class added to the share component.

"""

*
1. 编写 README 与文档
- 小项目 直接用 README概括所有的必需的说明
- 大项目 比较复杂的,多文件组织文档内容
将项目部署到Read the Docs
Sphinx + Github + Readthedocs的工作流编写和部署文档
*
1. 定义 python 包的元数据:(setup.py)

"""
    Flask-Share

    Create social share component in Jinja2 template based on share.js.

    :copyright: (c) 2022 by Gavin li.

    :license: MIT, see LICENSE for more details.

"""
form os import path
from codecs import open
form setuptools import setup

basedir = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(basedir,'README.md'), encoding='utf-8') as f:
  long_description = f.read()

setup(
  name='Flask-Share', # 包名称
  version='0.1.0',  # 版本
  url='https://github.com/lghpython/flask-share',
  license='MIT',
  author='xxx'
  author_email='xx@xx.com',
  description='xxx',
  long_description=long_description,
  long_description_content_type='text/markdown', # 默认渲染格式为 rst
  platforms='any',
  packages=['flask_share'], # 包含的包列表,包括子包,可用find_pakages()
  zip_safe=False,
  test_suite='test_flask_share', 测试包或模块
  include_package_data=True,
  install_requires=['Flask'],  # 安装依赖
  keywords='flask extension development', # 项目关键词
  classifiers=[ # 分类词, 在 PyPI 中设置分类
    'DevelopmentStatus::3-Alpha',
    'Environment::WebEnvironment',
    'IntendedAudience::Developers',
    'License::OSIApproved::MITLicense',
    'ProgrammingLanguage::Python',
    'ProgrammingLanguage::Python::2',
    'ProgrammingLanguage::Python::2.7',
    'ProgrammingLanguage::Python::3',
    'ProgrammingLanguage::Python::3.3',
    'ProgrammingLanguage::Python::3.4',
    'ProgrammingLanguage::Python::3.5',
    'ProgrammingLanguage::Python::3.6',
    'Topic::Internet::WWW/HTTP::DynamicContent',
    'Topic::SoftwareDevelopment::Libraries::PythonModules']
  ],
)

*
1. 指定打包其他文件: MANIFEST.in
需要在 setup()方法中设置: include_package_data=True

graft flask_share/static
include LICENSE test_flask_share.py
# exclude 用来排除匹配文件
# recursive-include 递归匹配
# recursive-exclude 递归排除匹配
# graft 目录 包含目录下所有
# prune 目录 配出目录下所有

*
1. 编写单元测试

import unittest

from flask import Flask, render_template_string, current_app
from flask_share import Share

class ShareTestCase(unittest.TestCase):

  def setUp(self):
    self.mobile_agent={{'HTTP_USER_AGENT':'Mozilla/5.0(iPhone;CPUiPhoneOS9_1likeMacOSX)\
        AppleWebKit/601.1.46(KHTML,likeGecko)Version/9.0Mobile/13B143Safari/601.1'}}
    app = Flask(__name__)
    app.testing=True
    self.share=Share(app)

    @app.route('/')
    def index():
      return render_template_string('{{share.load() }}\n {{share.create() }}')
    # 推送上下文
    self.context=app.app_context()
    self.context.push()
    self.client - app.test_client()

  def tearDown(self):
    self.context.pop()

  def test_create_on_mobile(self):
    current_app.config['SHARE_HIDE_ON_MOBILE'] = True
    response = self.client.get('/', environ_base=self.mobile_agent)
    data = response.get_data(as_text=True)
    self.assertIn('social-share.min.js', data)
    self.assertNotIn('

*
1. setup.cfg

Original: https://www.cnblogs.com/lghgo/p/15978176.html
Author: lghgo
Title: Flask 自建扩展