#80 Stick to the pep8 style standard inventory
Closed 6 years ago by astepano. Opened 6 years ago by sturivny.
Unknown source pep8-standard-inventory  into  master

@@ -12,7 +12,7 @@

      Run all standard inventory scripts and return their merged output.

  

      Note the standard inventory scripts clean up their spawned hosts

-     when they detect their parent processes go away. To accomodate

+     when they detect their parent processes go away. To accommodate

      that behavior, this script forks a child process to run the

      inventory scripts, send back the merged inventory, and then wait

      for the parent of this script (ansible) to die before silently
@@ -53,7 +53,7 @@

      # close the inherited output side of the pipe

      os.close(pipeout)

  

-     # send eveything from the child to stdout

+     # send everything from the child to stdout

      while True:

          data = os.read(pipein, 999)

          if not data:
@@ -70,7 +70,8 @@

          "TEST_DYNAMIC_INVENTORY_DIRECTORY", "/usr/share/ansible/inventory")

  

      ignore_ext_string = os.environ.get(

-         "ANSIBLE_INVENTORY_IGNORE", "~, .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo")

+         "ANSIBLE_INVENTORY_IGNORE",

+         "~, .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo")

      ignore_ext_list = []

      for s in ignore_ext_string.split(','):

          if s.strip():
@@ -92,8 +93,8 @@

  

          try:

              inv_out = subprocess.check_output(cmd, stdin=None, close_fds=True)

-         except subprocess.CalledProcessError as ex:

-             raise RuntimeError("Could not run: {0}".format(str(cmd)))

+         except subprocess.CalledProcessError:

+             raise RuntimeError("Could not run: {}".format(str(cmd)))

  

          merged.merge(inv_out.decode('utf-8'))

  
@@ -165,43 +166,58 @@

  

          if not isinstance(inventory, dict):

              raise ValueError(

-                 "inventory JSON does not contain the expected top level dictionary")

+                 "inventory JSON does not contain the expected "

+                 "top level dictionary")

  

-         if "subjects" not in inventory or not isinstance(inventory["subjects"], dict):

+         if "subjects" not in inventory or not isinstance(inventory["subjects"],

+                                                          dict):

              raise ValueError(

-                 "inventory JSON does not contain the expected [subjects] dictionary")

-         if "hosts" not in inventory["subjects"] or not isinstance(inventory["subjects"]["hosts"], list):

+                 "inventory JSON does not contain the expected [subjects] "

+                 "dictionary")

+         if "hosts" not in inventory["subjects"] or not isinstance(

+                 inventory["subjects"]["hosts"], list):

              raise ValueError(

-                 "inventory JSON does not contain the expected [subjects][hosts] list")

+                 "inventory JSON does not contain the expected "

+                 "[subjects][hosts] list")

  

          for h in inventory["subjects"]["hosts"]:

              self.hosts.append(h)

  

          if "_meta" not in inventory or not isinstance(inventory["_meta"], dict):

              raise ValueError(

-                 "inventory JSON does not contain the expected [_meta] dictionary")

-         if "hostvars" not in inventory["_meta"] or not isinstance(inventory["_meta"]["hostvars"], dict):

+                 "inventory JSON does not contain the expected [_meta] "

+                 "dictionary")

+         if "hostvars" not in inventory["_meta"] or not isinstance(

+                 inventory["_meta"]["hostvars"], dict):

              raise ValueError(

-                 "inventory JSON does not contain the expected [_meta][hostvars] dict")

+                 "inventory JSON does not contain the expected "

+                 "[_meta][hostvars] dict")

  

          for h in inventory["_meta"]["hostvars"]:

              if not isinstance(inventory["_meta"]["hostvars"][h], dict):

                  raise ValueError(

-                     "inventory JSON does not contain the expected [_meta][hostvars][{0}] dict".format(h))

+                     "inventory JSON does not contain the expected "

+                     "[_meta][hostvars][{0}] dict".format(h))

  

              self.variables[h] = inventory["_meta"]["hostvars"][h]

  

-         if "localhost" not in inventory or not isinstance(inventory["localhost"], dict):

+         if "localhost" not in inventory or not isinstance(

+                 inventory["localhost"], dict):

              raise ValueError(

-                 "inventory JSON does not contain the expected [localhost] dictionary")

-         if "hosts" not in inventory["localhost"] or not isinstance(inventory["localhost"]["hosts"], list):

+                 "inventory JSON does not contain the expected "

+                 "[localhost] dictionary")

+         if "hosts" not in inventory["localhost"] or not isinstance(

+                 inventory["localhost"]["hosts"], list):

I am sorry, but this very hard to read. Why not to use:

        if ("subjects" not in inventory or
                not isinstance(inventory["subjects"], dict)):

Now I see that 'one' OR 'another'. Clear.

              raise ValueError(

-                 "inventory JSON does not contain the expected [localhost][hosts] list")

+                 "inventory JSON does not contain the expected "

+                 "[localhost][hosts] list")

  

      def dumps(self):

-         data = {"subjects": {"hosts": self.hosts, "vars": {}}, "localhost": {

-             "hosts": self.hosts, "vars": {}}, "_meta": {"hostvars": self.variables}}

+         data = {"subjects": {"hosts": self.hosts, "vars": {}}, "localhost":

+                 {"hosts": self.hosts, "vars": {}}, "_meta":

+                 {"hostvars": self.variables}}

          return json.dumps(data, indent=4, separators=(',', ': '))

  

+ 

  if __name__ == '__main__':

      sys.exit(main(sys.argv))

Groome code to pep8 style.

I am sorry, but this very hard to read. Why not to use:

        if ("subjects" not in inventory or
                not isinstance(inventory["subjects"], dict)):

Now I see that 'one' OR 'another'. Clear.

Pull-Request has been closed by astepano

6 years ago
Metadata