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

Add screenshot 1342 #2039

Draft
wants to merge 46 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e2c8b46
fixed the header levels in the sprite doc
sabadam32 Mar 2, 2024
c2dcc0f
Revert "fixed the header levels in the sprite doc"
sabadam32 Mar 2, 2024
b0d6718
Merge branch 'pythonarcade:development' into development
sabadam32 Mar 4, 2024
5377d26
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Mar 7, 2024
b8ff2f4
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Mar 10, 2024
6d5b217
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Mar 14, 2024
37a5224
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Mar 29, 2024
7523e89
added screenshot functions
sabadam32 Mar 29, 2024
cc598c8
fix tests
sabadam32 Mar 29, 2024
3d47991
fix locations
sabadam32 Mar 29, 2024
e061b0c
removed stash file and updated screenshot oath
sabadam32 Mar 29, 2024
ce14121
fix tests
sabadam32 Mar 29, 2024
3472eab
Use cleaner pytest screenshot fixtures
pushfoo Mar 29, 2024
d38d28e
Try to make Window.save_screenshot friendlier + better documented
pushfoo Mar 31, 2024
b104c00
Use shorter intersphinx mapping instead of full URL
pushfoo Mar 31, 2024
931075e
Phrasing tweak for Window.save_screenshot docstring
pushfoo Mar 31, 2024
6811519
Sync implementation + docstring for window_command.save_screenshot
pushfoo Mar 31, 2024
b377f88
Use intersphinx link in screenshot kwargs field
pushfoo Mar 31, 2024
0dcdcf1
Add Window.save_timestamped_screenshot method + doc
pushfoo Mar 31, 2024
161566e
Add creenshot / helper doc, helpers, and examples
pushfoo Mar 31, 2024
29ccec7
Revert get_timestamped_screenshot
pushfoo Mar 31, 2024
2b74056
Spacing in Window.__init__
pushfoo Mar 31, 2024
f70685a
Revert whitespace noise in application.py
pushfoo Mar 31, 2024
6741ea9
Commit overlooked example py file + screenshot for doc
pushfoo Mar 31, 2024
eb900a6
Fix code-block directive spacing
pushfoo Mar 31, 2024
b6b5341
Better date and time section tweaks
pushfoo Mar 31, 2024
b53b413
Intro block revision
pushfoo Mar 31, 2024
c42872a
Second pair of intro tweaks
pushfoo Mar 31, 2024
ae00a90
Add module-level spacing above get_timestamp
pushfoo Apr 2, 2024
2dfd5b5
Put format string first in get_timestamp
pushfoo Apr 2, 2024
09a363e
Add tzinfo to get_timestamp
pushfoo Apr 2, 2024
50d38ee
Account for DX + Sabadam32's suggestions
pushfoo Apr 2, 2024
e6a708a
Add timezone to format string + clean up tests
pushfoo Apr 3, 2024
d7eeecb
Phrasing tweak to intro
pushfoo Apr 3, 2024
8e92ac5
Merge pull request #1 from pushfoo/screenshot_pr_cleaning
sabadam32 Apr 4, 2024
f585d83
add in get_image
sabadam32 Apr 4, 2024
37b0962
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Apr 5, 2024
caa493e
Merge branch 'development' into add_screenshot_1342
sabadam32 Apr 5, 2024
5aafe39
fix typing errors
sabadam32 Apr 5, 2024
90164e8
fix type error
sabadam32 Apr 5, 2024
58f2d70
removed datetime type
sabadam32 Apr 6, 2024
ff1abb1
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Apr 6, 2024
a2278d3
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Apr 20, 2024
d9c0178
Merge branch 'development' into add_screenshot_1342
sabadam32 Apr 20, 2024
856689a
Merge branch 'development' of https://github.com/pythonarcade/arcade …
sabadam32 Apr 30, 2024
43e6ac5
Merge branch 'development' into add_screenshot_1342
sabadam32 Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arcade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def configure_logging(level: Optional[int] = None):
from .window_commands import start_render
from .window_commands import unschedule
from .window_commands import schedule_once
from .window_commands import save_screenshot

from .camera import SimpleCamera, Camera
from .sections import Section, SectionManager
Expand Down Expand Up @@ -356,6 +357,7 @@ def configure_logging(level: Optional[int] = None):
'read_tmx',
'load_tilemap',
'run',
'save_screenshot',
'schedule',
'set_background_color',
'set_viewport',
Expand Down
16 changes: 15 additions & 1 deletion arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import os
import time
from typing import List, Tuple, Optional

from pathlib import Path
import pyglet
from PIL import Image

