Beispiel #1
0
    def _validate(self, validator: AttributeValidator, value,
                  attr_values) -> bool:
        if validator.type == 'required':
            return not gws.is_empty(value)

        if validator.type == 'length':
            s = gws.to_str(value).strip()
            return t.cast(float, validator.min) <= len(s) <= t.cast(
                float, validator.max)

        if validator.type == 'regex':
            s = gws.to_str(value).strip()
            return bool(re.search(t.cast(str, validator.pattern), s))

        if validator.type == 'greaterThan':
            other = attr_values.get(t.cast(str, validator.attribute))
            try:
                return value > other
            except TypeError:
                return False

        if validator.type == 'lessThan':
            other = attr_values.get(t.cast(str, validator.attribute))
            try:
                return value < other
            except TypeError:
                return False
Beispiel #2
0
    def __init__(self,
                 uid,
                 attributes=None,
                 category=None,
                 elements=None,
                 shape=None,
                 style=None):
        super().__init__()
        self.attributes = []
        self.category = category or ''
        self.data_model = None
        self.elements = {}
        self.layer = None
        self.shape = None
        self.style = None
        self.templates = None
        self.uid = ''

        if isinstance(uid, str) and _COMBINED_UID_DELIMITER in uid:
            uid = uid.split(_COMBINED_UID_DELIMITER)[-1]

        self.uid = gws.to_str(uid)

        if attributes:
            if isinstance(attributes, dict):
                attributes = [
                    gws.Attribute({
                        'name': k,
                        'value': v
                    }) for k, v in attributes.items()
                ]
            self.attributes = attributes

        if elements:
            self.elements = elements

        if shape:
            if isinstance(shape, gws.gis.shape.Shape):
                self.shape = shape
            else:
                self.shape = gws.gis.shape.from_props(shape)

        if style:
            self.style = gws.lib.style.from_props(gws.Props(style))
Beispiel #3
0
def _parse_header(req: gws.IWebRequest):
    h = req.header('Authorization')
    if not h:
        return

    a = h.strip().split()
    if len(a) != 2 or a[0].lower() != 'basic':
        return

    try:
        b = gws.to_str(base64.decodebytes(gws.to_bytes(a[1])))
    except ValueError:
        return

    c = b.split(':')
    if len(c) != 2:
        return

    return c
Beispiel #4
0
    def _format(self, val, type):
        if val is None:
            return self._quote('')

        if type == gws.AttributeType.float:
            s = '{:.{prec}f}'.format(float(val), prec=self.h.precision)
            s = s.replace('.', self.h.decimal)
            return self._quote(s) if self.h.quote_all else s

        if type == gws.AttributeType.int:
            s = str(val)
            return self._quote(s) if self.h.quote_all else s

        val = gws.to_str(val)

        if val and val.isdigit() and self.h.formula_hack:
            val = '=' + self._quote(val)

        return self._quote(val)
Beispiel #5
0
def parse(s: str, default=None) -> gws.Measurement:
    if isinstance(s, (int, float)):
        if not default:
            raise ValueError(f'invalid unit {s!r}')
        return s, default

    s = gws.to_str(s).strip()
    m = _unit_re.match(s)
    if not m:
        raise ValueError(f'invalid unit {s!r}')

    n = float(m.group('number'))
    u = m.group('unit').strip().lower()

    if not u:
        if not default:
            raise ValueError(f'invalid unit {s!r}')
        return n, default

    return n, u
Beispiel #6
0
def version():
    _, txt = gws.lib.os2.run([EXEC_PATH])
    m = re.search(r'QGis version (.+)', gws.to_str(txt))
    if m:
        return m.group(1).strip()
    return 'unknown'
Beispiel #7
0
def add_variables(source_text: str, d: dict) -> str:
    """Inject our variables into a project"""

    # @TODO rewrite relative paths to absolute
    bs = bs4.BeautifulSoup(source_text, 'lxml-xml')
    """
    The vars are stored like this in both 2 and 3:

    <qgis>
    ....
        <properties>
            ....
            <Variables>
              <variableNames type="QStringList">
                <value>ONE</value>
                <value>TWO</value>
              </variableNames>
              <variableValues type="QStringList">
                <value>11</value>
                <value>22</value>
              </variableValues>
            </Variables>
        </properties>
    </qgis>

    """

    props = bs.properties
    if not props:
        props = bs.new_tag('properties')
        bs.append(props)

    if props.Variables:
        vs = dict(
            zip(
                [
                    str(v.string)
                    for v in props.select('Variables variableNames value')
                ],
                [
                    str(v.string)
                    for v in props.select('Variables variableValues value')
                ],
            ))
        props.Variables.decompose()
    else:
        vs = {}

    vs.update(d)

    props.append(bs.new_tag('Variables'))
    vnames = bs.new_tag('variableNames', type='QStringList')
    vvals = bs.new_tag('variableValues', type='QStringList')

    props.Variables.append(vnames)
    props.Variables.append(vvals)

    for k, v in sorted(vs.items()):
        v = gws.to_str(v).replace('\n', ' ').strip()
        if v:
            tag = bs.new_tag('value')
            tag.append(k)
            vnames.append(tag)

            tag = bs.new_tag('value')
            tag.append(v)
            vvals.append(tag)

    return str(bs)
Beispiel #8
0
def run(root: gws.IRoot, env: dict):
    job_uid = env.get(b'job_uid')
    if not job_uid:
        raise ValueError('no job_uid found')
    gws.lib.job.run(root, gws.to_str(job_uid))
Beispiel #9
0
 def _quote(self, val):
     q = self.h.quote
     s = gws.to_str(val).replace(q, q + q)
     return q + s + q