Python 2 to 3 migration of collectd-ves-app 86/69286/2
authorPawel Tomaszewski <pawelx.tomaszewski@intel.com>
Tue, 5 Nov 2019 08:46:45 +0000 (08:46 +0000)
committerKamil Wiatrowski <kamilx.wiatrowski@intel.com>
Thu, 12 Dec 2019 07:58:28 +0000 (08:58 +0100)
Change-Id: I656a67864d821454e5ecc1ed60ce601c872da23d
Signed-off-by: Pawel Tomaszewski <pawelx.tomaszewski@intel.com>
Signed-off-by: Kamil Wiatrowski <kamilx.wiatrowski@intel.com>
3rd_party/collectd-ves-app/ves_app/normalizer.py
3rd_party/collectd-ves-app/ves_app/ves_app.py

index dcb02f2..0e92d1a 100644 (file)
@@ -17,7 +17,7 @@
 #   Volodymyr Mytnyk <volodymyrx.mytnyk@intel.com>
 #
 
-import yaml
+from . import yaml
 import logging
 import datetime
 import time
@@ -28,17 +28,12 @@ import re
 
 # import YAML loader
 try:
-    from yaml import CLoader as Loader
+    from .yaml import CLoader as Loader
 except ImportError:
-    from yaml import Loader
+    from .yaml import Loader
 
 # import synchronized queue
-try:
-    # python 2.x
-    import Queue as queue
-except ImportError:
-    # python 3.x
-    import queue
+import queue
 
 
 class Config(object):
@@ -80,7 +75,7 @@ class ItemIterator(object):
         self._collector = collector
         self._index = 0
 
-    def next(self):
+    def __next__(self):
         """Returns next item from the list"""
         if self._index == len(self._items):
             raise StopIteration
@@ -151,7 +146,7 @@ class Collector(object):
     def _check_aging(self):
         """Check aging time for all items"""
         self.lock()
-        for data_hash, data in self._metrics.items():
+        for data_hash, data in list(self._metrics.items()):
             age, item = data
             if ((time.time() - age) >= self._age_timeout):
                 # aging time has expired, remove the item from the collector
@@ -189,7 +184,7 @@ class Collector(object):
         """Returns locked (safe) item iterator"""
         metrics = []
         self.lock()
-        for k, item in self._metrics.items():
+        for k, item in list(self._metrics.items()):
             _, value = item
             for select in select_list:
                 if value.match(**select):
@@ -220,7 +215,7 @@ class CollectdData(object):
 
     def match(self, **kargs):
         # compare the metric
-        for key, value in kargs.items():
+        for key, value in list(kargs.items()):
             if self.is_regular_expression(value):
                 if re.match(value[1:-1], getattr(self, key)) is None:
                     return False
@@ -323,7 +318,7 @@ class Item(yaml.YAMLObject):
 
 class ValueItem(Item):
     """Class to process VlaueItem tag"""
-    yaml_tag = u'!ValueItem'
+    yaml_tag = '!ValueItem'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -343,7 +338,7 @@ class ValueItem(Item):
         # if VALUE key isn't given, use default VALUE key
         # format: `VALUE: !Number '{vl.value}'`
         if value_desc is None:
-            value_desc = yaml.ScalarNode(tag=u'!Number', value=u'{vl.value}')
+            value_desc = yaml.ScalarNode(tag='!Number', value='{vl.value}')
         # select collectd metric based on SELECT condition
         metrics = loader.collector.items(select)
         assert len(metrics) < 2, \
@@ -361,7 +356,7 @@ class ValueItem(Item):
 
 class ArrayItem(Item):
     """Class to process ArrayItem tag"""
-    yaml_tag = u'!ArrayItem'
+    yaml_tag = '!ArrayItem'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -415,12 +410,12 @@ class ArrayItem(Item):
 
 class Measurements(ArrayItem):
     """Class to process Measurements tag"""
-    yaml_tag = u'!Measurements'
+    yaml_tag = '!Measurements'
 
 
 class Events(Item):
     """Class to process Events tag"""
-    yaml_tag = u'!Events'
+    yaml_tag = '!Events'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -441,7 +436,7 @@ class Events(Item):
 
 class Bytes2Kibibytes(yaml.YAMLObject):
     """Class to process Bytes2Kibibytes tag"""
-    yaml_tag = u'!Bytes2Kibibytes'
+    yaml_tag = '!Bytes2Kibibytes'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -450,7 +445,7 @@ class Bytes2Kibibytes(yaml.YAMLObject):
 
 class Number(yaml.YAMLObject):
     """Class to process Number tag"""
-    yaml_tag = u'!Number'
+    yaml_tag = '!Number'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -462,7 +457,7 @@ class Number(yaml.YAMLObject):
 
 class StripExtraDash(yaml.YAMLObject):
     """Class to process StripExtraDash tag"""
-    yaml_tag = u'!StripExtraDash'
+    yaml_tag = '!StripExtraDash'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -471,7 +466,7 @@ class StripExtraDash(yaml.YAMLObject):
 
 class MapValue(yaml.YAMLObject):
     """Class to process MapValue tag"""
-    yaml_tag = u'!MapValue'
+    yaml_tag = '!MapValue'
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -485,7 +480,7 @@ class MapValue(yaml.YAMLObject):
         assert val is not None, "Mandatory VALUE key isn't set"
         assert val in mapping, \
             'Value "{}" cannot be mapped to any of {} values'.format(
-                val, mapping.keys())
+                val, list(mapping.keys()))
         return mapping[val]
 
 
@@ -514,10 +509,10 @@ class Normalizer(object):
                 measurements.append((key, value))
             if value.tag == Events.yaml_tag:
                 events.append((key, value))
-        measurements_yaml = yaml.MappingNode(u'tag:yaml.org,2002:map',
+        measurements_yaml = yaml.MappingNode('tag:yaml.org,2002:map',
                                              measurements)
         measurements_stream = yaml.serialize(measurements_yaml)
-        events_yaml = yaml.MappingNode(u'tag:yaml.org,2002:map', events)
+        events_yaml = yaml.MappingNode('tag:yaml.org,2002:map', events)
         events_stream = yaml.serialize(events_yaml)
         # return event & measurements definition
         return events_stream, measurements_stream
index 105c66e..fa4e934 100644 (file)
 import json
 import sys
 import base64
-import ConfigParser
+import configparser
 import logging
 import argparse
 
 from distutils.util import strtobool
 from kafka import KafkaConsumer
 
-from normalizer import Normalizer
-from normalizer import CollectdValue
-
-try:
-    # For Python 3.0 and later
-    import urllib.request as url
-except ImportError:
-    # Fall back to Python 2's urllib2
-    import urllib2 as url
+from .normalizer import Normalizer
+from .normalizer import CollectdValue
 
+import urllib.request as url
 
 class VESApp(Normalizer):
     """VES Application"""
@@ -110,7 +104,7 @@ class VESApp(Normalizer):
     def init(self, configfile, schema_file):
         if configfile is not None:
             # read VES configuration file if provided
-            config = ConfigParser.ConfigParser()
+            config = configparser.ConfigParser()
             config.optionxform = lambda option: option
             config.read(configfile)
             self.config(config)