Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions examples/ESRF_tune_example/esrf_tune_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from pyaml.pyaml import pyaml,PyAML
from pyaml.instrument import Instrument
from pyaml.configuration.factory import Factory
from pyaml.accelerator import Accelerator
import numpy as np
import os

Expand All @@ -14,8 +12,7 @@
# Normalize the path (resolves '..')
absolute_path = os.path.abspath(relative_path)

ml:PyAML = pyaml(absolute_path)
sr:Instrument = ml.get('sr')
sr:Accelerator = Accelerator.load(absolute_path)
sr.design.get_lattice().disable_6d()

quadForTuneDesign = sr.design.get_magnets("QForTune")
Expand Down Expand Up @@ -52,6 +49,3 @@
strs = quadForTuneLive.strengths.get()
strs += np.matmul(correctionmat,[0.1,0.05]) # Ask for correction [dqx,dqy]
quadForTuneLive.strengths.set(strs)


Factory.clear()
9 changes: 7 additions & 2 deletions pyaml/instrument.py → pyaml/accelerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from .lattice.simulator import Simulator
from .arrays.array import ArrayConfig
from pydantic import BaseModel,ConfigDict
from .configuration import load_accelerator

# Define the main class name for this module
PYAMLCLASS = "Instrument"
PYAMLCLASS = "Accelerator"

class ConfigModel(BaseModel):

Expand All @@ -31,7 +32,7 @@ class ConfigModel(BaseModel):
"""Element list"""


class Instrument(object):
class Accelerator(object):
"""PyAML top level class"""

def __init__(self, cfg: ConfigModel):
Expand Down Expand Up @@ -85,3 +86,7 @@ def live(self) -> ControlSystem:
@property
def design(self) -> Simulator:
return self.__design

@staticmethod
def load(filename:str) -> "Accelerator":
return load_accelerator(filename)
21 changes: 1 addition & 20 deletions pyaml/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,5 @@
PyAML configuration module
"""

from pathlib import Path
from typing import Union

ROOT = {"path": Path.cwd().resolve()}


def set_root_folder(path: Union[str, Path]):
"""
Set the root path for configuration files.
"""
ROOT["path"] = Path(path)


def get_root_folder() -> Path:
"""
Get the root path for configuration files.
"""
return ROOT["path"]

from .fileloader import load
from .fileloader import load_accelerator, set_root_folder, get_root_folder
from .factory import Factory
40 changes: 38 additions & 2 deletions pyaml/configuration/fileloader.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
# PyAML config file loader
import logging
import json
from typing import Union
from typing import Union, TYPE_CHECKING
from pathlib import Path
import io
import os

import yaml
from yaml.loader import SafeLoader
from yaml.constructor import ConstructorError
import collections.abc

from . import get_root_folder
from .. import PyAMLException
from pyaml.configuration.factory import Factory

if TYPE_CHECKING:
from pyaml.accelerator import Accelerator


logger = logging.getLogger(__name__)

accepted_suffixes = [".yaml", ".yml", ".json"]


ROOT = {"path": Path.cwd().resolve()}


def set_root_folder(path: Union[str, Path]):
"""
Set the root path for configuration files.
"""
ROOT["path"] = Path(path)


def get_root_folder() -> Path:
"""
Get the root path for configuration files.
"""
return ROOT["path"]

class PyAMLConfigCyclingException(PyAMLException):

def __init__(self, error_filename:str, path_stack:list[Path]):
Expand All @@ -25,6 +47,20 @@ def __init__(self, error_filename:str, path_stack:list[Path]):
super().__init__(f"Circular file inclusion of {error_filename}. File list before reaching it: {parent_file_stack}")
pass

def load_accelerator(filename:str, paths_stack:list=None) -> "Accelerator":
""" Load an instrument from file."""

# Asume that all files are referenced from folder where main AML file is stored
if not os.path.exists(filename):
raise PyAMLException(f"{filename} file not found")
rootfolder = os.path.abspath(os.path.dirname(filename))
set_root_folder(rootfolder)
config_dict = load(os.path.basename(filename))
aml = Factory.depth_first_build(config_dict)

Factory.clear()
return aml

def load(filename:str, paths_stack:list=None) -> Union[dict,list]:
"""Load recursively a configuration setup"""
if filename.endswith(".yaml") or filename.endswith(".yml"):
Expand Down
47 changes: 0 additions & 47 deletions pyaml/pyaml.py

This file was deleted.

Loading
Loading