arcomemory.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2015 Jörg Thalheim (Mic92)
  3. # Copyright (C) 2019, Krisztián Veress <krive001@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import division
  23. from libqtile.widget import base
  24. import subprocess
  25. def get_meminfo():
  26. val = {}
  27. with open('/proc/meminfo') as file:
  28. for line in file:
  29. key, tail = line.split(':')
  30. uv = tail.split()
  31. val[key] = int(uv[0]) // 1024
  32. val['MemUsed'] = (val['MemTotal'] + val['Shmem'] - val['MemFree'] -
  33. val['Buffers'] - val['Cached'] - val['SReclaimable'])
  34. # Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
  35. val['Memsza'] = (100 * val['MemUsed']) // val['MemTotal']
  36. return val
  37. class Memory(base.InLoopPollText):
  38. """Displays memory usage"""
  39. orientations = base.ORIENTATION_HORIZONTAL
  40. defaults = [
  41. ("fmt", "{MemUsed}M/{MemTotal}M", "see /proc/meminfo for field names"),
  42. ('execute', None, 'Command to execute on click'),
  43. ]
  44. def __init__(self, **config):
  45. super(Memory, self).__init__(**config)
  46. self.add_defaults(Memory.defaults)
  47. def poll(self):
  48. return self.fmt.format(**get_meminfo())
  49. def button_press(self, x, y, button):
  50. base.ThreadedPollText.button_press(self, x, y, button)
  51. if button == 1 and self.execute is not None:
  52. subprocess.Popen([self.execute], shell=True)
  53. mem_dict = get_meminfo()
  54. for k, v in mem_dict.items():
  55. print(k, "=>", v)
  56. # print(mem_dict)