import pyglet.gl as gl
import pyglet.window.mouse
Expand Down Expand Up @@ -930,6 +931,19 @@ def on_mouse_leave(self, x: int, y: int):
"""
pass

def save_screenshot(self, location: Optional[str] = None) -> Path:
sabadam32 marked this conversation as resolved.
Show resolved Hide resolved

img = self.ctx.get_framebuffer_image(self.ctx.screen)
if not location:
output_dir = Path().parent.absolute()
else:
output_dir = Path(location)

filename = f"{self.caption.lower().replace(' ', '_')}_{time.monotonic_ns()}.png"
full_file_path = output_dir / filename
img.save(full_file_path, 'PNG')
return full_file_path


def open_window(
width: int,
Expand Down
10 changes: 8 additions & 2 deletions arcade/window_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os

import pyglet

from pathlib import Path
from typing import (
Callable,
Optional,
Expand Down Expand Up @@ -37,7 +37,8 @@
"set_background_color",
"schedule",
"unschedule",
"schedule_once"
"schedule_once",
"save_screenshot"
]


Expand Down Expand Up @@ -362,3 +363,8 @@ def some_action(delta_time):
:param delay: Delay in seconds
"""
pyglet.clock.schedule_once(function_pointer, delay)


def save_screenshot(location: Optional[str] = None) -> Path:
sabadam32 marked this conversation as resolved.
Show resolved Hide resolved
window = get_window()
return window.save_screenshot(location)
243 changes: 243 additions & 0 deletions tash apply
sabadam32 marked this conversation as resolved.
Show resolved Hide resolved
sabadam32 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
GIT-STASH(1) Git Manual GIT-STASH(1)

NNAAMMEE
git-stash - Stash the changes in a dirty working directory away

SSYYNNOOPPSSIISS
_g_i_t _s_t_a_s_h list [<log-options>]
_g_i_t _s_t_a_s_h show [-u|--include-untracked|--only-untracked] [<diff-options>] [<stash>]
_g_i_t _s_t_a_s_h drop [-q|--quiet] [<stash>]
_g_i_t _s_t_a_s_h ( pop | apply ) [--index] [-q|--quiet] [<stash>]
_g_i_t _s_t_a_s_h branch <branchname> [<stash>]
_g_i_t _s_t_a_s_h [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]]
_g_i_t _s_t_a_s_h clear
_g_i_t _s_t_a_s_h create [<message>]
_g_i_t _s_t_a_s_h store [-m|--message <message>] [-q|--quiet] <commit>

DDEESSCCRRIIPPTTIIOONN
Use ggiitt ssttaasshh when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves
your local modifications away and reverts the working directory to match the HHEEAADD commit.

The modifications stashed away by this command can be listed with ggiitt ssttaasshh lliisstt, inspected with ggiitt ssttaasshh sshhooww, and restored (potentially on top of a different
commit) with ggiitt ssttaasshh aappppllyy. Calling ggiitt ssttaasshh without any arguments is equivalent to ggiitt ssttaasshh ppuusshh. A stash is by default listed as "WIP on _b_r_a_n_c_h_n_a_m_e ...", but
you can give a more descriptive message on the command line when you create one.

The latest stash you created is stored in rreeffss//ssttaasshh; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e.g.
ssttaasshh@@{{00}} is the most recently created stash, ssttaasshh@@{{11}} is the one before it, ssttaasshh@@{{22..hhoouurrss..aaggoo}} is also possible). Stashes may also be referenced by specifying
just the stash index (e.g. the integer nn is equivalent to ssttaasshh@@{{nn}}).

CCOOMMMMAANNDDSS
push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message <message>] [--pathspec-from-file=<file>
[--pathspec-file-nul]] [--] [<pathspec>...]
Save your local modifications to a new _s_t_a_s_h _e_n_t_r_y and roll them back to HEAD (in the working tree and in the index). The <message> part is optional and gives
the description along with the stashed state.

For quickly making a snapshot, you can omit "push". In this mode, non-option arguments are not allowed to prevent a misspelled subcommand from making an
unwanted stash entry. The two exceptions to this are ssttaasshh --pp which acts as alias for ssttaasshh ppuusshh --pp and pathspec elements, which are allowed after a double
hyphen ---- for disambiguation.

save [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]
This option is deprecated in favour of _g_i_t _s_t_a_s_h _p_u_s_h. It differs from "stash push" in that it cannot take pathspec. Instead, all non-option arguments are
concatenated to form the stash message.

list [<log-options>]
List the stash entries that you currently have. Each _s_t_a_s_h _e_n_t_r_y is listed with its name (e.g. ssttaasshh@@{{00}} is the latest entry, ssttaasshh@@{{11}} is the one before,
etc.), the name of the branch that was current when the entry was made, and a short description of the commit the entry was based on.

stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash

The command takes options applicable to the _g_i_t _l_o_g command to control what is shown and how. See ggiitt--lloogg(1).

show [-u|--include-untracked|--only-untracked] [<diff-options>] [<stash>]
Show the changes recorded in the stash entry as a diff between the stashed contents and the commit back when the stash entry was first created. By default, the
command shows the diffstat, but it will accept any format known to _g_i_t _d_i_f_f (e.g., ggiitt ssttaasshh sshhooww --pp ssttaasshh@@{{11}} to view the second most recent entry in patch
form). If no <<ddiiffff--ooppttiioonn>> is provided, the default behavior will be given by the ssttaasshh..sshhoowwSSttaatt, and ssttaasshh..sshhoowwPPaattcchh config variables. You can also use
ssttaasshh..sshhoowwIInncclluuddeeUUnnttrraacckkeedd to set whether ----iinncclluuddee--uunnttrraacckkeedd is enabled by default.

pop [--index] [-q|--quiet] [<stash>]
Remove a single stashed state from the stash list and apply it on top of the current working tree state, i.e., do the inverse operation of ggiitt ssttaasshh ppuusshh. The
working directory must match the index.

Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call ggiitt ssttaasshh
ddrroopp manually afterwards.

apply [--index] [-q|--quiet] [<stash>]
Like ppoopp, but do not remove the state from the stash list. Unlike ppoopp, <<ssttaasshh>> may be any commit that looks like a commit created by ssttaasshh ppuusshh or ssttaasshh
ccrreeaattee.

branch <branchname> [<stash>]
Creates and checks out a new branch named <<bbrraanncchhnnaammee>> starting from the commit at which the <<ssttaasshh>> was originally created, applies the changes recorded in
<<ssttaasshh>> to the new working tree and index. If that succeeds, and <<ssttaasshh>> is a reference of the form ssttaasshh@@{{<<rreevviissiioonn>>}}, it then drops the <<ssttaasshh>>.

This is useful if the branch on which you ran ggiitt ssttaasshh ppuusshh has changed enough that ggiitt ssttaasshh aappppllyy fails due to conflicts. Since the stash entry is applied
on top of the commit that was HEAD at the time ggiitt ssttaasshh was run, it restores the originally stashed state with no conflicts.

clear
Remove all the stash entries. Note that those entries will then be subject to pruning, and may be impossible to recover (see _E_x_a_m_p_l_e_s below for a possible
strategy).

drop [-q|--quiet] [<stash>]
Remove a single stash entry from the list of stash entries.

create
Create a stash entry (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace. This is intended to be
useful for scripts. It is probably not the command you want to use; see "push" above.

store
Store a given stash created via _g_i_t _s_t_a_s_h _c_r_e_a_t_e (which is a dangling merge commit) in the stash ref, updating the stash reflog. This is intended to be useful
for scripts. It is probably not the command you want to use; see "push" above.

OOPPTTIIOONNSS
-a, --all
This option is only valid for ppuusshh and ssaavvee commands.

All ignored and untracked files are also stashed and then cleaned up with ggiitt cclleeaann.

-u, --include-untracked, --no-include-untracked
When used with the ppuusshh and ssaavvee commands, all untracked files are also stashed and then cleaned up with ggiitt cclleeaann.

When used with the sshhooww command, show the untracked files in the stash entry as part of the diff.

--only-untracked
This option is only valid for the sshhooww command.

Show only the untracked files in the stash entry as part of the diff.

--index
This option is only valid for ppoopp and aappppllyy commands.

Tries to reinstate not only the working tree’s changes, but also the index’s ones. However, this can fail, when you have conflicts (which are stored in the
index, where you therefore can no longer apply the changes as they were originally).

-k, --keep-index, --no-keep-index
This option is only valid for ppuusshh and ssaavvee commands.

All changes already added to the index are left intact.

-p, --patch
This option is only valid for ppuusshh and ssaavvee commands.

Interactively select hunks from the diff between HEAD and the working tree to be stashed. The stash entry is constructed such that its index state is the same
as the index state of your repository, and its worktree contains only the changes you selected interactively. The selected changes are then rolled back from
your worktree. See the “Interactive Mode” section of ggiitt--aadddd(1) to learn how to operate the ----ppaattcchh mode.

The ----ppaattcchh option implies ----kkeeeepp--iinnddeexx. You can use ----nnoo--kkeeeepp--iinnddeexx to override this.

--pathspec-from-file=<file>
This option is only valid for ppuusshh command.

Pathspec is passed in <<ffiillee>> instead of commandline args. If <<ffiillee>> is exactly -- then standard input is used. Pathspec elements are separated by LF or CR/LF.
Pathspec elements can be quoted as explained for the configuration variable ccoorree..qquuootteePPaatthh (see ggiitt--ccoonnffiigg(1)). See also ----ppaatthhssppeecc--ffiillee--nnuull and global
----lliitteerraall--ppaatthhssppeeccss.

--pathspec-file-nul
This option is only valid for ppuusshh command.

Only meaningful with ----ppaatthhssppeecc--ffrroomm--ffiillee. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines
and quotes).

