/usr/lib/python3/dist-packages/lesscpy/plib/call.py is in python3-lesscpy 0.13.0+ds-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | # -*- coding: utf8 -*-
"""
.. module:: lesscpy.plib.call
:synopsis: Call parse node
Copyright (c)
See LICENSE for details.
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
"""
import re
import math
try:
from urllib.parse import quote as urlquote
except ImportError:
from urllib import quote as urlquote
import six
from .node import Node
import lesscpy.lessc.utility as utility
import lesscpy.lessc.color as Color
from lesscpy.lib.colors import lessColors
class Call(Node):
"""Call node. Node represents a function call.
All builtin none-color functions are in this node.
This node attempts calls on built-ins and lets non-builtins
through.
increment(3px) --> 4px
unknown(3px) --> unknown(3px)
"""
def parse(self, scope):
"""Parse Node within scope.
the functions ~( and e( map to self.escape
and %( maps to self.sformat
args:
scope (Scope): Current scope
"""
name = ''.join(self.tokens[0])
parsed = self.process(self.tokens[1:], scope)
if name == '%(':
name = 'sformat'
elif name in ('~', 'e'):
name = 'escape'
color = Color.Color()
args = [t for t in parsed
if not isinstance(t, six.string_types) or t not in '(),']
if hasattr(self, name):
try:
return getattr(self, name)(*args)
except ValueError:
pass
if hasattr(color, name):
try:
result = getattr(color, name)(*args)
try:
return result + ' '
except TypeError:
return result
except ValueError:
pass
return name + ''.join([p for p in parsed])
def escape(self, string, *args):
"""Less Escape.
args:
string (str): string to escape
returns:
str
"""
return utility.destring(string.strip('~'))
def sformat(self, string, *args):
""" String format.
args:
string (str): string to format
args (list): format options
returns:
str
"""
format = string
items = []
m = re.findall('(%[asdA])', format)
if m and not args:
raise SyntaxError('Not enough arguments...')
i = 0
for n in m:
v = {
'%A': urlquote,
'%s': utility.destring,
}.get(n, str)(args[i])
items.append(v)
i += 1
format = format.replace('%A', '%s')
format = format.replace('%d', '%s')
return format % tuple(items)
def isnumber(self, string, *args):
"""Is number
args:
string (str): match
returns:
bool
"""
try:
n, u = utility.analyze_number(string)
except SyntaxError:
return False
return True
def iscolor(self, string, *args):
"""Is color
args:
string (str): match
returns:
bool
"""
return (string in lessColors)
def isurl(self, string, *args):
"""Is url
args:
string (str): match
returns:
bool
"""
arg = utility.destring(string)
regex = re.compile(r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
# localhost...
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
# optional port
r'(?::\d+)?'
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return regex.match(arg)
def isstring(self, string, *args):
"""Is string
args:
string (str): match
returns:
bool
"""
regex = re.compile(r'\'[^\']*\'|"[^"]*"')
return regex.match(string)
def iskeyword(self, string, *args):
"""Is less keyword
args:
string (str): match
returns:
bool
"""
return (string in ('when', 'and', 'not'))
def increment(self, value, *args):
""" Increment function
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
return utility.with_unit(n + 1, u)
def decrement(self, value, *args):
""" Decrement function
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
return utility.with_unit(n - 1, u)
def add(self, *args):
""" Add integers
args:
args (list): target
returns:
str
"""
if(len(args) <= 1):
return 0
return sum([int(v) for v in args])
def round(self, value, *args):
""" Round number
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
return utility.with_unit(int(utility.away_from_zero_round(float(n))), u)
def ceil(self, value, *args):
""" Ceil number
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
return utility.with_unit(int(math.ceil(n)), u)
def floor(self, value, *args):
""" Floor number
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
return utility.with_unit(int(math.floor(n)), u)
def percentage(self, value, *args):
""" Return percentage value
args:
value (str): target
returns:
str
"""
n, u = utility.analyze_number(value)
n = int(n * 100.0)
u = '%'
return utility.with_unit(n, u)
|