Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
add force-quit support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 9, 2019
1 parent b069357 commit 4142acf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ This project is hosted in <https://github.com/openatx/weditor>
Only tested in `Google Chrome`, _IE_ seems not working well.

## Installation
**Require Python3.5+**
Tested with `python 3.6, 3.7`

```
pip install --upgrade weditor
pip3 install --upgrade weditor
```

## Usage

Create Shortcut in Desktop

```
python -m weditor --shortcut
weditor --shortcut
```

By click shortcut or run in command line

```
python -m weditor
weditor
```

This command will start a local server with port 17310,
and then open a browser tab for you to editor you code.

Port 17310 is to memorize the created day -- 2017/03/10

To see more usage run `weditor -h`

## Hotkeys(Both Mac and Win)
- Right click screen: `Dump Hierarchy`
Expand All @@ -47,7 +48,6 @@ Port 17310 is to memorize the created day -- 2017/03/10
- Ctrl+Enter: Run the whole code
- Ctrl+Shift+Enter: Run selected code or current line if not selected


## For Developers
See [DEVELOP.md](DEVELOP.md)

Expand Down
25 changes: 17 additions & 8 deletions weditor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,32 @@ def make_app(settings={}):
return application


def check_running(port: int):
def get_running_version(addr: str):
"""
sys.exit if already running
Returns:
None if not running
version string if running
"""
try:
r = requests.get(f"http://localhost:{port}/api/v1/version",
r = requests.get(f"{addr}/api/v1/version",
timeout=2.0)
if r.status_code == 200:
version = r.json().get("version", "dev")
sys.exit(f"Another weditor({version}) is already running")
return r.json().get("version", "dev")
except requests.exceptions.ConnectionError:
pass
except Exception as e:
print("Unknown error: %r" % e)


def run_web(debug=False, port=17310, open_browser=False):
check_running(port)
def run_web(debug=False, port=17310, open_browser=False, force_quit=False):
version = get_running_version(f"http://localhost:{port}")
if version:
if force_quit:
logger.info(f"quit previous weditor server (version: {version})")
requests.get(f"http://localhost:{port}/quit")
time.sleep(.5)
else:
sys.exit(f"Another weditor({version}) is already running")

if open_browser:
# webbrowser.open(url, new=2)
Expand Down Expand Up @@ -181,6 +189,7 @@ def main():
ap.add_argument('-q', '--quiet', action='store_true', help='quite mode, no open new browser')
ap.add_argument('-d', '--debug', action='store_true', help='open debug mode')
ap.add_argument('-p', '--port', type=int, default=17310, help='local listen port for weditor')
ap.add_argument("-f", "--force-quit", action='store_true', help="force quit before start")
ap.add_argument('--shortcut', action='store_true', help='create shortcut in desktop')
args = ap.parse_args()
# yapf: enable
Expand All @@ -194,7 +203,7 @@ def main():
return

open_browser = not args.quiet
run_web(args.debug, args.port, open_browser)
run_web(args.debug, args.port, open_browser, args.force_quit)


if __name__ == '__main__':
Expand Down

0 comments on commit 4142acf

Please sign in to comment.