Skip to content

Commit

Permalink
Merge 1bac080 into 7f069b7
Browse files Browse the repository at this point in the history
  • Loading branch information
Beakerboy committed Nov 3, 2023
2 parents 7f069b7 + 1bac080 commit 315c47e
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 83 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
Expand All @@ -16,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
Expand All @@ -27,9 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
pip install -e .[tests]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand All @@ -40,5 +37,26 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pytest --cov=excelAddinGenerator
pytest --cov=src
coveralls --service=github
flake8:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .[tests]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --show-source --statistics
1 change: 0 additions & 1 deletion excelAddinGenerator/__init__.py

This file was deleted.

44 changes: 0 additions & 44 deletions excelAddinGenerator/main.py

This file was deleted.

37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "excel_addin_generator"
version = "0.0.1"
authors = [
{ name="Kevin Nowaczyk", email="beakerboy99@yahoo.com" },
]
description = "Create an Excel addin with python."
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.optional-dependencies]
tests = [
'coveralls',
'filehash',
'flake8-annotations',
'pytest',
'pytest-cov'
]
[project.urls]
"Homepage" = "https://github.com/Beakerboy/vbaProject-Compiler"
"Bug Tracker" = "https://github.com/Beakerboy/vbaProject-Compiler/issues"

[tool.pytest.ini_options]
pythonpath = "src"
testpaths = [
"tests",
]
[tool.setuptools.package-data]
"*" = ["*.cls"]
7 changes: 0 additions & 7 deletions setup.py

This file was deleted.

1 change: 1 addition & 0 deletions src/excel_addin_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# None
23 changes: 23 additions & 0 deletions src/excel_addin_generator/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import sys
import excel_addin_generator.main as gen


def main() -> None:
args = []
base_path = os.path.dirname(__file__)
args[1] = sys.argv[1]
args[2] = sys.argv[2]
if len(args) > 2:
# check the extension on sys.argv[1] to determine which function to call
input_file = args[1]
output_file_name = args[2]
if input_file.endswith('.xlam'):
gen.createFromZip(input_file, base_path + '/data', output_file_name)
elif input_file.endswith('.bin'):
gen.create_from_bin(input_file, base_path + '/data', output_file_name)
else:
raise Exception(input_file, " is not a valid file format.")


main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions src/excel_addin_generator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import shutil
import sys
import zipfile


def create_from_bin(input_file: str, wrapper_dir: str, output_file_name: str) -> None:

"""Create a zip file containing the provided bin"""
# file must start with 'd0 cf 11 e0 a1 b1 1a e1'
fileSig = open(input_file, "rb").read(8).hex()
if fileSig != 'd0cf11e0a1b11ae1':
raise Exception('File signature {} is not as expected.', format(fileSig))
shutil.copy(input_file, wrapper_dir + "/xl/vbaProject.bin")
shutil.make_archive(output_file_name, 'zip', wrapper_dir)
shutil.move(output_file_name + ".zip", output_file_name)


def createFromZip(input_file: str, wrapper_dir: str, output_file_name: str) -> None:

"""Create a zip file containing the bin file within the provided zip file"""
extractBinFromZip(input_file)
create_from_bin('xl/vbaProject.bin', wrapper_dir, output_file_name)


def extractBinFromZip(input_file: str) -> None:

# check that input is a zip file
if zipfile.is_zipfile(input_file):
# check that the zip archive contains /xl/vbaProject.bin
with zipfile.ZipFile(input_file, 'r') as zip:
zip.extract('xl/vbaProject.bin')
else:
raise Exception(input_file, " is not a valid file format.")
1 change: 0 additions & 1 deletion tests/__init__.py

This file was deleted.

41 changes: 17 additions & 24 deletions tests/test_excelAddinGenerator.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
# test_excelAddinGenerator.py

import excel_addin_generator.main
import pytest
from excelAddinGenerator.main import *
from os.path import exists
from filehash import FileHash

def test_success_from_bin():

def test_success_from_bin() -> None:
"""Test that xlam is successfully generated from a OLE file"""
createFromBin("tests/vbaProject.bin", "src/data", "success_bin.xlam")
excel_addin_generator.main.create_from_bin("tests/vbaProject.bin", "src/excel_addin_generator/data", "success_bin.xlam")
# Assert that xlam file is created
assert exists("success_bin.xlam")
#assert that bin file within success_bin.xlam matches tests/vbaProject.bin
extractBinFromZip("success_bin.xlam")
# assert that bin file within success_bin.xlam matches tests/vbaProject.bin
excel_addin_generator.main.extractBinFromZip("success_bin.xlam")
md5hasher = FileHash('md5')
assert md5hasher.hash_file("tests/vbaProject.bin") == md5hasher.hash_file("xl/vbaProject.bin")

createFromZip("success_bin.xlam", "src/data", "success_xlam.xlam")
excel_addin_generator.main.createFromZip("success_bin.xlam", "src/excel_addin_generator/data", "success_xlam.xlam")
assert exists("success_xlam.xlam")
#assert that bin file within success_xlam.xlam matches bin file within success_bin.xlam
extractBinFromZip("success_xlam.xlam")
# assert that bin file within success_xlam.xlam matches bin file within success_bin.xlam
excel_addin_generator.main.extractBinFromZip("success_xlam.xlam")
assert md5hasher.hash_file("tests/vbaProject.bin") == md5hasher.hash_file("xl/vbaProject.bin")

def test_not_bin_exception():


def test_not_bin_exception() -> None:
""" Test that an exception is thrown if the bin file is not an OLE file"""
with pytest.raises(Exception) as e_info:
createFromBin("tests/blank.bin", "src/data", "./fail.xlam")

def test_xlam_not_zip():
""" Test that an exception is thrown if the zip is not a zip archive"""
with pytest.raises(Exception) as e_info:
createFromZip("tests/blank.bin", "src/data", "./fail.xlam")
with pytest.raises(Exception):
excel_addin_generator.main.createFromBin("tests/blank.bin", "src/excel_addin_generator/data", "./fail.xlam")

def test_main():
main(["./excelAddinGenerator", "./tests/vbaProject.bin", "success_bin.xlam"])
main(["./excelAddinGenerator", "success_bin.xlam", "success_xlam.xlam"])

def test_main_incorrect_type():
def test_xlam_not_zip() -> None:
""" Test that an exception is thrown if the zip is not a zip archive"""
with pytest.raises(Exception) as e_info:
main(["./excelAddinGenerator", "./src/data/xl/styles.xml", "fail.xlam"])
with pytest.raises(Exception):
excel_addin_generator.main.createFromZip("tests/blank.bin", "src/excel_addin_generator/data", "./fail.xlam")

0 comments on commit 315c47e

Please sign in to comment.