<commit_before>#!/usr/bin/env python
# vim:sw=4
"""
"""

import types

_no_default = object()

class Resource(object):
    """The Resource class holds a collection of application wide resources.
    """
    _resources = {}

    def __init__(self, initial_resources={}):
        self._resources.update(initial_resources)

    def __call__(self, r, default=_no_default):
        return self.resource(r, default)

    def resource(self, r, default=_no_default):
        """Locate a resource.

        Resource should be the class of the resource to look for or a string. In
        case of a string the resource will be looked up in the GConf configuration.

        example: Get the element factory:
                factory = gaphor.resource(gaphor.UML.ElementFactory)

        Also builtin resources are 'Name', 'Version' and 'DataDir'. In case main()
        is run, 'MainWindow' points to the main window of the application.

        If a class name is given as a resource, the resource is created if not
        yet available. If the resource is a string, KeyError is issued if the
        resource could not be localed, unless a default value was set.
        """
        _resources = self._resources

        # Return the existing resource
        try:
            return _resources[r]
        except KeyError:
            pass

        # Handle string-like resources 
        if isinstance (r, types.StringType):
            # TODO: It might be a GConf resource string

            if default is not _no_default:
                _resources[r] = default
                return default
            raise KeyError, 'No resource with name "%s"' % r
        # Instantiate the resource and return it
        i = r()
        _resources[r] = i
        _resources[r.__name__] = i
        return i

    def set(self, r, value):
        """Set a resource to a specific value.
        No smart things are done with classes and class names (like the
        resource() method does).
        """
        self._resources[r] = value
<commit_msg>*** empty log message ***<commit_after>#!/usr/bin/env python
# vim:sw=4
"""
"""

import sys
import types

_no_default = object()

class Resource(object):
    """The Resource class holds a collection of application wide resources.
    """
    _resources = {}

    def __init__(self, initial_resources={}):
        self._resources.update(initial_resources)
        self._persistent = []

    def __call__(self, r, default=_no_default):
        return self.resource(r, default)

    def dump(self, to=sys.stdout):
        """TODO: define resources that are persistent (have to be saved
        and loaded.
        """
        for name in self._persistent:
            print >> to, '%s: %s' % (name, self._resources.get(name))

    def resource(self, r, default=_no_default, persistent=False):
        """Locate a resource.

        Resource should be the class of the resource to look for or a string. In
        case of a string the resource will be looked up in the GConf configuration.

        example: Get the element factory:
                factory = gaphor.resource(gaphor.UML.ElementFactory)

        Also builtin resources are 'Name', 'Version' and 'DataDir'. In case main()
        is run, 'MainWindow' points to the main window of the application.

        If a class name is given as a resource, the resource is created if not
        yet available. If the resource is a string, KeyError is issued if the
        resource could not be localed, unless a default value was set.
        """
        _resources = self._resources

        # Return the existing resource
        try:
            return _resources[r]
        except KeyError:
            pass

        # Handle string-like resources 
        if isinstance (r, types.StringType):
            # TODO: It might be a GConf resource string

            if default is not _no_default:
                self.set(r, default, persistent)
                return default
            raise KeyError, 'No resource with name "%s"' % r

        # Instantiate the resource and return it
        i = r()
        self.set(r, i, persistent)
        self.set(r.__name__, i, persistent)
        return i

    def set(self, r, value, persistent=False):
        """Set a resource to a specific value.
        No smart things are done with classes and class names (like the
        resource() method does).
        """
        self._resources[r] = value
        if persistent or r in self._persistent:
            self.persist(r)

    def persist(self, r):
        """Save the property to a persistent storage.
        """
        if r not in self._persistent:
            self._persistent.append(r)

        # If GConf: add
        # If win32: add to registry
        # Else: create ~/.gaphor/resources
{'fraction_non_alphanumeric': 0.08260142199916352, 'fraction_numerical': 0.000836470096194061, 'mean_word_length': 2.1446416831032216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2723358', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1227, 'n_words': 513}