diff --git a/README.org b/README.org index 0848533..462fd93 100644 --- a/README.org +++ b/README.org @@ -45,11 +45,12 @@ For more information about the modules, visit [[https://github.com/novoid/Memacs *Existing modules*: +- *battery*: see docs/memacs_battery.org - *file name timestamps*: see docs/memacs_filenametimestamps.org - *emails*: - - [[http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol][IMAP]] : see docs/memacs_imap.org - - [[http://www.djcbsoftware.nl/code/mu/][Mu]] : see docs/memacs_mumail.org -- *RSS*: see docs/memacs_rss.org i.e. for twitter,delicious,... + - [[http://en.wikipedia.org/wiki/Internet_Message_Access_Protocol][IMAP]]: see docs/memacs_imap.org + - [[http://www.djcbsoftware.nl/code/mu/][Mu]]: see docs/memacs_mumail.org +- *RSS*: see docs/memacs_rss.org i.e. for twitter, github, … - *versioning systems*: - [[http://en.wikipedia.org/wiki/Git_(software)][git]]: see docs/memacs_git.org - [[http://en.wikipedia.org/wiki/Subversion][Subversion]]: see docs/svn/memacs_svn.org @@ -58,9 +59,7 @@ For more information about the modules, visit [[https://github.com/novoid/Memacs - *calendar*: - [[http://en.wikipedia.org/wiki/ICalendar][iCalendar]] (i.e. Google Calendar): see docs/memacs_calendar.org - [[http://en.wikipedia.org/wiki/Comma_seperated_values][CSV]] Files: see docs/memacs_csv.org -- *SMS*: - - docs/memacs_sms.org: SMS Backup and Restore (Android) - - docs/memacs_smssuperbackup.org: SuperBackup (Android) +- *SMS*: see docs/memacs_sms.org - *Phonecalls*: see docs/memacs_phonecalls.org - *Photos* (date from [[http://en.wikipedia.org/wiki/Exif][EXIF]]): see docs/memacs_photos.org - *example*: see docs/memacs_csv.org diff --git a/bin/memacs_battery.py b/bin/memacs_battery.py new file mode 100755 index 0000000..ed10868 --- /dev/null +++ b/bin/memacs_battery.py @@ -0,0 +1,24 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +from memacs.battery import Battery + + +PROG_VERSION_NUMBER = u"0.1" +PROG_VERSION_DATE = u"2017-02-24" +PROG_SHORT_DESCRIPTION = u"Memacs for battery" +PROG_TAG = u"battery" + +COPYRIGHT_YEAR = "2017" +COPYRIGHT_AUTHORS = """Manuel Koell """ + +if __name__ == "__main__": + memacs = Battery( + prog_version=PROG_VERSION_NUMBER, + prog_version_date=PROG_VERSION_DATE, + prog_short_description=PROG_SHORT_DESCRIPTION, + prog_tag=PROG_TAG, + copyright_year=COPYRIGHT_YEAR, + copyright_authors=COPYRIGHT_AUTHORS + ) + memacs.handle_main() diff --git a/docs/memacs_battery.org b/docs/memacs_battery.org new file mode 100644 index 0000000..00ee598 --- /dev/null +++ b/docs/memacs_battery.org @@ -0,0 +1,24 @@ +## This file is best viewed with GNU Emacs Org-mode: http://orgmode.org/ + +* memacs-battery + +** Options + +- ~-b, --battery~ select battery to read stats from, default ~BAT0~ +- ~--output-format~ format string to use for the output, see [[https://github.com/nicolargo/batinfo][batinfo]] default ~{battery.name}~ + +** Example + +: memacs_battery.py --battery BAT1 --output-format "{battery.name} {battery.status}" + +#+BEGIN_EXAMPLE +* Memacs for battery :Memacs:battery: +** <2017-02-24 Fri 12:20> BAT1 discharging + :PROPERTIES: + :STATUS: discharging + :CAPACITY: 88% + :CYCLE_COUNT: 866 + :CONSUMPTION: 12.5 W + :ID: 5248c6260f05f307533203be97e53cd38f8e628c + :END: +#+END_EXAMPLE diff --git a/error.org b/error.org deleted file mode 100644 index e69de29..0000000 diff --git a/memacs/battery.py b/memacs/battery.py new file mode 100755 index 0000000..ca541bf --- /dev/null +++ b/memacs/battery.py @@ -0,0 +1,89 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +import argparse +import datetime +import logging +import sys + +import batinfo + +from lib.orgproperty import OrgProperties +from lib.orgformat import OrgFormat +from lib.memacs import Memacs + +ROOT = '/sys/class/power_supply' + + +class Battery(Memacs): + def _parser_add_arguments(self): + """ + overwritten method of class Memacs + + add additional arguments + """ + Memacs._parser_add_arguments(self) + + self._parser.add_argument( + "-b", "--battery", dest="name", + action="store", default="BAT0", + help="select battery to read stats from") + + self._parser.add_argument( + "-p", "--path", dest="path", + action="store", default=ROOT, + help=argparse.SUPPRESS) + + self._parser.add_argument( + "--output-format", dest="output_format", + action="store", default="{battery.name}", + help="format string to use for the output" + ) + + def _parser_parse_args(self): + """ + overwritten method of class Memacs + + all additional arguments are parsed in here + """ + Memacs._parser_parse_args(self) + + def _handle_battery(self, bat): + """ + handle single battery, e.g. BAT0 + """ + + # calculate watt usage + consumption = float(bat.current_now / 1000000.0 * + bat.voltage_now / 1000000.0) + + timestamp = OrgFormat.datetime(datetime.datetime.now()) + output = self._args.output_format.format(battery=bat) + + properties = OrgProperties(data_for_hashing=timestamp) + properties.add("CYCLE_COUNT", bat.cycle_count) + properties.add("CAPACITY", '%s%%' % bat.capacity) + properties.add("STATUS", bat.status.lower()) + + if consumption: + properties.add("CONSUMPTION", '%.1f W' % consumption) + + self._writer.write_org_subitem(timestamp=timestamp, + output=output, + properties=properties) + + def _main(self): + """ + get's automatically called from Memacs class + """ + + try: + batteries = batinfo.Batteries(self._args.path) + + for bat in batteries.stat: + if self._args.name in bat.name: + self._handle_battery(bat) + + except OSError, e: + logging.error("no battery present") + sys.exit(1) \ No newline at end of file diff --git a/memacs/tests/battery_test.py b/memacs/tests/battery_test.py new file mode 100644 index 0000000..6bb8bc8 --- /dev/null +++ b/memacs/tests/battery_test.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +import unittest +import os + +from memacs.battery import Battery + + +class TestCsv(unittest.TestCase): + + def test_battery(self): + + path = os.path.dirname(os.path.abspath(__file__)) + + argv = [] + argv.append("-p") + argv.append(os.path.join(path, "data")) + + memacs = Battery(argv=argv) + data = memacs.test_get_entries() + + self.assertEqual(data[1], " :PROPERTIES:") + self.assertEqual(data[2], " :STATUS: discharging") + self.assertEqual(data[3], " :CAPACITY: 97%") + self.assertEqual(data[4], " :CYCLE_COUNT: 866") + self.assertEqual(data[5], " :CONSUMPTION: 11.9 W") + self.assertEqual(data[7], " :END:") \ No newline at end of file diff --git a/memacs/tests/data/BAT0/capacity b/memacs/tests/data/BAT0/capacity new file mode 100644 index 0000000..c17e934 --- /dev/null +++ b/memacs/tests/data/BAT0/capacity @@ -0,0 +1 @@ +97 diff --git a/memacs/tests/data/BAT0/current_now b/memacs/tests/data/BAT0/current_now new file mode 100644 index 0000000..33e0e4d --- /dev/null +++ b/memacs/tests/data/BAT0/current_now @@ -0,0 +1 @@ +955000 diff --git a/memacs/tests/data/BAT0/cycle_count b/memacs/tests/data/BAT0/cycle_count new file mode 100644 index 0000000..573541a --- /dev/null +++ b/memacs/tests/data/BAT0/cycle_count @@ -0,0 +1 @@ +0 diff --git a/memacs/tests/data/BAT0/status b/memacs/tests/data/BAT0/status new file mode 100644 index 0000000..4674475 --- /dev/null +++ b/memacs/tests/data/BAT0/status @@ -0,0 +1 @@ +Discharging diff --git a/memacs/tests/data/BAT0/type b/memacs/tests/data/BAT0/type new file mode 100644 index 0000000..6784dd3 --- /dev/null +++ b/memacs/tests/data/BAT0/type @@ -0,0 +1 @@ +Battery diff --git a/memacs/tests/data/BAT0/voltage_now b/memacs/tests/data/BAT0/voltage_now new file mode 100644 index 0000000..b603bdf --- /dev/null +++ b/memacs/tests/data/BAT0/voltage_now @@ -0,0 +1 @@ +12457000 diff --git a/requirements.txt b/requirements.txt index ff8b977..c16fbb5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,5 @@ Pillow==2.4.0 argparse==1.2.1 feedparser==5.1.2 icalendar==3.1 - - - +twython +batinfo