Skip to content

Commit

Permalink
Update: Make generation code common between fake-bpy-module and fake-…
Browse files Browse the repository at this point in the history
…bge-module
  • Loading branch information
nutti committed Aug 17, 2023
1 parent 2e0a562 commit c6338da
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 194 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fake-bpy-module-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
- name: Generate pip Packages
env:
RELEASE_VERSION: ${{ needs.set_versions.outputs.module_version }}
run: bash tools/pip_package/build_pip_package.sh release ${{ matrix.blender_version }} ./blender ./blender-bin/blender-v${{ matrix.blender_version }}-bin ${{ matrix.blender_version }}
run: bash tools/pip_package/build_pip_package.sh release blender ${{ matrix.blender_version }} ./blender ./blender-bin/blender-v${{ matrix.blender_version }}-bin ${{ matrix.blender_version }}

- name: Archive pip packages
uses: actions/upload-artifact@v2
Expand All @@ -130,7 +130,7 @@ jobs:
# For example: 00:29:45 UTC will create a file version
# "...dev2945-py3-none..." and not use {{ module_version }}
# "...dev002945-py3-none..."
run: bash tests/pylint_cycles.sh ${{ matrix.blender_version }} ./blender/ ./release/${{ matrix.blender_version }}/fake_bpy_module_${{ matrix.blender_version }}-*-py3-none-any.whl
run: bash tests/pylint_cycles.sh blender ${{ matrix.blender_version }} ./blender/ ./release/${{ matrix.blender_version }}/fake_bpy_module_${{ matrix.blender_version }}-*-py3-none-any.whl

- name: Collect failure state
if: failure()
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/fake-bpy-module-latest-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
- name: Generate pip Packages
env:
RELEASE_VERSION: ${{ needs.set_versions.outputs.module_version }}
run: bash tools/pip_package/build_pip_package.sh release latest ./blender ./blender-bin/blender-latest-bin
run: bash tools/pip_package/build_pip_package.sh release blender latest ./blender ./blender-bin/blender-latest-bin

- name: Archive pip packages
uses: actions/upload-artifact@v2
Expand All @@ -97,7 +97,7 @@ jobs:
run: bash tests/run_tests.sh raw_modules

- name: Test generated pip module against Cycles addon
run: bash tests/pylint_cycles.sh latest ./blender/ ./release/latest/fake_bpy_module_latest-*-py3-none-any.whl
run: bash tests/pylint_cycles.sh blender latest ./blender/ ./release/latest/fake_bpy_module_latest-*-py3-none-any.whl

- name: Collect failure state
if: failure()
Expand Down
35 changes: 20 additions & 15 deletions src/fake_bpy_module/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import copy

