<commit_before>#!/usr/bin/env python
# -*- coding: utf-8
from __future__ import absolute_import

import json
import logging
from collections import namedtuple

import six

from .client import Client, NotFound
from .fields import Field

LOG = logging.getLogger(__name__)
LOG.addHandler(logging.NullHandler())


class MetaModel(type):
    """Metaclass for Model

    Responsibilities:
    Creating the _meta attribute, with url_template (if present), list of fields
        and for convenience, a list of field names.
    Creates properties for name and namespace if the instance has a metadata field.
    Mixes in ApiMixIn if the Model has a Meta attribute, indicating a top level
        Model (not to be confused with _meta).
    """
    @staticmethod
    def __new__(mcs, cls, bases, attrs):
        attr_meta = attrs.pop("Meta", None)
        if attr_meta:
            bases += (ApiMixIn,)
        meta = {
            "url_template": getattr(attr_meta, "url_template", ""),
            "watch_list_url": getattr(attr_meta, "watch_list_url", ""),
            "fields": [],
            "field_names": []
        }
        field_names = meta["field_names"]
        fields = meta["fields"]
        for k, v in list(attrs.items()):
            if isinstance(v, Field):
                v.name = k
                field_names.append(k)
                fields.append(v)
        Meta = namedtuple("Meta", meta.keys())
        attrs["_meta"] = Meta(**meta)
        return super(MetaModel, mcs).__new__(mcs, cls, bases, attrs)


class ApiMixIn(object):
    """ApiMixIn class for top level Models

    Contains methods for working with the API
    """
    _client = Client()

    @classmethod
    def _build_url(cls, **kwargs):
        return cls._meta.url_template.format(**kwargs)

    @classmethod
    def find(cls, name, namespace="default"):
        url = cls._build_url(name="", namespace=namespace)
        resp = cls._client.get(url, params={"labelSelector": "app={}".format(name)})
        return [cls.from_dict(item) for item in resp.json()[u"items"]]

    @classmethod
    def list(cls, namespace="default"):
        url = cls._build_url(name="", namespace=namespace)
        resp = cls._client.get(url)
        return [cls.from_dict(item) for item in resp.json()[u"items"]]

    @classmethod
    def watch_list(cls):
        """Return a generator that yields WatchEvents of cls"""
        url = cls._meta.watch_list_url
        if not url:
            raise NotImplementedError("Cannot watch_list, no watch_list_url defined on class {}".format(cls))
        resp = cls._client.get(url, stream=True, timeout=None)
        for line in resp.iter_content(chunk_size=None):
            if line:
                event_json = json.loads(line)
                event = WatchEvent(event_json, cls)
                yield event

    @classmethod
    def get(cls, name, namespace="default"):
        """Get from API server if it exists"""
        url = cls._build_url(name=name, namespace=namespace)
        resp = cls._client.get(url)
        instance = cls.from_dict(resp.json())
        return instance

    @classmethod<commit_msg>DOCD-977 - Add a deployment_id field to app_spec<commit_after>#!/usr/bin/env python
# -*- coding: utf-8
from __future__ import absolute_import

import json
import logging
from collections import namedtuple

import six

from .client import Client, NotFound
from .fields import Field

LOG = logging.getLogger(__name__)
LOG.addHandler(logging.NullHandler())


class MetaModel(type):
    """Metaclass for Model

    Responsibilities:
    Creating the _meta attribute, with url_template (if present), list of fields
        and for convenience, a list of field names.
    Creates properties for name and namespace if the instance has a metadata field.
    Mixes in ApiMixIn if the Model has a Meta attribute, indicating a top level
        Model (not to be confused with _meta).
    """
    @staticmethod
    def __new__(mcs, cls, bases, attrs):
        attr_meta = attrs.pop("Meta", None)
        if attr_meta:
            bases += (ApiMixIn,)
        meta = {
            "url_template": getattr(attr_meta, "url_template", ""),
            "watch_list_url": getattr(attr_meta, "watch_list_url", ""),
            "fields": [],
            "field_names": []
        }
        field_names = meta["field_names"]
        fields = meta["fields"]
        for k, v in list(attrs.items()):
            if isinstance(v, Field):
                v.name = k
                field_names.append(k)
                fields.append(v)
        Meta = namedtuple("Meta", meta.keys())
        attrs["_meta"] = Meta(**meta)
        return super(MetaModel, mcs).__new__(mcs, cls, bases, attrs)


class ApiMixIn(object):
    """ApiMixIn class for top level Models

    Contains methods for working with the API
    """
    _client = Client()

    @classmethod
    def _build_url(cls, **kwargs):
        return cls._meta.url_template.format(**kwargs)

    @classmethod
    def find(cls, name, namespace="default"):
        url = cls._build_url(name="", namespace=namespace)
        resp = cls._client.get(url, params={"labelSelector": "app={}".format(name)})
        return [cls.from_dict(item) for item in resp.json()[u"items"]]

    @classmethod
    def list(cls, namespace="default"):
        url = cls._build_url(name="", namespace=namespace)
        resp = cls._client.get(url)
        return [cls.from_dict(item) for item in resp.json()[u"items"]]

    @classmethod
    def watch_list(cls):
        """Return a generator that yields WatchEvents of cls"""
        url = cls._meta.watch_list_url
        if not url:
            raise NotImplementedError("Cannot watch_list, no watch_list_url defined on class {}".format(cls))
        resp = cls._client.get(url, stream=True, timeout=None)
        for line in resp.iter_lines(chunk_size=None):
            if line:
                try:
                    event_json = json.loads(line)
                    event = WatchEvent(event_json, cls)
                    yield event
                except ValueError:
                    LOG.exception("Unable to parse JSON on watch event, discarding event. Line: %r", line)

    @classmethod
    def get(cls, name, namespace="default"):
        """Get from API server if it exists"""
        url = cls._build_url(name=name, namespace=namespace)
        resp = cls._client.get(url)
        instance = cls.from_dict(resp.json())
        return instance

    @classmethod
{'fraction_non_alphanumeric': 0.11176470588235295, 'fraction_numerical': 0.0007739938080495357, 'mean_word_length': 2.5402739726027397, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6831585', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1860, 'n_words': 536}