Changeset 11824 for main/waeup.kofa
- Timestamp:
- 26 Sep 2014, 11:53:55 (10 years ago)
- Location:
- main/waeup.kofa/trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/CHANGES.txt
r11822 r11824 47 47 * Allow external checks of system load before performing expensive 48 48 actions. 49 50 * Add helper to allow `meminfo` checks for cached memory, etc. 49 51 50 52 -
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r11660 r11824 773 773 return linenum + 1 774 774 return None 775 776 777 class MemInfo(dict): 778 """A dict with access to its items like if they are attributes. 779 """ 780 __getattr__ = dict.__getitem__ 781 __setattr__ = dict.__setitem__ 782 __delattr__ = dict.__delitem__ 783 784 785 def get_meminfo(src="/proc/meminfo"): 786 """Get local memory info as provided in /proc/meminfo. 787 788 Entries in /proc/meminfo are available as MemInfo attributes. 789 790 By default we lookup a file /proc/meminfo. Another path can be 791 lines = open(src, 'r').read()passed in as `src` parameter. In this 792 case `src` must be a regular file and contain meminfo-style data. 793 794 If the given `src` (or `/proc/meminfo`) are not available, `None` 795 lines = open(src, 'r').read()is returned. 796 """ 797 if not os.path.isfile(src): 798 return None 799 lines = open(src, 'r').read().splitlines() 800 result = MemInfo() 801 for line in lines: 802 key, value = line.split(':', 1) 803 value = int(value.split(' kB', 1)[0]) 804 result[key] = value 805 return result -
main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py
r11659 r11824 537 537 self.assertEqual(helpers.check_csv_charset(csv), 2) 538 538 539 540 class MemInfoTestCase(unittest.TestCase): 541 542 def test_getsetattrs(self): 543 # we can set/get attributes of MemInfos 544 info = helpers.MemInfo() 545 self.assertRaises( 546 KeyError, info.__getattr__, 'foo') 547 info['foo'] = 'bar' 548 assert info.foo == 'bar' 549 info.bar = 'baz' 550 assert info.bar == 'baz' 551 552 def test_getattrs(self): 553 # we can del attributes 554 info = helpers.MemInfo() 555 info['foo'] = 'bar' 556 del info['foo'] 557 self.assertRaises( 558 KeyError, info.__getattr__, 'foo') 559 info['bar'] = 'baz' 560 del info.bar 561 self.assertRaises( 562 KeyError, info.__getattr__, 'bar') 563 564 565 class GetMemInfoTestCase(unittest.TestCase): 566 567 @unittest.skipIf( 568 not os.path.exists('/proc/meminfo'), 569 reason="No /proc/meminfo found.") 570 def test_system(self): 571 info = helpers.get_meminfo() 572 assert isinstance(info, helpers.MemInfo) 573 574 def test_values(self): 575 sample_meminfo = os.path.join( 576 os.path.dirname(__file__), 'sample_meminfo') 577 info = helpers.get_meminfo(src=sample_meminfo) 578 assert info.Cached == 1013816 579 580 def test_invalid_src(self): 581 # we cope with invalid src files 582 info = helpers.get_meminfo(src="nOt-ExIsTiNg-FiLe") 583 assert info is None 584 585 539 586 def test_suite(): 540 587 suite = unittest.TestSuite() … … 554 601 SimpleHelpersTestCase, 555 602 CheckCSVCharsetTestCase, 603 MemInfoTestCase, 604 GetMemInfoTestCase, 556 605 ]: 557 606 suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.