replace fnmatch with oslo.utils.fnmatch

oslo.utils has implemented a thread safe fnmatch, we can use it
instead of maintaining a private one.

Change-Id: Idb8a3d9f2b0fb16ec403f2da3ad00ee0d982454e
This commit is contained in:
ZhiQiang Fan 2016-04-12 03:32:24 +08:00
parent 123fa26585
commit d526651260
6 changed files with 12 additions and 40 deletions

View File

@ -27,6 +27,7 @@ from keystoneclient import exceptions as ks_exceptions
from oslo_config import cfg
from oslo_log import log
import oslo_messaging
from oslo_utils import fnmatch
from six import moves
from six.moves.urllib import parse as urlparse
from stevedore import extension
@ -246,7 +247,7 @@ class AgentManager(service_base.BaseService):
def _match(pollster):
"""Find out if pollster name matches to one of the list."""
return any(utils.match(pollster.name, pattern) for
return any(fnmatch.fnmatch(pollster.name, pattern) for
pattern in pollster_list)
if type(namespaces) is not list:

View File

@ -26,6 +26,7 @@ from gnocchiclient import utils as gnocchi_utils
from keystoneauth1 import session as ka_session
from oslo_config import cfg
from oslo_log import log
from oslo_utils import fnmatch
import requests
import retrying
import six
@ -35,7 +36,6 @@ from ceilometer import declarative
from ceilometer import dispatcher
from ceilometer.i18n import _, _LE, _LW
from ceilometer import keystone_client
from ceilometer import utils
NAME_ENCODED = __name__.encode('utf-8')
CACHE_NAMESPACE = uuid.UUID(bytes=md5(NAME_ENCODED).digest())
@ -112,7 +112,7 @@ class ResourcesDefinition(object):
def match(self, metric_name):
for t in self.cfg['metrics']:
if utils.match(metric_name, t):
if fnmatch.fnmatch(metric_name, t):
return True
return False

View File

@ -16,13 +16,13 @@
from debtcollector import moves
from oslo_config import cfg
from oslo_log import log
from oslo_utils import fnmatch
from oslo_utils import timeutils
import six
from ceilometer import declarative
from ceilometer.event.storage import models
from ceilometer.i18n import _
from ceilometer import utils
OPTS = [
cfg.StrOpt('definitions_cfg_file',
@ -130,13 +130,13 @@ class EventDefinition(object):
def included_type(self, event_type):
for t in self._included_types:
if utils.match(event_type, t):
if fnmatch.fnmatch(event_type, t):
return True
return False
def excluded_type(self, event_type):
for t in self._excluded_types:
if utils.match(event_type, t):
if fnmatch.fnmatch(event_type, t):
return True
return False

View File

@ -19,13 +19,13 @@ from debtcollector import moves
from oslo_config import cfg
from oslo_log import log
import oslo_messaging
from oslo_utils import fnmatch
from stevedore import extension
from ceilometer.agent import plugin_base
from ceilometer import declarative
from ceilometer.i18n import _LE, _LW
from ceilometer import sample
from ceilometer import utils
OPTS = [
cfg.StrOpt('meter_definitions_cfg_file',
@ -97,7 +97,7 @@ class MeterDefinition(object):
def match_type(self, meter_name):
for t in self._event_type:
if utils.match(meter_name, t):
if fnmatch.fnmatch(meter_name, t):
return True
def to_samples(self, message, all_values=False):

View File

@ -25,6 +25,7 @@ import os
from oslo_config import cfg
from oslo_log import log
import oslo_messaging
from oslo_utils import fnmatch
from oslo_utils import timeutils
import six
from stevedore import extension
@ -36,7 +37,6 @@ from ceilometer.i18n import _, _LI, _LW
from ceilometer import publisher
from ceilometer.publisher import utils as publisher_utils
from ceilometer import sample as sample_util
from ceilometer import utils
OPTS = [
@ -274,11 +274,11 @@ class Source(object):
def is_supported(dataset, data_name):
# Support wildcard like storage.* and !disk.*
# Start with negation, we consider that the order is deny, allow
if any(utils.match(data_name, datapoint[1:])
if any(fnmatch.fnmatch(data_name, datapoint[1:])
for datapoint in dataset if datapoint[0] == '!'):
return False
if any(utils.match(data_name, datapoint)
if any(fnmatch.fnmatch(data_name, datapoint)
for datapoint in dataset if datapoint[0] != '!'):
return True

View File

@ -23,11 +23,8 @@ import calendar
import copy
import datetime
import decimal
import fnmatch
import hashlib
import re
import struct
import sys
from oslo_concurrency import processutils
from oslo_config import cfg
@ -257,29 +254,3 @@ def kill_listeners(listeners):
for listener in listeners:
listener.stop()
listener.wait()
if sys.version_info > (2, 7, 9):
match = fnmatch.fnmatch
else:
_MATCH_CACHE = {}
_MATCH_CACHE_MAX = 100
def match(string, pattern):
"""Thread safe fnmatch re-implementation.
Standard library fnmatch in Python versions <= 2.7.9 has thread safe
issue, this helper function is created for such case. see:
https://bugs.python.org/issue23191
"""
string = string.lower()
pattern = pattern.lower()
cached_pattern = _MATCH_CACHE.get(pattern)
if cached_pattern is None:
translated_pattern = fnmatch.translate(pattern)
cached_pattern = re.compile(translated_pattern)
if len(_MATCH_CACHE) >= _MATCH_CACHE_MAX:
_MATCH_CACHE.clear()
_MATCH_CACHE[pattern] = cached_pattern
return cached_pattern.match(string) is not None