Example #1
0
 def test_set_non_str_attribute_casts_to_string(self):
     # If the value of the attribute passed to set_attribute is not a
     # string, it's cast to a string.
     tag = Tag('whatever', self.req)
     tag.set_attribute('new-attribute', 42)
     result = tag.get_attribute('new-attribute')
     self.assertEqual('42', result)
Example #2
0
 def test_set_non_str_attribute_casts_to_string(self):
     # If the value of the attribute passed to set_attribute is not a
     # string, it's cast to a string.
     tag = Tag('whatever', self.req)
     tag.set_attribute('new-attribute', 42)
     result = tag.get_attribute('new-attribute')
     self.assertEqual('42', result)
Example #3
0
 def test_set_then_get_attribute(self):
     # Attributes set with set_attribute can be retrieved with
     # get_attribute.
     tag = Tag('whatever', self.req)
     tag.set_attribute('new-attribute', 'value')
     result = tag.get_attribute('new-attribute')
     self.assertEqual('value', result)
Example #4
0
 def test_set_then_get_attribute(self):
     # Attributes set with set_attribute can be retrieved with
     # get_attribute.
     tag = Tag('whatever', self.req)
     tag.set_attribute('new-attribute', 'value')
     result = tag.get_attribute('new-attribute')
     self.assertEqual('value', result)
Example #5
0
 def test_set_name_doesnt_call_save(self):
     # Setting the name attribute doesn't call save.
     save_calls = []
     tag = Tag('old', self.req)
     try:
         tag.set_attribute('name', 'new')
     except Set_Name_Attribute_Error:
         pass
     self.assertEqual(0, len(save_calls))
Example #6
0
 def test_set_name_doesnt_call_save(self):
     # Setting the name attribute doesn't call save.
     save_calls = []
     tag = Tag('old', self.req)
     try:
         tag.set_attribute('name', 'new')
     except Set_Name_Attribute_Error:
         pass
     self.assertEqual(0, len(save_calls))
Example #7
0
 def test_set_name_attribute_does_nothing(self):
     # The 'name' attribute is set by the constructor. After it is set, it
     # cannot be changed with further calls to set_attribute.
     tag = Tag('old', self.req)
     try:
         tag.set_attribute('name', 'new')
     except Set_Name_Attribute_Error:
         pass
     self.assertEqual('old', tag.get_name())
     self.assertEqual('old', tag.get_attribute('name'))
Example #8
0
    def new_search_tag(self, name, query, attributes={}):
        """
        Create a new search tag

        @returns GTG.core.tag.Tag: the new search tag/None for a invalid query
        """
        try:
            parameters = parse_search_query(query)
        except InvalidQuery as e:
            Log.warning("Problem with parsing query '%s' (skipping): %s" %
                        (query, e.message))
            return None

        # Create own copy of attributes and add special attributes label, query
        init_attr = dict(attributes)
        init_attr["label"] = name
        init_attr["query"] = query

        tag = Tag(name, req=self.requester, attributes=init_attr)
        self._add_new_tag(name,
                          tag,
                          search_filter,
                          parameters,
                          parent_id=SEARCH_TAG)
        self.save_tagtree()
        return tag
Example #9
0
    def new_tag(self, name, attributes={}):
        """
        Create a new tag

        @returns GTG.core.tag.Tag: the new tag
        """
        parameters = {'tag': name}
        tag = Tag(name, req=self.requester, attributes=attributes)
        self._add_new_tag(name, tag, self.treefactory.tag_filter, parameters)
        return tag
Example #10
0
 def test_set_name_attribute_does_nothing(self):
     # The 'name' attribute is set by the constructor. After it is set, it
     # cannot be changed with further calls to set_attribute.
     tag = Tag('old', self.req)
     try:
         tag.set_attribute('name', 'new')
     except Set_Name_Attribute_Error:
         pass
     self.assertEqual('old', tag.get_name())
     self.assertEqual('old', tag.get_attribute('name'))
Example #11
0
 def setUp(self):
     self.tag = Tag('foo', None)
     # Do not allow notifying related tasks
     self.tag.notify_related_tasks = lambda: None
Example #12
0
 def test_get_all_but_name(self):
     # If 'butname' is True, then exclude the 'name' attribute.
     tag = Tag('foo', self.req)
     self.assertEqual([], tag.get_all_attributes(butname=True))
     tag.set_attribute('bar', 'baz')
     self.assertEqual(['bar'], tag.get_all_attributes(butname=True))
Example #13
0
 def test_get_all_attributes_initial(self):
     # Initially, a Tag has only the name attribute.
     tag = Tag('foo', self.req)
     self.assertEqual(['name'], tag.get_all_attributes())
Example #14
0
 def setUp(self):
     self.tag = Tag('foo', None)
     # Do not allow notifying related tasks
     self.tag.notify_related_tasks = lambda: None
Example #15
0
 def test_missing_attribute_returns_none(self):
     # If get_attribute is called for an attribute that doesn't exist, it
     # returns None.
     tag = Tag('whatever', self.req)
     result = tag.get_attribute('no-such-attribute')
     self.assertEqual(None, result)
Example #16
0
 def test_name_is_attribute(self):
     # The name of the tag is also stored as an attribute.
     tag = Tag('foo', self.req)
     self.assertEqual('foo', tag.get_attribute('name'))
Example #17
0
 def test_name(self):
     # The first argument to the Tag constructor is the name, which you can
     # get with get_name().
     tag = Tag('foo', self.req)
     self.assertEqual('foo', tag.get_name())
Example #18
0
    def get_tags_tree(self, req):
        '''This create a liblarch tree suitable for tags,
        including the all_tags_tag and notag_tag.
        '''
        tagtree = Tree()

        ### building the initial tags
        # Build the "all tasks tag"
        alltag = Tag(CoreConfig.ALLTASKS_TAG, req=req)
        alltag.set_attribute("special", "all")
        alltag.set_attribute("label",
                             "<span weight='bold'>%s</span>" % _("All tasks"))
        alltag.set_attribute("icon", "gtg-tags-all")
        alltag.set_attribute("order", 0)
        tagtree.add_node(alltag)
        p = {}
        self.tasktree.add_filter(CoreConfig.ALLTASKS_TAG,
                                 self.alltag,
                                 parameters=p)
        # Build the "without tag tag"
        notag_tag = Tag(CoreConfig.NOTAG_TAG, req=req)
        notag_tag.set_attribute("special", "notag")
        notag_tag.set_attribute(
            "label", "<span weight='bold'>%s</span>" % _("Tasks with no tags"))
        notag_tag.set_attribute("icon", "gtg-tags-none")
        notag_tag.set_attribute("order", 2)
        tagtree.add_node(notag_tag)
        p = {}
        self.tasktree.add_filter(CoreConfig.NOTAG_TAG,
                                 self.notag,
                                 parameters=p)

        # Build the search tag
        search_tag = Tag(CoreConfig.SEARCH_TAG, req=req)
        search_tag.set_attribute("special", "search")
        search_tag.set_attribute("label",
                                 "<span weight='bold'>%s</span>" % _("Search"))
        search_tag.set_attribute("icon", "search")
        search_tag.set_attribute("order", 1)
        tagtree.add_node(search_tag)
        p = {}
        self.tasktree.add_filter(CoreConfig.SEARCH_TAG,
                                 search_filter,
                                 parameters=p)

        # Build the separator
        sep_tag = Tag(CoreConfig.SEP_TAG, req=req)
        sep_tag.set_attribute("special", "sep")
        sep_tag.set_attribute("order", 3)
        tagtree.add_node(sep_tag)

        #### Filters
        tagtree.add_filter('activetag', self.actively_used_tag)
        tagtree.add_filter('usedtag', self.used_tag)

        activeview = tagtree.get_viewtree(name='activetags', refresh=False)
        activeview.apply_filter('activetag')

        # This view doesn't seem to be used. So it's not useful to build it now
        #        usedview = tagtree.get_viewtree(name='usedtags',refresh=False)
        #        usedview.apply_filter('usedtag')

        self.tagtree = tagtree
        self.tagtree_loaded = True
        return tagtree
Example #19
0
 def test_name_is_attribute(self):
     # The name of the tag is also stored as an attribute.
     tag = Tag('foo', self.req)
     self.assertEqual('foo', tag.get_attribute('name'))
Example #20
0
 def test_save_not_called_on_construction(self):
     # The save callback isn't called by the constructor, despite the fact
     # that it sets the name attribute.
     save_calls = []
     Tag('old', self.req)
     self.assertEqual(0, len(save_calls))
Example #21
0
class TestTag(TestCase):
    def setUp(self):
        self.tag = Tag('foo', None)
        # Do not allow notifying related tasks
        self.tag.notify_related_tasks = lambda: None

    def test_has_name(self):
        self.assertEqual('foo', self.tag.get_name())

    def test_name_is_attribute(self):
        self.assertEqual('foo', self.tag.get_attribute('name'))

    def test_missing_attribute_returns_none(self):
        self.assertEqual(None, self.tag.get_attribute('no-such-attribute'))

    def test_set_then_get_attribute(self):
        self.tag.set_attribute('new-attribute', 'value')
        attr = self.tag.get_attribute('new-attribute')
        self.assertEqual('value', attr)

    def test_set_non_str_attribute_casts_to_string(self):
        self.tag.set_attribute('new-attribute', 42)
        attr = self.tag.get_attribute('new-attribute')
        self.assertEqual('42', attr)

    def test_initial_attribute_is_name_only(self):
        self.assertEqual(['name'], self.tag.get_all_attributes())

    def test_can_add_new_attributes(self):
        self.tag.set_attribute('bar', 'baz')
        self.assertEqual({'name', 'bar'}, set(self.tag.get_all_attributes()))

    def test_get_all_attributes_but_name(self):
        self.assertEqual([], self.tag.get_all_attributes(butname=True))
        self.tag.set_attribute('bar', 'baz')
        self.assertEqual(['bar'], self.tag.get_all_attributes(butname=True))

    def test_name_cannot_be_changed(self):
        self.assertEqual('foo', self.tag.get_name())

        with self.assertRaises(KeyError):
            self.tag.set_attribute('name', 'new')

        self.assertEqual('foo', self.tag.get_name())
        self.assertEqual('foo', self.tag.get_attribute('name'))
Example #22
0
 def test_name(self):
     # The first argument to the Tag constructor is the name, which you can
     # get with get_name().
     tag = Tag('foo', self.req)
     self.assertEqual('foo', tag.get_name())
Example #23
0
 def test_get_all_attributes_initial(self):
     # Initially, a Tag has only the name attribute.
     tag = Tag('foo', self.req)
     self.assertEqual(['name'], tag.get_all_attributes())
Example #24
0
 def test_missing_attribute_returns_none(self):
     # If get_attribute is called for an attribute that doesn't exist, it
     # returns None.
     tag = Tag('whatever', self.req)
     result = tag.get_attribute('no-such-attribute')
     self.assertEqual(None, result)
Example #25
0
 def test_get_all_attributes_after_setting(self):
     # After attributes are set, get_all_attributes includes those
     # attributes. The order is not guaranteed.
     tag = Tag('foo', self.req)
     tag.set_attribute('bar', 'baz')
     self.assertEqual(set(['name', 'bar']), set(tag.get_all_attributes()))
Example #26
0
 def test_get_all_but_name(self):
     # If 'butname' is True, then exclude the 'name' attribute.
     tag = Tag('foo', self.req)
     self.assertEqual([], tag.get_all_attributes(butname=True))
     tag.set_attribute('bar', 'baz')
     self.assertEqual(['bar'], tag.get_all_attributes(butname=True))
Example #27
0
class DataStore(object):
    """
    A wrapper around all backends that is responsible for keeping the backend
    instances. It can enable, disable, register and destroy backends, and acts
    as interface between the backends and GTG core.
    You should not interface yourself directly with the DataStore: use the
    Requester instead (which also sends signals as you issue commands).
    """
    def __init__(self, global_conf=CoreConfig()):
        """
        Initializes a DataStore object
        """
        # dictionary {backend_name_string: Backend instance}
        self.backends = {}
        self.treefactory = TreeFactory()
        self._tasks = self.treefactory.get_tasks_tree()
        self.requester = requester.Requester(self, global_conf)
        self.tagfile = None
        self._tagstore = self.treefactory.get_tags_tree(self.requester)
        self.load_tag_tree()
        self._backend_signals = BackendSignals()

        # Flag when turned to true, all pending operation should be
        # completed and then GTG should quit
        self.please_quit = False

        # The default backend must be loaded first. This flag turns to True
        # when the default backend loading has finished.
        self.is_default_backend_loaded = False
        self._backend_signals.connect('default-backend-loaded',
                                      self._activate_non_default_backends)
        self.filtered_datastore = FilteredDataStore(self)
        self._backend_mutex = threading.Lock()

    ### Accessor to embedded objects in DataStore ############################
    def get_tagstore(self):
        """
        Return the Tagstore associated with this DataStore

        @return GTG.core.tagstore.TagStore: the tagstore object
        """
        return self._tagstore

    def get_requester(self):
        """
        Return the Requester associate with this DataStore

        @returns GTG.core.requester.Requester: the requester associated with
                                               this datastore
        """
        return self.requester

    def get_tasks_tree(self):
        """
        Return the Tree with all the tasks contained in this Datastore

        @returns GTG.core.tree.Tree: a task tree (the main one)
        """
        return self._tasks

    ### Tags functions ########################################################
    def _add_new_tag(self, name, tag, filter_func, parameters, parent_id=None):
        """ Add tag into a tree """
        name = name.encode("UTF-8")
        if self._tagstore.has_node(name):
            raise IndexError('tag %s was already in the datastore' % name)

        self._tasks.add_filter(name, filter_func, parameters=parameters)
        self._tagstore.add_node(tag, parent_id=parent_id)
        tag.set_save_callback(self.save)

    def new_tag(self, name, attributes={}):
        """
        Create a new tag

        @returns GTG.core.tag.Tag: the new tag
        """
        name = name.encode("UTF-8")
        parameters = {'tag': name}
        tag = Tag(name, req=self.requester, attributes=attributes)
        self._add_new_tag(name, tag, self.treefactory.tag_filter, parameters)
        return tag

    def new_search_tag(self, name, query, attributes={}):
        """
        Create a new search tag

        @returns GTG.core.tag.Tag: the new search tag/None for a invalid query
        """
        try:
            parameters = parse_search_query(query)
        except InvalidQuery, e:
            Log.warning("Problem with parsing query '%s' (skipping): %s" %
                        (query, e.message))
            return None

        name = name.encode("UTF-8")

        # Create own copy of attributes and add special attributes label, query
        init_attr = dict(attributes)
        init_attr["label"] = name
        init_attr["query"] = query

        tag = Tag(name, req=self.requester, attributes=init_attr)
        self._add_new_tag(name,
                          tag,
                          search_filter,
                          parameters,
                          parent_id=CoreConfig.SEARCH_TAG)
        self.save_tagtree()
        return tag
Example #28
0
 def test_get_all_attributes_after_setting(self):
     # After attributes are set, get_all_attributes includes those
     # attributes. The order is not guaranteed.
     tag = Tag('foo', self.req)
     tag.set_attribute('bar', 'baz')
     self.assertEqual(set(['name', 'bar']), set(tag.get_all_attributes()))
Example #29
0
    def get_tags_tree(self, req):
        """This create a liblarch tree suitable for tags,
        including the all_tags_tag and notag_tag.
        """
        tagtree = Tree()

        ### building the initial tags
        # Build the "all tasks tag"
        alltag = Tag(CoreConfig.ALLTASKS_TAG, req=req)
        alltag.set_attribute("special", "all")
        alltag.set_attribute("label", "<span weight='bold'>%s</span>" % _("All tasks"))
        alltag.set_attribute("icon", "gtg-tags-all")
        alltag.set_attribute("order", 0)
        tagtree.add_node(alltag)
        p = {}
        self.tasktree.add_filter(CoreConfig.ALLTASKS_TAG, self.alltag, parameters=p)
        # Build the "without tag tag"
        notag_tag = Tag(CoreConfig.NOTAG_TAG, req=req)
        notag_tag.set_attribute("special", "notag")
        notag_tag.set_attribute("label", "<span weight='bold'>%s</span>" % _("Tasks with no tags"))
        notag_tag.set_attribute("icon", "gtg-tags-none")
        notag_tag.set_attribute("order", 2)
        tagtree.add_node(notag_tag)
        p = {}
        self.tasktree.add_filter(CoreConfig.NOTAG_TAG, self.notag, parameters=p)

        # Build the search tag
        search_tag = Tag(CoreConfig.SEARCH_TAG, req=req)
        search_tag.set_attribute("special", "search")
        search_tag.set_attribute("label", "<span weight='bold'>%s</span>" % _("Search"))
        search_tag.set_attribute("icon", "search")
        search_tag.set_attribute("order", 1)
        tagtree.add_node(search_tag)
        p = {}
        self.tasktree.add_filter(CoreConfig.SEARCH_TAG, search_filter, parameters=p)

        # Build the separator
        sep_tag = Tag(CoreConfig.SEP_TAG, req=req)
        sep_tag.set_attribute("special", "sep")
        sep_tag.set_attribute("order", 3)
        tagtree.add_node(sep_tag)

        #### Filters
        tagtree.add_filter("activetag", self.actively_used_tag)
        tagtree.add_filter("usedtag", self.used_tag)

        activeview = tagtree.get_viewtree(name="activetags", refresh=False)
        activeview.apply_filter("activetag")

        # This view doesn't seem to be used. So it's not useful to build it now
        #        usedview = tagtree.get_viewtree(name='usedtags',refresh=False)
        #        usedview.apply_filter('usedtag')

        self.tagtree = tagtree
        self.tagtree_loaded = True
        return tagtree
Example #30
0
 def test_str(self):
     # str(tag) is 'Tag: <name>'
     tag = Tag('foo', self.req)
     self.assertEqual('Tag: foo', str(tag))
Example #31
0
class TestTag(TestCase):
    def setUp(self):
        self.tag = Tag('foo', None)
        # Do not allow notifying related tasks
        self.tag.notify_related_tasks = lambda: None

    def test_has_name(self):
        self.assertEqual('foo', self.tag.get_name())

    def test_name_is_attribute(self):
        self.assertEqual('foo', self.tag.get_attribute('name'))

    def test_missing_attribute_returns_none(self):
        self.assertEqual(None, self.tag.get_attribute('no-such-attribute'))

    def test_set_then_get_attribute(self):
        self.tag.set_attribute('new-attribute', 'value')
        attr = self.tag.get_attribute('new-attribute')
        self.assertEqual('value', attr)

    def test_set_non_str_attribute_casts_to_string(self):
        self.tag.set_attribute('new-attribute', 42)
        attr = self.tag.get_attribute('new-attribute')
        self.assertEqual('42', attr)

    def test_initial_attribute_is_name_only(self):
        self.assertEqual(['name'], self.tag.get_all_attributes())

    def test_can_add_new_attributes(self):
        self.tag.set_attribute('bar', 'baz')
        self.assertEqual({'name', 'bar'}, set(self.tag.get_all_attributes()))

    def test_get_all_attributes_but_name(self):
        self.assertEqual([], self.tag.get_all_attributes(butname=True))
        self.tag.set_attribute('bar', 'baz')
        self.assertEqual(['bar'], self.tag.get_all_attributes(butname=True))

    def test_name_cannot_be_changed(self):
        self.assertEqual('foo', self.tag.get_name())

        with self.assertRaises(KeyError):
            self.tag.set_attribute('name', 'new')

        self.assertEqual('foo', self.tag.get_name())
        self.assertEqual('foo', self.tag.get_attribute('name'))