return _position(False, p) @register('opposite-position') def opposite_position(p): return _position(True, p) # ------------------------------------------------------------------------------ # Math @register('pi', 0) def pi(): return NumberValue(math.pi) COMPASS_HELPERS_LIBRARY.add(Value._wrap(math.sin), 'sin', 1) COMPASS_HELPERS_LIBRARY.add(Value._wrap(math.cos), 'cos', 1) COMPASS_HELPERS_LIBRARY.add(Value._wrap(math.tan), 'tan', 1) # ------------------------------------------------------------------------------ # Fonts def _font_url(path, only_path=False, cache_buster=True, inline=False): filepath = StringValue(path).value path = None if callable(config.STATIC_ROOT): try: _file, _storage = list(config.STATIC_ROOT(filepath))[0] d_obj = _storage.modified_time(_file) filetime = int(time.mktime(d_obj.timetuple()))
raise ValueError("Expected unitless number, got %r" % (base,)) if base is None: ret = math.log(number.value) else: ret = math.log(number.value, base.value) return Number(ret) @register('pow', 2) def pow(number, exponent): return number ** exponent COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.sqrt), 'sqrt', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.sin), 'sin', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.cos), 'cos', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.tan), 'tan', 1) # ------------------------------------------------------------------------------ # Fonts def _fonts_root(): return config.STATIC_ROOT if config.FONTS_ROOT is None else config.FONTS_ROOT def _font_url(path, only_path=False, cache_buster=True, inline=False): filepath = String.unquoted(path).value file = None
@register('to-lower-case', 1) def to_lower_case(string): expect_type(string, String) return String(string.value.lower(), quotes=string.quotes) # ------------------------------------------------------------------------------ # Number functions @register('percentage', 1) def percentage(value): expect_type(value, Number, unit=None) return value * Number(100, unit='%') CORE_LIBRARY.add(Number.wrap_python_function(abs), 'abs', 1) CORE_LIBRARY.add(Number.wrap_python_function(round), 'round', 1) CORE_LIBRARY.add(Number.wrap_python_function(math.ceil), 'ceil', 1) CORE_LIBRARY.add(Number.wrap_python_function(math.floor), 'floor', 1) # ------------------------------------------------------------------------------ # List functions def __parse_separator(separator, default_from=None): if separator is None: separator = 'auto' separator = String.unquoted(separator).value if separator == 'comma': return True
expect_type(string, String) return String(string.value.lower(), quotes=string.quotes) # ------------------------------------------------------------------------------ # Number functions @register('percentage', 1) def percentage(value): expect_type(value, Number, unit=None) return value * Number(100, unit='%') CORE_LIBRARY.add(Number.wrap_python_function(abs), 'abs', 1) CORE_LIBRARY.add(Number.wrap_python_function(round), 'round', 1) CORE_LIBRARY.add(Number.wrap_python_function(math.ceil), 'ceil', 1) CORE_LIBRARY.add(Number.wrap_python_function(math.floor), 'floor', 1) # ------------------------------------------------------------------------------ # List functions def __parse_separator(separator, default_from=None): if separator is None: separator = 'auto' separator = String.unquoted(separator).value if separator == 'comma': return True
@register('opposite-position') def opposite_position(p): return _position(True, p) # ------------------------------------------------------------------------------ # Math @register('pi', 0) def pi(): return NumberValue(math.pi) COMPASS_HELPERS_LIBRARY.add(NumberValue.wrap_python_function(math.sin), 'sin', 1) COMPASS_HELPERS_LIBRARY.add(NumberValue.wrap_python_function(math.cos), 'cos', 1) COMPASS_HELPERS_LIBRARY.add(NumberValue.wrap_python_function(math.tan), 'tan', 1) # ------------------------------------------------------------------------------ # Fonts def _font_url(path, only_path=False, cache_buster=True, inline=False): filepath = StringValue(path).value path = None if callable(config.STATIC_ROOT): try: _file, _storage = list(config.STATIC_ROOT(filepath))[0]
raise ValueError("Expected unitless number, got %r" % (base, )) if base is None: ret = math.log(number.value) else: ret = math.log(number.value, base.value) return Number(ret) @register('pow', 2) def pow(number, exponent): return number**exponent COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.sqrt), 'sqrt', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.sin), 'sin', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.cos), 'cos', 1) COMPASS_HELPERS_LIBRARY.add(Number.wrap_python_function(math.tan), 'tan', 1) # ------------------------------------------------------------------------------ # Fonts def _fonts_root(): return config.STATIC_ROOT if config.FONTS_ROOT is None else config.FONTS_ROOT def _font_url(path, only_path=False, cache_buster=True, inline=False): filepath = String.unquoted(path).value file = None
@register('quote') def quote(*args): return QuotedStringValue(' '.join([StringValue(s).value for s in args])) # ------------------------------------------------------------------------------ # Number functions @register('percentage', 1) def percentage(value): value = NumberValue(value) value.units = {'%': _units_weights.get('%', 1), '_': '%'} return value CORE_LIBRARY.add(Value._wrap(abs), 'abs', 1) CORE_LIBRARY.add(Value._wrap(round), 'round', 1) CORE_LIBRARY.add(Value._wrap(math.ceil), 'ceil', 1) CORE_LIBRARY.add(Value._wrap(math.floor), 'floor', 1) # ------------------------------------------------------------------------------ # List functions # TODO get the compass bit outta here @register('-compass-list-size') @register('length') def _length(*lst): if len(lst) == 1 and isinstance(lst[0], (list, tuple, ListValue)): lst = ListValue(lst[0]).values() lst = ListValue(lst)
return String(arg.value, quotes='"') else: return String(arg.render(), quotes='"') # ------------------------------------------------------------------------------ # Number functions @register("percentage", 1) def percentage(value): expect_type(value, Number, unit=None) return value * Number(100, unit="%") CORE_LIBRARY.add(Number.wrap_python_function(abs), "abs", 1) CORE_LIBRARY.add(Number.wrap_python_function(round), "round", 1) CORE_LIBRARY.add(Number.wrap_python_function(math.ceil), "ceil", 1) CORE_LIBRARY.add(Number.wrap_python_function(math.floor), "floor", 1) # ------------------------------------------------------------------------------ # List functions def __parse_separator(separator, default_from=None): if separator is None: return None separator = String.unquoted(separator).value if separator == "comma": return True