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

move installer to main module #19

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 0 additions & 68 deletions fancycompleter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,71 +453,3 @@ def interact(persist_history: str | None = None, namespace: dict | None = None):
# standard one is fake enough :-)
interact_pyrepl(namespace)
sys.exit()


class Installer:
"""Helper to install fancycompleter in PYTHONSTARTUP"""

def __init__(self, basepath, force):
fname = os.path.join(basepath, "python_startup.py")
self.filename = os.path.expanduser(fname)
self.force = force

def check(self):
PYTHONSTARTUP = os.environ.get("PYTHONSTARTUP")
if PYTHONSTARTUP:
return f"PYTHONSTARTUP already defined: {PYTHONSTARTUP}"
if os.path.exists(self.filename):
return f"{self.filename} already exists"

def install(self):
import textwrap

error = self.check()
if error and not self.force:
print(error)
print("Use --force to overwrite.")
return False
with open(self.filename, "w") as f:
f.write(
textwrap.dedent(
"""
import fancycompleter
fancycompleter.interact(persist_history=True)
"""
)
)
self.set_env_var()
return True

def set_env_var(self):
if sys.platform == "win32":
os.system(f'SETX PYTHONSTARTUP "{self.filename}"')
print(f"%PYTHONSTARTUP% set to {self.filename}")
else:
print(f"startup file written to {self.filename}")
print("Append this line to your ~/.bashrc:")
print(f" export PYTHONSTARTUP={self.filename}")


if __name__ == "__main__":

def usage():
print("Usage: python -m fancycompleter install [-f|--force]")
sys.exit(1)

cmd = None
force = False
for item in sys.argv[1:]:
if item in ("install",):
cmd = item
elif item in ("-f", "--force"):
force = True
else:
usage()
#
if cmd == "install":
installer = Installer("~", force)
installer.install()
else:
usage()
70 changes: 70 additions & 0 deletions fancycompleter/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import sys


class Installer:
"""Helper to install fancycompleter in PYTHONSTARTUP"""

def __init__(self, basepath, force):
fname = os.path.join(basepath, "python_startup.py")
self.filename = os.path.expanduser(fname)
self.force = force

def check(self):
PYTHONSTARTUP = os.environ.get("PYTHONSTARTUP")
if PYTHONSTARTUP:
return f"PYTHONSTARTUP already defined: {PYTHONSTARTUP}"
if os.path.exists(self.filename):
return f"{self.filename} already exists"

def install(self):
import textwrap

error = self.check()
if error and not self.force:
print(error)
print("Use --force to overwrite.")
return False
with open(self.filename, "w") as f:
f.write(
textwrap.dedent(
"""
import fancycompleter
fancycompleter.interact(persist_history=True)
"""
)
)
self.set_env_var()
return True

def set_env_var(self):
if sys.platform == "win32":
os.system(f'SETX PYTHONSTARTUP "{self.filename}"')
print(f"%PYTHONSTARTUP% set to {self.filename}")
else:
print(f"startup file written to {self.filename}")
print("Append this line to your ~/.bashrc:")
print(f" export PYTHONSTARTUP={self.filename}")


if __name__ == "__main__":

def usage():
print("Usage: python -m fancycompleter install [-f|--force]")
sys.exit(1)

cmd = None
force = False
for item in sys.argv[1:]:
if item in ("install",):
cmd = item
elif item in ("-f", "--force"):
force = True
else:
usage()

if cmd == "install":
installer = Installer("~", force)
installer.install()
else:
usage()
3 changes: 2 additions & 1 deletion tests/test_fancycompleter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fancycompleter import Color, Completer, DefaultConfig, Installer, commonprefix
from fancycompleter import Color, Completer, DefaultConfig, commonprefix
from fancycompleter.__main__ import Installer


class ConfigForTest(DefaultConfig):
Expand Down
Loading