Beispiel #1
0
    def update_todo_timestamp(self, isdone=True):
        """ return true if need to push to next tag """
        datetype = "CLOSED"
        if not isdone:
            return self.insert_timestamp_header(datetype, remove=True)

        info, _ = self.get_timestamp_header()

        try:
            # add time to scheduled
            datetype2 = 'SCHEDULED'
            scheduled = get_orgdate(info.get(datetype2))
            repeater = scheduled.repeater
            assert repeater

            currdate = date.today() if repeater[0] == '.' else scheduled
            newdate = get_orgdate(
                '<%s Abc %s>' %
                (self._modify_time(currdate, repeater[1:]), repeater))
            newdate.printall = True

            self.insert_timestamp_header(datetype2, usetime=newdate)
            return True
        except Exception as e:
            print(e)
            return self.insert_timestamp_header(datetype, usetime=False)
	def test_get_orgdate_parsing_with_invalid_dates(self):
		u"""
		Something like <2011-14-29 Mon> (invalid dates, they don't exist)
		should not be parsed
		"""
		datestr = u"<2011-14-30 Tue>"
		self.assertEqual(get_orgdate(datestr), None)

		datestr = u"<2012-03-40 Tue>"
		self.assertEqual(get_orgdate(datestr), None)

		datestr = u"<2012-03-40 Tue 24:70>"
		self.assertEqual(get_orgdate(datestr), None)
    def test_get_orgdate_parsing_with_invalid_dates(self):
        u"""
		Something like <2011-14-29 Mon> (invalid dates, they don't exist)
		should not be parsed
		"""
        datestr = u"<2011-14-30 Tue>"
        self.assertEqual(get_orgdate(datestr), None)

        datestr = u"<2012-03-40 Tue>"
        self.assertEqual(get_orgdate(datestr), None)

        datestr = u"<2012-03-40 Tue 24:70>"
        self.assertEqual(get_orgdate(datestr), None)
	def test_get_orgdate_parsing_with_list_of_texts(self):
		u"""
		get_orgdate should return the first date in the list.
		"""
		datelist = [u"<2011-08-29 Mon>"]
		result = get_orgdate(datelist)
		self.assertNotEquals(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 8)
		self.assertEqual(result.day, 29)

		datelist = [u"<2011-08-29 Mon>",
				u"<2012-03-30 Fri>"]
		result = get_orgdate(datelist)
		self.assertNotEquals(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 8)
		self.assertEqual(result.day, 29)

		datelist = [u"some <2011-08-29 Mon>text",
				u"<2012-03-30 Fri> is here"]
		result = get_orgdate(datelist)
		self.assertNotEquals(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 8)
		self.assertEqual(result.day, 29)

		datelist = [u"here is no date",
				u"some <2011-08-29 Mon>text",
				u"<2012-03-30 Fri> is here"]
		result = get_orgdate(datelist)
		self.assertNotEquals(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 8)
		self.assertEqual(result.day, 29)

		datelist = [u"here is no date",
				u"some <2011-08-29 Mon 20:10> text",
				u"<2012-03-30 Fri> is here"]
		result = get_orgdate(datelist)
		self.assertNotEquals(result, None)
		self.assertTrue(isinstance(result, OrgDateTime))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 8)
		self.assertEqual(result.day, 29)
		self.assertEqual(result.hour, 20)
		self.assertEqual(result.minute, 10)
    def test_get_orgdate_parsing_with_list_of_texts(self):
        u"""
		get_orgdate should return the first date in the list.
		"""
        datelist = [u"<2011-08-29 Mon>"]
        result = get_orgdate(datelist)
        self.assertNotEquals(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 8)
        self.assertEqual(result.day, 29)

        datelist = [u"<2011-08-29 Mon>", u"<2012-03-30 Fri>"]
        result = get_orgdate(datelist)
        self.assertNotEquals(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 8)
        self.assertEqual(result.day, 29)

        datelist = [u"some <2011-08-29 Mon>text", u"<2012-03-30 Fri> is here"]
        result = get_orgdate(datelist)
        self.assertNotEquals(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 8)
        self.assertEqual(result.day, 29)

        datelist = [
            u"here is no date", u"some <2011-08-29 Mon>text",
            u"<2012-03-30 Fri> is here"
        ]
        result = get_orgdate(datelist)
        self.assertNotEquals(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 8)
        self.assertEqual(result.day, 29)

        datelist = [
            u"here is no date", u"some <2011-08-29 Mon 20:10> text",
            u"<2012-03-30 Fri> is here"
        ]
        result = get_orgdate(datelist)
        self.assertNotEquals(result, None)
        self.assertTrue(isinstance(result, OrgDateTime))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 8)
        self.assertEqual(result.day, 29)
        self.assertEqual(result.hour, 20)
        self.assertEqual(result.minute, 10)
	def test_get_orgdatetime_parsing_active(self):
		u"""
		get_orgdate should recognize all orgdatetimes in a given text
		"""
		result = get_orgdate(u"<2011-09-12 Mon 10:20>")
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgDateTime))
		self.assertEqual(result.year, 2011)
		self.assertEqual(result.month, 9)
		self.assertEqual(result.day, 12)
		self.assertEqual(result.hour, 10)
		self.assertEqual(result.minute, 20)
		self.assertTrue(result.active)

		result = get_orgdate(u"some datetime <2011-09-12 Mon 10:20> stuff")
		self.assertTrue(isinstance(result, OrgDateTime))
    def test_get_orgdatetime_parsing_passive(self):
        u"""
		get_orgdate should recognize all orgdatetimes in a given text
		"""
        result = get_orgdate(u"[2011-09-12 Mon 10:20]")
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgDateTime))
        self.assertEqual(result.year, 2011)
        self.assertEqual(result.month, 9)
        self.assertEqual(result.day, 12)
        self.assertEqual(result.hour, 10)
        self.assertEqual(result.minute, 20)
        self.assertFalse(result.active)

        result = get_orgdate(u"some datetime [2011-09-12 Mon 10:20] stuff")
        self.assertTrue(isinstance(result, OrgDateTime))
	def test_get_orgdate_parsing_with_utf8(self):
		u"""
		get_orgdate should recognize all orgdates within a given utf-8 text
		"""
		result = get_orgdate(u'<2016-05-07 Sáb>')
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertEqual(result.year, 2016)
		self.assertEqual(result.month, 5)
		self.assertEqual(result.day, 7)
		self.assertTrue(result.active)

		datestr = u"This date <2016-05-07 Sáb> is embedded"
		self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))

		result = get_orgdate(u'[2016-05-07 Sáb]')
		self.assertFalse(result.active)

		datestr = u"This date [2016-05-07 Sáb] is embedded"
		self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))
    def test_get_orgdate_parsing_with_utf8(self):
        u"""
		get_orgdate should recognize all orgdates within a given utf-8 text
		"""
        result = get_orgdate(u'<2016-05-07 Sáb>')
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertEqual(result.year, 2016)
        self.assertEqual(result.month, 5)
        self.assertEqual(result.day, 7)
        self.assertTrue(result.active)

        datestr = u"This date <2016-05-07 Sáb> is embedded"
        self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))

        result = get_orgdate(u'[2016-05-07 Sáb]')
        self.assertFalse(result.active)

        datestr = u"This date [2016-05-07 Sáb] is embedded"
        self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))
	def test_get_orgdatetime_parsing_with_utf8(self):
		u"""
		get_orgdate should recognize all orgdatetimes in a given utf-8 text
		"""
		result = get_orgdate(u"<2016-05-07 Sáb 10:20>")
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgDateTime))
		self.assertEqual(result.year, 2016)
		self.assertEqual(result.month, 5)
		self.assertEqual(result.day, 7)
		self.assertEqual(result.hour, 10)
		self.assertEqual(result.minute, 20)
		self.assertTrue(result.active)

		result = get_orgdate(u"some datetime <2016-05-07 Sáb 10:20> stuff")
		self.assertTrue(isinstance(result, OrgDateTime))

		result = get_orgdate(u"[2016-05-07 Sáb 10:20]")
		self.assertFalse(result.active)

		result = get_orgdate(u"some datetime [2016-05-07 Sáb 10:20] stuff")
		self.assertTrue(isinstance(result, OrgDateTime))
    def test_get_orgdatetime_parsing_with_utf8(self):
        u"""
		get_orgdate should recognize all orgdatetimes in a given utf-8 text
		"""
        result = get_orgdate(u"<2016-05-07 Sáb 10:20>")
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgDateTime))
        self.assertEqual(result.year, 2016)
        self.assertEqual(result.month, 5)
        self.assertEqual(result.day, 7)
        self.assertEqual(result.hour, 10)
        self.assertEqual(result.minute, 20)
        self.assertTrue(result.active)

        result = get_orgdate(u"some datetime <2016-05-07 Sáb 10:20> stuff")
        self.assertTrue(isinstance(result, OrgDateTime))

        result = get_orgdate(u"[2016-05-07 Sáb 10:20]")
        self.assertFalse(result.active)

        result = get_orgdate(u"some datetime [2016-05-07 Sáb 10:20] stuff")
        self.assertTrue(isinstance(result, OrgDateTime))
	def test_get_orgtimerange_parsing_active(self):
		u"""
		get_orgdate should recognize all orgtimeranges in a given text
		"""
		daterangestr = u"<2011-09-12 Mon>--<2011-09-13 Tue>"
		result = get_orgdate(daterangestr)
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgTimeRange))
		self.assertEqual(unicode(result), daterangestr)
		self.assertTrue(result.active)

		daterangestr = u"<2011-09-12 Mon 10:20>--<2011-09-13 Tue 13:20>"
		result = get_orgdate(daterangestr)
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgTimeRange))
		self.assertEqual(unicode(result), daterangestr)
		self.assertTrue(result.active)

		daterangestr = u"<2011-09-12 Mon 10:20-13:20>"
		result = get_orgdate(daterangestr)
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgTimeRange))
		self.assertEqual(unicode(result), daterangestr)
		self.assertTrue(result.active)
    def test_get_orgtimerange_parsing_active(self):
        u"""
		get_orgdate should recognize all orgtimeranges in a given text
		"""
        daterangestr = u"<2011-09-12 Mon>--<2011-09-13 Tue>"
        result = get_orgdate(daterangestr)
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgTimeRange))
        self.assertEqual(unicode(result), daterangestr)
        self.assertTrue(result.active)

        daterangestr = u"<2011-09-12 Mon 10:20>--<2011-09-13 Tue 13:20>"
        result = get_orgdate(daterangestr)
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgTimeRange))
        self.assertEqual(unicode(result), daterangestr)
        self.assertTrue(result.active)

        daterangestr = u"<2011-09-12 Mon 10:20-13:20>"
        result = get_orgdate(daterangestr)
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgTimeRange))
        self.assertEqual(unicode(result), daterangestr)
        self.assertTrue(result.active)
	def test_get_orgdate_parsing_inactive(self):
		u"""
		get_orgdate should recognize all inactive orgdates in a given text
		"""
		result = get_orgdate(self.textinactive)
		self.assertNotEqual(result, None)
		self.assertTrue(isinstance(result, OrgDate))
		self.assertTrue(isinstance(get_orgdate(u"[2011-08-30 Tue]"), OrgDate))
		self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").year, 2011)
		self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").month, 8)
		self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").day, 30)
		self.assertFalse(get_orgdate(u"[2011-08-30 Tue]").active)

		datestr = u"This date [2011-08-30 Tue] is embedded"
		self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))
    def test_get_orgdate_parsing_inactive(self):
        u"""
		get_orgdate should recognize all inactive orgdates in a given text
		"""
        result = get_orgdate(self.textinactive)
        self.assertNotEqual(result, None)
        self.assertTrue(isinstance(result, OrgDate))
        self.assertTrue(isinstance(get_orgdate(u"[2011-08-30 Tue]"), OrgDate))
        self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").year, 2011)
        self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").month, 8)
        self.assertEqual(get_orgdate(u"[2011-08-30 Tue]").day, 30)
        self.assertFalse(get_orgdate(u"[2011-08-30 Tue]").active)

        datestr = u"This date [2011-08-30 Tue] is embedded"
        self.assertTrue(isinstance(get_orgdate(datestr), OrgDate))
