Skip to content

Commit

Permalink
Merge branch 'WWGolay:main' into maximTest
Browse files Browse the repository at this point in the history
  • Loading branch information
pgriffin17 authored Jan 21, 2024
2 parents ceda54b + f13040f commit 2c8f980
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ instance/

# Sphinx documentation
docs/_build/
docs/source/user_guide/observatory_info.csv

# PyBuilder
target/
Expand Down
4 changes: 2 additions & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ sphinx:
fail_on_warning: true

# Optionally build your docs in additional formats such as PDF and ePub
formats:
- pdf
# formats:
# - pdf

# Optional but recommended, declare the Python requirements required
# to build your documentation
Expand Down
11 changes: 11 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from packaging.version import parse
from sphinx_astropy.conf.v2 import *

sys.path.insert(0, pathlib.Path(__file__).parents[0].resolve().as_posix())

import headerCSVGenerator

sys.path.insert(0, pathlib.Path(__file__).parents[2].resolve().as_posix())

import pyscope
Expand Down Expand Up @@ -57,6 +61,13 @@

extensions = list(map(lambda x: x.replace("viewcode", "linkcode"), extensions))

# Generate CSV for header info
print("Generating CSV for header info...")
targetPath = os.path.join(
os.path.dirname(__file__), "user_guide", "observatory_info.csv"
)
headerCSVGenerator.HeaderCSVGenerator().generate_csv(targetPath)


def linkcode_resolve(domain, info):
"""
Expand Down
118 changes: 118 additions & 0 deletions docs/source/headerCSVGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import ast
import csv
import inspect
import re

from pyscope.observatory import Observatory


class HeaderCSVGenerator:
"""Generates a CSV file containing the header information for the Observatory class.
The CSV file contains the following columns:
- Header Key: The key of the header
- Header Value: The value of the header
- Header Description: The description of the header
The CSV file is generated by parsing the Observatory class for the info dictionaries
and then combining them into a master dictionary. The master dictionary is then
output to a CSV file.
"""

def __init__(self):
pass

def get_info_dicts(self):
descriptors = inspect.getmembers(
Observatory, predicate=inspect.isdatadescriptor
)
info = []
for descriptor in descriptors:
if "info" in descriptor[0]:
info.append(descriptor)

source_list = []
for descriptor in info:
source_list.append(inspect.getsource(descriptor[1].fget))

# Split source into lines, remove tabs
source_lines = []
for source in source_list:
source_lines.append(source.split("\n"))

# Remove leading whitespace
stripped_lines = []
for source in source_lines:
for line in source:
stripped_lines.append(line.lstrip())

# Return parts of source_list that contain 'info = {...}'
info_dict = []
for property in source_list:
# Use regex to find info = {[^}]} and add it to info_dict
info_dict.append(re.findall(r"info = {[^}]*}", property)[0])

# Remove 'info = ' from each string
for i, property in enumerate(info_dict):
info_dict[i] = property[7:]

# Encase any references to self. in quotes
for i, property in enumerate(info_dict):
info_dict[i] = re.sub(r"self\.([a-zA-Z0-9_.\[\]]+)", r'"\1"', property)

# Find references to str()
for i, property in enumerate(info_dict):
info_dict[i] = re.sub(r"(str\(([a-zA-Z\"\_\.\[\]]+)\))", r"\2", property)

# Replace any references to self.etc. with None
for i, property in enumerate(info_dict):
# Use regex "\(\s+(.*?\],)"gm
# info_dict[i] = re.sub(r'\(\\n\s+(.*?\],)', 'None', property)
group = re.findall(r"\(\n\s+([\s\S]*?\],)", property)
# replace the group with None
if group:
info_dict[i] = property.replace(group[0], "None,")

# Enclose any parts with (sep="dms") in quotes
for i, property in enumerate(info_dict):
info_dict[i] = re.sub(
r"(\"\S+\(sep=\"dms\"\))",
lambda m: '"' + m.group(1).replace('"', " ") + '"',
property,
)

# Remove any parts matching \% i(?=\)) (or replace with "")
for i, property in enumerate(info_dict):
info_dict[i] = re.sub(r"( \% i(?=\)))", "", property)

# Enclose in quotes any parts matching 'not \"\S+\" is None'
for i, property in enumerate(info_dict):
info_dict[i] = re.sub(
r"(not \"\S+\" is None)",
lambda m: '"' + m.group(1).replace('"', " ") + '"',
property,
)

# Pass each info_dict through ast.literal_eval to convert to dictionary
info_dict_parsed = []
for info in info_dict:
info_dict_parsed.append(ast.literal_eval(info))

return info_dict_parsed

def generate_csv(self, filename):
info_dicts = self.get_info_dicts()
# Add each dictionary to make a master dictionary
master_dict = {}
for info in info_dicts:
master_dict.update(info)
# Output to csv in the format key, value, description
# key is the key of the dictionary
# value is the first part of the tuple (the value)
# description is the second part of the tuple (the description)
with open(filename, "w", newline="") as csv_file:
writer = csv.writer(csv_file)
# Write header
writer.writerow(["Header Key", "Header Value", "Header Description"])
for key, value in master_dict.items():
writer.writerow([key, value[0], value[1]])
15 changes: 15 additions & 0 deletions docs/source/user_guide/header.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Header
======

This is a page listing all potential header keywords for an `Observatory` object.

.. Note::
This is auto-generated from the `info` dictionaries in the `Observatory` class. Some
of the information is not available for all observatories, and some header values may
contain nonsense due to the auto-generation script.

.. csv-table:: Sample Header
:file: observatory_info.csv
:widths: 4, 6, 10
:header-rows: 1

1 change: 1 addition & 0 deletions docs/source/user_guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ User Guide
:maxdepth: 2

examples
header
logging
config
help
Loading

0 comments on commit 2c8f980

Please sign in to comment.