用于将应用程序包装到定制的交互式shell中的包。

riposte的Python项目详细描述


报告

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<======================"BuiBuild StatuStatuStatuStatuStatuStatuStatuStatuStatuStatuStatuStatuStatuStatus" src "src" src "src" src "src src src src 374652e7376673f6272616e63683d6d6173746572" /><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<亚胺咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪咪67" />

Reposte允许您轻松地将应用程序包装在定制的 交互式外壳。关于构建repls的常见杂务被分解出来,并且 被照顾好,这样你就可以真正关注你的特定领域逻辑 应用程序。

建立反击的动机来自于许多不眠之夜 在 开发。喜欢 每一个其他的项目都是无辜地开始的,但是过了一段时间,当这个项目 得到了一些真正的牵引力,代码库迅速增长,shell逻辑开始 与领域逻辑交织,使事物越来越不可读 方便投稿。

而且,令我们惊讶的是,人们开始用叉子叉 routersproit不是因为他们 对嵌入式设备的安全性感兴趣,但仅仅是因为他们想 利用我们的交互式shell逻辑并使用类似的 概念。这些年来,他们一定说过:"一定有更好的办法!" 他们是完全正确的,更好的方法叫做"还击"。

目录

开始

安装

该软件包在pypi上,请使用 pip安装:

pip install riposte

Reposte支持Python3.6和更新版本。

示例用法

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 

手动

命令

首先,您需要注册一些命令,使您的repl 可诉的可以添加命令并将其与处理函数绑定 通过reposte.commanddecorator。

fromriposteimportRiposterepl=Riposte()@repl.command("hello")defhello():repl.success("Is it me you looking for?")repl.run()
riposte:~ $ hello
[+] Is it me you looking for?

此外,reposte.command还接受一些可选参数:

  • 说明 有意义的帮助
  • 如何解释传递的参数的定义

完成者

reposte支持命令的制表符完成。你可以注册 以注册命令的类似方式完成函数,只需使用还击。complete decorator并将其指向特定命令。

fromriposteimportRiposterepl=Riposte()START_SUBCOMMANDS=["foo","bar"]@repl.command("start")defstart(subcommand:str):ifsubcommandinSTART_SUBCOMMANDS:repl.status(f"{subcommand} started")else:repl.error("Unknown subcommand.")@repl.complete("start")defstart_completer(text,line,start_index,end_index):return[subcommandforsubcommandinSTART_SUBCOMMANDSifsubcommand.startswith(text)]repl.run()

completer函数由tab键触发。每个完成函数都应该 返回有效选项列表,并应接受以下参数:

  • 文本行中的最后一个单词
  • 整行内容
  • 开始索引行中最后一个单词的开始索引
  • 结束索引行中最后一个单词的结束索引

在我们的例子中:

反击:~$start ba<;tab>;

text -> "ba"
line -> "start ba"
start_index -> 6
end_index -> 8

有了这些信息,您可以为 每个命令。

指南

指南是一种说明命令应该如何解释参数的方法 由用户通过提示传递。反击依赖 键入提示以便执行此操作。

fromriposteimportRiposterepl=Riposte()@repl.command("guideme")defguideme(x:int,y:str):repl.print("x:",x,type(x))repl.print("y:",y,type(y))repl.run()
riposte:~ $ guideme 11
x: 1 <class 'int'>
y: 1 <class 'str'>

在这两种情况下,我们都将value1传递为xy。基于 参数的类型提示传递的参数在x的情况下解释为int。 对于y,则为str。您也可以将此技术用于不同的类型。

pip install riposte
0
pip install riposte
1

另一种更强大的定义函数参数指南的方法 直接从retposte.commanddecorator定义它。在这种情况下,指南 以这种方式定义的优先于类型提示。

pip install riposte
2
pip install riposte
3

为什么它更强大?因为这样你就可以把不同的向导连起来, 其中一个指南的输出是另一个指南的输入,创建验证或转换 输入更复杂的类型。

pip install riposte
4
pip install riposte
5

在引擎盖下,它是一个简单的函数调用,在这里传递输入字符串 在链条中的第一个导向功能。在这种情况下,呼叫如下:

pip install riposte
6

打印

Reposte提供内置的线程安全打印方法:

  • 打印
  • 信息
  • 错误
  • 状态
  • 成功

每个方法都遵循python内置的签名 print()函数。 除此之外,它们都提供与其名称对应的信息性颜色。

我们强烈建议您使用我们的线程安全打印api,但如果您是 感觉很快活,知道你在做什么,你百分之百确定 执行是在 您的应用程序可以随意使用python的内置 print()函数。

扩展printermixin

