22
33import json
44import logging
5- import os
65import textwrap
7- from typing import Literal , Optional
6+ from pathlib import Path
7+ from typing import Dict , List , Literal , Optional
88
99import questionary
1010
2727ModeLiteral = Literal ["both" , "start" , "end" , "none" ]
2828
2929
30- def _print_wrapped (text , fill_char = "-" , mode = "both" , width = 80 , indent = 0 , drop_whitespace = True ):
30+ def _print_wrapped (text , fill_char = "-" , mode = "both" , width = 80 , indent = 0 , drop_whitespace = True ) -> str :
3131 """Helper function to format text for the params-file template.
3232
3333 Args:
@@ -100,7 +100,7 @@ def __init__(
100100 self .wfs = nf_core .pipelines .list .Workflows ()
101101 self .wfs .get_remote_workflows ()
102102
103- def get_pipeline (self ):
103+ def get_pipeline (self ) -> Optional [ bool ] :
104104 """
105105 Prompt the user for a pipeline name and get the schema
106106 """
@@ -124,11 +124,14 @@ def get_pipeline(self):
124124 ).unsafe_ask ()
125125
126126 # Get the schema
127- self .schema_obj = nf_core .pipelines .schema .PipelineSchema ()
127+ self .schema_obj = PipelineSchema ()
128+ if self .schema_obj is None :
129+ return False
128130 self .schema_obj .get_schema_path (self .pipeline , local_only = False , revision = self .pipeline_revision )
129131 self .schema_obj .get_wf_params ()
132+ return True
130133
131- def format_group (self , definition , show_hidden = False ):
134+ def format_group (self , definition , show_hidden = False ) -> str :
132135 """Format a group of parameters of the schema as commented YAML.
133136
134137 Args:
@@ -167,7 +170,9 @@ def format_group(self, definition, show_hidden=False):
167170
168171 return out
169172
170- def format_param (self , name , properties , required_properties = (), show_hidden = False ):
173+ def format_param (
174+ self , name : str , properties : Dict , required_properties : List [str ] = [], show_hidden : bool = False
175+ ) -> Optional [str ]:
171176 """
172177 Format a single parameter of the schema as commented YAML
173178
@@ -188,6 +193,9 @@ def format_param(self, name, properties, required_properties=(), show_hidden=Fal
188193 return None
189194
190195 description = properties .get ("description" , "" )
196+ if self .schema_obj is None :
197+ log .error ("No schema object found" )
198+ return ""
191199 self .schema_obj .get_schema_defaults ()
192200 default = properties .get ("default" )
193201 type = properties .get ("type" )
@@ -209,7 +217,7 @@ def format_param(self, name, properties, required_properties=(), show_hidden=Fal
209217
210218 return out
211219
212- def generate_params_file (self , show_hidden = False ):
220+ def generate_params_file (self , show_hidden : bool = False ) -> str :
213221 """Generate the contents of a parameter template file.
214222
215223 Assumes the pipeline has been fetched (if remote) and the schema loaded.
@@ -220,6 +228,10 @@ def generate_params_file(self, show_hidden=False):
220228 Returns:
221229 str: Formatted output for the pipeline schema
222230 """
231+ if self .schema_obj is None :
232+ log .error ("No schema object found" )
233+ return ""
234+
223235 schema = self .schema_obj .schema
224236 pipeline_name = self .schema_obj .pipeline_manifest .get ("name" , self .pipeline )
225237 pipeline_version = self .schema_obj .pipeline_manifest .get ("version" , "0.0.0" )
@@ -234,13 +246,13 @@ def generate_params_file(self, show_hidden=False):
234246 out += "\n "
235247
236248 # Add all parameter groups
237- for definition in schema .get ("definitions" , {} ).values ():
249+ for definition in schema .get ("definitions" , schema . get ( "$defs" , {}) ).values ():
238250 out += self .format_group (definition , show_hidden = show_hidden )
239251 out += "\n "
240252
241253 return out
242254
243- def write_params_file (self , output_fn = "nf-params.yaml" , show_hidden = False , force = False ):
255+ def write_params_file (self , output_fn : Path = Path ( "nf-params.yaml" ) , show_hidden = False , force = False ) -> bool :
244256 """Build a template file for the pipeline schema.
245257
246258 Args:
@@ -254,7 +266,9 @@ def write_params_file(self, output_fn="nf-params.yaml", show_hidden=False, force
254266 """
255267
256268 self .get_pipeline ()
257-
269+ if self .schema_obj is None :
270+ log .error ("No schema object found" )
271+ return False
258272 try :
259273 self .schema_obj .load_schema ()
260274 self .schema_obj .validate_schema ()
@@ -265,11 +279,10 @@ def write_params_file(self, output_fn="nf-params.yaml", show_hidden=False, force
265279
266280 schema_out = self .generate_params_file (show_hidden = show_hidden )
267281
268- if os . path . exists (output_fn ) and not force :
282+ if output_fn . exists () and not force :
269283 log .error (f"File '{ output_fn } ' exists! Please delete first, or use '--force'" )
270284 return False
271- with open (output_fn , "w" ) as fh :
272- fh .write (schema_out )
273- log .info (f"Parameter file written to '{ output_fn } '" )
285+ output_fn .write_text (schema_out )
286+ log .info (f"Parameter file written to '{ output_fn } '" )
274287
275288 return True
0 commit comments