9
9
import shutil
10
10
import sys
11
11
import traceback
12
+ from typing import (
13
+ Any ,
14
+ Dict ,
15
+ Optional ,
16
+ TYPE_CHECKING ,
17
+ )
12
18
from urllib .request import urlopen
13
19
14
20
from planemo .config import read_global_config
15
21
22
+ if TYPE_CHECKING :
23
+ from planemo .config import OptionSource
24
+
16
25
17
26
class PlanemoContextInterface (metaclass = abc .ABCMeta ):
18
27
"""Interface Planemo operations use to access workspace context."""
@@ -53,29 +62,29 @@ def cache_download(self, url, destination):
53
62
class PlanemoContext (PlanemoContextInterface ):
54
63
"""Implementation of ``PlanemoContextInterface``"""
55
64
56
- def __init__ (self ):
65
+ def __init__ (self ) -> None :
57
66
"""Construct a Context object using execution environment."""
58
67
self .home = os .getcwd ()
59
- self ._global_config = None
68
+ self ._global_config : Optional [ Dict ] = None
60
69
# Will be set by planemo CLI driver
61
70
self .verbose = False
62
- self .planemo_config = None
71
+ self .planemo_config : Optional [ str ] = None
63
72
self .planemo_directory = None
64
- self .option_source = {}
73
+ self .option_source : Dict [ str , "OptionSource" ] = {}
65
74
66
- def set_option_source (self , param_name , option_source , force = False ):
75
+ def set_option_source (self , param_name : str , option_source : "OptionSource" , force : bool = False ) -> None :
67
76
"""Specify how an option was set."""
68
77
if not force :
69
- assert param_name not in self .option_source , "No option source for [%s]" % param_name
78
+ assert param_name not in self .option_source , f"Option source for [{ param_name } ] already set"
70
79
self .option_source [param_name ] = option_source
71
80
72
- def get_option_source (self , param_name ) :
81
+ def get_option_source (self , param_name : str ) -> "OptionSource" :
73
82
"""Return OptionSource value indicating how the option was set."""
74
- assert param_name in self .option_source , "No option source for [%s]" % param_name
83
+ assert param_name in self .option_source , f "No option source for [{ param_name } ]"
75
84
return self .option_source [param_name ]
76
85
77
86
@property
78
- def global_config (self ):
87
+ def global_config (self ) -> Dict [ str , Any ] :
79
88
"""Read Planemo's global configuration.
80
89
81
90
As defined most simply by ~/.planemo.yml.
@@ -84,21 +93,21 @@ def global_config(self):
84
93
self ._global_config = read_global_config (self .planemo_config )
85
94
return self ._global_config or {}
86
95
87
- def log (self , msg , * args ):
96
+ def log (self , msg : str , * args ) -> None :
88
97
"""Log a message."""
89
98
if args :
90
99
msg %= args
91
100
self ._log_message (msg )
92
101
93
- def vlog (self , msg , * args , ** kwds ):
102
+ def vlog (self , msg : str , * args , ** kwds ) -> None :
94
103
"""Log a message only if verbose is enabled."""
95
104
if self .verbose :
96
105
self .log (msg , * args )
97
106
if kwds .get ("exception" , False ):
98
107
traceback .print_exc (file = sys .stderr )
99
108
100
109
@property
101
- def workspace (self ):
110
+ def workspace (self ) -> str :
102
111
"""Create and return Planemo's workspace.
103
112
104
113
By default this will be ``~/.planemo``.
@@ -109,7 +118,7 @@ def workspace(self):
109
118
return self ._ensure_directory (workspace , "workspace" )
110
119
111
120
@property
112
- def galaxy_profiles_directory (self ):
121
+ def galaxy_profiles_directory (self ) -> str :
113
122
"""Create a return a directory for storing Galaxy profiles."""
114
123
path = os .path .join (self .workspace , "profiles" )
115
124
return self ._ensure_directory (path , "Galaxy profiles" )
@@ -125,27 +134,25 @@ def cache_download(self, url, destination):
125
134
with urlopen (url ) as fh :
126
135
content = fh .read ()
127
136
if len (content ) == 0 :
128
- raise Exception ("Failed to download [%s ]." % url )
137
+ raise Exception (f "Failed to download [{ url } ]." )
129
138
with open (cache_destination , "wb" ) as f :
130
139
f .write (content )
131
140
132
141
shutil .copy (cache_destination , destination )
133
142
134
- def _ensure_directory (self , path , name ) :
143
+ def _ensure_directory (self , path : str , name : str ) -> str :
135
144
if not os .path .exists (path ):
136
145
os .makedirs (path )
137
146
if not os .path .isdir (path ):
138
- template = "Planemo %s directory [%s] unavailable."
139
- message = template % (name , path )
140
- raise Exception (message )
147
+ raise Exception (f"Planemo { name } directory [{ path } ] unavailable." )
141
148
return path
142
149
143
150
def _log_message (self , message ):
144
151
"""Extension point for overriding I/O."""
145
152
print (message )
146
153
147
154
148
- def configure_standard_planemo_logging (verbose ) :
155
+ def configure_standard_planemo_logging (verbose : bool ) -> None :
149
156
"""Configure Planemo's default logging rules."""
150
157
logging_config = {
151
158
"version" : 1 ,
0 commit comments