基本思路:由于VB语言的的结构类型与常量类型的定义语法和python不同 因此需要转换为python能识别的类,所有语言的基本思想大致相同 所以转换语法定义也比较简单
比如VB的注释符号为单引号',python为#号所以利用正则表达式可直接替换 加上Windows的api文档编写规范 所以构造搜索模式比较容易 最后就是构造符合python语法就简单得多,主要让大家参考正则表达式的强大之处
下面我使用api文档部分内容进行演示
我分别演示的文档名字分别为:
1、vbtest.txt : 包含VB定义的结果和常量定义
2、将vb语言定义的结构或者常量文档转换为python文档.py :进行转换的python程序
3、 pythontext.txt :演示的输出结果
我会将详细代码给出 虽然结果有一点点未转换到 是因为我没有把所有情况的构造都分析完 但是基本思路已给出 稍微修改就行 同时我们可以参照此类做法将c++文档转换为python文档 这样既能增加我们对各种语言的复习 同时增加我们使用python编程的灵活性 我们可以借用其他语言的长处融入自己python程序 帮助自己解决更多的问题。以下是实现代码 大家同时坚持一个编程可读性原则 这样以后自己写的程序复用和修改都非常容易 由于仓促 我们并未将所有变量命名的可读性加强
1.vbtest.txt : 演示数据
' ------------------------------------------------------------------------
'
' WIN32API.TXT -- Win32 API Declarations for Visual Basic
'
' Copyright (C) 1994-98 Microsoft Corporation
'
' This file is required for the Visual Basic 6.0 version of the APILoader.
' Older versions of this file will not work correctly with the version
' 6.0 APILoader. This file is backwards compatible with previous releases
' of the APILoader with the exception that Constants are no longer declared
' as Global or Public in this file.
'
' This file contains only the Const, Type,
' and Declare statements for Win32 APIs.
'
' You have a royalty-free right to use, modify, reproduce and distribute
' this file (and/or any modified version) in any way you find useful,
' provided that you agree that Microsoft has no warranty, obligation or
' liability for its contents. Refer to the Microsoft Windows Programmer's
' Reference for further information.
'
' ------------------------------------------------------------------------
' Type definitions for Windows' basic types.
Const ANYSIZE_ARRAY = 1
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type RECTL
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Type POINTAPI
x As Long
y As Long
End Type
Type POINTL
x As Long
y As Long
End Type
Type Size
cx As Long
cy As Long
End Type
Type POINTS
x As Integer
y As Integer
End Type
Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Const DELETE = &H10000
Const READ_CONTROL = &H20000
Const WRITE_DAC = &H40000
Const WRITE_OWNER = &H80000
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF
Type SID_IDENTIFIER_AUTHORITY
Value(6) As Byte
End Type
Const SID_REVISION = (1) ' Current revision level
Const SID_MAX_SUB_AUTHORITIES = (15)
Const SID_RECOMMENDED_SUB_AUTHORITIES = (1) ' Will change to around 6 in a future release.
Const SidTypeUser = 1
Const SidTypeGroup = 2
Const SidTypeDomain = 3
Const SidTypeAlias = 4
Const SidTypeWellKnownGroup = 5
Const SidTypeDeletedAccount = 6
Const SidTypeInvalid = 7
Const SidTypeUnknown = 8
Type SID_AND_ATTRIBUTES
Sid As Long
Attributes As Long
End Type
' ///
' //
' Universal well-known SIDs //
' //
' Null SID S-1-0-0 //
' World S-1-1-0 //
' Local S-1-2-0 //
' Creator Owner ID S-1-3-0 //
' Creator Group ID S-1-3-1 //
' //
' (Non-unique IDs) S-1-4 //
' //
' ///
Const SECURITY_NULL_RID = &H0
2、将vb语言定义的结构或者常量文档转换为python文档.py 转换程序代码
import re def repacetTo01(matched): value = matched.group('second') return "class "+str(value)+'(ctypes.Structure):\n _fields_ = [\n' # '(?P<value>\d+)' def repacetTo02(matched): type02=matched.group("third") type02=str(type02) if type02=="Long": replace02="ctypes.c_long" elif type02=="Integer": replace02 = "ctypes.c_short" elif type02=="Byte": replace02 ="ctypes.c_byte" elif type02=="Double": replace02 ="ctypes.c_double" elif type02 == "String": replace02 = "ctypes.c_wchar_p" else: replace02="\"" + matched.group("third")+"\"" type02stringPart=matched.group("four") if type02stringPart: replace02stringPart="" type02First=str(matched.group("first")) return "\n (\""+type02First+"\","+replace02+")\n" #替换结构类型的结束语句 End Type def repacetTo03(matched): value = matched.group('find') return " ]\n" def repacetTo04(matched): value = matched.group('first') value1 = matched.group('second') value=str(value) return str(value1) def repacetTo05(matched): value1 = matched.group('first') value3 = matched.group('third') return str(value1)+'0x'+str(value3) #用于获取源文件内容 保存在一个字符串变量中 而不是返回元组 sourcestring="" #在匹配圆括号开始后 使用名称为返回的组命名 返回对象就使用group方法获取指定名称部分 #第01步 更改vb结构变量的声明开始行 思路以 “Type开始 接着一个空格 然后剩余部分为英文字符或者字符,最后是换行符 ” 来编写匹配模式 pattern01=r'(?P<first>Type (?P<second>\w+)\x0a)' #匹配按括号内容以元组形式返回 给每一部分取名 #第02步 匹配 名称As类型部分 vb数组定义(number) \x28是左括号的十六进制 \x29是右括号的十六进制 pattern02=r'\s+(?P<first>\w+)(?P<number>\x28\d+\x29)? (?P<second>As )(?P<third>\w+)(?P<four> \* \w+)?\x0a' #匹配按括号内容以元组形式返回 给每一部分命名 pattern03=r'(?P<find>End \w+\x0a)' #替换常量定义语句 pattern04=r'(?P<first>Const )(?P<second>.+\x0a)' #替换&H为0x pattern05=r'(?P<first>^[^\n]+)(?P<H>&H)(?P<third>.+\x0a)' #最简单的部分 替换单引号‘为# 第一部分:任意字符 不含换行符;第二部分:单引号;第三部分:任意字符及最后的换行符\x0a #定义函数:返回第一部分+“#”+第三部分 相当于替换第二部分的vb注释符’,改为python注释符# def replaceQuoteFunction(matched): value1 = matched.group('first') value3 = matched.group('last') return str(value1) + '#' + str(value3) #只对符合该模式的行进行替换 所以不会误替换不符合的模式 pattern_replace_quote=r'(?P<first>^[^\n]*)(?P<middle>\x27)(?P<last>.*\x0a)' with open('vbtest.txt','r') as f: for i in f.readlines(): sourcestring=sourcestring+i print("获取源文档内容:\n",sourcestring) #以pattern中括号按元组形式返回匹配结果的列表 # aa=re.findall(pattern01,sourcestring,re.ASCII) # print("匹配换行:",aa) #替换匹配到的表达式 替换部分使用函数 替换指定命名部分 replaceMent=re.sub(pattern01,repacetTo01,sourcestring,0,re.MULTILINE) replaceMent02=re.sub(pattern02,repacetTo02,replaceMent,0,re.MULTILINE) replaceMent03=re.sub(pattern03,repacetTo03,replaceMent02,0,re.MULTILINE) replaceMent04=re.sub(pattern04,repacetTo04,replaceMent03,0,re.MULTILINE) replaceMent05=re.sub(pattern05,repacetTo05,replaceMent04,0,re.MULTILINE) replace_To_Quote=re.sub(pattern_replace_quote,replaceQuoteFunction,replaceMent05,0,re.MULTILINE) print("替换后文档内容:\n",replace_To_Quote) with open('pythontext.txt','w') as f: f.write(replace_To_Quote)
3、转换结果:pythontext.txt
# ------------------------------------------------------------------------
#
# WIN32API.TXT -- Win32 API Declarations for Visual Basic
#
# Copyright (C) 1994-98 Microsoft Corporation
#
# This file is required for the Visual Basic 6.0 version of the APILoader.
# Older versions of this file will not work correctly with the version
# 6.0 APILoader. This file is backwards compatible with previous releases
# of the APILoader with the exception that Constants are no longer declared
# as Global or Public in this file.
#
# This file contains only the Const, Type,
# and Declare statements for Win32 APIs.
#
# You have a royalty-free right to use, modify, reproduce and distribute
# this file (and/or any modified version) in any way you find useful,
# provided that you agree that Microsoft has no warranty, obligation or
' liability for its contents. Refer to the Microsoft Windows Programmer#s
# Reference for further information.
#
# ------------------------------------------------------------------------
' Type definitions for Windows# basic types.
ANYSIZE_ARRAY = 1
class RECT(ctypes.Structure):
_fields_ = [
("Left",ctypes.c_long)
("Top",ctypes.c_long)
("Right",ctypes.c_long)
("Bottom",ctypes.c_long)
]
class RECTL(ctypes.Structure):
_fields_ = [
("Left",ctypes.c_long)
("Top",ctypes.c_long)
("Right",ctypes.c_long)
("Bottom",ctypes.c_long)
]
class POINTAPI(ctypes.Structure):
_fields_ = [
("x",ctypes.c_long)
("y",ctypes.c_long)
]
class POINTL(ctypes.Structure):
_fields_ = [
("x",ctypes.c_long)
("y",ctypes.c_long)
]
class Size(ctypes.Structure):
_fields_ = [
("cx",ctypes.c_long)
("cy",ctypes.c_long)
]
class POINTS(ctypes.Structure):
_fields_ = [
x As Integer
y As Integer
]
class MSG(ctypes.Structure):
_fields_ = [
("hwnd",ctypes.c_long)
("message",ctypes.c_long)
("wParam",ctypes.c_long)
("lParam",ctypes.c_long)
("time",ctypes.c_long)
("pt","POINTAPI")
]
DELETE = 0x10000
READ_CONTROL = 0x20000
WRITE_DAC = 0x40000
WRITE_OWNER = 0x80000
SYNCHRONIZE = 0x100000
STANDARD_RIGHTS_READ = (READ_CONTROL)
STANDARD_RIGHTS_WRITE = (READ_CONTROL)
STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
STANDARD_RIGHTS_REQUIRED = 0xF0000
STANDARD_RIGHTS_ALL = 0x1F0000
SPECIFIC_RIGHTS_ALL = 0xFFFF
class SID_IDENTIFIER_AUTHORITY(ctypes.Structure):
_fields_ = [
("Value",ctypes.c_byte)
]
SID_REVISION = (1) # Current revision level
SID_MAX_SUB_AUTHORITIES = (15)
SID_RECOMMENDED_SUB_AUTHORITIES = (1) # Will change to around 6 in a future release.
SidTypeUser = 1
SidTypeGroup = 2
SidTypeDomain = 3
SidTypeAlias = 4
SidTypeWellKnownGroup = 5
SidTypeDeletedAccount = 6
SidTypeInvalid = 7
SidTypeUnknown = 8
class SID_AND_ATTRIBUTES(ctypes.Structure):
_fields_ = [
("Sid",ctypes.c_long)
("Attributes",ctypes.c_long)
]
# ///
# //
# Universal well-known SIDs //
# //
# Null SID S-1-0-0 //
# World S-1-1-0 //
# Local S-1-2-0 //
# Creator Owner ID S-1-3-0 //
# Creator Group ID S-1-3-1 //
# //
# (Non-unique IDs) S-1-4 //
# //
# ///
SECURITY_NULL_RID = 0x0