def _handle_url(self, params): epoch = datetime.datetime(1970, 1, 1)-datetime.datetime(1601, 1, 1) url_time = params['timestamp']/1000000-epoch.total_seconds() if (url_time > 0) : timestamp = datetime.datetime.fromtimestamp(int(url_time)) else: timestamp = datetime.datetime(1970, 1, 1) if not self._args.omit_drawer: properties = OrgProperties() if (params['title'] == "") : params['title'] = params['url'] properties.add('URL', params['url']) properties.add('VISIT_COUNT', params['visit_count']) output = "" try: output = self._args.output_format.decode('utf-8').format(**params) except Exception: pass if self._args.omit_drawer: self._writer.write_org_subitem( timestamp=OrgFormat.datetime(timestamp), output=output, properties=None) else: self._writer.write_org_subitem( timestamp=OrgFormat.datetime(timestamp), output=output, properties=properties)
def startElement(self, name, attrs): """ at every <sms> tag write to orgfile """ logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs) if name == "sms": sms_subject = attrs['subject'] sms_date = int(attrs['date']) / 1000 # unix epoch sms_body = attrs['body'] sms_address = attrs['address'] sms_type_incoming = int(attrs['type']) == 1 contact_name = False if 'contact_name' in attrs: ## NOTE: older version of backup app did not insert contact_name into XML contact_name = attrs['contact_name'] skip = False if sms_type_incoming == True: output = "SMS from " if self._ignore_incoming: skip = True else: output = "SMS to " if self._ignore_outgoing: skip = True if not skip: name_string = "" if contact_name: name_string = '[[contact:' + contact_name + '][' + contact_name + ']]' else: name_string = "Unknown" output += name_string + ": " if sms_subject != "null": # in case of MMS we have a subject output += sms_subject notes = sms_body else: output += sms_body notes = "" timestamp = OrgFormat.datetime(time.gmtime(sms_date)) data_for_hashing = output + timestamp + notes properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("NUMBER", sms_address) properties.add("NAME", contact_name) self._writer.write_org_subitem(output=output, timestamp=timestamp, note=notes, properties=properties)
def startElement(self, name, attrs): """ at every <sms> tag write to orgfile """ logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs) if name == "sms": #sms_subject = attrs['subject'] sms_date = int(attrs['date']) / 1000 # unix epoch sms_body = attrs['body'] sms_address = attrs['address'] sms_time = attrs['time'] sms_service_center = attrs['service_center'] sms_type_incoming = int(attrs['type']) == 1 contact_name = attrs['name'] skip = False if sms_type_incoming == True: output = "SMS from " if self._ignore_incoming: skip = True else: output = "SMS to " if self._ignore_outgoing: skip = True if not skip: name_string = "" if contact_name: name_string = '[[contact:' + contact_name + '][' + contact_name + ']]' else: name_string = "Unknown" output += name_string + ": " #if sms_subject != "null": # in case of MMS we have a subject # output += sms_subject # notes = sms_body #else: # output += sms_body # notes = "" notes = sms_body timestamp = OrgFormat.datetime(time.gmtime(sms_date)) data_for_hashing = output + timestamp + notes properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("NUMBER", sms_address) properties.add("NAME", contact_name) properties.add("SMS_SERVICE_CENTER", sms_service_center) properties.add("TIME", sms_time) self._writer.write_org_subitem(output=output, timestamp=timestamp, note=notes, properties=properties)
def __get_item_data(self, item): """ gets information out of <item>..</item> @return: output, note, properties, tags variables for orgwriter.append_org_subitem """ try: #logging.debug(item) properties = OrgProperties() guid = item['id'] if not guid: logging.error("got no id") unformatted_link = item['link'] short_link = OrgFormat.link(unformatted_link, "link") # if we found a url in title # then append the url in front of subject if re.search("http[s]?://", item['title']) != None: output = short_link + ": " + item['title'] else: output = OrgFormat.link(unformatted_link, item['title']) note = item['description'] # converting updated_parsed UTC --> LOCALTIME timestamp = OrgFormat.datetime( time.localtime(calendar.timegm(item['updated_parsed']))) properties.add("guid", guid) except KeyError: logging.error("input is not a RSS 2.0") sys.exit(1) tags = [] dont_parse = ['title', 'description', 'updated', 'summary', 'updated_parsed', 'link', 'links'] for i in item: logging.debug(i) if i not in dont_parse: if (type(i) == unicode or type(i) == str) and \ type(item[i]) == unicode and item[i] != "": if i == "id": i = "guid" properties.add(i, item[i]) else: if i == "tags": for tag in item[i]: logging.debug("found tag: %s", tag['term']) tags.append(tag['term']) return output, note, properties, tags, timestamp
def __handle_vevent(self, component): """ handles a VCALENDAR Component sets timezone to calendar's timezone @param component: icalendar component """ logging.debug(component) summary = self.__vtext_to_unicode(component.get('summary'), nonetype="") location = self.__vtext_to_unicode(component.get('location')) description = self.__vtext_to_unicode(component.get('description')) # format: 20091207T180000Z or 20100122 dtstart = self.__vtext_to_unicode(component.get('DTSTART').to_ical()) # format: 20091207T180000Z or 20100122 if 'DTEND' in component.keys(): dtend = self.__vtext_to_unicode(component.get('DTEND').to_ical()) # format: 20091207T180000Z # not used: Datestamp created #dtstamp = self.__vtext_to_unicode(component.get('dtstamp')) # handle repeating events # not implemented due to org-mode datestime-range cannot be repeated # component.get('rrule') ## notice: end date/time is optional; no end date results in end date 9999-12-31 if 'DTEND' in component.keys(): orgdate = self.__get_datetime_range(dtstart, dtend) else: orgdate = self.__get_datetime(dtstart) + u"-<9999-12-31 Fri>" logging.debug(orgdate + " " + summary) # we need to set data_for_hashing=summary to really get a other sha1 data_for_hashing = orgdate + summary org_properties = OrgProperties(data_for_hashing=data_for_hashing) if location != None: org_properties.add("LOCATION", location) if description != None: org_properties.add("DESCRIPTION", description) self._writer.write_org_subitem(output=summary, properties=org_properties, timestamp=orgdate)
def __init__(self): """ Ctor """ self.__empty = True self.__subject = "" self.__body = "" self.__timestamp = "" self.__author = "" self.__properties = OrgProperties()
def __write(self): """ write attributes to writer (make an org_sub_item) """ logging.debug("msg:%s", self.__msg) self.__msg = self.__msg.splitlines() subject = "" notes = "" # idea: look for the first -nonempty- message if len(self.__msg) > 0: start_notes = 0 for i in range(len(self.__msg)): if self.__msg[i].strip() != "": subject = self.__msg[i].strip() start_notes = i + 1 break if len(self.__msg) > start_notes: for n in self.__msg[start_notes:]: if n != "": notes += n + "\n" output = "%s (r%d): %s" % (self.__author, self.__rev, subject) properties = OrgProperties(data_for_hashing=self.__author + subject) timestamp = OrgFormat.datetime( OrgFormat.datetupelutctimestamp(self.__date)) properties.add("REVISION", self.__rev) if self.__grepauthor == None or \ (self.__author.strip() == self.__grepauthor.strip()): self._writer.write_org_subitem(output=output, timestamp=timestamp, note=notes, properties=properties)
def _main(self): """ get's automatically called from Memacs class fetches all mails out of mu database """ command = self._query # command.extend(self._query) command = command+" --fields=t:#:d:#:f:#:g:#:s:#:i --format=plain" try: xml_mails = subprocess.check_output(command, shell=True) except: print("something goes wrong") exit() messages = self.__parse_Plain(xml_mails) properties = OrgProperties() for message in messages: (an,datum,von,flags,betreff,msgid) = message.split(":#:") betreff = betreff.replace("[","<") betreff = betreff.replace("]",">") properties.add('TO',an) if von != "": (sender,vname,vmail) = self.__create_mail_link(von) (an,aname,amail) = self.__create_mail_link(an) timestamp = self.__getTimestamp(datum) properties.add_data_for_hashing(timestamp + "_" + msgid) properties.add("FROM",sender) notes = "" if any(match in vmail for match in self._sender): output = output = "".join(["T: ",an,": [[mu4e:msgid:",msgid,"][",betreff,"]]"]) pre = 'WAITING ' else: output = "".join(["F: ",sender,": [[mu4e:msgid:",msgid,"][",betreff,"]]"]) pre = 'NEXT ' if (flags.find('F') >= 0 and self._todo): date = self.__getTimestamp(datum,True) notes = "SCHEDULED: "+date timestamp = "" output = pre+output self._writer.write_org_subitem(timestamp, output, notes, properties)
def _main(self): APP_KEY = self._get_config_option("APP_KEY") APP_SECRET = self._get_config_option("APP_SECRET") OAUTH_TOKEN = self._get_config_option("OAUTH_TOKEN") OAUTH_TOKEN_SECRET = self._get_config_option("OAUTH_TOKEN_SECRET") screen_name = self._get_config_option("screen_name") count = self._get_config_option("count") twitter = Twython( APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET ) try: home_timeline = twitter.get_home_timeline(screenname=screen_name, count=count) except TwythonError as e: logging.error(e) sys.exit(1) for tweet in home_timeline: # strptime doesn't support timezone info, so we are using dateutils. date_object = parser.parse(tweet['created_at']) timestamp = OrgFormat.datetime(date_object) try: # Data is already Unicode, so don't try to re-encode it. output = tweet['text'] except: logging.error(sys.exc_info()[0]) print "Error: ", sys.exc_info()[0] data_for_hashing = output + timestamp + output properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("name", tweet['user']['name']) properties.add("twitter_id", tweet['id']) properties.add("contributors", tweet['contributors']) properties.add("truncated", tweet['truncated']) properties.add("in_reply_to_status_id", tweet['in_reply_to_status_id']) properties.add("favorite_count", tweet['favorite_count']) properties.add("source", tweet['source']) properties.add("retweeted", tweet['retweeted']) properties.add("coordinates", tweet['coordinates']) properties.add("entities", tweet['entities']) self._writer.write_org_subitem(timestamp=timestamp, output = output, properties = properties)
def _generateOrgentry(self, e_time, e_name, e_batt, e_uptime, e_last_opposite_occurrence, e_last_occurrence): """ takes the data from the parameters and generates an Org-mode entry. @param e_time: time-stamp of the entry @param e_name: entry name/description @param e_batt: battery level @param e_uptime: uptime in seconds @param e_last_opposite_occurrence: time-stamp of previous opposite occurrence (if not False) @param e_last_occurrence: time-stamp of previous occurrence """ assert e_time.__class__ == datetime.datetime assert e_name.__class__ == unicode assert e_batt.__class__ == str assert e_uptime.__class__ == str assert (e_last_opposite_occurrence.__class__ == datetime.datetime or not e_last_opposite_occurrence) assert (e_last_occurrence.__class__ == datetime.datetime or not e_last_occurrence) last_info = u'' in_between_hms = u'' in_between_s = u'' ignore_occurrence = False if e_last_opposite_occurrence: in_between_s = (e_time - e_last_opposite_occurrence).seconds + \ (e_time - e_last_opposite_occurrence).days * 3600 * 24 in_between_hms = unicode(OrgFormat.get_hms_from_sec(in_between_s)) if e_name == u'boot': last_info = u' (off for ' elif e_name == u'shutdown': last_info = u' (on for ' elif e_name.endswith(u'-end'): last_info = u' (' + e_name[0:-4].replace('wifi-','') + u' for ' else: last_info = u' (not ' + e_name.replace('wifi-','') + u' for ' last_info += unicode(OrgFormat.get_dhms_from_sec(in_between_s)) + u')' if (e_name == u'boot') and \ (e_last_occurrence and e_last_opposite_occurrence) and \ (e_last_occurrence > e_last_opposite_occurrence): ## last boot is more recent than last shutdown -> crash has happened last_info = u' after crash' in_between_hms = u'' in_between_s = u'' ignore_occurrence = True properties = OrgProperties() properties.add("IN-BETWEEN", in_between_hms) properties.add("IN-BETWEEN-S", unicode(in_between_s)) properties.add("BATT-LEVEL", e_batt) properties.add("UPTIME", OrgFormat.get_hms_from_sec(int(e_uptime))) properties.add("UPTIME-S", e_uptime) self._writer.write_org_subitem(timestamp = e_time.strftime('<%Y-%m-%d %a %H:%M>'), output = e_name + last_info, properties = properties) ## the programmer recommends you to read "memacs/tests/simplephonelogs_test.py" ## test_generateOrgentry_* for less cryptic examples on how this looks: return u'** ' + e_time.strftime('<%Y-%m-%d %a %H:%M>') + u' ' + e_name + last_info + \ u'\n:PROPERTIES:\n:IN-BETWEEN: ' + in_between_hms + \ u'\n:IN-BETWEEN-S: ' + unicode(in_between_s) + \ u'\n:BATT-LEVEL: ' + e_batt + \ u'\n:UPTIME: ' + unicode(OrgFormat.get_hms_from_sec(int(e_uptime))) + \ u'\n:UPTIME-S: ' + unicode(e_uptime) + u'\n:END:\n', ignore_occurrence
def startElement(self, name, attrs): """ at every <call> write to orgfile """ logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs) if name == "call": call_number = attrs['number'] call_duration = int(attrs['duration']) call_date = int(attrs['date']) / 1000 # unix epoch call_type = int(attrs['type']) call_incoming = call_type == 1 call_outgoing = call_type == 2 call_missed = call_type == 3 call_cancelled = call_type == 5 call_name = call_number if 'contact_name' in attrs: ## NOTE: older version of backup app did not insert contact_name into XML call_name = attrs['contact_name'] output = "Phonecall " skip = False if call_incoming: output += "from " if self._ignore_incoming: skip = True elif call_outgoing: output += "to " if self._ignore_outgoing: skip = True elif call_missed: output += "missed " if self._ignore_missed: skip = True elif call_cancelled: output += "cancelled " if self._ignore_cancelled: skip = True else: raise Exception("Invalid Phonecall Type: %d", call_type) call_number_string = "" if call_number != "-1": call_number_string = call_number else: call_number_string = "Unknown Number" name_string = "" if call_name != "(Unknown)": name_string = '[[contact:' + call_name + '][' + call_name + ']]' else: name_string = "Unknown" output += name_string if call_duration < self._minimum_duration: skip = True timestamp = OrgFormat.datetime(time.gmtime(call_date)) end_datetimestamp = datetime.datetime.utcfromtimestamp(call_date + call_duration) logging.debug("timestamp[%s] duration[%s] end[%s]" % (str(timestamp), str(call_duration), str(end_datetimestamp))) end_timestamp_string = OrgFormat.datetime(end_datetimestamp) logging.debug("end_time [%s]" % end_timestamp_string) data_for_hashing = output + timestamp properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("NUMBER", call_number_string) properties.add("DURATION", call_duration) properties.add("NAME", call_name) if not skip: self._writer.write_org_subitem(output=output, timestamp=timestamp + '-' + end_timestamp_string, properties=properties )
def _generateOrgentry(self, e_time, e_name, e_batt, e_uptime, e_last_opposite_occurrence, e_last_occurrence, prev_office_sum, prev_office_first_begin): """ takes the data from the parameters and generates an Org-mode entry. @param e_time: time-stamp of the entry @param e_name: entry name/description @param e_batt: battery level @param e_uptime: uptime in seconds @param e_last_opposite_occurrence: time-stamp of previous opposite occurrence (if not False) @param e_last_occurrence: time-stamp of previous occurrence @param additional_paren_string: string that gets appended to the parenthesis @param prev_office_sum: holds the sum of all previous working duration today @param prev_office_first_begin: holds the first time-stamp of wifi-office for today """ assert e_time.__class__ == datetime.datetime assert e_name.__class__ == unicode assert e_batt.__class__ == unicode assert e_uptime.__class__ == unicode assert (e_last_opposite_occurrence.__class__ == datetime.datetime or not e_last_opposite_occurrence) assert (e_last_occurrence.__class__ == datetime.datetime or not e_last_occurrence) last_info = u'' in_between_hms = u'' in_between_s = u'' ignore_occurrence = False ## convert parameters to be writable: office_sum = prev_office_sum office_first_begin = prev_office_first_begin if e_last_opposite_occurrence: in_between_s = (e_time - e_last_opposite_occurrence).seconds + \ (e_time - e_last_opposite_occurrence).days * 3600 * 24 in_between_hms = unicode(OrgFormat.get_hms_from_sec(in_between_s)) if e_name == u'boot': last_info = u' (off for ' elif e_name == u'shutdown': last_info = u' (on for ' elif e_name.endswith(u'-end'): last_info = u' (' + e_name[0:-4].replace('wifi-', '') + u' for ' else: last_info = u' (not ' + e_name.replace('wifi-', '') + u' for ' ## handle special case: office hours additional_paren_string = "" if e_name == 'wifi-office-end': office_total = None ## calculate office_sum and office_total if not office_sum: office_sum = (e_time - e_last_opposite_occurrence).seconds office_total = office_sum else: assert(office_first_begin) assert(office_sum) office_sum = office_sum + (e_time - e_last_opposite_occurrence).seconds office_total = int(time.mktime(e_time.timetuple()) - time.mktime(office_first_begin.timetuple())) assert(type(office_total) == int) assert(type(office_sum) == int) assert(type(in_between_s) == int) ## come up with the additional office-hours string: additional_paren_string = u'; today ' + OrgFormat.get_hms_from_sec(office_sum) + \ '; today total ' + OrgFormat.get_hms_from_sec(office_total) if additional_paren_string: last_info += unicode(OrgFormat.get_dhms_from_sec(in_between_s)) + additional_paren_string + u')' else: last_info += unicode(OrgFormat.get_dhms_from_sec(in_between_s)) + u')' ## handle special case: office hours if e_name == 'wifi-office': if not office_sum or not office_first_begin: ## new day office_first_begin = e_time ## handle special case: boot without previous shutdown = crash if (e_name == u'boot') and \ (e_last_occurrence and e_last_opposite_occurrence) and \ (e_last_occurrence > e_last_opposite_occurrence): ## last boot is more recent than last shutdown -> crash has happened last_info = u' after crash' in_between_hms = u'' in_between_s = u'' ignore_occurrence = True properties = OrgProperties() properties.add("IN-BETWEEN", in_between_hms) properties.add("IN-BETWEEN-S", unicode(in_between_s)) properties.add("BATT-LEVEL", e_batt) properties.add("UPTIME", OrgFormat.get_hms_from_sec(int(e_uptime))) properties.add("UPTIME-S", e_uptime) self._writer.write_org_subitem(timestamp=e_time.strftime('<%Y-%m-%d %a %H:%M>'), output=e_name + last_info, properties=properties) return u'** ' + e_time.strftime('<%Y-%m-%d %a %H:%M>') + u' ' + e_name + last_info + \ u'\n:PROPERTIES:\n:IN-BETWEEN: ' + in_between_hms + \ u'\n:IN-BETWEEN-S: ' + unicode(in_between_s) + \ u'\n:BATT-LEVEL: ' + e_batt + \ u'\n:UPTIME: ' + unicode(OrgFormat.get_hms_from_sec(int(e_uptime))) + \ u'\n:UPTIME-S: ' + unicode(e_uptime) + u'\n:END:\n', \ ignore_occurrence, office_sum, office_first_begin
class Commit(object): """ class for representing one commit """ def __init__(self): """ Ctor """ self.__empty = True self.__subject = "" self.__body = "" self.__timestamp = "" self.__author = "" self.__properties = OrgProperties() def __set_author_timestamp(self, line): """ extracts the date + time from line: author Forename Lastname <mail> 1234567890 +0000 @param line """ self.__empty = False date_info = line[-16:] # 1234567890 +0000 seconds_since_epoch = float(date_info[:10]) #timezone_info = date_info[11:] self.__timestamp = OrgFormat.datetime( time.localtime(seconds_since_epoch)) self.__author = line[7:line.find("<")].strip() def add_header(self, line): """ adds line to the header if line contains "author" this method calls self.__set_author_timestamp(line) for setting right author + datetime created every line will be added as property i.e: commit <hashtag> would then be following property: :COMMIT: <hashtag> @param line: """ self.__empty = False if line != "": whitespace = line.find(" ") tag = line[:whitespace].upper() value = line[whitespace:] self.__properties.add(tag, value) if tag == "AUTHOR": self.__set_author_timestamp(line) def add_body(self, line): """ adds a line to the body if line starts with Signed-off-by, also a property of that line is added """ line = line.strip() if line != "": if line[:14] == "Signed-off-by:": self.__properties.add("SIGNED-OFF-BY", line[15:]) elif self.__subject == "": self.__subject = line else: self.__body += line + "\n" def is_empty(self): """ @return: True - empty commit False - not empty commit """ return self.__empty def get_output(self): """ @return tupel: output,properties,body for Orgwriter.write_sub_item() """ output = self.__author + ": " + self.__subject return output, self.__properties, self.__body, self.__author, \ self.__timestamp
def _main(self): APP_KEY = self._get_config_option("APP_KEY") APP_SECRET = self._get_config_option("APP_SECRET") OAUTH_TOKEN = self._get_config_option("OAUTH_TOKEN") OAUTH_TOKEN_SECRET = self._get_config_option("OAUTH_TOKEN_SECRET") screen_name = self._get_config_option("screen_name") count = self._get_config_option("count") twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) try: home_timeline = twitter.get_home_timeline(screenname=screen_name, count=count) except TwythonError as e: logging.error(e) sys.exit(1) for tweet in home_timeline: # strptime doesn't support timezone info, so we are using dateutils. date_object = parser.parse(tweet['created_at']) timestamp = OrgFormat.datetime(date_object) try: # Data is already Unicode, so don't try to re-encode it. output = tweet['text'] except: logging.error(sys.exc_info()[0]) print "Error: ", sys.exc_info()[0] data_for_hashing = output + timestamp + output properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("name", tweet['user']['name']) properties.add("twitter_id", tweet['id']) properties.add("contributors", tweet['contributors']) properties.add("truncated", tweet['truncated']) properties.add("in_reply_to_status_id", tweet['in_reply_to_status_id']) properties.add("favorite_count", tweet['favorite_count']) properties.add("source", tweet['source']) properties.add("retweeted", tweet['retweeted']) properties.add("coordinates", tweet['coordinates']) properties.add("entities", tweet['entities']) self._writer.write_org_subitem(timestamp=timestamp, output=output, properties=properties)
def _main(self): """ get's automatically called from Memacs class """ # do all the stuff # if you need something from config: # attention: foo will be unicode # foo = self._get_config_option("foo") logging.info("foo started") # how to handle config files ? # sample config file: # ---------8<----------- # [memacs-example] # foo = 0 # bar = 1 # --------->8----------- # to read it out, just do following: # foo = self._get_config_option("foo") # bar = self._get_config_option("bar") # use logging.debug() for debug messages # use logging.error() for error messages # use logging.info() instead of print for informing user # # on an fatal error: # use logging.error() and sys.exit(1) timestamp = OrgFormat.datetime(time.gmtime(0)) # note: timestamp has to be a struct_time object # Orgproperties # Option 1: no properties given, specify argument for hashing data properties = OrgProperties("hashing data :ALKJ!@# should be unique") # Option 2: add properties which are all-together unique # properties.add("Category","fun") # properties.add("from","*****@*****.**") # properties.add("body","foo") self._writer.write_org_subitem(timestamp=timestamp, output="foo", properties=properties) # writes following: # ** <1970-01-01 Thu 00:00> foo # :PROPERTIES: # :ID: da39a3ee5e6b4b0d3255bfef95601890afd80709 # :END: notes = "bar notes\nfoo notes" p = OrgProperties(data_for_hashing="read comment below") # if a hash is not unique only with its :PROPERTIES: , then # set data_for_hasing string additional information i.e. the output # , which then makes the hash really unique # # if you *really*, *really* have already a unique id, # then you can call following method: # p.set_id("unique id here") p.add("DESCRIPTION", "foooo") p.add("foo-property", "asdf") tags = [u"tag1", u"tag2"] self._writer.write_org_subitem(timestamp=timestamp, output="bar", note=notes, properties=p, tags=tags)
class Commit(object): """ class for representing one commit """ def __init__(self): """ Ctor """ self.__empty = True self.__subject = "" self.__body = "" self.__timestamp = "" self.__author = "" self.__properties = OrgProperties() def __set_author_timestamp(self, line): """ extracts the date + time from line: author Forename Lastname <mail> 1234567890 +0000 @param line """ self.__empty = False date_info = line[-16:] # 1234567890 +0000 seconds_since_epoch = float(date_info[:10]) # timezone_info = date_info[11:] self.__timestamp = OrgFormat.datetime(time.localtime(seconds_since_epoch)) self.__author = line[7 : line.find("<")].strip() def add_header(self, line): """ adds line to the header if line contains "author" this method calls self.__set_author_timestamp(line) for setting right author + datetime created every line will be added as property i.e: commit <hashtag> would then be following property: :COMMIT: <hashtag> @param line: """ self.__empty = False if line != "": whitespace = line.find(" ") tag = line[:whitespace].upper() value = line[whitespace:] self.__properties.add(tag, value) if tag == "AUTHOR": self.__set_author_timestamp(line) def add_body(self, line): """ adds a line to the body if line starts with Signed-off-by, also a property of that line is added """ line = line.strip() if line != "": if line[:14] == "Signed-off-by:": self.__properties.add("SIGNED-OFF-BY", line[15:]) elif self.__subject == "": self.__subject = line else: self.__body += line + "\n" def is_empty(self): """ @return: True - empty commit False - not empty commit """ return self.__empty def get_output(self): """ @return tupel: output,properties,body for Orgwriter.write_sub_item() """ output = self.__author + ": " + self.__subject return output, self.__properties, self.__body, self.__author, self.__timestamp
def startElement(self, name, attrs): """ at every <call> write to orgfile """ logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs) if name == "call": call_number = attrs['number'] call_duration = int(attrs['duration']) call_date = int(attrs['date']) / 1000 # unix epoch call_type = int(attrs['type']) call_incoming = call_type == 1 call_outgoing = call_type == 2 call_missed = call_type == 3 call_name = call_number if 'contact_name' in attrs: ## NOTE: older version of backup app did not insert contact_name into XML call_name = attrs['contact_name'] output = "Phonecall " skip = False if call_incoming: output += "from " if self._ignore_incoming: skip = True elif call_outgoing: output += "to " if self._ignore_outgoing: skip = True elif call_missed: output += "missed " if self._ignore_missed: skip = True else: raise Exception("Invalid Phonecall Type: %d", call_type) call_number_string = "" if call_number != "-1": call_number_string = call_number else: call_number_string = "Unknown Number" name_string = "" if call_name != "(Unknown)": name_string = '[[contact:' + call_name + '][' + call_name + ']]' else: name_string = "Unknown" output += name_string if call_duration < self._minimum_duration: skip = True timestamp = OrgFormat.datetime(time.gmtime(call_date)) end_datetimestamp = datetime.datetime.utcfromtimestamp(call_date + call_duration) logging.debug("timestamp[%s] duration[%s] end[%s]" % (str(timestamp), str(call_duration), str(end_datetimestamp))) end_timestamp_string = OrgFormat.datetime(end_datetimestamp) logging.debug("end_time [%s]" % end_timestamp_string) data_for_hashing = output + timestamp properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("NUMBER", call_number_string) properties.add("DURATION", call_duration) properties.add("NAME", call_name) if not skip: self._writer.write_org_subitem(output=output, timestamp=timestamp + '-' + end_timestamp_string, properties=properties )
def _generateOrgentry(self, e_time, e_name, e_batt, e_uptime, e_last_opposite_occurrence, e_last_occurrence, prev_office_sum, prev_office_first_begin): """ takes the data from the parameters and generates an Org-mode entry. @param e_time: time-stamp of the entry @param e_name: entry name/description @param e_batt: battery level @param e_uptime: uptime in seconds @param e_last_opposite_occurrence: time-stamp of previous opposite occurrence (if not False) @param e_last_occurrence: time-stamp of previous occurrence @param additional_paren_string: string that gets appended to the parenthesis @param prev_office_sum: holds the sum of all previous working duration today @param prev_office_first_begin: holds the first time-stamp of wifi-office for today """ assert e_time.__class__ == datetime.datetime assert e_name.__class__ == unicode assert e_batt.__class__ == unicode assert e_uptime.__class__ == unicode assert (e_last_opposite_occurrence.__class__ == datetime.datetime or not e_last_opposite_occurrence) assert (e_last_occurrence.__class__ == datetime.datetime or not e_last_occurrence) last_info = u'' in_between_hms = u'' in_between_s = u'' ignore_occurrence = False ## convert parameters to be writable: office_sum = prev_office_sum office_first_begin = prev_office_first_begin if e_last_opposite_occurrence: in_between_s = (e_time - e_last_opposite_occurrence).seconds + \ (e_time - e_last_opposite_occurrence).days * 3600 * 24 in_between_hms = unicode(OrgFormat.get_hms_from_sec(in_between_s)) if e_name == u'boot': last_info = u' (off for ' elif e_name == u'shutdown': last_info = u' (on for ' elif e_name.endswith(u'-end'): last_info = u' (' + e_name[0:-4].replace('wifi-', '') + u' for ' else: last_info = u' (not ' + e_name.replace('wifi-', '') + u' for ' ## handle special case: office hours additional_paren_string = "" if e_name == 'wifi-office-end': office_total = None ## calculate office_sum and office_total if not office_sum: office_sum = (e_time - e_last_opposite_occurrence).seconds office_total = office_sum else: assert (office_first_begin) assert (office_sum) office_sum = office_sum + ( e_time - e_last_opposite_occurrence).seconds office_total = int( time.mktime(e_time.timetuple()) - time.mktime(office_first_begin.timetuple())) assert (type(office_total) == int) assert (type(office_sum) == int) assert (type(in_between_s) == int) ## come up with the additional office-hours string: additional_paren_string = u'; today ' + OrgFormat.get_hms_from_sec(office_sum) + \ '; today total ' + OrgFormat.get_hms_from_sec(office_total) if additional_paren_string: last_info += unicode(OrgFormat.get_dhms_from_sec( in_between_s)) + additional_paren_string + u')' else: last_info += unicode( OrgFormat.get_dhms_from_sec(in_between_s)) + u')' ## handle special case: office hours if e_name == 'wifi-office': if not office_sum or not office_first_begin: ## new day office_first_begin = e_time ## handle special case: boot without previous shutdown = crash if (e_name == u'boot') and \ (e_last_occurrence and e_last_opposite_occurrence) and \ (e_last_occurrence > e_last_opposite_occurrence): ## last boot is more recent than last shutdown -> crash has happened last_info = u' after crash' in_between_hms = u'' in_between_s = u'' ignore_occurrence = True properties = OrgProperties() properties.add("IN-BETWEEN", in_between_hms) properties.add("IN-BETWEEN-S", unicode(in_between_s)) properties.add("BATT-LEVEL", e_batt) properties.add("UPTIME", OrgFormat.get_hms_from_sec(int(e_uptime))) properties.add("UPTIME-S", e_uptime) self._writer.write_org_subitem( timestamp=e_time.strftime('<%Y-%m-%d %a %H:%M>'), output=e_name + last_info, properties=properties) return u'** ' + e_time.strftime('<%Y-%m-%d %a %H:%M>') + u' ' + e_name + last_info + \ u'\n:PROPERTIES:\n:IN-BETWEEN: ' + in_between_hms + \ u'\n:IN-BETWEEN-S: ' + unicode(in_between_s) + \ u'\n:BATT-LEVEL: ' + e_batt + \ u'\n:UPTIME: ' + unicode(OrgFormat.get_hms_from_sec(int(e_uptime))) + \ u'\n:UPTIME-S: ' + unicode(e_uptime) + u'\n:END:\n', \ ignore_occurrence, office_sum, office_first_begin
def startElement(self, name, attrs): """ at every <sms> tag write to orgfile """ logging.debug("Handler @startElement name=%s,attrs=%s", name, attrs) htmlparser = HTMLParser.HTMLParser() if name == "sms": sms_subject = attrs['subject'] sms_date = int(attrs['date']) / 1000 # unix epoch sms_body = attrs['body'] sms_address = attrs['address'].strip().replace('-',u'').replace('/',u'').replace(' ',u'').replace('+',u'00') sms_type_incoming = int(attrs['type']) == 1 contact_name = False if 'contact_name' in attrs: ## NOTE: older version of backup app did not insert contact_name into XML contact_name = attrs['contact_name'] else: if self._numberdict: if sms_address in self._numberdict.keys(): contact_name = self._numberdict[sms_address] skip = False if sms_type_incoming == True: output = "SMS from " if self._ignore_incoming: skip = True else: output = "SMS to " if self._ignore_outgoing: skip = True if not skip: name_string = "" if contact_name: name_string = '[[contact:' + contact_name + '][' + contact_name + ']]' else: name_string = "Unknown" output += name_string + ": " ## reverse encoding hack from just before: sms_body = htmlparser.unescape(sms_body.replace(u'EnCoDiNgHaCk42', u'&#')) for emoji in self.EMOJIS.keys(): ## FIXXME: this is a horrible dumb brute-force algorithm. ## In case of bad performance, this can be optimized dramtically sms_body = sms_body.replace(emoji, self.EMOJI_ENCLOSING_CHARACTER + \ self.EMOJIS[emoji] + self.EMOJI_ENCLOSING_CHARACTER).replace(u'\n', u'⏎') if sms_subject != "null": # in case of MMS we have a subject output += sms_subject notes = sms_body else: output += sms_body notes = "" timestamp = OrgFormat.datetime(time.gmtime(sms_date)) data_for_hashing = output + timestamp + notes properties = OrgProperties(data_for_hashing=data_for_hashing) properties.add("NUMBER", sms_address) properties.add("NAME", contact_name) self._writer.write_org_subitem(output=output, timestamp=timestamp, note=notes, properties=properties)
def _main(self): """ get's automatically called from Memacs class """ # do all the stuff # if you need something from config: # attention: foo will be unicode # foo = self._get_config_option("foo") logging.info("foo started") # how to handle config files ? # sample config file: # ---------8<----------- # [memacs-example] # foo = 0 # bar = 1 # --------->8----------- # to read it out, just do following: # foo = self._get_config_option("foo") # bar = self._get_config_option("bar") # use logging.debug() for debug messages # use logging.error() for error messages # use logging.info() instead of print for informing user # # on an fatal error: # use logging.error() and sys.exit(1) timestamp = OrgFormat.datetime(time.gmtime(0)) # note: timestamp has to be a struct_time object # Orgproperties # Option 1: no properties given, specify argument for hashing data properties = OrgProperties("hashing data :ALKJ!@# should be unique") # Option 2: add properties which are all-together unique # properties.add("Category","fun") # properties.add("from","*****@*****.**") # properties.add("body","foo") self._writer.write_org_subitem(timestamp=timestamp, output="foo", properties=properties) # writes following: #** <1970-01-01 Thu 00:00> foo # :PROPERTIES: # :ID: da39a3ee5e6b4b0d3255bfef95601890afd80709 # :END: notes = "bar notes\nfoo notes" p = OrgProperties(data_for_hashing="read comment below") # if a hash is not unique only with its :PROPERTIES: , then # set data_for_hasing string additional information i.e. the output # , which then makes the hash really unique # # if you *really*, *really* have already a unique id, # then you can call following method: # p.set_id("unique id here") p.add("DESCRIPTION", "foooo") p.add("foo-property", "asdf") tags = [u"tag1", u"tag2"] self._writer.write_org_subitem(timestamp=timestamp, output="bar", note=notes, properties=p, tags=tags)