Beispiel #16
0
    def parse_heading_from_data(cls,
                                data,
                                allowed_todo_states,
                                document=None,
                                orig_start=None):
        u""" Construct a new heading from the provided data

		:data:			List of lines
		:allowed_todo_states: TODO???
		:document:		The document object this heading belongs to
		:orig_start:	The original start of the heading in case it was read
						from a document. If orig_start is provided, the
						resulting heading will not be marked dirty.

		:returns:	The newly created heading
		"""
        test_not_empty = lambda x: x != u''

        def parse_title(heading_line):
            # WARNING this regular expression fails if there is just one or no
            # word in the heading but a tag!
            m = REGEX_HEADING.match(heading_line)
            if m:
                r = m.groupdict()
                level = len(r[u'level'])
                todo = None
                title = u''
                tags = filter(test_not_empty,
                              r[u'tags'].split(u':')) if r[u'tags'] else []
                tags = list(tags)

                # if there is just one or no word in the heading, redo the parsing
                mt = REGEX_TAG.match(r[u'title'])
                if not tags and mt:
                    r = mt.groupdict()
                    tags = filter(
                        test_not_empty,
                        r[u'tags'].split(u':')) if r[u'tags'] else []
                    tags = list(tags)
                if r[u'title'] is not None:
                    _todo_title = [
                        i.strip() for i in r[u'title'].split(None, 1)
                    ]
                    if _todo_title and _todo_title[0] in allowed_todo_states:
                        todo = _todo_title[0]
                        if len(_todo_title) > 1:
                            title = _todo_title[1]
                    else:
                        title = r[u'title'].strip()

                return (level, todo, title, tags)
            raise ValueError(u'Data doesn\'t start with a heading definition.')

        if not data:
            raise ValueError(u'Unable to create heading, no data provided.')

        # create new heaing
        new_heading = cls()
        new_heading.level, new_heading.todo, new_heading.title, new_heading.tags = parse_title(
            data[0])
        new_heading.body = data[1:]
        if orig_start is not None:
            new_heading._dirty_heading = False
            new_heading._dirty_body = False
            new_heading._orig_start = orig_start
            new_heading._orig_len = len(new_heading)
        if document:
            new_heading._document = document

        # try to find active dates
        tmp_orgdate = get_orgdate(data)
        if tmp_orgdate and tmp_orgdate.active \
         and not isinstance(tmp_orgdate, OrgTimeRange):
            new_heading.active_date = tmp_orgdate
        else:
            new_heading.active_date = None

        return new_heading
