Skip to content

Commit

Permalink
Merge pull request #394 from salil-gtm/master
Browse files Browse the repository at this point in the history
Number Of Processors for Linux Platform
  • Loading branch information
akshayaurora authored Apr 28, 2018
2 parents 6e1f86c + 1fabd8c commit b21c688
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Text to speech X X X X X
Unique ID X X X X X
Vibrator X X
Wifi X X X
Number of Processors X
================================== ======= === ======= ==== =====

Support
Expand Down
5 changes: 4 additions & 1 deletion plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'compass', 'email', 'filechooser', 'flash', 'gps', 'gravity',
'gyroscope', 'irblaster', 'light', 'orientation', 'notification',
'proximity', 'sms', 'tts', 'uniqueid', 'vibrator', 'wifi',
'temperature', 'humidity', 'spatialorientation', 'brightness', 'storagepath')
'temperature', 'humidity', 'spatialorientation', 'brightness', 'storagepath','processors')

__version__ = '1.3.1dev'

Expand Down Expand Up @@ -102,3 +102,6 @@

#: StoragePath proxy to :class:`plyer.facades.StoragePath`
storagepath = Proxy('storagepath', facades.StoragePath)

#: Processors proxy to :class:`plyer.facades.Processors`
processors = Proxy('processors', facades.Processors)
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'Compass', 'Email', 'FileChooser', 'GPS', 'Gravity', 'Gyroscope',
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness','Processors',
'StoragePath', 'keystore')

from plyer.facades.accelerometer import Accelerometer
Expand Down Expand Up @@ -42,3 +42,4 @@
from plyer.facades.brightness import Brightness
from plyer.facades.keystore import Keystore
from plyer.facades.storagepath import StoragePath
from plyer.facades.processors import Processors
39 changes: 39 additions & 0 deletions plyer/facades/processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'''
Number of Processors
=======
Simple Example
---------------
To get battery status::
>>> from plyer import processors
>>> processors.status
{'Number_of_Processors': '4'}
Supported Platforms
-------------------
Linux
'''


class Processors(object):
'''
Number of Processors info facade.
'''

@property
def status(self):
'''
Property that contains a dict with the following fields:
* **Number_of_Processors** *(int)*: Number of Processors in the system
.. warning::
If any of the fields is not readable, it is set as
None.
'''
return self.get_state()

def get_state(self):
return self._get_state()

# private

def _get_state(self):
raise NotImplementedError()
37 changes: 37 additions & 0 deletions plyer/platforms/linux/processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from subprocess import Popen, PIPE
from plyer.facades import Processors
from plyer.utils import whereis_exe

from os import environ


class LinuxProcessors(Processors):
def _get_state(self):
old_lang = environ.get('LANG')
environ['LANG'] = 'C'

status = {"Number_of_Processors": None}

dev = "--all"
nproc_process = Popen(
["nproc", dev],
stdout=PIPE
)
output = nproc_process.communicate()[0]

environ['LANG'] = old_lang

if not output:
return status

status['Number_of_Processors'] = output.rstrip()

return status


def instance():
import sys
if whereis_exe('nproc'):
return LinuxProcessors()
sys.stderr.write("nproc not found.")
return Processors()

0 comments on commit b21c688

Please sign in to comment.