-q, --quiet
This option is only valid for aappppllyy, ddrroopp, ppoopp, ppuusshh, ssaavvee, ssttoorree commands.

Quiet, suppress feedback messages.

--
This option is only valid for ppuusshh command.

Separates pathspec from options for disambiguation purposes.

<pathspec>...
This option is only valid for ppuusshh command.

The new stash entry records the modified states only for the files that match the pathspec. The index entries and working tree files are then rolled back to
the state in HEAD only for these files, too, leaving files that do not match the pathspec intact.

For more details, see the _p_a_t_h_s_p_e_c entry in ggiittgglloossssaarryy(7).

<stash>
This option is only valid for aappppllyy, bbrraanncchh, ddrroopp, ppoopp, sshhooww commands.

A reference of the form ssttaasshh@@{{<<rreevviissiioonn>>}}. When no <<ssttaasshh>> is given, the latest stash is assumed (that is, ssttaasshh@@{{00}}).

DDIISSCCUUSSSSIIOONN
A stash entry is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HHEEAADD when the entry was
created. The tree of the second parent records the state of the index when the entry is made, and it is made a child of the HHEEAADD commit. The ancestry graph looks
like this:

.----W
/ /
-----H----I

where HH is the HHEEAADD commit, II is a commit that records the state of the index, and WW is a commit that records the state of the working tree.

EEXXAAMMPPLLEESS
Pulling into a dirty tree
When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing. When your local changes do
not conflict with the changes in the upstream, a simple ggiitt ppuullll will let you move forward.

However, there are cases in which your local changes do conflict with the upstream changes, and ggiitt ppuullll refuses to overwrite your changes. In such a case, you
can stash your changes away, perform a pull, and then unstash, like this:

$ git pull
...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop

Interrupted workflow
When you are in the middle of something, your boss comes in and demands that you fix something immediately. Traditionally, you would make a commit to a
temporary branch to store your changes away, and return to your original branch to make the emergency fix, like this:

# ... hack hack hack ...
$ git switch -c my_wip
$ git commit -a -m "WIP"
$ git switch master
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git switch my_wip
$ git reset --soft HEAD^
# ... continue hacking ...

You can use _g_i_t _s_t_a_s_h to simplify the above, like this:

# ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash pop
# ... continue hacking ...

Testing partial commits
You can use ggiitt ssttaasshh ppuusshh ----kkeeeepp--iinnddeexx when you want to make two or more commits out of the changes in the work tree, and you want to test each change before
committing:

# ... hack hack hack ...
$ git add --patch foo # add just first part to the index
$ git stash push --keep-index # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part' # commit fully tested change
$ git stash pop # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'

Recovering stash entries that were cleared/dropped erroneously
If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms. However, you can try the following incantation to
get a list of stash entries that are still in your repository, but not reachable any more:

git fsck --unreachable |
grep commit | cut -d\ -f3 |
xargs git log --merges --no-walk --grep=WIP

SSEEEE AALLSSOO
ggiitt--cchheecckkoouutt(1), ggiitt--ccoommmmiitt(1), ggiitt--rreefflloogg(1), ggiitt--rreesseett(1), ggiitt--sswwiittcchh(1)

GGIITT
Part of the ggiitt(1) suite

Git 2.34.1 07/07/2023 GIT-STASH(1)
28 changes: 28 additions & 0 deletions tests/unit/window/test_screenshot.py
sabadam32 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import arcade
import glob
import os

def test_no_location(window: arcade.Window):
window.save_screenshot()
file_list = glob.glob('testing_*.png')
assert file_list
os.remove(file_list[0])


def test_location(window: arcade.Window):
window.save_screenshot('doc/')
file_list = glob.glob('doc/testing_*.png')
assert file_list
os.remove(file_list[0])

def test_command(window: arcade.Window):
arcade.save_screenshot()
file_list = glob.glob('testing_*.png')
assert file_list
os.remove(file_list[0])

def test_command_with_location(window: arcade.Window):
arcade.save_screenshot('doc')
file_list = glob.glob('doc/testing_*.png')
assert file_list
os.remove(file_list[0])
Loading