Blockly文件积木开发

Blockly文件积木开发

今天出一期关于Blockly文件积木开发的教程,废话不多说,上代码

文件积木

文件积木借鉴mixly部分文件积木,python代码可以参考www.runoob.com/python/file... www.runoob.com/python/pyth...

一:构建积木

1,涉及要修改得文件(blocks,file这个是要自己创建) 2,blocks文件引入file文件,并在export导出积木

javascript 复制代码
file文件代码

/**
 * @license
 * Copyright 2012 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

// Former goog.module ID: Blockly.libraryBlocks.files
import '../core/field_dropdown.js';
import {
  createBlockDefinitionsFromJsonArray,
  defineBlocks,
} from '../core/common.js';

export const blocks = createBlockDefinitionsFromJsonArray([
    {
        type: 'file_is_writable',
        style: 'mechanical_arm',
        output: 'Boolean',
        message0: "%{BKY_FILE_IS_WRITABLE}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_get_name',
        style: 'mechanical_arm',
        output: 'String',
        message0: "%{BKY_FILE_GET_NAME}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_close',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        message0: "%{BKY_FILE_CLOSE}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_listdir',
        style: 'mechanical_arm',
        output: 'Array',
        message0: "%{BKY_FILE_LISTDIR}",
        args0: [],
    },
    {
        type: 'file_getcwd',
        style: 'mechanical_arm',
        output: 'String',
        message0: "%{BKY_FILE_GETCWD}",
        args0: [],
    },
    {
        type: 'file_chdir',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        message0: "%{BKY_FILE_CHDIR}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_mkdir',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        message0: "%{BKY_FILE_MKDIR}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            },
            {
                type: "field_dropdown",
                name: "type",
                options: [
                    ["文件夹", 'mkdir'],
                    ["嵌套的文件夹", "makedirs"]
                ]
            }
        ]
    },
    {
        type: 'file_remove',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        message0: "%{BKY_FILE_REMOVE}",
        args0: [
            {
                type: "field_dropdown",
                name: "type",
                options: [
                    ["删除文件", 'remove'],
                    ["递归删除目录", "removedirs"]
                ]
            },
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_rename',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        inputsInline: true,
        message0: "%{BKY_FILE_RENAME}",
        args0: [
            {
                type: 'input_value',
                name: 'OLD',
                check: 'String'
            },
            {
                type: 'input_value',
                name: 'NEW',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_tell',
        style: 'mechanical_arm',
        output: 'String',
        message0: "%{BKY_FILE_TELL}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            }
        ]
    },
    {
        type: 'file_seek',
        style: 'mechanical_arm',
        previousStatement: null,
        nextStatement: null,
        message0: "%{BKY_FILE_SEEK}",
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: 'String'
            },
            {
                type: "field_dropdown",
                name: "type",
                options: [
                    ["从文件开头算起", '0'],
                    ["从文件当前位置算起", "1"],
                    ["从文件结尾算起", "2"]
                ]
            },
            {
                type: 'input_value',
                name: 'POSITION',
                check: 'Number'
            }
        ]
    }
])

// Register provided blocks.
defineBlocks(blocks);

至此文件积木已经完成,看一下效果图

二:构建python

1,涉及要修改得文件(python,file这个是要自己创建) 2,python文件引入file文件,并在export导出python代码

javascript 复制代码
file文件代码

/**
 * @license
 * Copyright 2012 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @file Generating Python for files blocks.
 */

// Former goog.module ID: Blockly.Python.files

import type {Block} from '../../core/block.js';
import type {PythonGenerator} from './python_generator.js';
import {Order} from './python_generator.js';

export function file_is_writable(block: Block, generator: PythonGenerator): [string, Order] {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let code = `${text}.writable()\n`
    return [code, Order.ATOMIC]
}

export function file_get_name(block: Block, generator: PythonGenerator): [string, Order] {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let code = `${text}.name()\n`
    return [code, Order.ATOMIC]
}

export function file_close(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let code = `${text}.close()\n`
    return code
}

export function file_listdir(block: Block, generator: PythonGenerator): [string, Order] {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let code = `os.listdir()\n`
    return [code, Order.ATOMIC]
}

export function file_getcwd(block: Block, generator: PythonGenerator): [string, Order] {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let code = `os.getcwd()\n`
    return [code, Order.ATOMIC]
}

export function file_chdir(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let code = `os.chdir(${text})\n`
    return code
}

export function file_mkdir(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let type = block.getFieldValue('type')
    let code = `os.${type}(${text})\n`
    return code
}

export function file_remove(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let type = block.getFieldValue('type')
    let code = `os.${type}(${text})\n`
    return code
}

export function file_rename(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let old = generator.valueToCode(block, 'OLD', Order.NONE) || "''";
    let news = generator.valueToCode(block, 'NEW', Order.NONE) || "''";
    let code = `os.rename(${old}, ${news})\n`
    return code
}

export function file_tell(block: Block, generator: PythonGenerator): [string, Order] {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let code = `${text}.tell()\n`
    return [code, Order.ATOMIC]
}

export function file_seek(block: Block, generator: PythonGenerator) {
    (generator as AnyDuringMigration).definitions_['import_os'] = 'import os'

    let text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
    let type = block.getFieldValue('type')
    let position = generator.valueToCode(block, 'POSITION', Order.NONE)
    let code = `${text}.seek(${position}, ${type})\n`
    return code
}

至此文件python已经完成,看一下效果图 总结:文件目录在一些文档里面是有,可以更具自己的功能需求去做相应的积木,开发难度不大

相关推荐
陈天伟教授12 分钟前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
信看1 小时前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
Tony Bai1 小时前
【API 设计之道】04 字段掩码模式:让前端决定后端返回什么
前端
苏打水com1 小时前
第十四篇:Day40-42 前端架构设计入门——从“功能实现”到“架构思维”(对标职场“大型项目架构”需求)
前端·架构
king王一帅2 小时前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
苏打水com2 小时前
第十八篇:Day52-54 前端跨端开发进阶——从“多端适配”到“跨端统一”(对标职场“全栈化”需求)
前端
Bigger2 小时前
后端拒写接口?前端硬核自救:纯前端实现静态资源下载全链路解析
前端·浏览器·vite
BD_Marathon2 小时前
【JavaWeb】路径问题_前端绝对路径问题
前端
whyfail3 小时前
Vue原理(暴力版)
前端·vue.js