<commit_before># -*- coding: utf-8 -*-
import copy
import os

from copy import deepcopy

import solar

from solar.core import actions
from solar.core import observer
from solar.core import signals
from solar import utils
from solar.core import validation

from solar.core.connections import ResourcesConnectionGraph
from solar.interfaces.db import get_db

db = get_db()


class Resource(object):
    def __init__(self, name, metadata, args, tags=None):
        self.name = name
        self.metadata = metadata
        self.actions = metadata['actions'].keys() if metadata['actions'] else None
        self.tags = tags or []
        self.set_args_from_dict(args)

    @property
    def args(self):
        ret = {}

        raw_resource = db.read(self.name, collection=db.COLLECTIONS.resource)
        if raw_resource is None:
            return {}

        args = raw_resource['input']

        for arg_name, metadata_arg in self.metadata['input'].items():
            type_ = validation.schema_input_type(metadata_arg.get('schema', 'str'))

            value = args.get(arg_name, {}).get('value')
            if value is None and metadata_arg['value'] is not None:
                value = metadata_arg['value']

            ret[arg_name] = observer.create(type_, self, arg_name, value)

        return ret

    def set_args_from_dict(self, new_args):
        args = {}

        metadata = copy.deepcopy(self.metadata)

        raw_resource = db.read(self.name, collection=db.COLLECTIONS.resource)
        if raw_resource:
            args = {k: v['value'] for k, v in raw_resource['input'].items()}

        args.update(new_args)

        metadata['tags'] = self.tags
        for k, v in args.items():
            if k not in metadata['input']:
                raise NotImplementedError('Argument {} not implemented for resource {}'.format(k, self))

            metadata['input'][k]['value'] = v

        db.save(self.name, metadata, collection=db.COLLECTIONS.resource)

    def set_args(self, args):
        self.set_args_from_dict({k: v.value for k, v in args.items()})

    def __repr__(self):
        return ("Resource(name='{name}', metadata={metadata}, args={args}, "
                "tags={tags})").format(**self.to_dict())

    def to_dict(self):
        return {
            'name': self.name,
            'metadata': self.metadata,
            'args': self.args_show(),
            'tags': self.tags,
        }

    def args_show(self):
        def formatter(v):
            if isinstance(v, observer.ListObserver):
                return v.value
            elif isinstance(v, observer.Observer):
                return {
                    'emitter': v.emitter.attached_to.name if v.emitter else None,
                    'value': v.value,
                }

            return v

        return {k: formatter(v) for k, v in self.args.items()}

    def args_dict(self):
        return {k: v.value for k, v in self.args.items()}

    def add_tag(self, tag):
        if tag not in self.tags:
            self.tags.append(tag)

    def remove_tag(self, tag):
        try:
            self.tags.remove(tag)
        except ValueError:
            pass<commit_msg>Resource: args_dict refactor<commit_after># -*- coding: utf-8 -*-
import copy
import os

from copy import deepcopy

import solar

from solar.core import actions
from solar.core import observer
from solar.core import signals
from solar import utils
from solar.core import validation

from solar.core.connections import ResourcesConnectionGraph
from solar.interfaces.db import get_db

db = get_db()


class Resource(object):
    def __init__(self, name, metadata, args, tags=None):
        self.name = name
        self.metadata = metadata
        self.actions = metadata['actions'].keys() if metadata['actions'] else None

        # TODO: read tags from DB on demand
        self.tags = tags or []
        self.set_args_from_dict(args)

    @property
    def args(self):
        ret = {}

        args = self.args_dict()

        for arg_name, metadata_arg in self.metadata['input'].items():
            type_ = validation.schema_input_type(metadata_arg.get('schema', 'str'))

            ret[arg_name] = observer.create(
                type_, self, arg_name, args.get(arg_name)
            )

        return ret

    def args_dict(self):
        raw_resource = db.read(self.name, collection=db.COLLECTIONS.resource)
        if raw_resource is None:
            return {}

        self.metadata = raw_resource

        args = self.metadata['input']

        return {k: v['value'] for k, v in args.items()}

    def set_args_from_dict(self, new_args):
        args = self.args_dict()
        args.update(new_args)

        self.metadata['tags'] = self.tags
        for k, v in args.items():
            if k not in self.metadata['input']:
                raise NotImplementedError(
                    'Argument {} not implemented for resource {}'.format(k, self)
                )

            self.metadata['input'][k]['value'] = v

        db.save(self.name, self.metadata, collection=db.COLLECTIONS.resource)

    def set_args(self, args):
        self.set_args_from_dict({k: v.value for k, v in args.items()})

    def __repr__(self):
        return ("Resource(name='{name}', metadata={metadata}, args={args}, "
                "tags={tags})").format(**self.to_dict())

    def to_dict(self):
        return {
            'name': self.name,
            'metadata': self.metadata,
            'args': self.args_show(),
            'tags': self.tags,
        }

    def args_show(self):
        def formatter(v):
            if isinstance(v, observer.ListObserver):
                return v.value
            elif isinstance(v, observer.Observer):
                return {
                    'emitter': v.emitter.attached_to.name if v.emitter else None,
                    'value': v.value,
                }

            return v

        return {k: formatter(v) for k, v in self.args.items()}

    def add_tag(self, tag):
        if tag not in self.tags:
            self.tags.append(tag)

    def remove_tag(self, tag):
        try:
            self.tags.remove(tag)
        except ValueError:
            pass
{'fraction_non_alphanumeric': 0.11613632692618317, 'fraction_numerical': 0.00032304958811177516, 'mean_word_length': 2.108433734939759, 'pattern_counts': {'":': 0, '<': 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': '2201976', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1739, 'n_words': 464}