Ejemplo n.º 1
0
    def __init__(self,
                 filepath: str,
                 name: str = None,
                 klass: Optional[Type] = None,
                 always_import_strs: Optional[Sequence[str]] = None,
                 always_assign_strs: Optional[Sequence[str]] = None):
        super().__init__(filepath,
                         name=name,
                         klass=klass,
                         always_import_strs=always_import_strs,
                         always_assign_strs=always_assign_strs)
        # Override class definitions with object specific definitions, if specifics are passed
        if self.always_import_strs:
            imports = []
            for import_str in self.always_import_strs:
                try:
                    imports.append(ObjectImportStatement.from_str(import_str))
                except ExtractedIncorrectTypeOfImportException:
                    imports.append(ModuleImportStatement.from_str(import_str))
            self.always_imports = imports
        elif self.always_import_strs == []:
            # None passed, remove default imports
            self.always_imports = []

        if self.always_assign_strs:
            self.always_assigns = [
                AssignmentStatement.from_str(assign_str)
                for assign_str in self.always_assign_strs
            ]
        elif self.always_assign_strs == []:
            # None passed, remove default assignments
            self.always_assigns = []
Ejemplo n.º 2
0
class FunctionConfigFile(ConfigFileBase):
    """
    Represents config file on filesystem. Handles low-level functions for writing and reading config file
    """

    # lines to always import. pass import objects
    always_imports = [
        ObjectImportStatement.from_str('from pyfileconf import Selector', preferred_position='begin')
    ]

    # assignment lines to always include at beginning. pass assign objects
    always_assigns = [
        AssignmentStatement.from_str('s = Selector()', preferred_position='begin'),
    ]

    # always assign dict, where assigns will get added if item name matches dict key
    always_assign_with_names_dict = {
        'DataPipeline': [
            AssignmentStatement.from_str('cleanup_kwargs = dict(\n    \n)', preferred_position='end')
        ]
    }

    # class to use for interfacing with file
    # no need to override default
    # interface_class = ConfigFileInterface

    def save(self, config: 'ConfigBase'):
        # If empty cleanup kwargs on DataPipeline, then delete so can be replaced with dict() constructor
        # from always assigns
        if _should_replace_cleanup_kwargs(self, config):
            config.pop('cleanup_kwargs')

        super().save(config)
Ejemplo n.º 3
0
 def _create_item(self, n_clicks: int, section_path: str, function_class_import: Optional[str]):
     if not section_path:
         return dash.no_update
     if function_class_import:
         imp = ObjectImportStatement.from_str(function_class_import)
         if len(imp.objs) != 1:
             raise ValueError(f'must have exactly one object import, got {imp.objs}')
         mod = importlib.import_module(imp.module)
         func_or_class = getattr(mod, imp.objs[0])
     else:
         func_or_class = None
     output = self.gui.runner.create(section_path, func_or_class)
     return f'Created {section_path}'
Ejemplo n.º 4
0
class SpecificClassConfigFile(ConfigFileBase):
    # lines to always import. pass import objects
    always_imports = [
        ObjectImportStatement.from_str('from pyfileconf import Selector',
                                       preferred_position='begin')
    ]

    # assignment lines to always include at beginning. pass assign objects
    always_assigns = [
        AssignmentStatement.from_str('s = Selector()',
                                     preferred_position='begin'),
    ]

    def __init__(self,
                 filepath: str,
                 name: str = None,
                 klass: Optional[Type] = None,
                 always_import_strs: Optional[Sequence[str]] = None,
                 always_assign_strs: Optional[Sequence[str]] = None):
        super().__init__(filepath,
                         name=name,
                         klass=klass,
                         always_import_strs=always_import_strs,
                         always_assign_strs=always_assign_strs)
        # Override class definitions with object specific definitions, if specifics are passed
        if self.always_import_strs:
            imports = []
            for import_str in self.always_import_strs:
                try:
                    imports.append(ObjectImportStatement.from_str(import_str))
                except ExtractedIncorrectTypeOfImportException:
                    imports.append(ModuleImportStatement.from_str(import_str))
            self.always_imports = imports
        elif self.always_import_strs == []:
            # None passed, remove default imports
            self.always_imports = []

        if self.always_assign_strs:
            self.always_assigns = [
                AssignmentStatement.from_str(assign_str)
                for assign_str in self.always_assign_strs
            ]
        elif self.always_assign_strs == []:
            # None passed, remove default assignments
            self.always_assigns = []

    def __call__(self, *args, **kwargs):
        """
        For compatibility with BaseConfig which expects to call class, while here an object will be used
        """
        # Create new object
        # Use defaults from this object
        obj_kwargs = dict(name=self.name,
                          klass=self.klass,
                          always_import_strs=self.always_import_strs,
                          always_assign_strs=self.always_assign_strs)
        obj_kwargs.update(kwargs)
        obj = self.__class__(*args, **obj_kwargs)
        return obj

    def load(self, config_class: type = None) -> 'SpecificClassConfig':
        # Override base class method to pull a single dict, and not pass annotations
        from pyfileconf.data.models.config import SpecificClassConfig

        config_dict, annotation_dict = self.interface.load()

        if config_class is None:
            config_class = SpecificClassConfig

        return config_class(
            d=config_dict,
            annotations=annotation_dict,
            imports=self.interface.imports,
            _file=self,
            name=self.name,
            klass=self.klass,
            always_assign_strs=self.always_assign_strs,
            always_import_strs=self.always_import_strs,
        )