<commit_before>import pathlib
import queue
import shutil
import sys
import tempfile

import addon_utils
import bpy
from bpy.app.handlers import persistent
from bpy.props import BoolProperty, EnumProperty, IntProperty, StringProperty
from bpy_extras.io_utils import ExportHelper, path_reference_mode


sys.path.append(str(pathlib.Path(__file__).parent))
import previz

from . import utils
from . import three_js_exporter


bl_info = {
    'name': "dandelion + burdock's Previz integration",
    'author': "<NAME> (<EMAIL>)",
    'version': (0, 0, 7),
    'blender': (2, 76, 0),
    'location': "File > Export",
    'description': "Upload scenes to Previz.",
    'category': 'Import-Export'
}


TEMPORARY_DIRECTORY_PREFIX = 'blender-{}-'.format(__name__)


def previz_preferences(context):
    prefs = context.user_preferences.addons[__name__].preferences
    return prefs.api_root, prefs.api_token


###################
# Debugging tools #
###################


def log_function_name(func):
    if bpy.app.debug:
        print('====', func.__qualname__)


def log_call(func):
    def wrapper(*args, **kwargs):
        log_function_name(func)
        return func(*args, **kwargs)

    return wrapper


def log_execute(func):
    def wrapper(self, context):
        log_function_name(func)
        return func(self, context)
    return wrapper


log_draw = log_execute


def log_invoke(func):
    def wrapper(self, context, event):
        log_function_name(func)
        return func(self, context, event)
    return wrapper


####################
# Export to Previz #
####################


class ExportPreviz(utils.BackgroundTasksOperator):
    bl_idname = 'export_scene.previz'
    bl_label = 'Export scene to Previz'

    debug_run_modal = utils.BackgroundTasksOperator.debug_run_modal

    api_root = StringProperty(
        name='API root'
    )

    api_token = StringProperty(
        name='API token'
    )

    project_id = IntProperty(
        name='Previz project ID',
        default=-1
    )

    debug_cleanup = BoolProperty(
        name='Cleanup temporary folder',
        default=True,
        options={'HIDDEN'}
    )

    debug_tmpdir = StringProperty(
        name="Temporary directory absolute path",
        description="Absolute path to temporarily store assets. If empty, a path will be generated automatically.",
        options={'HIDDEN'}
    )

    debug_run_api_requests = BoolProperty(
        name="Run API requests",
        description="Run the requets againsts the Previz API.",
        default=True,
        options={'HIDDEN'}
    )

    def __init__(self):
        utils.BackgroundTasksOperator.__init__(self)

        self.g = {}

    @classmethod
    def poll(cls, context):
        api_root, api_token = previz_preferences(context)
        api_root_is_valid = len(api_root) > 0
        api_token_is_valid = len(api_token) > 0
        previz_project_is_valid = context.scene.previz_project_id >=0

        return api_root_is_valid and api_token_is_valid and previz_project_is_valid

    def build_tasks(self, context):
        tasks = [
            {'func': ExportPreviz.task_make_tmpdir},
            {'func': self.build_task_report('Made temporary directory {tmpdir}')},
            {'func': self.build_task_report('exporting three.js JSON')},
            {'func': ExportPreviz.task_export_three_js}
        ]

        if self.debug_run_api_requests:
            tasks.extend([
                {'func': self.build_task_report('starting API calls')},
                {'func': self.build_task_report('uploading scene')},
                {'func': ExportPreviz.task_update_previz_scene,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('uploading assets')},
                {'func': ExportPreviz.task_update_previz_assets,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('updating state')},
                {'func': ExportPreviz.task_set_previz_state,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('done all API calls')}
            ])

        if self.debug_cleanup:
            tasks.extend([
                {'func': ExportPreviz.task_cleanup_tmpdir},
                {'func': self.build_task_report('removed temporary directory {tmpdir}')}
            ])

        self.tasks.put({'func': self.build_task_report('done')})

        self.g['context'] = context # This is probably not the safest, but only a main process task uses it

        return tasks

    def build_task_report(self, msg):
        def task(g):
            self.report({'INFO'}, 'Previz: ' + msg.format(**g))
            return g
        return task

    @staticmethod
    def task_make_tmpdir(g):<commit_msg>Append last task to the task to be returned, not the initial task anymore... Doh...<commit_after>import pathlib
import queue
import shutil
import sys
import tempfile

