Skip to content

Commit

Permalink
Fix context cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
neoxelox committed Apr 8, 2022
1 parent e4e3135 commit a49827a
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions superinvoke/extensions/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
from typing import List, Literal, Optional

Expand Down Expand Up @@ -95,7 +96,7 @@ def changes(context: Context, scope: int = 1) -> List[str]:
# - copy
# - move X X
# - create X X
# - remove
# - remove X X
# - read X -
# - write
# - exists X X
Expand All @@ -104,38 +105,84 @@ def changes(context: Context, scope: int = 1) -> List[str]:

# Creates a file or a directory in the specified path.
def create(context: Context, path: str, data: List[str] = [""], dir: bool = False) -> None:
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

utils.create(path, data, dir=dir)

os.chdir(prev_cwd)


# Reads a file in the specified path.
def read(context: Context, path: str) -> List[str]:
return utils.read(path)
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

result = utils.read(path)

os.chdir(prev_cwd)

return result


# Checks if the specified path exists and whether it is a file or a directory.
def exists(context: Context, path: str) -> Optional[Literal["file", "dir"]]:
return utils.exists(path)
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

result = utils.exists(path)

os.chdir(prev_cwd)

return result


# Moves a file or a directory to the specified path.
def move(context: Context, source_path: str, dest_path: str) -> None:
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

utils.move(source_path, dest_path)

os.chdir(prev_cwd)


# Removes a file or a directory in the specified path.
def remove(context: Context, path: str, dir: bool = False) -> None:
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

utils.remove(path, dir=dir)

os.chdir(prev_cwd)


# Extracts a zip, tar, gztar, bztar, or xztar file in the specified path.
def extract(context: Context, source_path: str, dest_path: str) -> None:
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

utils.extract(source_path, dest_path)

os.chdir(prev_cwd)


# Downloads a file to the specified path.
def download(context: Context, url: str, path: str) -> None:
prev_cwd = os.getcwd()
if context.cwd:
os.chdir(context.cwd)

utils.download(url, path)

os.chdir(prev_cwd)


# Extends Pyinvoke's Context methods.
def init() -> None:
Expand Down

0 comments on commit a49827a

Please sign in to comment.