Files
xahaud/python/beast/util/String.py
Tom Ritchford 6b0cec1189 Improvements to scons build for beast.
* Common code extracted to Python directories.
* Read ~/.scons file for scons environment defaults.
* Override scons settings with shell environment variables.
* New "tags" for debug, nodebug, optimize, nooptimize builds.
* Universal platform detection.
* Default value of environment variables set through prefix dictionaries.
* Check for correct Boost value and fail otherwise.
* Extract git describe --tags into a preprocesor variable, -DTIP_BRANCH
* More colors - blue for unchanged defaults, green for changed defaults, red for error.
* Contain unit tests for non-obvious stuff.
* Check to see that boost libraries have been built.
* Right now, we accept both .dylib and .a versions but it'd be easy to enforce .a only.
2014-04-11 10:40:26 -07:00

61 lines
1.8 KiB
Python

from __future__ import absolute_import, division, print_function, unicode_literals
import functools
from beast.util import Iter
from beast.util.Terminal import warn
def is_string(s):
"""Is s a string? - in either Python 2.x or 3.x."""
return isinstance(s, (str, unicode))
def stringify(item, joiner=''):
"""If item is not a string, stringify its members and join them."""
try:
len(item)
except:
return str(item)
if not item or is_string(item):
return item or ''
else:
return joiner.join(str(i) for i in item)
def single_line(line, report_errors=True, joiner='+'):
"""Force a string to be a single line with no carriage returns, and report
a warning if there was more than one line."""
lines = line.strip().splitlines()
if report_errors and len(lines) > 1:
print('multiline result:', lines)
return joiner.join(lines)
# Copied from
# https://github.com/lerugray/pickett/blob/master/pickett/ParseScript.py
def remove_comment(line):
"""Remove trailing comments from one line."""
start = 0
while True:
loc = line.find('#', start)
if loc == -1:
return line.replace('\\#', '#')
elif not (loc and line[loc - 1] == '\\'):
return line[:loc].replace('\\#', '#')
start = loc + 1
def remove_quotes(line, quote='"', print=print):
if not line.startswith(quote):
return line
if line.endswith(quote):
return line[1:-1]
warn('line started with %s but didn\'t end with one:' % quote, print)
print(line)
return line[1:]
def fields_after_prefix(prefix, line):
line = line.strip()
return line.startswith(prefix) and line[len(prefix):].split()
def first_fields_after_prefix(prefix, sequence):
condition = functools.partial(fields_after_prefix, prefix)
return Iter.first(condition, sequence) or []