Skip to content

Commit

Permalink
Merge pull request #6 from Tomansion/Pypi-implementation
Browse files Browse the repository at this point in the history
Changed the project structure to be able to publish it on pypi
  • Loading branch information
Tomansion authored Oct 19, 2022
2 parents 801518d + 39035b1 commit 44158bb
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 39 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: build

on:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build wheel
- name: Build package
run: ./build_package.sh
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: build_package
28 changes: 28 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will install Python dependencies and run tests with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: tests

on:
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install flake8 pytest-cov
pip install -r requirements.txt
- name: Test with pytest
run: |
pytest --cov=factorio_blueprint_analyser tests/test_all_blueprints.py -s
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __pycache__/
analysed_blueprint.json

# Tests coverage & profiling
lib/
.coverage
.pytest_cache
htmlcov/
Expand Down
4 changes: 2 additions & 2 deletions blueprint_analyser
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
from src import (
from factorio_blueprint_analyser import (
options,
blueprint_analyser
)
Expand All @@ -12,7 +12,7 @@ if __name__ == "__main__":

blueprint_analyser.init()

analysed_blueprint = blueprint_analyser.calculate_blueprint_bottleneck(
analysed_blueprint = blueprint_analyser.analyse_blueprint_from_path(
options.input)

# Export analysed blueprint in a json file
Expand Down
2 changes: 1 addition & 1 deletion config/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ factorio:
# This default data corresponds to the vanilla game
# To set your own data, you can follow our mod guide
# (Comming soon)
data_file_path: "src/assets/factorio_raw/factorio_raw_min.json"
data_file_path: "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json"

network:
# The alogrithm will displat the
Expand Down
2 changes: 1 addition & 1 deletion doc/use_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ factorio:
# This default data corresponds to the vanilla game
# To set your own data, you can follow our mod guide
# (Comming soon)
data_file_path: "src/assets/factorio_raw/factorio_raw_min.json"
data_file_path: "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json"

network:
# The alogrithm will displat the
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
# It's easier to work with a more readable Json file
# -----------------------------------------------------------

data_file_path = "src/assets/factorio_raw/factorio_raw.json"
data_file_export_path = "src/assets/factorio_raw/factorio_raw_min.json"
data_file_path = "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw.json"
data_file_export_path = "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json"

key_to_keep = [
"recipe",
Expand Down
File renamed without changes.
25 changes: 18 additions & 7 deletions src/blueprint.py → factorio_blueprint_analyser/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from src import utils, entity, config
from factorio_blueprint_analyser import utils, entity, config

# -----------------------------------------------------------
# Read the blueprint from the given file
Expand Down Expand Up @@ -118,7 +118,8 @@ def __init__(self, bp_json):
'position': {'x': drop_coord[0], 'y': drop_coord[1]}
}, virtual=True)

utils.verbose(f"Adding temporary entity {container}")
utils.verbose(
f"Adding temporary entity {container}")
self.array[drop_coord[1]
][drop_coord[0]] = container

Expand Down Expand Up @@ -319,17 +320,27 @@ def _get_entity(self, entity_number, entites):
return {}


def load_blueprint(file):
def load_blueprint(blueprint_sting):
try:
# Try to read the string directly as a JSON
blueprint_json = json.loads(blueprint_sting)
except json.decoder.JSONDecodeError:
# If it fails, try to decode it
blueprint_json = utils.decode(blueprint_sting)

return Blueprint(blueprint_json)


def load_blueprint_from_path(file_path):
# Read the file
if file.endswith(".json"):
if file_path.endswith(".json"):
# No need to decode the json
with open(file, 'r') as f:
with open(file_path, 'r') as f:
bp_json = json.load(f)

else:
with open(file, 'r') as f:
with open(file_path, 'r') as f:
bp_encoded = f.read()
bp_json = utils.decode(bp_encoded)

# Return the blueprint
return Blueprint(bp_json)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from src import (
from factorio_blueprint_analyser import (
factorio,
blueprint,
network,
Expand All @@ -15,9 +15,17 @@ def init(config_path=None):
factorio.load_data()


def calculate_blueprint_bottleneck(blueprint_path):
# Read the input blueprint
bp = blueprint.load_blueprint(blueprint_path)
def analyse_blueprint(blueprint_string):
bp = blueprint.load_blueprint(blueprint_string)
return _process_blueprint(bp)


def analyse_blueprint_from_path(blueprint_path):
bp = blueprint.load_blueprint_from_path(blueprint_path)
return _process_blueprint(bp)


def _process_blueprint(bp):
bp.display()

# Creade a node network from the blueprint
Expand Down
2 changes: 1 addition & 1 deletion src/config.py → factorio_blueprint_analyser/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# factorio:
# difficulty: normal
# inserter_capacity_bonus: 0
# data_file_path: "src/assets/factorio_raw/factorio_raw_min.json"
# data_file_path: "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json"
# verbose_level: 3
# network:
# display: true
Expand Down
2 changes: 1 addition & 1 deletion src/entity.py → factorio_blueprint_analyser/entity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import floor
from termcolor import colored

from src import factorio, recipe, utils, config
from factorio_blueprint_analyser import factorio, recipe, utils, config

# -----------------------------------------------------------
# Base class for all entities
Expand Down
4 changes: 2 additions & 2 deletions src/factorio.py → factorio_blueprint_analyser/factorio.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import json

from src import utils, config
from factorio_blueprint_analyser import utils, config

# -----------------------------------------------------------
# Provide for the other files Factorio data
# from the src/assets/factorio_raw/factorio_raw_min.json file
# from the factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json file
# -----------------------------------------------------------

recipies_key = "recipe"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/network.py → factorio_blueprint_analyser/network.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src import node as node_service, utils
from factorio_blueprint_analyser import node as node_service, utils
from pyvis.network import Network as NetworkDisplay

# -----------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/node.py → factorio_blueprint_analyser/node.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src import utils, item
from factorio_blueprint_analyser import utils, item

# -----------------------------------------------------------
# Network nodes properties
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/recipe.py → factorio_blueprint_analyser/recipe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from src import factorio, item, utils
from factorio_blueprint_analyser import factorio, item, utils

# -----------------------------------------------------------
# Assembly machines recipe class
Expand Down
2 changes: 1 addition & 1 deletion src/utils.py → factorio_blueprint_analyser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import base64
from termcolor import colored

from src import config
from factorio_blueprint_analyser import config


def verbose(content, end="\n", level=3):
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
colored
termcolor==2.0.1
PyYAML==5.4.1
pyvis==0.3.0
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[coverage:run]
branch = True
omit = src/assets/factorio_raw/exctract.py
omit = factorio_blueprint_analyser/assets/factorio_raw/exctract.py

[coverage:report]
show_missing = True
skip_covered = True

[tool:pytest]
addopts = --cov src/
addopts = --cov factorio_blueprint_analyser/
18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
from setuptools import setup
from pathlib import Path
import json

parent_dir = Path(__file__).resolve().parent

setup(
name='factorioBlueprintAnalyser',
version='1.0.0',
description="A python library analyse Factorio Blueprints and find bottlenecks.",
url="https://github.com/tomansion/factorio_blueprint_analyser_app/",
author="Tom Mansion",
license="MIT License",
install_requires=parent_dir.joinpath(
"requirements.txt").read_text().splitlines(),
packages=['factorioBlueprintAnalyser'],
)
22 changes: 11 additions & 11 deletions tests/test_all_blueprints.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from src import blueprint_analyser
from factorio_blueprint_analyser import blueprint_analyser
from os import listdir

# Profiling
import pytest
import pstats
import cProfile
profiler = cProfile.Profile()
# import cProfile
# profiler = cProfile.Profile()


config_file_path = "config/config_tests.yaml"
Expand All @@ -14,11 +14,11 @@

blueprint_analyser.init(config_file_path)

nb_tests_to_do = 10

nb_tests_to_do = 20

def test_all_blueprints():
profiler.enable()
print("Testing all blueprints")
# profiler.enable()

for (i, blueprint) in enumerate(blueprints):
if i >= nb_tests_to_do:
Expand All @@ -28,9 +28,9 @@ def test_all_blueprints():
f" === Analysing blueprint: {blueprint}, {i+1}/{len(blueprints)}")
blueprint_path = f"{blueprints_path}/{blueprint}"

blueprint_analyser.calculate_blueprint_bottleneck(blueprint_path)
blueprint_analyser.analyse_blueprint_from_path(blueprint_path)

profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats(pstats.SortKey.TIME)
stats.dump_stats(filename="stats.prof")
# profiler.disable()
# stats = pstats.Stats(profiler)
# stats.sort_stats(pstats.SortKey.TIME)
# stats.dump_stats(filename="stats.prof")

0 comments on commit 44158bb

Please sign in to comment.