import addon_utils
import bpy
from bpy.app.handlers import persistent
from bpy.props import BoolProperty, EnumProperty, IntProperty, StringProperty
from bpy_extras.io_utils import ExportHelper, path_reference_mode


sys.path.append(str(pathlib.Path(__file__).parent))
import previz

from . import utils
from . import three_js_exporter


bl_info = {
    'name': "dandelion + burdock's Previz integration",
    'author': "<NAME> (<EMAIL>)",
    'version': (0, 0, 7),
    'blender': (2, 76, 0),
    'location': "File > Export",
    'description': "Upload scenes to Previz.",
    'category': 'Import-Export'
}


TEMPORARY_DIRECTORY_PREFIX = 'blender-{}-'.format(__name__)


def previz_preferences(context):
    prefs = context.user_preferences.addons[__name__].preferences
    return prefs.api_root, prefs.api_token


###################
# Debugging tools #
###################


def log_function_name(func):
    if bpy.app.debug:
        print('====', func.__qualname__)


def log_call(func):
    def wrapper(*args, **kwargs):
        log_function_name(func)
        return func(*args, **kwargs)

    return wrapper


def log_execute(func):
    def wrapper(self, context):
        log_function_name(func)
        return func(self, context)
    return wrapper


log_draw = log_execute


def log_invoke(func):
    def wrapper(self, context, event):
        log_function_name(func)
        return func(self, context, event)
    return wrapper


####################
# Export to Previz #
####################


class ExportPreviz(utils.BackgroundTasksOperator):
    bl_idname = 'export_scene.previz'
    bl_label = 'Export scene to Previz'

    debug_run_modal = utils.BackgroundTasksOperator.debug_run_modal

    api_root = StringProperty(
        name='API root'
    )

    api_token = StringProperty(
        name='API token'
    )

    project_id = IntProperty(
        name='Previz project ID',
        default=-1
    )

    debug_cleanup = BoolProperty(
        name='Cleanup temporary folder',
        default=True,
        options={'HIDDEN'}
    )

    debug_tmpdir = StringProperty(
        name="Temporary directory absolute path",
        description="Absolute path to temporarily store assets. If empty, a path will be generated automatically.",
        options={'HIDDEN'}
    )

    debug_run_api_requests = BoolProperty(
        name="Run API requests",
        description="Run the requets againsts the Previz API.",
        default=True,
        options={'HIDDEN'}
    )

    def __init__(self):
        utils.BackgroundTasksOperator.__init__(self)

        self.g = {}

    @classmethod
    def poll(cls, context):
        api_root, api_token = previz_preferences(context)
        api_root_is_valid = len(api_root) > 0
        api_token_is_valid = len(api_token) > 0
        previz_project_is_valid = context.scene.previz_project_id >=0

        return api_root_is_valid and api_token_is_valid and previz_project_is_valid

    def build_tasks(self, context):
        tasks = [
            {'func': ExportPreviz.task_make_tmpdir},
            {'func': self.build_task_report('Made temporary directory {tmpdir}')},
            {'func': self.build_task_report('exporting three.js JSON')},
            {'func': ExportPreviz.task_export_three_js}
        ]

        if self.debug_run_api_requests:
            tasks.extend([
                {'func': self.build_task_report('starting API calls')},
                {'func': self.build_task_report('uploading scene')},
                {'func': ExportPreviz.task_update_previz_scene,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('uploading assets')},
                {'func': ExportPreviz.task_update_previz_assets,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('updating state')},
                {'func': ExportPreviz.task_set_previz_state,
                 'run_in_subprocess': True},
                {'func': self.build_task_report('done all API calls')}
            ])

        if self.debug_cleanup:
            tasks.extend([
                {'func': ExportPreviz.task_cleanup_tmpdir},
                {'func': self.build_task_report('removed temporary directory {tmpdir}')}
            ])

        tasks.append({'func': self.build_task_report('done')})

        self.g['context'] = context # This is probably not the safest, but only a main process task uses it

        return tasks

    def build_task_report(self, msg):
        def task(g):
            self.report({'INFO'}, 'Previz: ' + msg.format(**g))
            return g
        return task

    @staticmethod
    def task_make_tmpdir(g):
{'fraction_non_alphanumeric': 0.12230817478569936, 'fraction_numerical': 0.002299811833577253, 'mean_word_length': 2.808519108280255, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102081', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2778, 'n_words': 635}