Skip to content

Instantly share code, notes, and snippets.

@apple-phi
Created August 17, 2021 15:47
Show Gist options
  • Save apple-phi/25f9497d8b51041a4a9cff39cea302bb to your computer and use it in GitHub Desktop.
Save apple-phi/25f9497d8b51041a4a9cff39cea302bb to your computer and use it in GitHub Desktop.
Neutralinojs plugin for Eel
# MIT License
# Copyright (c) 2021 Lucas Ng
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Neutralinojs plugin for Eel (https://github.com/ChrisKnott/Eel).
To use, just import and it will patch Eel so that you can use `mode="neutralino" in `eel.start`.
For more information about the helper functions contained here, read the docstrings below.
"""
import sys
import os
import fnmatch
import subprocess as sps
import platform
import pathlib
from eel.browsers import set_path, _browser_modules
name = "Neutralinojs"
if not "neutralino" in _browser_modules:
_browser_modules.update({"neutralino": sys.modules[__name__]})
def run(path, options, start_urls):
"""Function called by Eel to start the Neutralinojs webview."""
cmd = [path] + options["cmdline_args"]
sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE)
def _get_glob():
if sys.platform == "darwin":
_neu_platform = "mac"
elif "win" in sys.platform: # allow for cygwin
_neu_platform = "win"
elif sys.platform == "linux":
_neu_platform = "linux"
else:
return None
arch = platform.machine()
if "64" in arch:
_neu_arch = "x64"
elif "ia32" in arch:
_neu_arch = "ia32"
else:
return None
return f"*-{_neu_platform}_{_neu_arch}*"
custom_glob: str = ""
"""
Instead of using the autogenerated glob to search for binaries
when using `neutralino.find_path` (or when Eel calls it internally),
you can specify a different glob or filename.
"""
def find_path(directory: str = os.getcwd()):
"""
Attempt to find the Neutralinojs compiled binary for the current machine.
This is called by Eel automatically if a path is not set by `eel.set_path` or `neutralino.set_dir`.
You can adjust the glob used by setting `neutralino.custom_glob`.
Parameters
----------
directory : str, default = current working directory
The directory tree to search through recursively.
"""
# neutralino (v2.6 at time of writing) names binaries weirdly, so some custom handling must take place.
# it takes the original WebView binary name from https://github.com/neutralinojs/neutralinojs-cli/blob/6821328ff19e937821fbe29c9d5ba2a6a5fe36a2/src/constants.js
# and then replaces 'neutralino' with the `cli.binaryName` from the neutralino.config.json (src at https://github.com/neutralinojs/neutralinojs-cli/blob/6821328ff19e937821fbe29c9d5ba2a6a5fe36a2/src/modules/bundler.js)
if _neu_glob := (custom_glob or _get_glob()):
for root, dirs, files in os.walk(directory):
for filename in files:
if not "neutralino" in filename and fnmatch.fnmatch(
filename, _neu_glob
):
return os.path.join(root, filename)
return None
def set_dir(directory: str) -> None:
"""
Set the Neutralinojs directory.
This will be searched recursively for a binary file
with a filename matching Neutralinojs builds.
Parameters
----------
directory : str
The directory tree to search through recursively.
"""
set_path("neutralino", find_path(directory))
def get_include(destination: str, directory: str = os.getcwd()) -> list:
"""
Generate a list of PyInstaller arguments in order to include the Neutralinojs binaries.
Parameters
----------
destination : str
The folder in which the Neutralinojs resource file, configuration file and binary will be placed.
directory : str, default = current working directory
The directory tree to search through recursively.
"""
bin_path = find_path(directory)
assert (
bin_path is not None
), "`directory` could not be found; perhaps you got the arguments the wrong way round?"
bin_path_obj = pathlib.Path(bin_path)
include = [
"--add-data=" + bin_path + os.pathsep + destination,
"--add-data=" + str(bin_path_obj.parent / "res.neu") + os.pathsep + destination,
"--add-data="
+ str(bin_path_obj.parent.parent.parent / "neutralino.config.json")
+ os.pathsep
+ destination,
]
return include
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment