Coverage for HARK/helpers.py: 0%
47 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-02 05:14 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-02 05:14 +0000
1"""
2Functions for manipulating the file system or environment.
3"""
5# ------------------------------------------------------------------------------
6# Code to copy entire modules to a local directory
7# ------------------------------------------------------------------------------
10# Define a function to run the copying:
11def copy_module(target_path, my_directory_full_path, my_module):
12 """
13 Helper function for copy_module_to_local(). Provides the actual copy
14 functionality, with highly cautious safeguards against copying over
15 important things.
17 Parameters
18 ----------
19 target_path : string
20 String, file path to target location
22 my_directory_full_path: string
23 String, full pathname to this file's directory
25 my_module : string
26 String, name of the module to copy
28 Returns
29 -------
30 none
31 """
33 if target_path == "q" or target_path == "Q":
34 print("Goodbye!")
35 return
36 elif target_path == os.path.expanduser("~") or os.path.normpath(
37 target_path
38 ) == os.path.expanduser("~"):
39 print(
40 "You have indicated that the target location is "
41 + target_path
42 + " -- that is, you want to wipe out your home directory with the contents of "
43 + my_module
44 + ". My programming does not allow me to do that.\n\nGoodbye!"
45 )
46 return
47 elif os.path.exists(target_path):
48 print(
49 "There is already a file or directory at the location "
50 + target_path
51 + ". For safety reasons this code does not overwrite existing files.\n Please remove the file at "
52 + target_path
53 + " and try again."
54 )
55 return
56 else:
57 user_input = input(
58 """You have indicated you want to copy module:\n """
59 + my_module
60 + """\nto:\n """
61 + target_path
62 + """\nIs that correct? Please indicate: y / [n]\n\n"""
63 )
64 if user_input == "y" or user_input == "Y":
65 # print("copy_tree(",my_directory_full_path,",", target_path,")")
66 copy_tree(my_directory_full_path, target_path)
67 else:
68 print("Goodbye!")
69 return
72def print_helper():
73 my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
75 print(my_directory_full_path)
78def copy_module_to_local(full_module_name):
79 """
80 This function contains simple code to copy a submodule to a location on
81 your hard drive, as specified by you. The purpose of this code is to provide
82 users with a simple way to access a *copy* of code that usually sits deep in
83 the Econ-ARK package structure, for purposes of tinkering and experimenting
84 directly. This is meant to be a simple way to explore HARK code. To interact
85 with the codebase under active development, please refer to the documentation
86 under github.com/econ-ark/HARK/
88 To execute, do the following on the Python command line:
90 from HARK.core import copy_module_to_local
91 copy_module_to_local("FULL-HARK-MODULE-NAME-HERE")
93 For example, if you want SolvingMicroDSOPs you would enter
95 from HARK.core import copy_module_to_local
96 copy_module_to_local("HARK.SolvingMicroDSOPs")
98 """
100 # Find a default directory -- user home directory:
101 home_directory_RAW = os.path.expanduser("~")
102 # Thanks to https://stackoverflow.com/a/4028943
104 # Find the directory of the HARK.core module:
105 # my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
106 hark_core_directory_full_path = os.path.dirname(os.path.realpath(__file__))
107 # From https://stackoverflow.com/a/5137509
108 # Important note from that answer:
109 # (Note that the incantation above won't work if you've already used os.chdir()
110 # to change your current working directory,
111 # since the value of the __file__ constant is relative to the current working directory and is not changed by an
112 # os.chdir() call.)
113 #
114 # NOTE: for this specific file that I am testing, the path should be:
115 # '/home/npalmer/anaconda3/envs/py3fresh/lib/python3.6/site-packages/HARK/SolvingMicroDSOPs/---example-file---
117 # Split out the name of the module. Break if proper format is not followed:
118 all_module_names_list = full_module_name.split(
119 "."
120 ) # Assume put in at correct format
121 if all_module_names_list[0] != "HARK":
122 print(
123 "\nWarning: the module name does not start with 'HARK'. Instead it is: '"
124 + all_module_names_list[0]
125 + "' --please format the full namespace of the module you want. \n"
126 "For example, 'HARK.SolvingMicroDSOPs'"
127 )
128 print("\nGoodbye!")
129 return
131 # Construct the pathname to the module to copy:
132 my_directory_full_path = hark_core_directory_full_path
133 for a_directory_name in all_module_names_list[1:]:
134 my_directory_full_path = os.path.join(my_directory_full_path, a_directory_name)
136 head_path, my_module = os.path.split(my_directory_full_path)
138 home_directory_with_module = os.path.join(home_directory_RAW, my_module)
140 print("\n\n\nmy_directory_full_path:", my_directory_full_path, "\n\n\n")
142 # Interact with the user:
143 # - Ask the user for the target place to copy the directory
144 # - Offer use "q/y/other" option
145 # - Check if there is something there already
146 # - If so, ask if should replace
147 # - If not, just copy there
148 # - Quit
150 target_path = input(
151 """You have invoked the 'replicate' process for the current module:\n """
152 + my_module
153 + """\nThe default copy location is your home directory:\n """
154 + home_directory_with_module
155 + """\nPlease enter one of the three options in single quotes below, excluding the quotes:
157 'q' or return/enter to quit the process
158 'y' to accept the default home directory: """
159 + home_directory_with_module
160 + """
161 'n' to specify your own pathname\n\n"""
162 )
164 if target_path == "n" or target_path == "N":
165 target_path = input(
166 """Please enter the full pathname to your target directory location: """
167 )
169 # Clean up:
170 target_path = os.path.expanduser(target_path)
171 target_path = os.path.expandvars(target_path)
172 target_path = os.path.normpath(target_path)
174 # Check to see if they included the module name; if not add it here:
175 temp_head, temp_tail = os.path.split(target_path)
176 if temp_tail != my_module:
177 target_path = os.path.join(target_path, my_module)
179 elif target_path == "y" or target_path == "Y":
180 # Just using the default path:
181 target_path = home_directory_with_module
182 else:
183 # Assume "quit"
184 return
186 if target_path != "q" and target_path != "Q" or target_path == "":
187 # Run the copy command:
188 copy_module(target_path, my_directory_full_path, my_module)
190 return
192 if target_path != "q" and target_path != "Q" or target_path == "":
193 # Run the copy command:
194 copy_module(target_path, my_directory_full_path, my_module)
196 return