Skip to content

Commit

Permalink
Merge pull request #38 from TOMToolkit/fix/bring-project-up-to-date
Browse files Browse the repository at this point in the history
tom_lt: Fix/bring project up to date
  • Loading branch information
phycodurus authored Apr 16, 2024
2 parents 014b999 + 7257ace commit fc143f8
Show file tree
Hide file tree
Showing 8 changed files with 2,565 additions and 58 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/github-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Create Github Release

on:
push:
tags:
- "*.*.*"
- "*.*.*-alpha.*"
- "*.*.*-beta.*"
- "*.*.*-rc.*"

jobs:
run_tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry install
- name: Run tests
run: echo "write tests!"

create_release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: true
29 changes: 29 additions & 0 deletions .github/workflows/pypi-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release to PyPi

on:
release:
types: [published]

jobs:
publish_to_pypi:
runs-on: ubuntu-latest
environment: release # configured in repo Settings/Environments; referenced in PyPI OIDC
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry self add "poetry-dynamic-versioning[plugin]"
- name: Build package with poetry
run: |
poetry build -f wheel
poetry build -f sdist
- name: Publish to PyPI with pypi-publish (trusted publishing)
uses: pypa/[email protected]

38 changes: 38 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: run-tests

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry install --with lint
- name: Style Checks
run: poetry run flake8 tom_* --exclude=*/migrations/* --max-line-length=120

run_tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry install
- name: Run tests
run: poetry run python -m unittest discover -v
2,345 changes: 2,345 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[tool.poetry]
name = "tom-lt"
# this version is a placeholder: version supplied by poetry-dynamic-versioning
version = "0.0.0"
description = "TOM Toolkit Facility module for the Liverpool Telescope"
authors = [
"Doug Arnold <[email protected]>",
"David Collom <[email protected]>",
"Lindy Lindstrom <[email protected]>"]
license = "GPL-3.0-or-later"
readme = "README.md"
packages = [{include = "tom_lt"}]

[tool.poetry.dependencies]
python = ">=3.8.1,<3.12"
tomtoolkit = ">=2.15"
suds-py3 = "~1.4"
lxml = "~5.2"

[tool.poetry.group.lint.dependencies]
flake8 = "~6.0"

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
style = "pep440"
# the default pattern regex makes the 'v' manditory
# this pattern modifies the default regex in order to make the 'v' optional
# ('v' becomes '[v]?' meaning a single v, [v], and ? means optional)
pattern = "(?x)^[v]?((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)([-._]?((?P<stage>[a-zA-Z]+)[-._]?(?P<revision>\\d+)?))?(\\+(?P<tagged_metadata>.+))?$"

# substitute version not only in pyproject.toml (which the config block above does)
# but also the __version__.py file
[tool.poetry-dynamic-versioning.substitution]


[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning"]
build-backend = "poetry_dynamic_versioning.backend"
# poetry_dynamic_versioning.backend is a thin wrapper around the standard build-backend = "poetry.core.masonry.api"
36 changes: 0 additions & 36 deletions setup.py

This file was deleted.

2 changes: 2 additions & 0 deletions tom_lt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# placeholder for poetry-dynamic-versioning (see pyproject.toml)
__version__ = "0.0.0"
92 changes: 70 additions & 22 deletions tom_lt/lt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging
import time

from lxml import etree
from suds import Client
from dateutil.parser import parse
from datetime import datetime

from django import forms
from django.conf import settings
Expand All @@ -17,6 +16,10 @@
from tom_observations.facility import BaseRoboticObservationForm, BaseRoboticObservationFacility
from tom_targets.models import Target

from tom_lt import __version__

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

try:
LT_SETTINGS = settings.FACILITIES['LT']
Expand Down Expand Up @@ -67,7 +70,8 @@ def __init__(self, *args, **kwargs):
self.common_layout,
self.layout(),
self.extra_layout(),
self.button_layout()
self.button_layout(),
self.version_layout(),
)

def is_valid(self):
Expand Down Expand Up @@ -109,6 +113,12 @@ def layout(self):
css_class='form-row'
)

def version_layout(self):
return Div(HTML('<hr>'
'<em><a href="http://telescope.livjm.ac.uk" target="_blank">Liverpool Telescope</a>'
' Facility module v{{version}}</em>'
))

def extra_layout(self):
return Div()

Expand Down Expand Up @@ -177,10 +187,12 @@ def observation_payload(self):


class LT_IOO_ObservationForm(LTObservationForm):
binning = forms.ChoiceField(choices=[('1x1', '1x1'), ('2x2', '2x2')], initial=('2x2', '2x2'),
help_text='2x2 binning is usual, giving 0.3 arcsec/pixel, \
faster readout and lower readout noise. 1x1 binning should \
only be selected if specifically required.')
binning = forms.ChoiceField(
choices=[('1x1', '1x1'), ('2x2', '2x2')],
initial=('2x2', '2x2'),
help_text='2x2 binning is usual, giving 0.3 arcsec/pixel, \
faster readout and lower readout noise. 1x1 binning should \
only be selected if specifically required.')

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -428,28 +440,64 @@ class LTFacility(BaseRoboticObservationFacility):
name = 'LT'
observation_types = [('IOO', 'IO:O'), ('IOI', 'IO:I'), ('SPRAT', 'SPRAT'), ('FRODO', 'FRODOSpec')]

# observation_forms should be a dictionary
# * it's .items() method is called in views.py::ObservationCreateView.get_context_data()
# * the keys are the observation_types; values are the ObservationForm classes

# TODO: this (required) addition seems redudant to the get_form() method below.
# TODO: see how get_form() is used and if it's still required
observation_forms = {
'IOO': LT_IOO_ObservationForm,
'IOI': LT_IOI_ObservationForm,
'SPRAT': LT_SPRAT_ObservationForm,
'FRODO': LT_FRODO_ObservationForm
}

SITES = {
'La Palma': {
'sitecode': 'orm',
'sitecode': 'orm', # TODO: what does this mean? and document it.
'latitude': 28.762,
'longitude': -17.872,
'elevation': 2363}
}

def get_form(self, observation_type):
if observation_type == 'IOO':
return LT_IOO_ObservationForm
elif observation_type == 'IOI':
return LT_IOI_ObservationForm
elif observation_type == 'SPRAT':
return LT_SPRAT_ObservationForm
elif observation_type == 'FRODO':
return LT_FRODO_ObservationForm
else:
return LT_IOO_ObservationForm
"""
"""
try:
return self.observation_forms[observation_type]
except KeyError:
return self.observation_forms['IOO']
# This is the original implementation of this method below.
# I've rewritten it to use the observation_forms dictionary above.
#
# if observation_type == 'IOO':
# return LT_IOO_ObservationForm
# elif observation_type == 'IOI':
# return LT_IOI_ObservationForm
# elif observation_type == 'SPRAT':
# return LT_SPRAT_ObservationForm
# elif observation_type == 'FRODO':
# return LT_FRODO_ObservationForm
# else:
# return LT_IOO_ObservationForm

def get_facility_context_data(self, **kwargs):
"""Provide Facility-specific data to context for ObservationCreateView's template
This method is called by ObservationCreateView.get_context_data() and returns a
dictionary of context data to be added to the View's context
"""
facility_context_data = super().get_facility_context_data(**kwargs)
new_context_data = {
'version': __version__, # from tom_tl/__init__.py
}

facility_context_data.update(new_context_data)
return facility_context_data

def submit_observation(self, observation_payload):
if(LT_SETTINGS['DEBUG']):
if (LT_SETTINGS['DEBUG']):
payload = etree.fromstring(observation_payload)
f = open("created.rtml", "w")
f.write(etree.tostring(payload, encoding="unicode", pretty_print=True))
Expand Down Expand Up @@ -478,7 +526,7 @@ def cancel_observation(self, observation_id):
payload.append(form._build_project())

def validate_observation(self, observation_payload):
if(LT_SETTINGS['DEBUG']):
if (LT_SETTINGS['DEBUG']):
return []
else:
headers = {
Expand All @@ -495,8 +543,8 @@ def validate_observation(self, observation_payload):
# Send payload, and receive response string, removing the encoding tag which causes issue with lxml parsing
try:
response = client.service.handle_rtml(validate_payload).replace('encoding="ISO-8859-1"', '')
except:
return ['Error with connection to Liverpool Telescope',
except Exception as e:
return [f'Error with connection to Liverpool Telescope: {e}',
'This could be due to incorrect credentials, or IP / Port settings',
'Occassionally, this could be due to the rebooting of systems at the Telescope Site',
'Please retry at another time.',
Expand Down

0 comments on commit fc143f8

Please sign in to comment.