Skip to content

Commit

Permalink
Merge pull request #33 from mankoell/battery_module
Browse files Browse the repository at this point in the history
Battery module
  • Loading branch information
novoid committed Mar 20, 2017
2 parents ace3331 + 9415000 commit 33584aa
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 9 deletions.
11 changes: 5 additions & 6 deletions README.org
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions 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 <mankoell@gmail.com>"""

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()
24 changes: 24 additions & 0 deletions 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
Empty file removed error.org
Empty file.
89 changes: 89 additions & 0 deletions 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)
27 changes: 27 additions & 0 deletions 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:")
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/capacity
@@ -0,0 +1 @@
97
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/current_now
@@ -0,0 +1 @@
955000
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/cycle_count
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/status
@@ -0,0 +1 @@
Discharging
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/type
@@ -0,0 +1 @@
Battery
1 change: 1 addition & 0 deletions memacs/tests/data/BAT0/voltage_now
@@ -0,0 +1 @@
12457000
5 changes: 2 additions & 3 deletions requirements.txt
Expand Up @@ -3,6 +3,5 @@ Pillow==2.4.0
argparse==1.2.1
feedparser==5.1.2
icalendar==3.1



twython
batinfo

0 comments on commit 33584aa

Please sign in to comment.