class ModuleForm(NoCsrfForm, FormMixin): # TODO implement validators python_file = DynamicFileField( 'python file', description='python file to upload with function', validators=[Optional(), Extension(['py'])]) python_module = StringField( 'python module', description='name of python module, if no file is provided', validators=[Optional()], render_kw={'nullable': True}) def get_formatted(self): formatted_dict = super().get_formatted() if formatted_dict['python_file'] is None and formatted_dict[ 'python_module'] is None: raise LorisError('No python file or module was given.') elif formatted_dict['python_file']: return formatted_dict['python_file'] else: return formatted_dict['python_module']
def _get_field(cls, key, value, required, default, description, iterate, loc, folderpath): """get initialized field """ def post_process(x): return x if required: kwargs = { 'validators': [InputRequired()], 'render_kw': { 'nullable': False } } else: kwargs = { 'validators': [Optional()], 'render_kw': { 'nullable': True } } kwargs['default'] = default kwargs['label'] = key.replace('_', ' ') kwargs['description'] = (key if description is None else description) if loc is None and not isinstance(value, dict): if value == 'list': kwargs['validators'].append(JsonSerializableValidator()) field = ListField(**kwargs) elif value == 'dict': kwargs['validators'].append(JsonSerializableValidator()) field = DictField(**kwargs) elif value == 'str': field = StringField(**kwargs) elif value == 'set': kwargs['validators'].append(JsonSerializableValidator()) post_process = set field = ListField(**kwargs) elif value == 'tuple': kwargs['validators'].append(JsonSerializableValidator()) post_process = tuple field = ListField(**kwargs) elif value == 'int': field = IntegerField(**kwargs) elif value == 'float': field = FloatField(**kwargs) elif value == 'bool': kwargs['validators'] = [Optional()] field = BooleanField(**kwargs) elif value == 'numpy.array': kwargs['validators'].append(Extension()) post_process = cls.file_processing(value) field = DynamicFileField(**kwargs) elif value == 'numpy.recarray': kwargs['validators'].append(Extension()) post_process = cls.file_processing(value) field = DynamicFileField(**kwargs) elif value == 'pandas.DataFrame': kwargs['validators'].append(Extension()) post_process = cls.file_processing(value) field = DynamicFileField(**kwargs) elif value == 'pandas.Series': kwargs['validators'].append(Extension()) post_process = cls.file_processing(value) field = DynamicFileField(**kwargs) elif value == 'json': kwargs['validators'].append(Extension(['json'])) post_process = cls.file_processing(value) field = DynamicFileField(**kwargs) elif value == 'file': kwargs['validators'].append( Extension(config['attach_extensions'])) field = DynamicFileField(**kwargs) elif isinstance(value, list): choices = [ str(ele).strip().strip('"').strip("'") for ele in value ] post_process = EnumReader(value, choices) if default is None: choices = ['NULL'] + choices kwargs['choices'] = [(ele, ele) for ele in choices] field = SelectField(**kwargs) else: raise LorisError( f"field value {value} not accepted for {key}.") elif loc is not None and isinstance(value, str): loc = secure_filename(loc) locpath = os.path.join(folderpath, loc) # try up to three base directories down if not os.path.exists(locpath): # try main autoscript folder locpath = os.path.join(os.path.dirname(folderpath), loc) if not os.path.exists(locpath): locpath = os.path.join( os.path.dirname(os.path.dirname(folderpath)), loc) if not os.path.exists(locpath): raise LorisError( f'Folder "{loc}" does not exist in ' f'autoscript folder ' f'"{os.path.basename(folderpath)}" ' f'and also not in the main autoscript folder.') # get all files from folder files = glob.glob(os.path.join(locpath, '*')) # only match certain extensions if (value == 'pandas.DataFrame') or (value == 'numpy.recarray'): files = [ ifile for ifile in files if (ifile.endswith('.pkl') or ifile.endswith('.npy') or ifile.endswith('.csv') or ifile.endswith('.json')) ] elif value == 'numpy.array': files = [ ifile for ifile in files if (ifile.endswith('.pkl') or ifile.endswith('.npy') or ifile.endswith('.csv')) ] elif (value == 'json') or (value == 'pandas.Series'): files = [ifile for ifile in files if ifile.endswith('.json')] else: # skip file that start with two underscores e.g. __init__.py files = [ ifile for ifile in files if not os.path.basename(ifile).startswith('__') ] # setup as choices choices = [(str(ele), os.path.split(ele)[-1]) for ele in files] # setup None choice if default is None and not required: choices = [('NULL', 'NULL')] + choices kwargs['choices'] = choices post_process = cls.file_processing(value) field = SelectField(**kwargs) elif isinstance(value, dict): form, post_process = dynamic_autoscripted_form( value, folderpath, NoCsrfForm) field = FormField(form) # TODO set number of fieldlists (startswith numeric) else: raise LorisError(f"field value {value} not accepted for {key}.") # make iterator (can add multiple values together) if iterate: field = FieldList( field, min_entries=int(required) + 1 # have one required field if so ) post_process = ListReader(post_process) return field, post_process
def blob_field(self, kwargs): kwargs['validators'] = [Optional()] # TODO fix for editing kwargs['validators'].append(Extension()) return BlobFileField(**kwargs)
def attach_field(self, kwargs): kwargs['validators'] = [Optional()] # TODO fix for editing kwargs['validators'].append(Extension(config['attach_extensions'])) return AttachFileField(**kwargs)
class AutomaticFolderForm(Form, FormMixin): folder = DynamicFileField( 'autoscript folder', description='upload your zipped autoscript folder', validators=[InputRequired(), Extension(['zip'])])