当前位置:网站首页>Tencent cloud client command line tool tccli main process analysis
Tencent cloud client command line tool tccli main process analysis
2022-07-23 12:23:00 【Dendrobium candidum HF】
List of articles
environmental information
- python: 3.8.5
- tccli: 3.0.666.1
List only key code
Command line files
from tccli.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
Entry function
main.py file
def main():
cli_version = __version__.rsplit(".", 1)[0]
if sdkVersion < cli_version:
sys.stderr.write("Version is inconsistent, python sdk version:%s tccli version:%s" % (sdkVersion, __version__))
return
try:
log.info("tccli %s" % ' '.join(sys.argv[1:]))
CLICommand()()
main The function mainly calls CLICommand()().
command.CLICommand analysis
class CLICommand(BaseCommand):
def __init__(self):
self._command_map = None
self._argument_map = None
super(CLICommand, self).__init__()
def __call__(self, args=None):
if args is None:
# Save all parameters to args
args = sys.argv[1:]
if len(args) > 0 and args[0] == "as":
args[0] = "autoscaling"
command_map = self._get_command_map()
parser = self._create_parser(command_map)
# Solve the interface version (--version) and tccli edition (--version) Field conflict
self._handle_service_version_argumnet(args, parser)
self._handle_warning(args)
parsed_args, remaining = parser.parse_known_args(args)
return command_map[parsed_args.command](remaining, parsed_args)
command_map object
command_map It's a OrderedDict, When service by dnspod when , give the result as follows . To put it bluntly , Namely {"func_name": func} Such a dictionary , It is convenient to find the real function with the same name through the string .
The following example , Namely dnspod This service Under the Action.
Use tccli dnspod help You can see .
OrderedDict([('CreateDomain', <tccli.command.ActionCommand object at 0x7f6d7f0c16a0>),
('CreateDomainAlias', <tccli.command.ActionCommand object at 0x7f6d7f0a5370>),
('CreateDomainBatch', <tccli.command.ActionCommand object at 0x7f6d7f275970>),
('CreateDomainGroup', <tccli.command.ActionCommand object at 0x7f6d7eae5310>),
('CreateRecord', <tccli.command.ActionCommand object at 0x7f6d7f283160>),
('CreateRecordBatch', <tccli.command.ActionCommand object at 0x7f6d7e7de430>),
('DeleteDomain', <tccli.command.ActionCommand object at 0x7f6d7f2831c0>), ...])
parser object
This is actually CLIArgParser Class .
CLIArgParser come from argparser.py file
class CLIArgParser(BaseArgParser):
Its parent class BaseArgParser Inherited from argparse.ArgumentParser
class BaseArgParser(argparse.ArgumentParser):
parse_known_args() Method in BaseArgParser Rewrote .
Don't read it in detail , Let's take a direct look at the relevant results .
stay CLICommand Of __call__ Add print code under method .
print("parsed_args", parsed_args)
print("remaining", remaining)
sys.stdout.flush()
Output example 1
# tccli cls ModifyTopic help --detail
parsed_args Namespace(cli_input_json=None, cli_unfold_argument=False, command='cls', detail=True, endpoint=None, filter=None, generate_cli_skeleton=None, https_proxy=None, output=None, profile=None, region=None, role_arn=None, role_session_name=None, secretId=None, secretKey=None, service_version=None, timeout=None, token=None, use_cvm_role=False, waiter=None, warning=False)
remaining ['ModifyTopic', 'help']
Example 2
# tccli cls DescribeTopics --Filters '[ {"Key": "topicName", "Values": [ "test-log" ] }]'
parsed_args Namespace(cli_input_json=None, cli_unfold_argument=False, command='cls', detail=False, endpoint=None, filter=None, generate_cli_skeleton=None, https_proxy=None, output=None, profile=None, region=None, role_arn=None, role_session_name=None, secretId=None, secretKey=None, service_version=None, timeout=None, token=None, use_cvm_role=False, waiter=None, warning=False)
remaining ['DescribeTopics', '--Filters', '[ {"Key": "topicName", "Values": [ "test-log" ] }]']
Actually Action function , With doDescribeTopics For example
This function is defined in cls/cls_client.py The file of .
According to the following call , Its handle
- remaining As args Pass in
- parsed_args As parsed_globals Pass in
def doDescribeTopics(args, parsed_globals):
...
model = models.DescribeTopicsRequest()
model.from_json_string(json.dumps(args))
start_time = time.time()
while True:
rsp = client.DescribeTopics(model)
result = rsp.to_json_string()
...
It's easy to understand , Part of it is “ Global parameter ”, Part of it is “Action function ” Parameters of .
It will be “ Global parameter ” Turn into g_param, A print . give the result as follows :
g_param {'filter': None, 'output': 'json', 'secretId': 'xxxxxxxxx', 'secretKey': 'xxxxxxxxxxx', 'token': None, 'role_arn': None, 'role_session_name': None, 'use_cvm_role': False, 'detail': False, 'profile': 'default', 'region': 'ap-beijing', 'endpoint': 'cls.tencentcloudapi.com', 'timeout': None, 'generate_cli_skeleton': None, 'cli_input_json': None, 'cli_unfold_argument': False, 'https_proxy': None, 'warning': False, 'waiter': None, 'command': 'cls', 'service_version': None, 'version': 'v20201016'}
model The object is v20201016/models.py in DescribeTopicsRequest Example . It mainly realizes _deserialize Method .
class DescribeTopicsRequest(AbstractModel):
from_json_string Method comes from abstract_model.py Medium AbstractModel class . yes DescribeTopicsRequest Parent class of .
And in the from_json_string The above _deserialize Method .
Here we are , Come to an end .
Invoke the sample
import sys
from tccli.main import main
args = ['tccli', 'cls', 'DescribeTopics',
'--Filters',
'[ {"Key": "topicName", "Values": [ "test-log" ] }]']
sys.argv = args
main()
Code above , In your own script , call tccli The function in .
Of course , Because of its main Always output results to stdout in , So if you want to save the result to a variable , You also need to write your own functions to handle it . This part can refer to python Save the standard output of the function to the variable problem .
format_output.py file
This file defines the operations related to the output format .
Response Class is the base class of other formats
__call__ Method self._format_response() Methods are implemented by subclasses , Such as JSONResult, TableResult,TextResult.
In the method , And settings stream Code for .
def __call__(self, command, response, stream=None):
if stream is None:
if not six.PY2:
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,
encoding='utf-8')
stream = sys.stdout
try:
self._format_response(command, response, stream)
except IOError as e:
pass
finally:
self._flush_stream(stream)
- First of all, judge whether there is
streamCome in , In the current situation without modifying the source code , It can't be transmittedstreamParametric . So it must enter the logic of judgment - stay
pythonThe version is not2.xUnder the circumstances , takesys.stdoutPoint toio.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')Result .- This step , Mainly increased buffer effect ( At the same time, the
encoding), Writing data will not be seen immediately , Examples are as follows
- This step , Mainly increased buffer effect ( At the same time, the
- And then
streamPoint tosys.stdout,
io.TextIOWrapper Effect demonstration
In [51]: sys.stdout =old_out
In [52]: print("hahha")
hahha
In [53]: print("hhehe")
hhehe
In [54]: sys.stdout = io.TextIOWrapper(sys.stdout.buffer,
...: encoding='utf-8')
In [55]: print("hahha")
In [56]: print("haha")
In [57]: sys.stdout.flush()
hahha
haha
Use io.TextIOWrapper after ,print The result of will not show immediately , Instead, you need to call sys.stdout.flush() Function to see .
problem
In such as doDescribeRecordList In equal function , If used print() sentence , Then it needs to be implemented immediately sys.stdout.flush() Method , To see the output , Otherwise, only the corresponding function result will be output , I can't see what I added print() effect , Why is that ?
This reason is because later FormatOutput.output function , Changed the io flow , new io The flow is different from before . The verification code is as follows :
import sys
import io
import time
old_stdout = sys.stdout
sys.stdout = io.TextIOWrapper(old_stdout.buffer, encoding='utf-8')
print("I'm sys.stdout.")
# Without this line , The script will report an error , I don't understand
old_stdout = sys.stdout
sys.stdout = io.TextIOWrapper(old_stdout.buffer, encoding='utf-8')
print(time.ctime(), "heheh")
print(time.ctime(),"hahah")
print("over")
function , The script above , You won't see I'm sys.stdout. Of , Because twice before and after io The flow is different .
But actually tccli in , Although it is also set twice sys.stdout, But it is not used as above old_stdout = sys.stdout This kind of sentence , Its script uses reload(sys).
main.pySet once in the scriptsys.stdoutformat_output.pySet once insys.stdout- When called by other functions
Try setting twice sys.stdout In different files , Not good either. .
Supported output formats
As in the code , Currently only supported 3 Ways of planting .
Think about it here , The possibility of customizing a format ?
Write a format by yourself , Put it in the source code , then , Specify this format yourself , Is it feasible ?
[[email protected] ~]# tccli --output table1 dnspod DescribeDomain --Domain hongyuan.com
usage: tccli [options] <command> <subcommand> [<subcommand> ...] [parameters]
To tccli help text, you can run:
tccli help
tccli configure help
tccli service[cvm] help
tccli service[cvm] action[RunInstances] help
tccli: error: argument --output: Invalid choice, valid choices are:
json | text
table
Invalid choice: 'table1', maybe you meant:
* table
Custom parsing format
There are parameter checks before starting , You need to cross this level first .
[[email protected] ~]# tccli --output json1 dnspod DescribeDomain --Domain hongyuan.com
usage: tccli [options] <command> <subcommand> [<subcommand> ...] [parameters]
To tccli help text, you can run:
tccli help
tccli configure help
tccli service[cvm] help
tccli service[cvm] action[RunInstances] help
tccli: error: argument --output: Invalid choice, valid choices are:
json | text
table
Invalid choice: 'json1', maybe you meant:
* json
边栏推荐
- 谈谈转动惯量
- Connaissance du matériel 1 - schéma et type d'interface (basé sur le tutoriel vidéo complet de l'exploitation du matériel de baiman)
- Interpretation of the paper: "bert4bitter: a basic model for improving bitter peptide prediction based on transformer (BERT) bidirectional encoder representation"
- Using pycaret: low code, automated machine learning framework to solve regression problems
- 高分子物理名词解释归纳
- Analyze the pre integration of vio with less rigorous but logical mathematical theory
- Comment se développe le serveur GPU refroidi à l'eau dans le Centre de données dans le cadre de l'informatique est - Ouest?
- NLP自然语言处理-机器学习和自然语言处理介绍(一)
- NLP natural language processing - Introduction to machine learning and natural language processing (2)
- ARM架构与编程6--重定位(基于百问网ARM架构与编程教程视频)
猜你喜欢