from . import support
from .common import (
BuiltinDataType,
CustomDataType,
Expand Down Expand Up @@ -54,20 +55,20 @@ def make_next_level(self, spaces_to_add: str) -> 'RstLevel':

class BaseAnalyzer:
def __init__(self):
self.support_bge: bool = False
self.target: str = None # "blender" or "upbge"
self.current_file: str = None
self.current_module: str = None
self.current_base_classes: str = None
self.blender_version: str = None
self.target_version: str = None # Ex: "2.80"

def set_blender_version(self, version: str):
self.blender_version = version
def set_target_version(self, version: str):
self.target_version = version

def enable_bge_support(self):
self.support_bge = True
def set_target(self, target: str):
self.target = target

def _is_bge_supported(self) -> bool:
return self.support_bge
def _target(self) -> str:
return self.target

def _cleanup_string(self, line: str) -> str:
result = line
Expand Down Expand Up @@ -108,11 +109,11 @@ def _parse_module(self, file: IO[Any], level: int) -> str:

module_name = m.group(2)

if not self.support_bge:
if self.blender_version == "2.90":
if self.target == "blender":
if self.target_version == "2.90":
if module_name.startswith("bpy.types."):
module_name = module_name[:module_name.rfind(".")]
elif self.blender_version in [
elif self.target_version in [
"2.91", "2.92", "2.93",
"3.0", "3.1", "3.2", "3.3", "3.4", "latest"]:
if module_name == "bpy.data":
Expand Down Expand Up @@ -991,7 +992,7 @@ def _parse_static_method(
elif re.match(r"^\s{" + str(level.num_spaces()) + r"}(\s+)\.\. attribute::", line): # noqa # pylint: disable=C0301
next_level_spaces = re.match(r"^\s{" + str(level.num_spaces()) + r"}(\s+)\.\. attribute::", line).group(1) # noqa # pylint: disable=C0301
is_deprecated = re.search(r"\(Deprecated", line) is not None
if self._is_bge_supported() and is_deprecated:
if self._target() == "upbge" and is_deprecated:
self._skip_until_next_le_level(
file, level=level.make_next_level(next_level_spaces))
else:
Expand Down Expand Up @@ -1059,7 +1060,7 @@ def _analyze_by_file(self, filename: str) -> 'SectionInfo':
line = file.readline()
section = SectionInfo()
self.current_base_classes = None
if self._is_bge_supported() and \
if self._target() == "upbge" and \
re.search(r"/bge\.types\.(?!rst)", filename) is not None:
self.current_module = "bge.types"
else:
Expand All @@ -1083,7 +1084,7 @@ def _analyze_by_file(self, filename: str) -> 'SectionInfo':
section.add_info(class_info)
elif re.match(r"^\.\. function::", line):
deprecated = re.search(r"\(Deprecated", line) is not None
if self._is_bge_supported() and deprecated:
if self._target() == "upbge" and deprecated:
self._skip_until_next_le_level(file, level=RstLevel())
else:
file.seek(last_pos)
Expand All @@ -1097,7 +1098,7 @@ def _analyze_by_file(self, filename: str) -> 'SectionInfo':
section.add_info(function_info)
elif re.match(r"^\.\. (data|DATA)::", line):
deprecated = re.search(r"\(Deprecated", line) is not None
if self._is_bge_supported() and deprecated:
if self._target() == "upbge" and deprecated:
self._skip_until_next_le_level(file, level=RstLevel())
else:
file.seek(last_pos)
Expand Down Expand Up @@ -1142,6 +1143,10 @@ def _analyze_by_file(self, filename: str) -> 'SectionInfo':
return section_none_removed

def analyze(self, filenames: List[str]) -> 'AnalysisResult':
assert self.target in support.SUPPORTED_TARGET
assert (self.target_version in support.SUPPORTED_BLENDER_VERSION or
self.target_version in support.SUPPORTED_UPBGE_VERSION)

result = AnalysisResult()
for f in filenames:
info = self._analyze_by_file(f)
Expand Down
10 changes: 4 additions & 6 deletions src/fake_bpy_module/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ def __init__(self):
self.os: str = "Linux"
self.style_format: str = "pep8"
self.dump: bool = False
self.blender_version: str = None
self.target: str = "blender"
self.target_version: str = None
self.mod_version: str = None
self.support_bge: bool = False


class PackageGenerationRule:
Expand Down Expand Up @@ -576,10 +576,8 @@ def _analyze_by_rule(
# replace windows path separator
target_files = [f.replace("\\", "/") for f in rule.target_files()]
# analyze all .rst files
if self._config.support_bge:
rule.analyzer().enable_bge_support()
if self._config.blender_version is not None:
rule.analyzer().set_blender_version(self._config.blender_version)
rule.analyzer().set_target(self._config.target)
rule.analyzer().set_target_version(self._config.target_version)
result = rule.analyzer().analyze(target_files)

return result
Expand Down
37 changes: 37 additions & 0 deletions src/fake_bpy_module/support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List

SUPPORTED_TARGET: List[str] = [
"blender",
"upbge"
]

SUPPORTED_STYLE_FORMAT: List[str] = [
"none",
"pep8"
]

SUPPORTED_MOD_BLENDER_VERSION: List[str] = [
"2.78", "2.79",
"2.80", "2.81", "2.82", "2.83",
"2.90", "2.91", "2.92", "2.93",
"3.0", "3.1", "3.2", "3.3", "3.4",
"latest"
]

SUPPORTED_MOD_UPBGE_VERSION: List[str] = [
"0.2.5",
"latest"
]

SUPPORTED_BLENDER_VERSION: List[str] = [
"2.78", "2.79",
"2.80", "2.81", "2.82", "2.83",
"2.90", "2.91", "2.92", "2.93",
"3.0", "3.1", "3.2", "3.3", "3.4",
"latest"
]

SUPPORTED_UPBGE_VERSION: List[str] = [
"0.2.5",
"latest"
]
90 changes: 58 additions & 32 deletions src/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@
import fake_bpy_module as fbm

INPUT_DIR: str = "."
SUPPORTED_TARGET: List[str] = ["pycharm"]
SUPPORTED_STYLE_FORMAT: List[str] = ["none", "pep8"]
SUPPORTED_MOD_BLENDER_VERSION: List[str] = [
"2.78", "2.79",
"2.80", "2.81", "2.82", "2.83",
"2.90", "2.91", "2.92", "2.93",
"3.0", "3.1", "3.2", "3.3", "3.4",
"latest"
]
SUPPORTED_BLENDER_VERSION: List[str] = [
"2.78", "2.79",
"2.80", "2.81", "2.82", "2.83",
"2.90", "2.91", "2.92", "2.93",
"3.0", "3.1", "3.2", "3.3", "3.4",
"latest"
]
MOD_FILES_DIR: str = os.path.dirname(os.path.abspath(__file__))


Expand Down Expand Up @@ -167,6 +151,14 @@ def make_bl_math_rule(
fbm.BaseGenerator())


def make_bge_rule(
_: 'fbm.PackageGeneratorConfig') -> 'fbm.PackageGenerationRule':
files = glob.glob(INPUT_DIR + "/bge*.rst")
files.extend(glob.glob(INPUT_DIR + "/bge_types/bge*.rst"))
return fbm.PackageGenerationRule(
"bge", files, fbm.BaseAnalyzer(), fbm.BaseGenerator())


def make_other_rules(config: 'fbm.PackageGeneratorConfig') -> List['fbm.PackageGenerationRule']: # noqa # pylint: disable=C0301
mod_files = glob.glob(
f"{MOD_FILES_DIR}/mods/generated_mods/gen_modules_modfile/*.json"
Expand Down Expand Up @@ -200,9 +192,10 @@ def make_other_rules(config: 'fbm.PackageGeneratorConfig') -> List['fbm.PackageG

def parse_options(config: 'fbm.PackageGeneratorConfig'):
# pylint: disable=W0603
global INPUT_DIR, SUPPORTED_TARGET # pylint: disable=W0602
global INPUT_DIR # pylint: disable=W0602
usage = f"Usage: python {__file__} [-i <input_dir>] [-o <output_dir>] " \
"[-d] [-f <style_format>] [-m <mod_version>]"
"[-T <target>] [-t <target_version>] [-d] [-f <style_format>] " \
"[-m <mod_version>]"
parser = argparse.ArgumentParser(usage)
parser.add_argument(
"-i", dest="input_dir", type=str, help="Input directory"
Expand All @@ -223,8 +216,12 @@ def parse_options(config: 'fbm.PackageGeneratorConfig'):
"(ex. 2.79, 2.80)"
)
parser.add_argument(
"-b", dest="blender_version", type=str,
help="Blender version (ex. 2.79, 2.80)"
"-T", dest="target", type=str,
help="Target (blender, upbge)"
)
parser.add_argument(
"-t", dest="target_version", type=str,
help="Target version (ex. 2.79, 2.80)"
)
parser.add_argument(
"-l", dest="output_log_level", type=str,
Expand All @@ -236,27 +233,54 @@ def parse_options(config: 'fbm.PackageGeneratorConfig'):
if args.output_dir:
config.output_dir = args.output_dir

if args.style_format in SUPPORTED_STYLE_FORMAT:
if args.style_format in fbm.support.SUPPORTED_STYLE_FORMAT:
config.style_format = args.style_format
else:
raise RuntimeError(
f"Not supported style format {args.style_format}. "
f"(Supported Style Format: {SUPPORTED_STYLE_FORMAT})")
if args.mod_version:
if args.mod_version in SUPPORTED_MOD_BLENDER_VERSION:
config.mod_version = args.mod_version
f"(Supported Style Format: {fbm.support.SUPPORTED_STYLE_FORMAT})")

if args.target in fbm.support.SUPPORTED_TARGET:
config.target = args.target
else:
raise RuntimeError(
f"Not supported target {args.target}."
f"(Supported Target: {fbm.support.SUPPORTED_TARGET})")

if args.target == "blender":
if args.target_version in fbm.support.SUPPORTED_BLENDER_VERSION:
config.target_version = args.target_version
else:
raise RuntimeError(
f"Not supported mod version {args.mod_version}. "
f"(Supported Version: {SUPPORTED_MOD_BLENDER_VERSION})")
f"Not supported blender version {args.target_version}. "
f"(Supported Version: "
f"{fbm.support.SUPPORTED_BLENDER_VERSION})")

if args.blender_version:
if args.blender_version in SUPPORTED_BLENDER_VERSION:
config.blender_version = args.blender_version
if args.target == "upbge":
if args.target_version in fbm.support.SUPPORTED_UPBGE_VERSION:
config.target_version = args.target_version
else:
raise RuntimeError(
f"Not supported blender version {args.blender_version}. "
f"(Supported Version: {SUPPORTED_BLENDER_VERSION})")
f"Not supported upbge version {args.target_version}. "
f"(Supported Version: {fbm.support.SUPPORTED_UPBGE_VERSION})")

if args.mod_version:
if config.target == "blender":
if args.mod_version in fbm.support.SUPPORTED_MOD_BLENDER_VERSION:
config.mod_version = args.mod_version
else:
raise RuntimeError(
f"Not supported mod version {args.mod_version}. "
f"(Supported Version: "
f"{fbm.support.SUPPORTED_MOD_BLENDER_VERSION})")
elif config.target == "upbge":
if args.mod_version in fbm.support.SUPPORTED_MOD_UPBGE_VERSION:
config.mod_version = args.mod_version
else:
raise RuntimeError(
f"Not supported mod version {args.mod_version}. "
f"(Supported Version: "
f"{fbm.support.SUPPORTED_MOD_UPBGE_VERSION})")

if args.output_log_level:
ARG_TO_LOG_LEVEL = {
Expand Down Expand Up @@ -291,6 +315,8 @@ def main():
pkg_generator.add_rule(make_idprop_rule(config))
pkg_generator.add_rule(make_imbuf_rule(config))
pkg_generator.add_rule(make_bl_math_rule(config))
if config.target == "upbge":
pkg_generator.add_rule(make_bge_rule(config))
for rule in make_other_rules(config):
pkg_generator.add_rule(rule)
pkg_generator.generate()
Expand Down
Loading

0 comments on commit c6338da

Please sign in to comment.