本文整理汇总了Python中misc.filetool.read函数的典型用法代码示例。如果您正苦于以下问题:Python read函数的具体用法?Python read怎么用?Python read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: migrateFile
def migrateFile(filePath, compiledPatches, compiledInfos, patchFile, options=None, encoding="UTF-8"):
logging.info(" - File: %s" % filePath)
# Read in original content
fileContent = filetool.read(filePath, encoding)
fileId = extractFileContentId(fileContent)
# Apply patches
patchedContent = fileContent
if patchFile and fileId is not None:
# import patch
patch = {}
execfile(patchFile, patch)
tree = treegenerator.createFileTree(tokenizer.Tokenizer().parseStream(fileContent))
# If there were any changes, compile the result
if patch["patch"](fileId, tree):
options.prettyPrint = True # make sure it's set
result = [u""]
# result = pretty.prettyNode(tree, options, result)
result = formatter_.formatNode(tree, options, result)
patchedContent = u"".join(result)
# apply RE patches
patchedContent = regtool(patchedContent, compiledPatches, True, filePath)
patchedContent = regtool(patchedContent, compiledInfos, False, filePath)
# Write file
if patchedContent != fileContent:
logging.info(" - %s has been modified. Storing modifications ..." % filePath)
filetool.save(filePath, patchedContent, encoding)
示例2: _getSourceTree
def _getSourceTree(self, cacheId, tradeSpaceForSpeed):
cache = self.context['cache']
console = self.context['console']
# Lookup for unoptimized tree
tree, _ = cache.read(cacheId, self.path, memory=tradeSpaceForSpeed)
# Tree still undefined?, create it!
if tree == None:
console.debug("Parsing file: %s..." % self.id)
console.indent()
fileContent = filetool.read(self.path, self.encoding)
tokens = tokenizer.parseStream(fileContent, self.id)
console.outdent()
console.debug("Generating tree: %s..." % self.id)
console.indent()
tree = treegenerator.createSyntaxTree(tokens) # allow exceptions to propagate
# store unoptimized tree
#print "Caching %s" % cacheId
cache.write(cacheId, tree, memory=tradeSpaceForSpeed, writeToFile=True)
console.outdent()
return tree
示例3: tree
def tree(self, treegen=treegenerator, force=False):
cache = self.context["cache"]
console = self.context["console"]
tradeSpaceForSpeed = (
False
) # Caution: setting this to True seems to make builds slower, at least on some platforms!?
cacheId = "tree%s-%s-%s" % (treegen.tag, self.path, util.toString({}))
self.treeId = cacheId
# Lookup for unoptimized tree
tree, _ = cache.read(cacheId, self.path, memory=tradeSpaceForSpeed)
# Tree still undefined?, create it!
if tree == None or force:
console.debug("Parsing file: %s..." % self.id)
console.indent()
fileContent = filetool.read(self.path, self.encoding)
tokens = tokenizer.parseStream(fileContent, self.id)
console.outdent()
console.debug("Generating tree: %s..." % self.id)
console.indent()
tree = treegen.createSyntaxTree(tokens) # allow exceptions to propagate
# store unoptimized tree
# print "Caching %s" % cacheId
cache.write(cacheId, tree, memory=tradeSpaceForSpeed, writeToFile=True)
console.outdent()
return tree
示例4: main
def main():
(options, args) = get_args()
if len(args) == 0:
print ">>> Missing filename!"
return
for fileName in args:
if not options.quiet:
print fileName, ":"
print ">>> Parsing file..."
fileContent = filetool.read(fileName, "utf-8")
if options.config:
read_config(options)
if options.lint:
run_lint(fileName, fileContent, options, args)
elif options.pretty:
run_pretty(fileName, fileContent, options, args)
elif options.tree:
run_tree(fileName, fileContent, options, args)
elif options.dependencies:
run_dependencies(fileName, fileContent, options, args)
else:
run_compile(fileName, fileContent, options, args)
return
示例5: migrateFile
def migrateFile(
filePath, compiledPatches, compiledInfos,
hasPatchModule=False, options=None, encoding="UTF-8"):
logging.info(" - File: %s" % filePath)
# Read in original content
fileContent = filetool.read(filePath, encoding)
fileId = extractFileContentId(fileContent);
# Apply patches
patchedContent = fileContent
if hasPatchModule and fileId is not None:
import patch
tree = treegenerator.createSyntaxTree(tokenizer.parseStream(fileContent))
# If there were any changes, compile the result
if patch.patch(fileId, tree):
options.prettyPrint = True # make sure it's set
result = [u'']
result = pretty.prettyNode(tree, options, result)
patchedContent = u''.join(result)
# apply RE patches
patchedContent = regtool(patchedContent, compiledPatches, True, filePath)
patchedContent = regtool(patchedContent, compiledInfos, False, filePath)
# Write file
if patchedContent != fileContent:
logging.info(" - %s has been modified. Storing modifications ..." % filePath)
filetool.save(filePath, patchedContent, encoding)
示例6: tree
def tree(self, treegen=treegenerator, force=False):
cache = self.context['cache']
console = self.context['console']
tradeSpaceForSpeed = False # Caution: setting this to True seems to make builds slower, at least on some platforms!?
cacheId = "tree%s-%s-%s" % (treegen.tag, self.path, util.toString({}))
self.treeId = cacheId
# Lookup for unoptimized tree
tree, _ = cache.read(cacheId, self.path, memory=tradeSpaceForSpeed)
# Tree still undefined?, create it!
if tree == None or force:
console.debug("Parsing file: %s..." % self.id)
console.indent()
# Tokenize
fileContent = filetool.read(self.path, self.encoding)
fileId = self.path if self.path else self.id
try:
tokens = tokenizer.Tokenizer().parseStream(fileContent, self.id)
except SyntaxException, e:
# add file info
e.args = (e.args[0] + "\nFile: %s" % fileId,) + e.args[1:]
raise e
# Parse
try:
tree = treegen.createFileTree(tokens, fileId)
except SyntaxException, e:
# add file info
e.args = (e.args[0] + "\nFile: %s" % fileId,) + e.args[1:]
raise
示例7: getCode
def getCode(self, compOptions, treegen=treegenerator, featuremap={}):
# source versions
if not compOptions.optimize:
compiled = filetool.read(self.path)
# assure trailing \n (e.g. to utilise ASI)
if compiled[-1:] != "\n":
compiled += '\n'
# compiled versions
else:
optimize = compOptions.optimize
variants = compOptions.variantset
format_ = compOptions.format
classVariants = self.classVariants()
# relevantVariants is the intersection between the variant set of this job
# and the variant keys actually used in the class
relevantVariants = self.projectClassVariantsToCurrent(classVariants, variants)
variantsId = util.toString(relevantVariants)
optimizeId = self._optimizeId(optimize)
cache = self.context["cache"]
cacheId = "compiled-%s-%s-%s-%s" % (self.path, variantsId, optimizeId, format_)
compiled, _ = cache.read(cacheId, self.path)
if compiled == None:
tree = self.optimize(None, optimize, variants, featuremap)
compiled = self.serializeTree(tree, optimize, format_)
if not "statics" in optimize:
cache.write(cacheId, compiled)
return compiled
示例8: main
def main():
(options, args) = get_args()
if len(args) == 0:
print(">>> Missing filename!")
return
for fileName in args:
if not options.quiet:
print(fileName, ":")
print(">>> Parsing file...")
if fileName == "-":
# enables: echo "1+2" | compile.py -
fileContent = sys.stdin.read()
else:
fileContent = filetool.read(fileName, "utf-8")
if options.config:
read_config(options)
if options.lint:
run_lint(fileName, fileContent, options, args)
elif options.pretty:
run_pretty(fileName, fileContent, options, args)
elif options.tree:
run_tree(fileName, fileContent, options, args)
elif options.dependencies:
run_dependencies(fileName, fileContent, options, args)
else:
run_compile(fileName, fileContent, options, args)
return
示例9: getMeta
def getMeta(self, fileId):
fileEntry = self._classes[fileId]
filePath = fileEntry["path"]
cacheId = "meta-%s" % fileId
meta = self._cache.readmulti(cacheId, filePath)
if meta != None:
return meta
meta = {}
self._console.indent()
content = filetool.read(filePath, fileEntry["encoding"])
meta["loadtimeDeps"] = self._extractLoadtimeDeps(content, fileId)
meta["runtimeDeps"] = self._extractRuntimeDeps(content, fileId)
meta["optionalDeps"] = self._extractOptionalDeps(content)
meta["ignoreDeps"] = self._extractIgnoreDeps(content)
meta["assetDeps"] = self._extractAssetDeps(content)
self._console.outdent()
self._cache.writemulti(cacheId, meta)
return meta
示例10: toResinfo
def toResinfo(self):
result = super(self.__class__, self).toResinfo()
if self.format == "b64" and self.path:
cont = filetool.read(self.path)
cont = json.loads(cont)
result.append(cont)
return result
示例11: getData
def getData():
data = os.path.join(filetool.root(), os.pardir, "data", "icon", "qooxdoo.dat")
lines = filetool.read(data).split("\n")
result = {}
for line in lines:
if line == "" or line.startswith(" ") or line.startswith("#"):
continue
if ":" in line:
alternative = line[line.index(":")+1:].split(",")
key = line[:line.index(":")]
else:
alternative = []
key = line
if key in result:
console.error("Duplicate key found: %s" % key)
sys.exit(1)
result[key] = alternative
# convert to array
arr = []
keys = result.keys()
keys.sort()
for key in keys:
tmp = []
tmp.append(key)
tmp.extend(result[key])
arr.append(tmp)
return arr
示例12: loaderTemplate
def loaderTemplate(script, compConf):
templatePath = compConf.get("paths/loader-template", None)
if not templatePath:
# use default template
templatePath = os.path.join(filetool.root(), os.pardir, "data", "generator", "loader.tmpl.js")
templateCont = filetool.read(templatePath)
return templateCont, templatePath
示例13: runFix
def runFix(jobconf, classesObj):
def fixPng():
return
def removeBOM(fpath):
content = open(fpath, "rb").read()
if content.startswith(codecs.BOM_UTF8):
console.debug("removing BOM: %s" % filePath)
open(fpath, "wb").write(content[len(codecs.BOM_UTF8):])
return
# - Main ---------------------------------------------------------------
if not isinstance(jobconf.get("fix-files", False), types.DictType):
return
console = Context.console
classes = classesObj.keys()
fixsettings = ExtMap(jobconf.get("fix-files"))
# Fixing JS source files
console.info("Fixing whitespace in source files...")
console.indent()
console.info("Fixing files: ", False)
numClasses = len(classes)
eolStyle = fixsettings.get("eol-style", "LF")
tabWidth = fixsettings.get("tab-width", 2)
for pos, classId in enumerate(classes):
console.progress(pos+1, numClasses)
classEntry = classesObj[classId]
filePath = classEntry.path
fileEncoding = classEntry.encoding
fileContent = filetool.read(filePath, fileEncoding)
# Caveat: as filetool.read already calls any2Unix, converting to LF will
# not work as the file content appears unchanged to this function
if eolStyle == "CR":
fixedContent = textutil.any2Mac(fileContent)
elif eolStyle == "CRLF":
fixedContent = textutil.any2Dos(fileContent)
else:
fixedContent = textutil.any2Unix(fileContent)
fixedContent = textutil.normalizeWhiteSpace(textutil.removeTrailingSpaces(textutil.tab2Space(fixedContent, tabWidth)))
if fixedContent != fileContent:
console.debug("modifying file: %s" % filePath)
filetool.save(filePath, fixedContent, fileEncoding)
# this has to go separate, as it requires binary operation
removeBOM(filePath)
console.outdent()
# Fixing PNG files -- currently just a stub!
if fixsettings.get("fix-png", False):
console.info("Fixing PNGs...")
console.indent()
fixPng()
console.outdent()
return
示例14: loadTemplate
def loadTemplate(bootCode):
if bootCode:
loaderFile = os.path.join(filetool.root(), os.pardir, "data", "generator", "loader-build.tmpl.js")
else:
loaderFile = os.path.join(filetool.root(), os.pardir, "data", "generator", "loader-source.tmpl.js")
template = filetool.read(loaderFile)
return template
示例15: __init__
def __init__(self, filename, logger=None):
self.filename = filename
content = filetool.read(filename)
self.tree = treegenerator.createSyntaxTree(tokenizer.parseStream(content))
self.script = Script(self.tree, self.filename)
if not logger:
self.logger = ConsoleLogger()
else:
self.logger = logger