-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more rules, recording, updated test example
- Loading branch information
Showing
8 changed files
with
553 additions
and
240 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
penv | ||
__pycache__ | ||
build | ||
dist | ||
self_driving_desktop.egg-info/ |
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 |
---|---|---|
@@ -1,133 +1,31 @@ | ||
import time | ||
import click | ||
import pyautogui | ||
import subprocess | ||
from lark import Lark | ||
from Xlib import display, X | ||
|
||
from self_driving_desktop import parser as ourlang | ||
from self_driving_desktop import parser as P | ||
from self_driving_desktop import grammar as G | ||
from self_driving_desktop import recorder as R | ||
|
||
d = display.Display() | ||
|
||
playlists = {} | ||
wins = {} | ||
|
||
def do(t): | ||
if t.data == "item": | ||
do(t.children[0]) | ||
return | ||
|
||
if t.data == "play": | ||
for playname in t.children: | ||
playlist = playlists[playname] | ||
do(playlist) | ||
|
||
return | ||
|
||
if t.data == "name_active": | ||
name = t.children[0] | ||
w = d.get_input_focus().focus | ||
wins[name] = w | ||
return | ||
|
||
if t.data == "focus": | ||
name = t.children[0] | ||
w = wins[name] | ||
w.set_input_focus(X.RevertToNone, X.CurrentTime) | ||
w.configure(stack_mode=X.Above) | ||
d.sync() | ||
return | ||
|
||
if t.data == "comment": | ||
return | ||
|
||
if t.data == "string": | ||
text = t.children[0][1:-1] | ||
text = text.replace("\\n", "\n") | ||
return text | ||
|
||
if t.data == "int": | ||
return int(t.children[0]) | ||
|
||
if t.data == "number": | ||
return float(t.children[0]) | ||
|
||
if t.data == "playlist": | ||
name, pl = t.children | ||
playlists[name] = pl | ||
return | ||
|
||
if t.data == "playlist_body": | ||
for c in t.children: | ||
do(c) | ||
|
||
return | ||
|
||
if t.data == "step": | ||
step = t.children[0] | ||
do(step) | ||
return | ||
|
||
if t.data == "sleep": | ||
sec = do(t.children[0]) | ||
time.sleep(sec) | ||
return | ||
|
||
if t.data == "mv": | ||
cs = [] | ||
for c in t.children: | ||
cs.append(do(c)) | ||
|
||
pyautogui.moveTo(*cs, pyautogui.easeOutQuad) | ||
return | ||
|
||
if t.data == "click": | ||
pyautogui.click() | ||
return | ||
|
||
if t.data == "hotkeys": | ||
cs = [] | ||
for c in t.children: | ||
cs.append(do(c)) | ||
|
||
pyautogui.hotkey(*cs) | ||
return | ||
|
||
if t.data == "shell": | ||
cs = [] | ||
for c in t.children: | ||
cs.append(do(c)) | ||
|
||
subprocess.Popen(cs) | ||
return | ||
|
||
if t.data == "write": | ||
text = do(t.children[0]) | ||
interval=0.1 | ||
if len(t.children) == 2: | ||
interval = do(t.children[1]) | ||
|
||
pyautogui.typewrite(text, interval=interval) | ||
return | ||
|
||
if t.data == "wspace_up": | ||
pyautogui.hotkey('ctrl', 'alt', 'up') | ||
return | ||
@click.command() | ||
@click.argument('playlist') | ||
@click.option('--record', is_flag=True, help='Record to a playlist.') | ||
def drive(playlist, record): | ||
if record is True: | ||
doRecord(playlist) | ||
else: | ||
doPlay(playlist) | ||
|
||
if t.data == "wspace_down": | ||
pyautogui.hotkey('ctrl', 'alt', 'down') | ||
return | ||
|
||
raise SyntaxError('Unknown instruction: %s' % t.data) | ||
|
||
@click.command() | ||
@click.option('--playlist', default="test.txt", help='Playlist to run.') | ||
def drive(playlist): | ||
parser = Lark(ourlang.grammar, parser='lalr') | ||
def doPlay(playlist): | ||
parser = Lark(G.grammar, parser='lalr') | ||
|
||
with open(playlist) as f: | ||
tree = parser.parse(f.read()) | ||
# print(tree) | ||
# print("="*16) | ||
for t in tree.children: | ||
do(t) | ||
P.do(t) | ||
|
||
def doRecord(playlist): | ||
R.do(playlist) | ||
|
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,79 @@ | ||
grammar = r""" | ||
start: (item ";")+ | ||
item: playlist | step | ||
playlist : "playlist" WORD playlist_body | ||
playlist_body : "{" (step ";")* "}" | ||
step : play | ||
| active | ||
| focus | ||
| delay | ||
| sleep | ||
| shell | ||
| drag | ||
| mouse | ||
| click | ||
| btndown | ||
| btnup | ||
| scroll | ||
| hscroll | ||
| keydown | ||
| keyup | ||
| hotkeys | ||
| write | ||
| copy | ||
| paste | ||
| save_clipboard | ||
| load_clipboard | ||
| copy_clipboard | ||
| paste_clipboard | ||
play: "play" WORD+ | ||
active: "active" WORD | ||
focus: "focus" WORD | ||
delay: "delay" number | ||
sleep: "sleep" number | ||
shell: ("shell"|"sh") string+ | ||
mouse: ("mouse"|"mv"|"mm") number number number | ||
drag: ("drag"|"md") string number number number | ||
click: "click" | ||
btnclick: ("btnclick"|"bc") string | ||
btndown: ("btndown"|"bd") string | ||
btnup: ("btnup"|"bu") string | ||
scroll: "scroll" int | ||
hscroll: "hscroll" int | ||
keypress: ("keypress"|"kp") string | ||
keydown: ("keydown"|"kd") string | ||
keyup: ("keyup"|"ku") string | ||
hotkeys: ("hotkeys"|"hk") string+ | ||
write: ("write"|"w"|"type"|"t") string number? | ||
copy: "copy" | ||
paste: "paste" | ||
save_clipboard: ("save_clipboard"|"scb") string | ||
load_clipboard: ("load_clipboard"|"lcb") string | ||
copy_clipboard: ("copy_clipboard"|"ccb") string | ||
paste_clipboard: ("paste_clipboard"|"pcb") string | ||
int: INT | ||
number: SIGNED_NUMBER | ||
string: ESCAPED_STRING | ||
COMMENT: /#[^\n]*/ | ||
WORD: LETTER+ | ||
%import common.LETTER | ||
%import common.ESCAPED_STRING | ||
%import common.INT | ||
%import common.SIGNED_NUMBER | ||
%import common.WS | ||
%ignore COMMENT | ||
%ignore WS | ||
""" |
Oops, something went wrong.