Skip to content

Commit

Permalink
Support to get android device name or hostname for linux and windows (#…
Browse files Browse the repository at this point in the history
…630)

* Added device-name facades
* added device-name support for android
* Added Linux api for DeviceName
* Added Windows api for DeviceName
* Added device-name support for MacOSX
* added unit test for devicename
* added proxy for devicename
* bumped up version number
* added devicename feature in README.md file
  • Loading branch information
ljnath committed Jun 19, 2021
1 parent c17fd01 commit daf23c3
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ implement the api in the easiest way, depending on the current platform.
| Camera (taking picture) ||| | | |
| Compass ||| | | |
| CPU count | | ||||
| Devicename || ||||
| Email (open mail client) ||||||
| Flash ||| | | |
| GPS ||| | | |
Expand All @@ -59,6 +60,7 @@ implement the api in the easiest way, depending on the current platform.
| Vibrator ||| | | |
| Wifi | | ||||


## Installation

To use on desktop: `pip install plyer`
Expand Down
7 changes: 5 additions & 2 deletions plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
'flash', 'gps', 'gravity', 'gyroscope', 'humidity', 'irblaster',
'keystore', 'light', 'notification', 'orientation', 'processors',
'proximity', 'screenshot', 'sms', 'spatialorientation', 'storagepath',
'stt', 'temperature', 'tts', 'uniqueid', 'vibrator', 'wifi'
'stt', 'temperature', 'tts', 'uniqueid', 'vibrator', 'wifi', 'devicename'
)

__version__ = '2.0.1.dev0'
__version__ = '2.1.0.dev0'


from plyer import facades
Expand Down Expand Up @@ -119,3 +119,6 @@

#: Screenshot proxy to :class:`plyer.facades.Screenshot`
screenshot = Proxy('screenshot', facades.Screenshot)

#: devicename proxy to :class:`plyer.facades.DeviceName`
devicename = Proxy('devicename', facades.DeviceName)
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
'Processors', 'StoragePath', 'Keystore', 'Bluetooth', 'Screenshot',
'STT')
'STT', 'DeviceName')

from plyer.facades.accelerometer import Accelerometer
from plyer.facades.audio import Audio
Expand Down Expand Up @@ -48,3 +48,4 @@
from plyer.facades.processors import Processors
from plyer.facades.cpu import CPU
from plyer.facades.screenshot import Screenshot
from plyer.facades.devicename import DeviceName
44 changes: 44 additions & 0 deletions plyer/facades/devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''DeviceName facade.
Returns the following depending on the platform:
* **Android**: Android Device name
* **Linux**: Hostname of the machine
* **OS X**: Hostname of the machine
* **Windows**: Hostname of the machine
Simple Example
--------------
To get the unique ID::
>>> from plyer import devicename
>>> devicename.device_name
'Oneplus 3'
.. versionadded:: 2.1.0
- first release
Supported Platforms
-------------------
Android, Windows, OS X, Linux
'''


class DeviceName:
'''
DeviceName facade.
'''

@property
def device_name(self):
'''
Property that returns the device name of the platform.
'''
return self._get_device_name()

# private
def _get_device_name(self):
raise NotImplementedError()
28 changes: 28 additions & 0 deletions plyer/platforms/android/devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
Module of Android API for plyer.devicename.
'''

from jnius import autoclass
from plyer.platforms.android import activity
from plyer.facades import DeviceName

Secure = autoclass('android.provider.Global$Secure')


class AndroidDeviceName(DeviceName):
'''
Implementation of Android devicename API.
'''

def _get_device_name(self):
return Secure.getString(
activity.getContentResolver(),
Secure.DEVICE_NAME
)


def instance():
'''
Instance for facade proxy.
'''
return AndroidDeviceName()
23 changes: 23 additions & 0 deletions plyer/platforms/linux/devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Module of Linux API for plyer.devicename.
'''

import socket
from plyer.facades import DeviceName


class LinuxDeviceName(DeviceName):
'''
Implementation of Linux DeviceName API.
'''

def _get_device_name(self):
hostname = socket.gethostname()
return hostname


def instance():
'''
Instance for facade proxy.
'''
return LinuxDeviceName()
23 changes: 23 additions & 0 deletions plyer/platforms/macosx/devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Module of MacOSX API for plyer.devicename.
'''

import socket
from plyer.facades import DeviceName


class OSXDeviceName(DeviceName):
'''
Implementation of MacOSX DeviceName API.
'''

def _get_device_name(self):
hostname = socket.gethostname()
return hostname


def instance():
'''
Instance for facade proxy.
'''
return OSXDeviceName()
23 changes: 23 additions & 0 deletions plyer/platforms/win/devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
Module of Win API for plyer.devicename.
'''

import socket
from plyer.facades import DeviceName


class WinDeviceName(DeviceName):
'''
Implementation of Linux DeviceName API.
'''

def _get_device_name(self):
hostname = socket.gethostname()
return hostname


def instance():
'''
Instance for facade proxy.
'''
return WinDeviceName()
77 changes: 77 additions & 0 deletions plyer/tests/test_devicename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'''
TestDeviceName
============
Tested platforms:
* Windows
'''

import unittest
from mock import patch
from plyer.tests.common import PlatformTest, platform_import
import socket


class TestDeviceName(unittest.TestCase):
'''
TestCase for plyer.devicename.
'''

@PlatformTest('win')
def test_devicename_win(self):
'''
Test Windows API for plyer.devicename.
'''
devicename = platform_import(platform='win',
module_name='devicename'
)
devicename_instance = devicename.instance()

with patch.object(socket,
'gethostname',
return_value='mocked_windows_hostname'
) as _:

evaluated_device_name = devicename_instance.device_name
self.assertEqual(evaluated_device_name, 'mocked_windows_hostname')

@PlatformTest('linux')
def test_devicename_linux(self):
'''
Test Linux API for plyer.devicename.
'''
devicename = platform_import(platform='linux',
module_name='devicename'
)
devicename_instance = devicename.instance()

with patch.object(socket,
'gethostname',
return_value='mocked_linux_hostname'
) as _:

evaluated_device_name = devicename_instance.device_name
self.assertEqual(evaluated_device_name, 'mocked_linux_hostname')

@PlatformTest('macosx')
def test_devicename_macosx(self):
'''
Test MacOSX API for plyer.devicename.
'''
devicename = platform_import(platform='macosx',
module_name='devicename'
)
devicename_instance = devicename.instance()

with patch.object(socket,
'gethostname',
return_value='mocked_macosx_hostname'
) as _:

evaluated_device_name = devicename_instance.device_name
self.assertEqual(evaluated_device_name, 'mocked_macosx_hostname')


if __name__ == '__main__':
unittest.main()

0 comments on commit daf23c3

Please sign in to comment.