nuclearpinguin commented on a change in pull request #7191: [AIRFLOW-4030] 
second attempt to add singularity to airflow
URL: https://github.com/apache/airflow/pull/7191#discussion_r368289791
 
 

 ##########
 File path: airflow/contrib/operators/singularity_operator.py
 ##########
 @@ -0,0 +1,176 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+from spython.main import Client
+import shutil
+import ast
+import os
+
+
+class SingularityOperator(BaseOperator):
+    """
+    Execute a command inside a Singularity container
+
+    Singularity has more seamless connection to the host than Docker, so
+    no special binds are needed to ensure binding content in the user $HOME
+    and temporary directories. If the user needs custom binds, this can
+    be done with --volumes
+
+    :param image: Singularity image or URI from which to create the container.
+    :type image: str
+    :param auto_remove: Delete the container when the process exits
+                        The default is False.
+    :type auto_remove: bool
+    :param command: Command to be run in the container. (templated)
+    :type command: str or list
+    :param start_command: start command to pass to the container instance
+    :type start_command: string or list
+    :param environment: Environment variables to set in the container. 
(templated)
+    :type environment: dict
+    :param force_pull: Pull the image on every run. Default is False.
+    :type force_pull: bool
+    :param volumes: List of volumes to mount into the container, e.g.
+        ``['/host/path:/container/path', '/host/path2:/container/path2']``.
+    :param options: other flags (list) to provide to the instance start
+    :type options: list
+    :param working_dir: Working directory to
+        set on the container (equivalent to the -w switch the docker client)
+    :type working_dir: str
+    """
+    template_fields = ('command', 'environment',)
+    template_ext = ('.sh', '.bash',)
+
+    @apply_defaults
+    def __init__(
+            self,
+            image,
+            api_version=None,
+            command=None,
+            start_command=None,
+            environment=None,
+            pull_folder=None,
+            force_pull=False,
+            volumes=None,
+            options=None,
+            working_dir=None,
+            auto_remove=False,
 
 Review comment:
   Type hints allow us to easily indicate what types arguments have and if they 
are necessary:
   
https://github.com/apache/airflow/blob/e2524fac59df1a405a38f7f71b99436b44a47378/airflow/providers/google/cloud/operators/pubsub.py#L113-L128
   
   In above example you can see that `labels` argument is `Optional` and if 
passed should be the type of `Dict[str, str]` meaning that it should be a 
dictionary with all keys and values being strings. It's really easy to add and 
helps a lot (no need to search for info in docstring) and we can run static 
types checks that can discover some nasty edge cases. More information here: 
https://docs.python.org/3/library/typing.html
   
   You have already figured types in `:type param_name:` in the docstring, so 
it's just a matter of adding them in the constructor like `param: expected_type 
= default_value` :) Also, type annotations should be in line with `:type _:` in 
docstrings. 
   
   Some more interesting will be:
   ```
   start_command: Optional[Union[str, List[str]]] = None
   environment: Optional[Dict[str, Any]] = None
   options: Optional[List[str]] = None
   ```
   
   As you can see the good type of annotation should bring as much information 
as it can. At first, it can seem to be unnecessary work but in the end, it 
creates a big value.
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to