Skip to content

Commit

Permalink
started fixing the config as PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ-Author committed Oct 18, 2024
1 parent 706f826 commit dea423d
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions ontologytimemachine/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,89 @@
import argparse
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from typing import Dict, Any
from typing import Dict, Any, Type, TypeVar


class LogLevel(Enum):
class EnumValuePrint(Enum): # redefine how the enum is printed such that it will show up properly the cmd help message (choices)
def __str__(self):
return self.value

class LogLevel(EnumValuePrint):
DEBUG = "debug"
INFO = "info"
WARNING = "warning"
ERROR = "error"


class OntoFormat(Enum):
class OntoFormat(EnumValuePrint):
TURTLE = "turtle"
NTRIPLES = "ntriples"
RDFXML = "rdfxml"
HTMLDOCU = "htmldocu"


class OntoPrecedence(Enum):
class OntoPrecedence(EnumValuePrint):
DEFAULT = "default"
ENFORCED_PRIORITY = "enforcedPriority"
ALWAYS = "always"


class OntoVersion(Enum):
class OntoVersion(EnumValuePrint):
ORIGINAL = "original"
ORIGINAL_FAILOVER_LIVE_LATEST = "originalFailoverLiveLatest"
LATEST_ARCHIVED = "latestArchived"
TIMESTAMP_ARCHIVED = "timestampArchived"
DEPENDENCY_MANIFEST = "dependencyManifest"


class HttpsInterception(Enum):
class HttpsInterception(EnumValuePrint):
NONE = "none"
ALL = "all"
BLOCK = "block"
ARCHIVO = "archivo"

@dataclass
class OntoFormatConfig:
format: OntoFormat = OntoFormat.NTRIPLES
precedence: OntoPrecedence = OntoPrecedence.ENFORCED_PRIORITY
patchAcceptUpstream: bool = False

@dataclass
class Config:
logLevel: LogLevel = LogLevel.INFO
ontoFormat: Dict[str, Any] = None
ontoVersion: OntoVersion = (OntoVersion.ORIGINAL_FAILOVER_LIVE_LATEST,)
ontoFormatConf: OntoFormatConfig = field(default_factory=OntoFormatConfig)
ontoVersion: OntoVersion = OntoVersion.ORIGINAL_FAILOVER_LIVE_LATEST
restrictedAccess: bool = False
httpsInterception: HttpsInterception = (HttpsInterception.ALL,)
httpsInterception: HttpsInterception = HttpsInterception.ALL
disableRemovingRedirects: bool = False
timestamp: str = ""
# manifest: Dict[str, Any] = None


def enum_parser(enum_class, value):
# Define a TypeVar for the enum class
E = TypeVar('E', bound=Enum)
def enum_parser(enum_class: Type[E], value: str) -> E:
value_lower = value.lower()
try:
return next(e.value for e in enum_class if e.value.lower() == value_lower)
except StopIteration:
return next(e for e in enum_class if e.value.lower() == value_lower)
except StopIteration as exc:
valid_options = ", ".join([e.value for e in enum_class])
raise ValueError(
raise argparse.ArgumentTypeError(
f"Invalid value '{value}'. Available options are: {valid_options}"
)
) from exc


def parse_arguments() -> Config:
default_cfg : Config = Config()
parser = argparse.ArgumentParser(description="Process ontology format and version.")

# Defining ontoFormat argument with nested options
parser.add_argument(
"--ontoFormat",
type=lambda s: enum_parser(OntoFormat, s),
default=OntoFormat.TURTLE.value,
default=default_cfg.ontoFormatConf.format,
choices=list(OntoFormat),
help="Format of the ontology: turtle, ntriples, rdfxml, htmldocu",
)

parser.add_argument(
"--ontoPrecedence",
type=lambda s: enum_parser(OntoPrecedence, s),
default=OntoPrecedence.ENFORCED_PRIORITY.value,
default=OntoPrecedence.ENFORCED_PRIORITY,
help="Precedence of the ontology: default, enforcedPriority, always",
)

Expand Down Expand Up @@ -148,22 +155,20 @@ def parse_arguments() -> Config:
# else:
# manifest = None

# Create ontoFormat dictionary
ontoFormat = {
"format": args.ontoFormat,
"precedence": args.ontoPrecedence,
"patchAcceptUpstream": args.patchAcceptUpstream,
}
#print the default configuration with all nested members
print(default_cfg) #TODO remove

# Initialize the Config class with parsed arguments
config = Config(
logLevel=args.logLevel,
ontoFormat=ontoFormat,
ontoFormatConf=OntoFormatConfig(args.ontoFormat, args.ontoPrecedence, args.patchAcceptUpstream),
ontoVersion=args.ontoVersion,
restrictedAccess=args.restrictedAccess,
httpsInterception=args.httpsInterception,
disableRemovingRedirects=args.disableRemovingRedirects,
timestamp=args.timestamp if hasattr(args, "timestamp") else "",
)

print(config) #TODO remove

return config

0 comments on commit dea423d

Please sign in to comment.