Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux ScreenShot feature added #319

Closed
wants to merge 13 commits into from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ venv

bin
.buildozer
.idea
.idea
*.jpg
*.mp3
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Notifications X X X X
Text to speech X X X X X
Email (open mail client) X X X X X
Vibrator X X
Sms (send messages) X X
Sms (send messages) X X
Screenshot X
Compass X X
Unique ID X X X X X
Gyroscope X X
Expand Down
45 changes: 45 additions & 0 deletions examples/screenshot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout


Builder.load_string('''
#:import screenshot plyer.screenshot
<ScreenShotDemo>:
screenshot: screenshot
orientation: 'vertical'
padding: '50dp'
spacing: '20dp'
Label:
id: location_label
size_hint_y: None
height: sp(40)
text: 'ScreenShot Location: ' + str(root.screenshot.file_path)

Button:
id: record_button
text: 'Take Shot'
on_release: root.shot()
''')


class ScreenShotDemo(BoxLayout):
'''Root Widget.'''
screenshot = ObjectProperty()

def shot(self):
self.screenshot.shot()


class ScreenShotApp(App):

def build(self):
return ScreenShotDemo()

def on_pause(self):
return True

if __name__ == "__main__":
ScreenShotApp().run()
5 changes: 4 additions & 1 deletion plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'compass', 'email', 'filechooser', 'flash', 'gps', 'gravity',
'gyroscope', 'irblaster', 'light', 'orientation', 'notification',
'proximity', 'sms', 'tts', 'uniqueid', 'vibrator', 'wifi',
'temperature')
'temperature', 'screenshot')

__version__ = '1.3.1dev'

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

#: Temperature proxy to :class:`plyer.facades.Temperature`
temperature = Proxy('temperature', facades.Temperature)

#: Screenshot proxy to :class:`plyer.facades.Screenshot`
screenshot = Proxy('screenshot', facades.Screenshot)
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')
'Temperature', 'Screenshot')

from plyer.facades.accelerometer import Accelerometer
from plyer.facades.audio import Audio
Expand All @@ -36,3 +36,4 @@
from plyer.facades.vibrator import Vibrator
from plyer.facades.wifi import Wifi
from plyer.facades.temperature import Temperature
from plyer.facades.screenshot import Screenshot
62 changes: 62 additions & 0 deletions plyer/facades/screenshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'''
Screenshot
=====

The :class:`Screenshot` is used for recording audio.

Default path for taking screenshot is set in platform implementation.

Simple Examples
---------------

To get the file path::

>>> screenshot.file_path
'/sdcard/test.jpg'

To set the file path::

>>> import os
>>> current_list = os.listdir('.')
['/sdcard/testrecorder.jpg', '/sdcard/testrecorder1.jpg',
'/sdcard/testrecorder2.jpg', '/sdcard/testrecorder3.jpg']
>>> file_path = current_list[2]
>>> screenshot.file_path = file_path

To take screenshot::

>>> from plyer import screenshot
>>> screenshot.shot()
'''


class Screenshot(object):
'''
ScreenShot facade.
'''
_file_path = ''

def __init__(self, file_path):
super(Screenshot, self).__init__()
self._file_path = file_path

def shot(self):
self._shot()

@property
def file_path(self):
return self._file_path

@file_path.setter
def file_path(self, location):
'''
Location of the screenshot.
'''
assert isinstance(location, (basestring, unicode)), \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tried it yet, but this seems smelly to me. Have you tried it both with py2 and py3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also have been used in the audio facade from long.

Copy link
Contributor

@KeyWeeUsr KeyWeeUsr Apr 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely doesn't work on py3. To be more precise:

>>> basestring
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'basestring' is not defined
>>> unicode
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined

If there isn't some background magic going on, then I believe even the audio part is broken for py3, unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What say If I remove it. @KeyWeeUsr

'Location must be string or unicode'
self._file_path = location

# private

def _shot(self, **kwargs):
raise NotImplementedError()
29 changes: 29 additions & 0 deletions plyer/platforms/linux/screenshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import subprocess
from plyer.facades import Screenshot
from plyer.utils import whereis_exe


class GnomeScreenshot(Screenshot):
def __init__(self, file_path=None):
default_path = 'test.jpg'
super(GnomeScreenshot, self).__init__(file_path or default_path)

def _take_shot(self):
subprocess.call(["gnome-screenshot", "-d", "2", self.file_path])


class ImportScreenshot(Screenshot):
def __init__(self, file_path=None):
default_path = 'test.jpg'
super(ImportScreenshot, self).__init__(file_path or default_path)

def _take_shot(self):
subprocess.call(["import", "-window", "root", self.file_path])


def instance():
if whereis_exe('gnome-screenshot'):
return GnomeScreenshot()
elif whereis_exe('import'):
return ImportScreenshot()
return Screenshot()