Skip to content

Commit e773d0c

Browse files
authored
Split the actions (#108)
1 parent 9dce995 commit e773d0c

File tree

13 files changed

+1740
-1688
lines changed

13 files changed

+1740
-1688
lines changed

mlc/CacheAction.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from .action import Action
2+
import os
3+
import json
4+
from . import utils
5+
from .logger import logger
6+
7+
class CacheAction(Action):
8+
9+
def __init__(self, parent=None):
10+
#super().__init__(parent)
11+
self.parent = parent
12+
self.__dict__.update(vars(parent))
13+
14+
def search(self, i):
15+
i['target_name'] = "cache"
16+
#logger.debug(f"Searching for cache with input: {i}")
17+
return self.parent.search(i)
18+
19+
find = search
20+
21+
def rm(self, i):
22+
i['target_name'] = "cache"
23+
#logger.debug(f"Removing cache with input: {i}")
24+
return self.parent.rm(i)
25+
26+
def show(self, run_args):
27+
self.action_type = "cache"
28+
res = self.search(run_args)
29+
logger.info(f"Showing cache with tags: {run_args.get('tags')}")
30+
cached_meta_keys_to_show = ["uid", "tags", "dependent_cached_path", "associated_script_item"]
31+
cached_state_keys_to_show = ["new_env", "new_state", "version"]
32+
for item in res['list']:
33+
print(f"""Location: {item.path}:
34+
Cache Meta:""")
35+
for key in cached_meta_keys_to_show:
36+
if key in item.meta:
37+
print(f""" {key}: {item.meta[key]}""")
38+
print("""Cached State:""")
39+
cached_state_meta_file = os.path.join(item.path, "mlc-cached-state.json")
40+
if not os.path.exists(cached_state_meta_file):
41+
continue
42+
try:
43+
# Load and parse the JSON file containing the cached state
44+
with open(cached_state_meta_file, 'r') as file:
45+
meta = json.load(file)
46+
for key in cached_state_keys_to_show:
47+
if key in meta:
48+
print(f""" {key}:""", end="")
49+
if meta[key] and isinstance(meta[key], dict):
50+
print("")
51+
utils.printd(meta[key], yaml=False, sort_keys=True, begin_spaces=8)
52+
else:
53+
print(f""" {meta[key]}""")
54+
except json.JSONDecodeError as e:
55+
logger.error(f"Error decoding JSON: {e}")
56+
print("......................................................")
57+
print("")
58+
59+
return {'return': 0}
60+
61+
def list(self, args):
62+
self.action_type = "cache"
63+
run_args = {"fetch_all": True} # to fetch the details of all the caches generated
64+
65+
res = self.search(run_args)
66+
if res['return'] > 0:
67+
return res
68+
69+
logger.info(f"Listing all the caches and their paths")
70+
print("......................................................")
71+
for item in res['list']:
72+
print(f"tags: {item.meta['tags'] if item.meta.get('tags') else 'None'}")
73+
print(f"Location: {item.path}")
74+
print("......................................................")
75+
76+
return {'return': 0}

mlc/CfgAction.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import yaml
2+
import os
3+
from . import utils
4+
from .action import Action, default_parent
5+
from .logger import logger
6+
7+
class CfgAction(Action):
8+
def __init__(self, parent=None):
9+
if parent is None:
10+
parent = default_parent
11+
#super().__init__(parent)
12+
self.parent = parent
13+
self.__dict__.update(vars(parent))
14+
15+
def load(self, args):
16+
"""
17+
Load the configuration.
18+
19+
Args:
20+
args (dict): Contains the configuration details such as file path, etc.
21+
"""
22+
#logger.info("In cfg load")
23+
default_config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.yaml')
24+
config_file = args.get('config_file', default_config_path)
25+
logger.info(f"In cfg load, config file = {config_file}")
26+
if not config_file or not os.path.exists(config_file):
27+
logger.error(f"Error: Configuration file '{config_file}' not found.")
28+
return {'return': 1, 'error': f"Error: Configuration file '{config_file}' not found."}
29+
30+
#logger.info(f"Loading configuration from {config_file}")
31+
32+
# Example loading YAML configuration (can be modified based on your needs)
33+
try:
34+
with open(config_file, 'r') as file:
35+
config_data = yaml.safe_load(file)
36+
logger.info(f"Loaded configuration: {config_data}")
37+
# Store configuration in memory or perform other operations
38+
self.cfg = config_data
39+
except yaml.YAMLError as e:
40+
logger.error(f"Error loading YAML configuration: {e}")
41+
42+
return {'return': 0, 'config': self.cfg}

mlc/ExperimentAction.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from .action import Action, default_parent
2+
from .logger import logger
3+
import os
4+
from . import utils
5+
6+
class ExperimentAction(Action):
7+
def __init__(self, parent=None):
8+
if parent is None:
9+
parent = default_parent
10+
#super().__init__(parent)
11+
self.parent = parent
12+
self.__dict__.update(vars(parent))
13+
14+
def show(self, args):
15+
logger.info(f"Showing experiment with identifier: {args.details}")
16+
return {'return': 0}
17+
18+
def list(self, args):
19+
logger.info("Listing all experiments.")
20+
return {'return': 0}
21+

0 commit comments

Comments
 (0)