• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RAMP-project / RAMP / 9364900772

04 Jun 2024 09:46AM UTC coverage: 77.375%. First build
9364900772

Pull #150

github

web-flow
Merge df93fb41e into 8b66734bb
Pull Request #150: Release v0.5.2

285 of 316 new or added lines in 17 files covered. (90.19%)

1409 of 1821 relevant lines covered (77.38%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.27
/ramp/ramp_convert_old_input_files.py
1
# %% Import required modules
2

3
import sys, os, importlib
1✔
4

5
sys.path.append("../")
1✔
6
import argparse
1✔
7
from ramp.core.core import UseCase
1✔
8

9
parser = argparse.ArgumentParser(
1✔
10
    prog="ramp_convert", description="Convert RAMP python input files to xlsx ones"
11
)
12
parser.add_argument(
1✔
13
    "-i",
14
    dest="fname_path",
15
    nargs="+",
16
    type=str,
17
    help="path to the input file (including filename)",
18
)
19
parser.add_argument(
1✔
20
    "-o", dest="output_path", type=str, help="path where to save the converted filename"
21
)
22
parser.add_argument(
1✔
23
    "--suffix",
24
    dest="suffix",
25
    type=str,
26
    help="suffix appended to the converted filename",
27
    default="",
28
)
29

30

31
convert_names = """
1✔
32
# Code automatically added by ramp_convert_old_input_files.py
33
from ramp.core.core import Appliance
34
local_var_names = [(i, a) for i, a in locals().items() if isinstance(a, Appliance)]
35
for i, a in local_var_names:
36
    a.name = i
37
"""
38

39

40
def convert_old_user_input_file(
1✔
41
    fname_path, output_path=None, suffix="", keep_names=True
42
):
43
    """Convert old RAMP python input files to xlsx ones
44

45
    The old (RAMP version < 0.5) .py input files defined all users and gathered them in a variable named User_list,
46
    this variable must be defined in the .py file to be converted to .xlsx.
47

48
    To convert a .py input file to an .xlsx using the UseCase objects, please refer to
49
    https://rampdemand.readthedocs.io/en/latest/examples/using_excel/using_excel.html#exporting-the-database
50

51
    Parameters
52
    ----------
53
    fname_path: path
54
        path to a .py ramp input file containing a variable named User_list
55
    output_path: path, optional
56
        path to the converted .xlsx ramp input file, by default the same folder as the .py file
57
    suffix: str, optional
58
        suffix to be added to the converted .xlsx ramp input file name, default ''
59
    keep_names: bool, optional
60
        keep the variable names of the Appliance instances as their 'name' attribute, default True
61

62
    """
63

64
    line_to_change = -1
1✔
65

66
    # Check if the lines to save the names of the Appliance instances is already there
67
    # And check if the import of the User class is correct, otherwise adapt the file
68
    with open(fname_path, "r") as fp:
1✔
69
        lines = fp.readlines()
1✔
70
        if "# Code automatically added by ramp_convert_old_input_files.py\n" in lines:
1✔
71
            keep_names = False
×
72
        for i, l in enumerate(lines):
1✔
73
            if "from core import" in l:
1✔
74
                line_to_change = i
×
75
        # Change import statement by explicitly naming the full package path
76
        lines[line_to_change] = lines[line_to_change].replace(
1✔
77
            "from core import", "from ramp.core.core import"
78
        )
79

80
    # Modify import statement in file
81
    if line_to_change != -1:
1✔
82
        with open(fname_path, "w") as fp:
×
83
            fp.writelines(lines)
×
84

85
    # Add code to the input file to assign the variable name of the Appliance instances to their name attribute
86
    if keep_names is True:
1✔
87
        with open(fname_path, "a") as fp:
1✔
88
            fp.write(convert_names)
1✔
89

90
    fname = fname_path.replace(".py", "").replace(os.path.sep, ".")
1✔
91

92
    if output_path is None:
1✔
93
        output_path = os.path.dirname(fname_path)
×
94
    output_fname = fname_path.split(os.path.sep)[-1].replace(".py", suffix)
1✔
95
    output_fname = os.path.join(output_path, output_fname)
1✔
96

97
    if os.name != "posix":
1✔
NEW
98
        sys.path.insert(0, os.path.dirname(os.path.abspath(fname_path)))
×
99
    file_module = importlib.import_module(fname)
1✔
100

101
    user_list = file_module.User_list
1✔
102

103
    UseCase(users=user_list).save(output_fname)
1✔
104

105

106
def cli():
1✔
107
    args = vars(parser.parse_args())
×
108
    fname = args["fname_path"]
×
109
    output_path = args.get("output_path")
×
110
    suffix = args.get("suffix")
×
111

112
    if fname is None:
×
113
        print("Please provide path to input file with option -i")
×
114
    else:
115
        if isinstance(fname, list):
×
116
            fnames = fname
×
117
            for fname in fnames:
×
118
                convert_old_user_input_file(
×
119
                    fname, output_path=output_path, suffix=suffix
120
                )
121
        else:
122
            convert_old_user_input_file(fname, output_path=output_path, suffix=suffix)
×
123

124

125
if __name__ == "__main__":
1✔
NEW
126
    cli()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc