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 pathlib import for handling paths #43

Closed
wants to merge 6 commits into from
Closed
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
59 changes: 27 additions & 32 deletions pyscope/telrun/init_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import platform
import shutil
import subprocess
from pathlib import Path

import click

Expand All @@ -13,8 +15,8 @@
@click.command()
@click.argument(
"path",
type=click.Path(resolve_path=True),
default="./pyscope-observatory/",
type=click.Path(resolve_path=True, path_type=Path),
default=Path("pyscope-observatory"),
required=False,
)
@click.version_option()
Expand All @@ -24,40 +26,37 @@

logger.info("Initializing a telrun home directory at %s" % path)

if os.path.exists(path):
if Path(path).exists():

Check warning on line 29 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L29

Added line #L29 was not covered by tests
logger.error(f"Path {path} already exists")
raise click.Abort()

logger.info(f"Creating directory {path}")
os.mkdir(path)
logger.info(f"Creating directory {str(path)}")
Path(path).mkdir()

Check warning on line 34 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L34

Added line #L34 was not covered by tests

logger.info(f"Creating subdirectories")
os.mkdir(os.path.join(path, "config"))
os.mkdir(os.path.join(path, "schedules"))
os.mkdir(os.path.join(path, "images"))
os.mkdir(os.path.join(path, "logs"))
Path(path, "config").mkdir()
Path(path, "schedules").mkdir()

Check warning on line 38 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L37-L38

Added lines #L37 - L38 were not covered by tests
Path(path, "images").mkdir()
Path(path, "logs").mkdir()

Check warning on line 40 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L40

Added line #L40 was not covered by tests

logger.info("Creating empty config files")
shutil.copyfile(
os.path.join(os.path.dirname(__file__), "../config/telrun.cfg"),
os.path.join(path, "config/telrun.cfg"),
Path(__file__).parents[1] / "config/telrun.cfg", Path(path, "config/telrun.cfg")
)

shutil.copyfile(
os.path.join(os.path.dirname(__file__), "../config/observatory.cfg"),
os.path.join(path, "config/observatory.cfg"),
Path(__file__).parents[1] / "config/observatory.cfg",
Path(path, "config/observatory.cfg"),
)

logger.info("Copying default shortcut startup script")
if platform.system() == "Windows":
shutil.copyfile(
os.path.join(os.path.dirname(__file__), "start_telrun.bat"),
os.path.join(path, "start_telrun.bat"),
Path(__file__).parent / "start_telrun.bat", Path(path, "start_telrun.bat")
)
else:
shutil.copyfile(
os.path.join(os.path.dirname(__file__), "start_telrun"),
os.path.join(path, "start_telrun"),
Path(__file__).parent / "start_telrun", Path(path, "start_telrun")
)

logger.info("Done")
Expand All @@ -76,35 +75,32 @@

logger.info("Initializing a remote telrun home directory from %s" % path)

if not os.path.exists(os.path.join(path, "config/syncfiles.cfg")):
syncfiles_path = Path(path, "config/syncfiles.cfg")
if not syncfiles_path.exists():
logger.info("No syncfiles.cfg found, creating one" % path)

shutil.copyfile(
os.path.join(os.path.dirname(__file__), "../config/syncfiles.cfg"),
os.path.join(path, "config/syncfiles.cfg"),
Path(__file__).parents[1] / "config/syncfiles.cfg", syncfiles_path
)

logger.info(
"Opening text editor... Please complete all fields and save the file"
)
if platform.system() == "Windows":
os.system(os.path.join(path, "config/syncfiles.cfg"))
subprocess.call(syncfiles_path, shell=True)
else:
try:
logger.info("Trying to open $EDITOR")
os.system(
"%s %s"
% (os.getenv("EDITOR"), os.path.join(path, "config/syncfiles.cfg"))
)
subprocess.call(os.getenv("EDITOR", "vi"), syncfiles_path)

Check warning on line 94 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L94

Added line #L94 was not covered by tests
except:
logger.warning("Could not open $EDITOR, trying to open vi")
os.system("vi " + os.path.join(path, "config/syncfiles.cfg"))
subprocess.call("vi", syncfiles_path)

input("Press enter to continue...")

logger.info("Reading syncfiles.cfg")

local_config_dir = os.path.join(path, "config")
local_config_dir = Path(path, "config")

Check warning on line 103 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L103

Added line #L103 was not covered by tests

(
uname,
Expand All @@ -118,7 +114,7 @@
remote_images_dir,
local_logs_dir,
remote_logs_dir,
) = _read_syncfiles_cfg(os.path.join(local_config_dir, "syncfiles.cfg"))
) = _read_syncfiles_cfg(Path(local_config_dir, "syncfiles.cfg"))

logger.info("Performing initial single sync")

Expand All @@ -145,13 +141,12 @@
logger.info("Copying default shortcut startup script")
if platform.system() == "Windows":
shutil.copyfile(
os.path.join(os.path.dirname(__file__), "start_syncfiles.bat"),
os.path.join(path, "start_syncfiles.bat"),
Path(__file__).parent / "start_syncfiles.bat",
Path(path, "start_syncfiles.bat"),

Check warning on line 145 in pyscope/telrun/init_dirs.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/init_dirs.py#L145

Added line #L145 was not covered by tests
)
else:
shutil.copyfile(
os.path.join(os.path.dirname(__file__), "start_syncfiles"),
os.path.join(path, "start_syncfiles"),
Path(__file__).parent / "start_syncfiles", Path(path, "start_syncfiles")
)


Expand Down
4 changes: 2 additions & 2 deletions pyscope/telrun/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@
)

if type(observatory) is str:
observatory = os.path.abspath(observatory)
if os.path.exists(observatory):
observatory = Path(observatory).absolute()

Check warning on line 170 in pyscope/telrun/rst.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/rst.py#L170

Added line #L170 was not covered by tests
if observatory.is_file():
config = configparser.ConfigParser()
config.read(observatory)
lat = coord.Latitude(config["site"]["latitude"], unit=u.deg)
Expand Down
1 change: 1 addition & 0 deletions pyscope/telrun/schedtel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import logging
import os
import pathlib
import shlex

import astroplan
Expand Down
8 changes: 3 additions & 5 deletions pyscope/telrun/startup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pathlib import Path

import click

Expand All @@ -15,9 +15,7 @@
)
@click.version_option()
def start_telrun_cli(path, gui):
telrun = TelrunOperator(
config_path=os.path.join(path, "config/telrun.cfg"), gui=gui
)
telrun = TelrunOperator(config_path=Path(path) / "config" / "telrun.cfg", gui=gui)

Check warning on line 18 in pyscope/telrun/startup.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/startup.py#L18

Added line #L18 was not covered by tests
telrun.mainloop()


Expand All @@ -27,7 +25,7 @@
)
@click.version_option()
def start_syncfiles_cli(path):
syncfiles(config=os.path.join(path, "config/"))
syncfiles(config=Path(path) / "config")


start_telrun = start_telrun_cli.callback
Expand Down
23 changes: 12 additions & 11 deletions pyscope/telrun/summary_report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from pathlib import Path

import click
import markdown
Expand Down Expand Up @@ -65,7 +66,7 @@
)
@click.version_option()
def summary_report_cli(
schedule, images_dir="./images/", preamble=None, code=None, verbose=-1
schedule=None, images_dir="./images/", preamble=None, code=None, verbose=-1
):
"""
Generates an HTML summary report on the requested schedule.\b
Expand All @@ -84,11 +85,11 @@

Parameters
----------
schedule : `str`, optional
schedule : Path-like, optional
Path to the schedule file. If none provided, the default behavior
is to check the ./schedules/ directory for the latest schedule.

images_dir : `str`, default='./images/'
images_dir : Path-like, default='./images/'
Path to the directory containing the images.

preamble : `str`, optional
Expand Down Expand Up @@ -144,28 +145,28 @@
# Get schedule
if schedule is None:
last_mtime = 0
for fname in glob.glob("./schedules/*.ecsv"):
if os.path.getmtime(schedule) > last_mtime:
last_mtime = os.path.getmtime(schedule)
schedule = fname
for f in Path("schedules").glob("*.ecsv"):
if f.stat().st_mtime > last_mtime:
last_mtime = f.stat().st_mtime
schedule = f

Check warning on line 151 in pyscope/telrun/summary_report.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/summary_report.py#L149-L151

Added lines #L149 - L151 were not covered by tests
schedule = table.Table.read(schedule, format="ascii.ecsv")

# Check images all exist
fnames = []
files = []

Check warning on line 155 in pyscope/telrun/summary_report.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/summary_report.py#L155

Added line #L155 was not covered by tests
headers = []
for row in schedule:
for fname in row["configuration"]["filename"].split(","):
if not os.path.exists(os.path.join(images_dir, fname)):
if not Path(images_dir, fname).is_file():

Check warning on line 159 in pyscope/telrun/summary_report.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/summary_report.py#L159

Added line #L159 was not covered by tests
raise click.BadOptionUsage(f"Image {fname} does not exist.")
else:
fnames.append(os.path.join(images_dir, fname))
files.append(Path(images_dir, fname))

Check warning on line 162 in pyscope/telrun/summary_report.py

View check run for this annotation

Codecov / codecov/patch

pyscope/telrun/summary_report.py#L162

Added line #L162 was not covered by tests
headers.append(fits.getheader(fnames[-1]))

tbl = table.Table(
[
[hdr["OBSCODE"] for hdr in headers],
[hdr["OBSERVER"] for hdr in headers],
fnames,
[str(f) for f in files],
[hdr["TARGET"] for hdr in headers],
[
astrotime.Time(hdr["DATE-OBS"], format="fits", scale="utc")
Expand Down
Loading