Skip to content

Implement a class version of EvoraServer and add argument parsing #41

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Pixadus
Copy link
Contributor

@Pixadus Pixadus commented Jul 12, 2024

Added argument parsing using argparse, and to turn evora-server into a bit of a nicer command-line application.

> python app.py --help
usage: Evora Server [-h] [--debug] [--port PORT] [--path PATH]

High-level server to interface with the MRO Evora CCD.

options:
  -h, --help   show this help message and exit
  --debug      Run Evora in debug mode (suitable for local development)
  --port PORT  Port to run Evora on
  --path PATH  Directory where Evora will store files

The PEP-8 style guide generally frowns on globals (unless absolutely necessary). Since EvoraServer is a pretty self-contained app, I threw all our functions into a global EvoraServer class that we can call internal functions from. This way, functions are able to access the EvoraServer "class variables" (i.e. self.FILTER_POSITION, self.ABORT_FLAG, etc) without having to redeclare them as globals within every function.

See the end of app.py for implementation.

self.FILTER_DICT = {'Ha': 0, 'B': 1, 'V': 2, 'g': 3, 'r': 4, 'i': 5}
self.FILTER_DICT_REVERSE = {0: 'Ha', 1: 'B', 2: 'V', 3: 'g', 4: 'r', 5: 'i'}

# If we're debugging, use a local directory instead - create if doesn't exist
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this wasn't a change you made, but we should have this documented that using debug mode will save in a local directory. I wonder if we even need this functionality?

Copy link
Member

@seb-cap seb-cap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great changes that will make our code much more readable and usable. While testing locally, I found that calls were failing from the client. This may mean we need to make changes to the client as well, or we need to make some extra edits to the server (like a wrapper for each route). Will look into it. Outstanding work, thank you!

INFO:werkzeug:127.0.0.1 - - [20/Jul/2024 14:45:32] "GET /getTemperature HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 1488, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 1466, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask_cors\extension.py", line 178, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 1463, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 872, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask_cors\extension.py", line 178, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 870, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\sebas\anaconda3\lib\site-packages\flask\app.py", line 855, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
TypeError: route_getTemperature() missing 1 required positional argument: 'self'

start_time = datetime.now()

while (datetime.now() - start_time).total_seconds() < float(req["exptime"]):
if ABORT_FLAG:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

if filter not in FILTER_DICT:
payload['error'] = f'Unknown filter {filter}.'
return jsonify(payload)
if DEBUGGING:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.DEBUGGING


date = Time.now().utc.isot.split("T")[0].replace("-", "")

path = os.path.join(DEFAULT_PATH, date)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

# ensure nothing gets overwritten
num = 0
length = len(file[0:-5])
while os.path.isfile(f"{DEFAULT_PATH}/{file}"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

file_name = (
f'{self.DEFAULT_PATH}/temp.fits'
if exptype == 'Real Time'
else getFilePath(None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

if self.DEBUGGING:
return jsonify({'success': True, 'filter': self.FILTER_DICT_REVERSE[self.DUMMY_FILTER_POSITION], 'error': ''})

status, reply = await send_to_wheel('get')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.


global DUMMY_FILTER_POSITION
status, reply = await send_to_wheel(f'move {filter_num}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

await asyncio.sleep(2)
DUMMY_FILTER_POSITION = filter_num
payload['success'] = True
status, reply = await send_to_wheel('home')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.

app.run(host='127.0.0.1', port=3000, debug=True, processes=1, threaded=True)
# TODO:
# - Add arg to download astrometry data
# - Put everything into a class to make globals more pythonic to access
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants