Implement "get_file_contents" helper

This commit is contained in:
Ivan Kravets
2018-12-27 14:48:22 +02:00
parent 74218f4f93
commit aa2bc4a63b
5 changed files with 17 additions and 15 deletions

View File

@ -17,6 +17,7 @@
from __future__ import absolute_import
import codecs
import hashlib
import os
import re
@ -74,13 +75,7 @@ class LibBuilderFactory(object):
if not env.IsFileWithExt(
fname, piotool.SRC_BUILD_EXT + piotool.SRC_HEADER_EXT):
continue
content = ""
try:
with open(join(root, fname)) as f:
content = f.read()
except UnicodeDecodeError:
with open(join(root, fname), encoding="latin-1") as f:
content = f.read()
content = util.get_file_contents(join(root, fname))
if not content:
continue
if "Arduino.h" in content and include_re.search(content):
@ -481,7 +476,8 @@ class ArduinoLibBuilder(LibBuilderBase):
manifest = {}
if not isfile(join(self.path, "library.properties")):
return manifest
with open(join(self.path, "library.properties")) as fp:
manifest_path = join(self.path, "library.properties")
with codecs.open(manifest_path, encoding="utf-8") as fp:
for line in fp.readlines():
if "=" not in line:
continue

View File

@ -76,8 +76,7 @@ class InoToCPPConverter(object):
def process(self, contents):
out_file = self._main_ino + ".cpp"
assert self._gcc_preprocess(contents, out_file)
with open(out_file) as fp:
contents = fp.read()
contents = util.get_file_contents(out_file)
contents = self._join_multiline_strings(contents)
with open(out_file, "w") as fp:
fp.write(self.append_prototypes(contents))

View File

@ -202,7 +202,7 @@ def device_monitor(ctx, **kwargs):
sleep(0.1)
if not t.is_alive():
return
kwargs['port'] = open(sock_file).read()
kwargs['port'] = util.get_file_contents(sock_file)
ctx.invoke(cmd_device_monitor, **kwargs)
t.join(2)
finally:

View File

@ -122,10 +122,8 @@ class ProjectGenerator(object):
contents.encode("utf8") if util.PY2 else contents)
def _render_tpl(self, tpl_path):
content = ""
with open(tpl_path) as f:
content = f.read()
return bottle.template(content, **self._tplvars)
return bottle.template(
util.get_file_contents(tpl_path), **self._tplvars)
@staticmethod
def _merge_contents(dst_path, contents):

View File

@ -852,6 +852,15 @@ def ensure_udev_rules():
return True
def get_file_contents(path):
try:
with open(path) as f:
return f.read()
except UnicodeDecodeError:
with open(path, encoding="latin-1") as f:
return f.read()
def rmtree_(path):
def _onerror(_, name, __):