Beispiel #17
0
	def parse_heading_from_data(cls, data, allowed_todo_states, document=None,
			orig_start=None):
		u""" Construct a new heading from the provided data

		:data:			List of lines
		:allowed_todo_states: TODO???
		:document:		The document object this heading belongs to
		:orig_start:	The original start of the heading in case it was read
						from a document. If orig_start is provided, the
						resulting heading will not be marked dirty.

		:returns:	The newly created heading
		"""
		def parse_title(heading_line):
			# WARNING this regular expression fails if there is just one or no
			# word in the heading but a tag!
			m = REGEX_HEADING.match(heading_line)
			if m:
				r = m.groupdict()
				level = len(r[u'level'])
				todo = None
				title = u''
				tags = filter(lambda x: x != u'', r[u'tags'].split(u':')) if r[u'tags'] else []

				# if there is just one or no word in the heading, redo the parsing
				mt = REGEX_TAGS.match(r[u'title'])
				if not tags and mt:
					r = mt.groupdict()
					tags = filter(lambda x: x != u'', r[u'tags'].split(u':')) if r[u'tags'] else []
				if r[u'title'] is not None:
					_todo_title = [i.strip() for i in r[u'title'].split(None, 1)]
					if _todo_title and _todo_title[0] in allowed_todo_states:
						todo = _todo_title[0]
						if len(_todo_title) > 1:
							title = _todo_title[1]
					else:
						title = r[u'title'].strip()

				return (level, todo, title, tags)
			raise ValueError(u'Data doesn\'t start with a heading definition.')

		if not data:
			raise ValueError(u'Unable to create heading, no data provided.')

		# create new heaing
		new_heading = cls()
		new_heading.level, new_heading.todo, new_heading.title, new_heading.tags = parse_title(data[0])
		new_heading.body = data[1:]
		if orig_start is not None:
			new_heading._dirty_heading = False
			new_heading._dirty_body = False
			new_heading._orig_start = orig_start
			new_heading._orig_len = len(new_heading)
		if document:
			new_heading._document = document

		# try to find active dates
		tmp_orgdate = get_orgdate(data)
		if tmp_orgdate and tmp_orgdate.active \
				and not isinstance(tmp_orgdate, OrgTimeRange):
			new_heading.active_date = tmp_orgdate
		else:
			new_heading.active_date = None

		return new_heading
	def test_get_orgdate_parsing_with_invalid_input(self):
		self.assertEquals(get_orgdate(u"NONSENSE"), None)
		self.assertEquals(get_orgdate(u"No D<2011- Date 08-29 Mon>"), None)
		self.assertEquals(get_orgdate(u"2011-08-r9 Mon]"), None)
		self.assertEquals(get_orgdate(u"<2011-08-29 Mon"), None)
		self.assertEquals(get_orgdate(u"<2011-08-29 Mon]"), None)
		self.assertEquals(get_orgdate(u"2011-08-29 Mon"), None)
		self.assertEquals(get_orgdate(u"2011-08-29"), None)
		self.assertEquals(get_orgdate(u"2011-08-29 mon"), None)
		self.assertEquals(get_orgdate(u"<2011-08-29 mon>"), None)

		self.assertEquals(get_orgdate(u"wrong date embedded <2011-08-29 mon>"), None)
		self.assertEquals(get_orgdate(u"wrong date <2011-08-29 mon>embedded "), None)
    def test_get_orgdate_parsing_with_invalid_input(self):
        self.assertEquals(get_orgdate(u"NONSENSE"), None)
        self.assertEquals(get_orgdate(u"No D<2011- Date 08-29 Mon>"), None)
        self.assertEquals(get_orgdate(u"2011-08-r9 Mon]"), None)
        self.assertEquals(get_orgdate(u"<2011-08-29 Mon"), None)
        self.assertEquals(get_orgdate(u"<2011-08-29 Mon]"), None)
        self.assertEquals(get_orgdate(u"2011-08-29 Mon"), None)
        self.assertEquals(get_orgdate(u"2011-08-29"), None)
        self.assertEquals(get_orgdate(u"2011-08-29 mon"), None)
        self.assertEquals(get_orgdate(u"<2011-08-29 mon>"), None)

        self.assertEquals(get_orgdate(u"wrong date embedded <2011-08-29 mon>"),
                          None)
        self.assertEquals(get_orgdate(u"wrong date <2011-08-29 mon>embedded "),
                          None)