-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to get android device name or hostname for linux and windows (#…
…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
Showing
9 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |