Skip to content

Commit

Permalink
Updated logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Mar 2, 2023
1 parent b499350 commit 51b2f3c
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 215 deletions.
24 changes: 18 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
"version": "0.2.0",
"configurations": [
{
"name":"E4EDM Status",
"type":"python",
"request":"launch",
"module":"e4e_data_management.cli",
"justMyCode":true,
"args": ["status"]
"name": "E4EDM Prune",
"type": "python",
"request": "launch",
"module": "e4e_data_management.cli",
"justMyCode": true,
"args": [
"prune"
]
},
{
"name": "E4EDM Status",
"type": "python",
"request": "launch",
"module": "e4e_data_management.cli",
"justMyCode": true,
"args": [
"status"
]
}
]
}
117 changes: 116 additions & 1 deletion e4e_data_management/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,125 @@
'''E4E Data Management Core
'''
import argparse
import datetime as dt
import sys
from pathlib import Path
from typing import Callable, Dict, List

from e4e_data_management.initialization import init_dataset, init_mission
from e4e_data_management.core import DataManager
from e4e_data_management.metadata import Metadata


def init_dataset(args: List[str]) -> None:
"""initialize_dataset command line interface
Args:
args (List[str]): Command Line Arguments
"""
app = DataManager()
parser = argparse.ArgumentParser(
prog='e4edm init_dataset'
)
parser.add_argument('--date', '-d',
help='Date of expedition (YY.MM)',
required=True,
type=dt.date.fromisoformat)
parser.add_argument('--project', '-p',
help='Project',
required=True)
parser.add_argument('--location', '-l',
help='Expedition location (common name)',
required=True)
parser.add_argument('--path',
default=Path('.'),
type=Path,
help='Dataset location, defaults to current directory')

args = parser.parse_args(args=args)
date: dt.date = args.date
project: str = args.project
location: str = args.location
directory: Path = args.path
app.initialize_dataset(
date=date,
project=project,
location=location,
directory=directory
)

def init_mission(args: List[str]) -> None:
"""`init_mission` command line interface
Args:
args (List[str]): Arguments
"""
app = DataManager()

if app.active_dataset is None:
print('No dataset active!')
return

parser = argparse.ArgumentParser(
prog='e4edm init_mission'
)
parser.add_argument('--timestamp', '-t',
help='Mission timestamp',
required=True,
type=dt.datetime.fromisoformat)
parser.add_argument('--device', '-d',
help='Mission device identifier',
required=True,
type=str)
if app.active_dataset.last_country:
parser.add_argument('--country', '-c',
help='Mission country',
required=False,
default=app.active_dataset.last_country)
else:
parser.add_argument('--country', '-c',
help='Mission country',
required=True)

if app.active_dataset.last_region:
parser.add_argument('--region', '-r',
help='Mission region',
required=False,
default=app.active_dataset.last_region)
else:
parser.add_argument('--region', '-r',
help='Mission region',
required=True)

if app.active_dataset.last_site:
parser.add_argument('--site', '-s',
help='Mission site',
required=False,
default=app.active_dataset.last_site)
else:
parser.add_argument('--site', '-s',
help='Mission site',
required=True)

parser.add_argument('--name', '-n',
help='Mission name',
required=True)
parser.add_argument('--message', '-m',
help='Mission notes')
args = parser.parse_args(args=args)

metadata = Metadata(
timestamp=args.timestamp,
device=args.device,
country=args.country,
region=args.region,
site=args.site,
mission=args.name,
notes=args.message
)
app.initialize_mission(
metadata=metadata
)


def print_help():
"""Prints the top level help
Expand Down
45 changes: 1 addition & 44 deletions e4e_data_management/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,12 @@
from __future__ import annotations

import pickle
import datetime as dt
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Optional, List
from typing import Dict, Optional

import appdirs

@dataclass
class ExpeditionConfiguration:
"""Expedition configuration parameters
Returns:
ExpeditionConfiguration: Configuraton parameters
"""
root_path: Path
day_0: dt.date
last_country: str = ''
last_region: str = ''
last_site: str = ''
countries: List[str] = field(default_factory=list)
regions: List[str] = field(default_factory=list)
sites: List[str] = field(default_factory=list)

@classmethod
def load(cls, path: Path) -> ExpeditionConfiguration:
"""Loads the configuration from disk
Args:
path (Path): Path to expedition root
Returns:
ExpeditionConfiguration: Configuration
"""
config_dir = path

config_file = config_dir.joinpath('.e4edm.pkl')
with open(config_file, 'rb') as handle:
return pickle.load(handle)

def save(self, path: Path) -> None:
"""Saves the configuration to disk
"""
config_dir = path

config_file = config_dir.joinpath('.e4edm.pkl')
if not config_file.exists():
config_file.parent.mkdir(parents=True, exist_ok=True)
with open(config_file, 'wb') as handle:
pickle.dump(self, handle)

@dataclass
class AppConfiguration:
Expand Down
52 changes: 17 additions & 35 deletions e4e_data_management/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
import datetime as dt
import logging
from pathlib import Path
from typing import Dict, Optional, List
from typing import Optional, List

from e4e_data_management.config import (AppConfiguration,
ExpeditionConfiguration)
from e4e_data_management.config import AppConfiguration
from e4e_data_management.data import Dataset

from e4e_data_management.metadata import Metadata

class DataManager:
"""Data Manager Application Core
"""
def __init__(self, *, app_config_dir: Optional[Path] = None):
self.__log = logging.getLogger('DataManager')
self.appconfig = AppConfiguration.get_instance(config_dir=app_config_dir)
self.active_dataset: Optional[Dataset] = None
self.dataset_config: Optional[ExpeditionConfiguration] = None
if self.appconfig.current_dataset:
try:
self.active_dataset = Dataset(self.appconfig.current_dataset)
self.dataset_config = ExpeditionConfiguration.load(self.appconfig.current_dataset)
self.active_dataset = Dataset.load(self.appconfig.current_dataset)
except Exception: # pylint: disable=broad-except
self.__log.error('Failed to load dataset %s', self.appconfig.current_dataset_name)
self.active_dataset = None
self.dataset_config = None
self.appconfig.current_dataset = None
self.appconfig.current_dataset_name = None
self.appconfig.current_mission = None
Expand All @@ -41,49 +39,32 @@ def initialize_dataset(self, date: dt.date, project: str, location: str, directo
"""
dataset_name = f'{date.year:04d}.{date.month:02d}.{project}.{location}'
dataset_path = directory.joinpath(dataset_name)
dataset_path.mkdir(parents=True, exist_ok=True)

self.appconfig.add_dataset(
name=dataset_name,
path=dataset_path.absolute()
)

self.active_dataset = Dataset(dataset_path.absolute())
self.active_dataset.generate_manifest()

expedition_config = ExpeditionConfiguration(
root_path=dataset_path.absolute(),
self.active_dataset = Dataset(
root=dataset_path.absolute(),
day_0=date
)
expedition_config.save(dataset_path)
self.active_dataset.create()
self.active_dataset.save()

def initialize_mission(self,
timestamp: dt.datetime,
device: str,
country: str,
region: str,
site: str,
mission: str,
notes: str = '',
properties: Optional[Dict] = None) -> None:
metadata: Metadata) -> None:
"""Initializes a new mission. This should create the appropriate folder structure and
open the staging area
Args:
timestamp (dt.datetime): Mission timestamp
device (str): Mission device
country (str): Mission country
region (str): Mission region
site (str): Mission site
mission (str): Mission name
notes (str, optional): Mission notes. Defaults to ''.
properties (Optional[Dict], optional): Additional properties. Defaults to None.
metadata (Metadata): Mission metadata
"""
if self.active_dataset is None:
raise RuntimeError('Dataset not active')
expedition_day = (timestamp.date() - self.dataset_config.day_0).days
day_path = Path(self.dataset_config.root_path, f'ED-{expedition_day:02d}')
mission_path = day_path.joinpath(mission)
self.active_dataset.add_mission(
metadata=metadata
)

def status(self) -> str:
"""Generates a status string
Expand Down Expand Up @@ -184,7 +165,8 @@ def prune(self) -> None:
"""
items_to_remove: List[str] = []
for name, path in self.appconfig.datasets.items():
if not path.is_dir():
if not path.exists():
items_to_remove.append(name)
for remove in items_to_remove:
self.appconfig.datasets.pop(remove)
self.appconfig.save()
Loading

0 comments on commit 51b2f3c

Please sign in to comment.