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

相关推荐
百万蹄蹄向前冲2 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙2 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan3 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen3 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理4 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
IT_陈寒5 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端
Blanche15005 小时前
利用 RAG 为答疑机器人扩展知识范围
前端
天若有情6735 小时前
【纯前端小工具】公历生日转农历,批量查询每年农历生日对应的公历日期(GitHub Pages在线直接用)
前端·javascript·github pages·农历转换·lunisolar·网页小工具
SoonITer5 小时前
怎样构建一个 Agent-friendly 的网站
前端·agent
颜进强6 小时前
01 · NestJS 是什么:用途、解决什么问题、与热门框架对比
前端·后端·ai编程