diff --git a/.gitignore b/.gitignore index d09b336..dd9d50b 100644 --- a/.gitignore +++ b/.gitignore @@ -261,25 +261,8 @@ tags # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 -# User-specific stuff -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/**/usage.statistics.xml -.idea/**/dictionaries -.idea/**/shelf - -# Sensitive or high-churn files -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml -.idea/**/dbnavigator.xml - -# Gradle -.idea/**/gradle.xml -.idea/**/libraries +# IDE +.idea/ # CMake cmake-build-*/ diff --git a/.travis.yml b/.travis.yml index ee3a11f..fe3a6a7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,15 +4,11 @@ language: python notifications: email: false python: - - &pypy2 pypy2.7-6.0 - - &pypy3 pypy3.5-6.0 - 3.8-dev - 3.7 - 3.6 - 3.5 - - 2.7 before_install: - - if [[ $TRAVIS_PYTHON_VERSION == "2.6" ]]; then pip install flake8==2.6.2; fi - if [[ -f min_requirements.txt && "$MINREQ" -eq 1 ]]; then mv min_requirements.txt requirements.txt ; fi diff --git a/CHANGELOG.rst b/CHANGELOG.rst deleted file mode 100644 index 7398c8c..0000000 --- a/CHANGELOG.rst +++ /dev/null @@ -1,2 +0,0 @@ -Change log -=========== diff --git a/Makefile b/Makefile index 26d9b68..4fbbe8b 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,3 @@ all: test test: bash test.sh - -document: - bash document.sh diff --git a/Pipfile b/Pipfile index 5357dac..38635bb 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,7 @@ name = 'pypi' python_version= '3.6' [packages] +selenium = "*" [dev-packages] nose = "*" diff --git a/README.md b/README.md new file mode 100644 index 0000000..996aa42 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# snapshot-selenium + +![http://travis-ci.org/pyecharts/snapshot-selenium](https://api.travis-ci.org/pyecharts/snapshot-selenium.svg) ![https://codecov.io/github/pyecharts/snapshot-selenium](https://codecov.io/github/pyecharts/snapshot-selenium/coverage.png) + +## Installation + +You can install snapshot-selenium via pip: + +```bash +$ pip install snapshot-selenium +``` + +or clone it and install it: + +```bash +$ git clone https://github.com/pyecharts/snapshot-selenium.git +$ cd snapshot-selenium +$ pip install -r requirements.txt +$ python setup.py install +``` diff --git a/README.rst b/README.rst deleted file mode 100644 index c3035c8..0000000 --- a/README.rst +++ /dev/null @@ -1,33 +0,0 @@ -================================================================================ -snapshot-selenium -================================================================================ - -.. image:: https://api.travis-ci.org/pyecharts/snapshot-selenium.svg - :target: http://travis-ci.org/pyecharts/snapshot-selenium - -.. image:: https://codecov.io/github/pyecharts/snapshot-selenium/coverage.png - :target: https://codecov.io/github/pyecharts/snapshot-selenium - - -.. image:: https://readthedocs.org/projects/snapshot-selenium/badge/?version=latest - :target: http://snapshot-selenium.readthedocs.org/en/latest/ - - -Installation -================================================================================ - - -You can install snapshot-selenium via pip: - -.. code-block:: bash - - $ pip install snapshot-selenium - - -or clone it and install it: - -.. code-block:: bash - - $ git clone https://github.com/pyecharts/snapshot-selenium.git - $ cd snapshot-selenium - $ python setup.py install diff --git a/docs/source/conf.py b/docs/source/conf.py deleted file mode 100644 index 5d075bb..0000000 --- a/docs/source/conf.py +++ /dev/null @@ -1,62 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# http://www.sphinx-doc.org/en/master/config - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -# import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) - -# -- Project information ----------------------------------------------------- - -project = '' -copyright = '' -author = 'C.W.' -# The short X.Y version -version = '0.0.1' -# The full version, including alpha/beta/rc tags -release = '0.0.1' - -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# -# source_suffix = ['.rst', '.md'] -source_suffix = '' - -# The master toctree document. -master_doc = '' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -exclude_patterns = [] - - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -html_theme = 'alabaster' - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['static'] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e69de29..7cb6656 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +selenium diff --git a/setup.py b/setup.py index 5939423..5438c4e 100644 --- a/setup.py +++ b/setup.py @@ -1,18 +1,15 @@ #!/usr/bin/env python3 -# Template by pypi-mobans -import os -import sys import codecs import locale + +# Template by pypi-mobans +import os import platform +import sys from shutil import rmtree -from setuptools import Command, setup, find_packages - -PY2 = sys.version_info[0] == 2 -PY26 = PY2 and sys.version_info[1] < 7 -PY33 = sys.version_info < (3, 4) +from setuptools import Command, find_packages, setup # Work around mbcs bug in distutils. # http://bugs.python.org/issue10945 @@ -22,67 +19,57 @@ try: lc = locale.getlocale() pf = platform.system() - if pf != 'Windows' and lc == (None, None): - locale.setlocale(locale.LC_ALL, 'C.UTF-8') + if pf != "Windows" and lc == (None, None): + locale.setlocale(locale.LC_ALL, "C.UTF-8") except (ValueError, UnicodeError, locale.Error): - locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') - -NAME = 'snapshot-selenium' -AUTHOR = 'C.W.' -VERSION = '0.0.1' -EMAIL = 'info@pyecharts.com' -LICENSE = 'MIT' -DESCRIPTION = ( - 'Render echarts using selenium' -) -URL = 'https://github.com/pyecharts/snapshot-selenium' -DOWNLOAD_URL = '%s/archive/0.0.1.tar.gz' % URL -FILES = ['README.rst', 'CHANGELOG.rst'] -KEYWORDS = [ - 'python', -] + locale.setlocale(locale.LC_ALL, "en_US.UTF-8") + +NAME = "snapshot-selenium" +AUTHOR = "pyecharts dev team" +VERSION = "0.0.1" +EMAIL = "info@pyecharts.com" +LICENSE = "MIT" +DESCRIPTION = "Render echarts using selenium" +URL = "https://github.com/pyecharts/snapshot-selenium" +DOWNLOAD_URL = "%s/archive/0.0.1.tar.gz" % URL +FILES = ["README.md"] + CLASSIFIERS = [ - 'Topic :: Software Development :: Libraries', - 'Programming Language :: Python', - 'Intended Audience :: Developers', - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', + "Topic :: Software Development :: Libraries", + "Programming Language :: Python", + "Intended Audience :: Developers", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", ] -INSTALL_REQUIRES = [ -] +INSTALL_REQUIRES = ["selenium"] SETUP_COMMANDS = {} -PACKAGES = find_packages(exclude=['ez_setup', 'examples', 'tests']) +PACKAGES = find_packages(exclude=["ez_setup", "examples", "tests"]) EXTRAS_REQUIRE = {} # You do not need to read beyond this line -PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format( - sys.executable) -GS_COMMAND = ('gs snapshot-selenium v0.0.1 ' + - "Find 0.0.1 in changelog for more details") -NO_GS_MESSAGE = ('Automatic github release is disabled. ' + - 'Please install gease to enable it.') -UPLOAD_FAILED_MSG = ( - 'Upload failed. please run "%s" yourself.' % PUBLISH_COMMAND) +PUBLISH_COMMAND = "{0} setup.py sdist bdist_wheel upload -r pypi".format(sys.executable) +GS_COMMAND = "gs snapshot-selenium v0.0.1 " + "Find 0.0.1 in changelog for more details" +NO_GS_MESSAGE = ( + "Automatic github release is disabled. " + "Please install gease to enable it." +) +UPLOAD_FAILED_MSG = 'Upload failed. please run "%s" yourself.' % PUBLISH_COMMAND HERE = os.path.abspath(os.path.dirname(__file__)) class PublishCommand(Command): """Support setup.py upload.""" - description = 'Build and publish the package on github and pypi' + description = "Build and publish the package on github and pypi" user_options = [] @staticmethod def status(s): """Prints things in bold.""" - print('\033[1m{0}\033[0m'.format(s)) + print("\033[1m{0}\033[0m".format(s)) def initialize_options(self): pass @@ -92,14 +79,14 @@ def finalize_options(self): def run(self): try: - self.status('Removing previous builds...') - rmtree(os.path.join(HERE, 'dist')) - rmtree(os.path.join(HERE, 'build')) - rmtree(os.path.join(HERE, 'snapshot_selenium.egg-info')) + self.status("Removing previous builds...") + rmtree(os.path.join(HERE, "dist")) + rmtree(os.path.join(HERE, "build")) + rmtree(os.path.join(HERE, "snapshot_selenium.egg-info")) except OSError: pass - self.status('Building Source and Wheel (universal) distribution...') + self.status("Building Source and Wheel (universal) distribution...") run_status = True if has_gease(): run_status = os.system(GS_COMMAND) == 0 @@ -112,9 +99,7 @@ def run(self): sys.exit() -SETUP_COMMANDS.update({ - 'publish': PublishCommand -}) +SETUP_COMMANDS.update({"publish": PublishCommand}) def has_gease(): @@ -125,6 +110,7 @@ def has_gease(): """ try: import gease # noqa + return True except ImportError: return False @@ -142,7 +128,7 @@ def read_files(*files): def read(afile): """Read a file into setup""" the_relative_file = os.path.join(HERE, afile) - with codecs.open(the_relative_file, 'r', 'utf-8') as opened_file: + with codecs.open(the_relative_file, "r", "utf-8") as opened_file: content = filter_out_test_code(opened_file) content = "".join(list(content)) return content @@ -151,11 +137,11 @@ def read(afile): def filter_out_test_code(file_handle): found_test_code = False for line in file_handle.readlines(): - if line.startswith('.. testcode:'): + if line.startswith(".. testcode:"): found_test_code = True continue if found_test_code is True: - if line.startswith(' '): + if line.startswith(" "): continue else: empty_line = line.strip() @@ -165,14 +151,14 @@ def filter_out_test_code(file_handle): found_test_code = False yield line else: - for keyword in ['|version|', '|today|']: + for keyword in ["|version|", "|today|"]: if keyword in line: break else: yield line -if __name__ == '__main__': +if __name__ == "__main__": setup( test_suite="tests", name=NAME, @@ -184,13 +170,12 @@ def filter_out_test_code(file_handle): download_url=DOWNLOAD_URL, long_description=read_files(*FILES), license=LICENSE, - keywords=KEYWORDS, extras_require=EXTRAS_REQUIRE, - tests_require=['nose'], + tests_require=["nose"], install_requires=INSTALL_REQUIRES, packages=PACKAGES, include_package_data=True, zip_safe=False, classifiers=CLASSIFIERS, - cmdclass=SETUP_COMMANDS + cmdclass=SETUP_COMMANDS, ) diff --git a/snapshot_selenium/__init__.py b/snapshot_selenium/__init__.py index ce5d0d7..9d7ac2d 100644 --- a/snapshot_selenium/__init__.py +++ b/snapshot_selenium/__init__.py @@ -1,3 +1,2 @@ # flake8: noqa -from snapshot_selenium._version import __version__ -from snapshot_selenium._version import __author__ +from snapshot_selenium._version import __author__, __version__ diff --git a/snapshot_selenium/_version.py b/snapshot_selenium/_version.py index bade78b..2140ee5 100644 --- a/snapshot_selenium/_version.py +++ b/snapshot_selenium/_version.py @@ -1,2 +1,2 @@ __version__ = "0.0.1" -__author__ = "C.W." +__author__ = "pyecharts dev team" diff --git a/snapshot_selenium/snapshot.py b/snapshot_selenium/snapshot.py index b6fb97c..7f0de4a 100644 --- a/snapshot_selenium/snapshot.py +++ b/snapshot_selenium/snapshot.py @@ -1,9 +1,8 @@ -import time import os +import time +from typing import Any from selenium import webdriver -from selenium.common import exceptions -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities SNAPSHOT_JS = """ var ele = document.querySelector('div[_echarts_instance_]'); @@ -14,6 +13,7 @@ excludeComponents: ['toolbox'] }); """ + SNAPSHOT_SVG_JS = """ var element = document.querySelector('div[_echarts_instance_] div'); return element.innerHTML; @@ -25,45 +25,38 @@ def make_snapshot( file_type: str, pixel_ratio: int = 2, delay: int = 2, - browser='Chrome' + browser="Chrome", + driver: Any = None, ): if delay < 0: - raise Exception('Time travel is not possible') - if browser == 'Chrome': - driver = get_chrome() - elif browser == 'Safari': - driver = get_safari() - else: - raise Exception('Unknown browser!') - driver.set_script_timeout(delay + 1) - - if file_type == 'svg': + raise Exception("Time travel is not possible") + if not driver: + if browser == "Chrome": + driver = get_chrome_driver() + elif browser == "Safari": + driver = get_safari_driver() + else: + raise Exception("Unknown browser!") + + if file_type == "svg": snapshot_js = SNAPSHOT_SVG_JS else: snapshot_js = SNAPSHOT_JS % (file_type, pixel_ratio) if not html_path.startswith("http"): - html_path = 'file://' + os.path.abspath(html_path) + html_path = "file://" + os.path.abspath(html_path) + driver.get(html_path) time.sleep(delay) - try: - output = driver.execute_script(snapshot_js) - driver.close() - return output - except exceptions.TimeoutException: - raise Exception("Failed to get snapshot content") + return driver.execute_script(snapshot_js) -def get_chrome(): - option = webdriver.ChromeOptions() - option.add_argument("headless") - capabilities = DesiredCapabilities.CHROME - capabilities["loggingPrefs"] = {"browser": "ALL"} - return webdriver.Chrome( - options=option, - desired_capabilities=capabilities) +def get_chrome_driver(): + options = webdriver.ChromeOptions() + options.add_argument("headless") + return webdriver.Chrome(options=options) -def get_safari(): - return webdriver.Safari(executable_path='/usr/bin/safaridriver') +def get_safari_driver(): + return webdriver.Safari() diff --git a/test.sh b/test.sh index c134240..9577d4e 100644 --- a/test.sh +++ b/test.sh @@ -1,2 +1,2 @@ pip freeze -nosetests --with-coverage --cover-package snapshot_selenium --cover-package tests tests docs/source snapshot_selenium && flake8 . --exclude=.moban.d,docs --builtins=unicode,xrange,long +nosetests --with-coverage --cover-package snapshot_selenium --cover-package tests tests snapshot_selenium && flake8 . --exclude=.moban.d --builtins=unicode,xrange,long