Skip to content

Commit

Permalink
refactor: cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau committed Oct 29, 2024
1 parent 2186a91 commit a0d5fba
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 145 deletions.
54 changes: 27 additions & 27 deletions inventory/ee_class_list.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
[
"Array",
"Blob",
"Classifier",
"Clusterer",
"ConfusionMatrix",
"Date",
"DateRange",
"Dictionary",
"ErrorMargin",
"Feature",
"FeatureCollection",
"Filter",
"Geometry",
"Image",
"ImageCollection",
"Join",
"Kernel",
"List",
"Model",
"Number",
"PixelType",
"Projection",
"Reducer",
"String",
"Terrain",
"Algorithms"
]
"Array",
"Blob",
"Classifier",
"Clusterer",
"ConfusionMatrix",
"Date",
"DateRange",
"Dictionary",
"ErrorMargin",
"Feature",
"FeatureCollection",
"Filter",
"Geometry",
"Image",
"ImageCollection",
"Join",
"Kernel",
"List",
"Model",
"Number",
"PixelType",
"Projection",
"Reducer",
"String",
"Terrain",
"Algorithms"
]
134 changes: 75 additions & 59 deletions inventory/generate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""The code used to generate the sphinx inventory of the GEE objects"""
import json
from pathlib import Path
"""The code used to generate the sphinx inventory of the GEE objects."""

import inspect
import json
import logging
from pathlib import Path

import sphobjinv as soi
import ee
import sphobjinv as soi

HERE = Path(__file__).parent
"""The current folder"""
Expand All @@ -24,99 +25,114 @@

# add the main module manually
logger.info("Adding the main module")
inv.objects.append(soi.DataObjStr(
name = ee.__name__,
domain = "py",
role = str(type(ee)),
priority = "1",
uri = "",
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=ee.__name__,
domain="py",
role=str(type(ee)),
priority="1",
uri="",
dispname="-",
)
)

# Not all the objects from ee should be added to the inventory as most of them are undocumented
# the list have been carefully built manually and saved in a json file. For each object, all the methods
# are added to the inventory. Some of them might not be documented but they will be removed thanks to
# user feedback.
# the list have been carefully built manually and saved in a json file. For each object, all the
# methods are added to the inventory. Some of them might not be documented but they will be removed
# thanks to user feedback.
logger.info("Adding the main classes and methods")
class_list = json.loads((HERE / "ee_class_list.json").read_text())
public_members = lambda obj: [o for n, o in inspect.getmembers(obj) if not n.startswith("_")]
for class_name in class_list:

# first inject the class itself
cls = getattr(ee, class_name)
atoms = ["ee", class_name]
inv.objects.append(soi.DataObjStr(
name = ".".join(atoms),
domain = "py",
role = "class",
priority = "1",
uri = "-".join(atoms).lower(),
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=".".join(atoms),
domain="py",
role="class",
priority="1",
uri="-".join(atoms).lower(),
dispname="-",
)
)

# then all its downstream methods
method_list = public_members(cls)
for meth in method_list:
atoms = ["ee", class_name, meth.__name__]
inv.objects.append(soi.DataObjStr(
name = ".".join(atoms),
domain = "py",
role = "method",
priority = "1",
uri = "-".join(atoms).lower(),
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=".".join(atoms),
domain="py",
role="method",
priority="1",
uri="-".join(atoms).lower(),
dispname="-",
)
)

# Initialize and Authenticate even if they start with a capital letter are not objects but
# functions that must be documented
logger.info("Adding the 2 oauth functions")
for func in ["Initialize", "Authenticate"]:
atoms = ["ee", func]
inv.objects.append(soi.DataObjStr(
name = ".".join(atoms),
domain = "py",
role = "function",
priority = "1",
uri = "-".join(atoms).lower(),
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=".".join(atoms),
domain="py",
role="function",
priority="1",
uri="-".join(atoms).lower(),
dispname="-",
)
)

# From the ee.data module, only the methods should be added into the inventory as the rest is
# undocumented so it reduces vastly the number of items to update
logger.info("Adding the data module")
meth_list = [o for n, o in inspect.getmembers(ee.data) if not (n.startswith("_") or inspect.isclass(o) or n[0].isupper())]
meth_list = [
o
for n, o in inspect.getmembers(ee.data)
if not (n.startswith("_") or inspect.isclass(o) or n[0].isupper())
]
for meth in meth_list:
atoms = ["ee", "data", meth.__name__]
inv.objects.append(soi.DataObjStr(
name = ".".join(atoms),
domain = "py",
role = "method",
priority = "1",
uri = "-".join(atoms).lower(),
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=".".join(atoms),
domain="py",
role="method",
priority="1",
uri="-".join(atoms).lower(),
dispname="-",
)
)

# batch module is only embeding 5 abstract classes that manage exportation processes.
# batch module is only embedding 5 abstract classes that manage exportation processes.
# The rest should remain undocumented
logger.info("Adding the batch module")
batch_classes = public_members(ee.batch.Export)
for cls in batch_classes:
method_list = public_members(cls)
for meth in method_list:
atoms = ["ee", "batch", "Export", cls.__name__, meth.__name__]
inv.objects.append(soi.DataObjStr(
name = ".".join(atoms),
domain = "py",
role = "method",
priority = "1",
uri = "-".join(atoms[2:]).lower(),
dispname = "-",
))
inv.objects.append(
soi.DataObjStr(
name=".".join(atoms),
domain="py",
role="method",
priority="1",
uri="-".join(atoms[2:]).lower(),
dispname="-",
)
)

# export the file as a inventory file in the repository
logger.info("Writing the inventory file")
text = inv.data_file(contract=True)
ztext = soi.compress(text)
soi.writebytes(str(HERE/"earthengine-api.inv"), ztext)
soi.writebytes(str(HERE / "earthengine-api.inv"), ztext)

logger.info(f"Inventory file generated with {len(inv.objects)} objects.")
7 changes: 4 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""All the process that can be run using nox.
The nox run are build in isolated environment that will be stored in .nox. to force the venv update, remove the .nox/xxx folder.
The nox run are build in isolated environment that will be stored in .nox. to force the venv
update, remove the .nox/xxx folder.
"""

import nox
Expand All @@ -15,16 +16,16 @@ def lint(session):
session.run("pre-commit", "run", "--all-files", *session.posargs)



@nox.session(reuse_venv=True)
def mypy(session):
"""Run a mypy check of the lib."""
session.install("mypy")
test_files = session.posargs or ["inventory"]
session.run("mypy", *test_files)


@nox.session(reuse_venv=True)
def generate(session):
"""Generate the inventory file."""
session.install("-r", "./requirements.txt")
session.run("python", "inventory/generate.py", silent=False)
session.run("python", "inventory/generate.py", silent=False)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ignore-init-module-imports = true
fix = true
select = ["E", "F", "W", "I", "D", "RUF"]
ignore = [
"E501", # line too long | Black take care of it
"D212", # Multi-line docstring | We use D213
"E731", # Do not assign a `lambda` expression, use a `def` | I prefer to use lambda
]

[tool.ruff.flake8-quotes]
Expand Down
Loading

0 comments on commit a0d5fba

Please sign in to comment.