使用PyOD来进行异常值检测

Connaissance du matériel 1 - schéma et type d'interface (basé sur le tutoriel vidéo complet de l'exploitation du matériel de baiman)

单片机学习笔记5--STM32时钟系统(基于百问网STM32F103系列教程)

深度学习-神经网络

ARM架构与编程7--异常与中断(基于百问网ARM架构与编程教程视频)

High level API of propeller realizes image rain removal

2021可信隐私计算高峰论坛暨数据安全产业峰会上百家争鸣

2021 TOP10 development trend of information science. Deep learning? Convolutional neural network?

论文解读:《基于注意力的多标签神经网络用于12种广泛存在的RNA修饰的综合预测和解释》

时间序列的数据分析(三):经典时间序列分解
随机推荐
论文解读:《Deep-4mcw2v: 基于序列的预测器用于识别大肠桿菌中的 N4- 甲基胞嘧啶(4mC)位点》
Solve Sudoku puzzles with Google or tools
【AUTOSAR CanTP 1.学习UDS诊断的网络层协议】
NLP自然语言处理-机器学习和自然语言处理介绍(二)
生命科学领域下的医药研发通过什么技术?冷冻电镜?分子模拟?IND?
Comment se développe le serveur GPU refroidi à l'eau dans le Centre de données dans le cadre de l'informatique est - Ouest?
ARM架构与编程7--异常与中断(基于百问网ARM架构与编程教程视频)
Smart pointer shared_ PTR and unique_ ptr
永磁电机参数的测量获取(电感、电阻、极对数、磁链常数)
编码器的一点理解
数据分析(二)
读写文件数据
Deep learning neural network
高等代数100道题及答案解析
硬件知識1--原理圖和接口類型(基於百問網硬件操作大全視頻教程)
Using pycaret: low code, automated machine learning framework to solve classification problems
Use pyod to detect outliers
Neo4j 知识图谱的图数据科学-如何助力数据科学家提升数据洞察力线上研讨会于6月8号举行
Practical convolution correlation trick
液冷数据中心如何构建,蓝海大脑液冷技术保驾护航