我已经多次在argparse中使用命令行参数,但是我似乎无法理解为什么在尝试使用这些参数时会出现TypeError。我已经写了一个脚本,需要5个参数。我已经包含了下面的脚本。参数值只是几个目录或文件路径。你知道我在这里做错了什么吗?
这是回溯:
Traceback (most recent call last):
File "C:/program.py", line 186, in <module>
sys.exit(builder.main())
File "C:/program.py", line 119, in main
filename_config = os.path(self.config)
TypeError: 'module' object is not callable
代码如下:
import argparse
import sys
import os
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
class Builder():
def __init__(self):
parser = argparse.ArgumentParser(description='builder: ' + 'version 1.0.0')
parser.add_argument('-s', '--source', help="source dir", type=str)
parser.add_argument('-t', '--target', help="destination dir.", type=str)
parser.add_argument('-l', '--lua', help="path and filename of lua code", type=str)
parser.add_argument('-c', '--config', help="path and filename of configuration", type=str)
parser.add_argument('-d', '--documentation', help="path and filename of documentation", type=str)
args = parser.parse_args()
self.source = args.source
self.target = args.target
self.lua = args.lua
self.config = args.config
self.documentation = args.documentation
if not os.path.isdir((self.target)):
os.makedirs(self.target)
def replace_lua(self, filename, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, filename))
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_version(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_documentation(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def main_XXX(self):
if not os.path.isdir(self.target):
os.makedirs(self.target)
#lua pattern
pattern = "<script><![CDATA[]]></script>"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
read_lua = f.read()
f.closed
subst = "<script><![CDATA[\n" + read_lua + "\n]]></script>"
#version pattern
old_version = "<version>1</version>"
new_version = "<version>2</version>"
#documentation
old_doc = "<documentation></documentation>"
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
read_doc = f.read()
f.closed
read_doc = "<documentation>\n" + read_doc + "\n</documentation>"
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#file_path = os.path.join(subdir, file)
self.replace_lua(file, pattern, subst)
#replace_version(file, old_version, new_version)
self.replace_documentation(file, old_doc, read_doc)
def main(self):
#create expression objects
version = re.compile(r"^(\s*)<version>.*</version>\s*")
mod_date = re.compile(r"^(\s*)<modified>.*</modified>\s*")
lua_script = re.compile(r"^(\s*)<script>.*</script>\s*")
doc = re.compile(r"^(\s*)<documentation>.*</documentation>\s*")
#get version
filename_config = os.path(self.config)
with open(filename_config, 'r') as f:
for line in f:
m_obj = version.search(line)
if m_obj:
new_version = line
else:
m_obj = mod_date.search(line)
if m_obj:
new_mod_date = line + "\n"
f.closed
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
new_doc = f.read()
f.closed
new_doc = "<documentation>\n" + new_doc + "\n</documentation>\n"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
new_lua = f.read()
f.closed
#iterate
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, file))
for line in old_file:
m_obj = version.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_version)
else:
m_obj = mod_date.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_mod_date)
else:
m_obj = doc.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_doc)
else:
m_obj = lua_script.search(line)
if m_obj:
new_file.write(m_obj.group(1) + "<script><![CDATA[\n" + new_lua + "\n]]></script>\n")
else:
new_file.write(line)
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, file))
if __name__ == "__main__":
builder = Builder()
sys.exit(builder.main())
目前没有回答
相关问题 更多 >
编程相关推荐