如果要更改现有方法的样式或添加自定义方法,请 扩展printermixin类。

pip install riposte
7

自定义printermix在

对现有的打印api不满意?没问题,你也可以自己建 从头开始使用printerbasemixin及其线程安全的print方法。

pip install riposte
8

托盘着色输出

如果您想在输出中添加一些颜色,可以始终使用托盘

pip install riposte
9

托盘采用以下输出格式:

  • 灰色
  • 红色
  • 绿色
  • 黄色
  • 蓝色
  • 洋红色
  • 青色
  • 白色
  • 粗体

历史记录

命令历史记录存储在主目录的文件中。 默认长度为100行。两种设置都可以使用 历史记录文件历史记录长度参数。

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
0

提示

默认提示是retposte:~$,但您可以轻松自定义它:

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
1

您还可以根据某些对象的状态动态解析提示布局 只需覆盖retposte.prompt属性。在下面的示例中,我们将 根据模块的值确定提示:

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
2
fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
3

横幅

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
4
fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
5

如果由于某种原因您不想显示横幅(可能您有自定义 输入流?)您可以设置reposte.print_banner属性 false

内联命令执行

与bash类似,如果用分号分隔命令,则可以触发 在一行中执行多个命令。

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
6

reposte还为您的应用程序公开了cli,使您能够 使用-c开关传递命令:

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
7

考虑到所有这些,您还可以开始将应用程序视为 可以转换成自动脚本。

CLI

如果应用程序需要自定义cli参数 通过覆盖retposte.setup_cli()方法来实现它。假设你想 在应用程序中引入--verbose标志:

fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
8
fromriposteimportRipostecalculator=Riposte(prompt="calc:~$ ")MEMORY=[]@calculator.command("add")defadd(x:int,y:int):result=f"{x} + {y} = {x + y}"MEMORY.append(result)calculator.success(result)@calculator.command("multiply")defmultiply(x:int,y:int):result=f"{x} * {y} = {x * y}"MEMORY.append(result)calculator.success(result)@calculator.command("memory")defmemory():forentryinMEMORY:calculator.print(entry)calculator.run()
9

reposte.parser是python内置的一个实例 因此,有关添加cli参数的所有进一步说明,请遵循 argparse 文档。

传递的参数将在retposte.run()中分析并存储在 反驳.参数以便您可以在应用程序中访问它。如果你需要 要在进入主计算循环之前访问它们,可以覆盖 反击.parse_cliu arguments()

calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
0

输入流

输入流是一个抽象,它告诉您如何使用 命令。现在,您可以使用以下开箱即用的选项。

提示

默认值,允许您使用传统提示输入命令。

CLI

reposte还为您的应用程序公开了cli,使您能够 使用-c开关传递命令:

calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
1

文件

也可以将包含命令的文本文件作为参数传递给 应用程序:

calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
2

commands.rpst包含要执行的命令的文本文件:

calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
3
calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
4

添加自定义输入流

如果出于某种原因,您需要一种自定义的方法来使用命令来填充 您始终可以添加自定义输入流。输入流是一个生成器 生成函数,调用该函数后返回字符串(命令) 生成器[callable[],str],none,none]。假设你是个邪恶的天才 并希望通过 为它提供某种信息系统。

calc:~$ add 22[+]2 + 2=4
calc:~$ multiply 33[+]3 * 3=9
calc:~$ memory
2 + 2=43 * 3=9
calc:~$ 
5

项目状态

正在开发还击。它可能被认为是处于beta阶段。 未来可能会有一些突破性的变化,尽管有很多概念 在场的人已经在 routersproit开发。

贡献

有关我们的行为准则和 向我们提交请求的过程。

版本控制

项目使用版本控制。对于版本 可用,请参阅版本

许可证

Reposte是根据麻省理工学院的许可证授权的-请参见 许可证有关详细信息的文件

致谢

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
orm如何在Java中实现规范化映射?   java以编程方式修改Xtext生成的Mwe2工作流   java正在覆盖外部文件中的现有数据,如何停止?   java在ANTLR BNF语法符号中epsilon的等价物是什么?   java如何使用Hibernate@Anyrelated注释?   代码生成生成java类并在运行时加载它   java Maven无法在本地jar文件中收集依赖项   java NetBeans IDE 8.2不显示错误消息   java Selenium web驱动程序找不到元素   java如何修复“拒绝访问属性”invoke“”的权限?   JavaApacheJClouds,从比日期更早的blob中删除blob的最佳方法   java如何比较和排序树集中的项目?   使用JavaSpring和无头/解耦CMS   java使用swagercodegen在不同的类中生成端点   Java外部Keylistener没有响应