def indices(self):
     l = getLogger("{}.{}".format(self.__class__.__name__, inspect_stack()[0][3]))
     if self._indices is None:
         self._indices = [x for x in self.stats.get(u'_all', {}).get(u'indices', {}).keys() if x.startswith(u'logstash')]
         self._indices.sort()
         l.debug('indices: %r', self._indices)
     return self._indices
Exemple #2
0
def current_usage(path):
    l = getLogger('{0}'.format(inspect_stack()[0][3]))
    filesystem, blocks, blocks_used, blocks_avail, percentage, mountpoint = \
        check_output(['/bin/df', path]).split('\n')[1].split()
    usage = int(percentage[:-1])
    l.debug("usage: %r", usage)
    return usage
 def es(self):
     l = getLogger("{}.{}".format(self.__class__.__name__, inspect_stack()[0][3]))
     if self._es is None:
         uri = '{0}:{1}'.format(self.host, self.port)
         l.debug('connecting to: %r', uri)
         self._es = Elastic(uri)
     return self._es
    def _setup_patches(self, patch_args):
        """Creates mock instance attributes.

        Naming is as follows:
        @patch_object(object, attr) gets mock_object_attr
        @patch_dict(dictname) gets mock_dict_dictname
        @patch(thing) gets mock_thing

        Yes, you could name things such that they clobber each other,
        and no, this module won't protect you against that.
        So... it's probably a good idea if you Don't Do That.
        """

        l = getLogger("{0}.{1}".format(self.__class__.__name__,
                                       inspect_stack()[0][3]))
        l.debug("self.patches: {0!r} patch_args: {1!r}".format(
            self.patches, patch_args))

        self.assertEquals(
            len(self.patches[self.__class__.__name__]), len(patch_args),
            "self.patches[{0!r}] = {1!r}. This is not equal to {2!r}".format(
                self.__class__.__name__,
                self.patches.get(
                    self.__class__.__name__,
                    "missing patches for {0!r}".format(
                        self.__class__.__name__)), patch_args))

        for (cls, attr), mock in zip(self.patches[self.__class__.__name__],
                                     reversed(patch_args)):
            m = self.pretty_attribute.match(attr)
            readable_attr = m.group("tail") if m else attr
            mock_name = "mock_{0}".format(readable_attr) if cls is None \
                else "mock_{0}_{1}".format(cls.__name__, readable_attr)
            l.debug("setting {0!r} to {1!r}".format(mock_name, mock))
            setattr(self, mock_name, mock)
Exemple #5
0
 def stats(self):
     l = getLogger("{}.{}".format(self.__class__.__name__,
                                  inspect_stack()[0][3]))
     if self._stats is None:
         self._stats = self.es.get('_stats')
         l.debug('stats: %r', self._stats)
     return self._stats
def current_usage(path):
    l = getLogger('{0}'.format(inspect_stack()[0][3]))
    filesystem, blocks, blocks_used, blocks_avail, percentage, mountpoint = \
        check_output(['/bin/df', path]).split('\n')[1].split()
    usage = int(percentage[:-1])
    l.debug("usage: %r", usage)
    return usage
    def _setup_patches(self, patch_args):
        """Creates mock instance attributes.

        Naming is as follows:
        @patch_object(object, attr) gets mock_object_attr
        @patch_dict(dictname) gets mock_dict_dictname
        @patch(thing) gets mock_thing

        Yes, you could name things such that they clobber each other,
        and no, this module won't protect you against that.
        So... it's probably a good idea if you Don't Do That.
        """

        l = getLogger("{0}.{1}".format(self.__class__.__name__, inspect_stack()[0][3]))
        l.debug("self.patches: {0!r} patch_args: {1!r}".format(self.patches, patch_args))

        self.assertEquals(len(self.patches[self.__class__.__name__]), len(patch_args),
                "self.patches[{0!r}] = {1!r}. This is not equal to {2!r}".format(
                    self.__class__.__name__,
                    self.patches.get(self.__class__.__name__, "missing patches for {0!r}". format(self.__class__.__name__)),
                    patch_args))

        for (cls, attr), mock in zip(self.patches[self.__class__.__name__], reversed(patch_args)):
            m = self.pretty_attribute.match(attr)
            readable_attr = m.group("tail") if m else attr
            mock_name = "mock_{0}".format(readable_attr) if cls is None \
                else "mock_{0}_{1}".format(cls.__name__, readable_attr)
            l.debug("setting {0!r} to {1!r}".format(mock_name, mock))
            setattr(self, mock_name, mock)
Exemple #8
0
    def __init__(self,
                 typ: str = None,
                 addition: str = None,
                 src_file: str = None,
                 debug: (int, bool) = None):
        if typ is None:
            typ = 'core'

        if src_file is None:
            self.name = inspect_getfile(inspect_stack()[1][0])

        else:
            self.name = src_file

        if debug is None:
            self.debug = True if config.AGENT.debug == 1 else False

        else:
            if type(debug) == bool:
                self.debug = debug

            else:
                self.debug = True if debug == 1 else False

        self.log_dir = f"{config.AGENT.path_log}/{typ}/{now('%Y')}"
        self.log_level = config.AGENT.log_level

        if addition is None:
            self.log_file = f"{self.log_dir}/{now('%m')}_{typ}.log"

        else:
            self.log_file = f"{self.log_dir}/{now('%m')}_{typ}_{addition.replace(' ', '_')}.log"

        self.status = self._check()
 def __init__(self):
     super(B, self).__init__()
     l = getLogger('{0}.{1}'.format(self.__class__.__name__,
                                    inspect_stack()[0][3]))
     l.debug('B instantiated')
     l.info('B info')
     l.error('B exploding!!!')
Exemple #10
0
 def es(self):
     l = getLogger("{}.{}".format(self.__class__.__name__,
                                  inspect_stack()[0][3]))
     if self._es is None:
         uri = '{0}:{1}'.format(self.host, self.port)
         l.debug('connecting to: %r', uri)
         self._es = Elastic(uri)
     return self._es
Exemple #11
0
def device_log(output: str, add: str, level: int = 1) -> bool:
    _src = inspect_getfile(inspect_stack()[1][0])

    if config.AGENT.device_log == 1:
        return MultiLog([
            Log(src_file=_src),
            Log(typ='device', addition=add, src_file=_src)
        ]).write(output=output, level=level)

    else:
        return Log(src_file=_src).write(output=output, level=level)
Exemple #12
0
 def indices(self):
     l = getLogger("{}.{}".format(self.__class__.__name__,
                                  inspect_stack()[0][3]))
     if self._indices is None:
         self._indices = [
             x for x in self.stats.get(u'_all', {}).get(
                 u'indices', {}).keys() if x.startswith(u'logstash')
         ]
         self._indices.sort()
         l.debug('indices: %r', self._indices)
     return self._indices
def get_current_func_name():
    """
    获取当前被调用的函数名
    eg:
    class MyClass:
        def function_one(self):
            print("%s.%s invoked" % (self.__class__.__name__, get_current_function_name()))
    a = MyClass()
    a.function_one()
    :return:
    """
    return inspect_stack()[1][3]
Exemple #14
0
def log(output: str,
        level: int = 1,
        logger_instance: Log = None,
        src_file: str = None) -> bool:
    # wrapper function so we don't need always to call the .write method
    if logger_instance is None:
        if src_file is None:
            src_file = inspect_getfile(inspect_stack()[1][0])

        return Log(src_file=src_file).write(output=output, level=level)

    else:
        return logger_instance.write(output=output, level=level)
Exemple #15
0
 def dprint(self, buf):
     if self.debug:
         stack = inspect_stack()
         caller_method = stack[1][0].f_code.co_name
         buf = "({0}:{1}): {2}".format(self.name, caller_method, buf)
         if self.logger:
             self.logger.setLevel(DEBUG)
             self.logger.debug(buf)
         else:
             if not buf.endswith("\n"):
                 buf += "\n"
             self.stderr.write(magenta(buf))
             self.stderr.flush()
Exemple #16
0
 def dprint(self, buf):
     if self.debug:
         stack = inspect_stack()
         caller_method = stack[1][0].f_code.co_name
         buf = "({0}:{1}): {2}".format(self.name, caller_method, buf)
         if self.logger:
             self.logger.setLevel(DEBUG)
             self.logger.debug(buf)
         else:
             if not buf.endswith("\n"):
                 buf += "\n"
             self.stderr.write(magenta(buf))
             self.stderr.flush()
Exemple #17
0
def real_parser(iostream):
    """Receipt parser. This could use a little work.
    """
    l = getLogger("{}".format(inspect_stack()[1][3]))
    r = dict()
    line = iostream.readline()
    while line:
        l.debug("read: %r", line)
        m = re.match(r"Store #(?P<store>\d+) +(?:eat|tko|tkc) +(?P<month>\d\d)/(?P<day>\d\d)/(?P<year>\d+) (?P<hour>\d\d):(?P<minute>\d\d):(?P<second>\d\d) *\n", line)
        if m:
            l.debug("match: %r", m.groups())
            store, month, day, year, hour, minute, second  = [int(x) for x in m.groups()]
            r["store_number"] = store
            r["date"] = datetime(2000 + year, month, day, hour, minute, second).isoformat(sep=" ")
            break
        else:
            l.debug("no match")
        line = iostream.readline()
    # I should probably parse a little more of the receipt...
    return r
Exemple #18
0
    def get_memberUserObjects(self, ids_only=False):
        """ Get the user objects corresponding to the membership list,
assuming we can."""
        member_groups = self.getProperty('member_groups',
                                         ['%s_member' % self.listId()])
        uids = []
        for gid in member_groups:
            group = self.acl_users.getGroupById(gid)
            uids += group.getUsers()

        if ids_only:
            retval = uids
        else:
            m = 'Getting all the user-objects in "%s"' % self.listId()
            log.warning(m)
            s = inspect_stack()[:2]
            log.warning(s)
            users = []
            for uid in uids:
                user = self.acl_users.getUser(uid)
                if user:
                    users.append(user)
            retval = users
        return retval
    def get_memberUserObjects(self, ids_only=False):
        """ Get the user objects corresponding to the membership list,
assuming we can."""
        member_groups = self.getProperty('member_groups',
                                         ['%s_member' % self.listId()])
        uids = []
        for gid in member_groups:
            group = self.acl_users.getGroupById(gid)
            uids += group.getUsers()

        if ids_only:
            retval = uids
        else:
            m = 'Getting all the user-objects in "%s"' % self.listId()
            log.warning(m)
            s = inspect_stack()[:2]
            log.warning(s)
            users = []
            for uid in uids:
                user = self.acl_users.getUser(uid)
                if user:
                    users.append(user)
            retval = users
        return retval
 def my_method(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__,
                                    inspect_stack()[0][3]))
     l.debug('debug output in my_method')
     l.info('info input in my_method')
 def __init__(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__, inspect_stack()[0][3]))
     l.debug('A instantiated')
     l.info('A info')
     l.error('A exploding!!!')
 def my_interesting_method(self):
     super(B, self).my_interesting_method()
     l = getLogger('{0}.{1}'.format(self.__class__.__name__, inspect_stack()[0][3]))
     l.debug('debug output in my_interesting_method')
     l.info('info input in my_interesting_method')
 def __init__(self):
     super(B, self).__init__()
     l = getLogger('{0}.{1}'.format(self.__class__.__name__, inspect_stack()[0][3]))
     l.debug('B instantiated')
     l.info('B info')
     l.error('B exploding!!!')
 def my_noisy_method(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__, inspect_stack()[0][3]))
     for x in range(1, 10):
         l.info('noisy %d', x)
     l.error('a real warning message!!!')
Exemple #25
0
def web_log(output: str, level: int = 1) -> bool:
    _src = inspect_getfile(inspect_stack()[1][0])
    debug = config.SERVER.debug
    return Log(typ='web', src_file=_src, debug=debug).write(output=output,
                                                            level=level)
Exemple #26
0
def get_caller_name(level):
	"""level = 0 returns current function name"""
	from inspect import stack as inspect_stack
	return inspect_stack()[level + 1][3]
Exemple #27
0
 def delete_oldest_index(self):
     l = getLogger("{}.{}".format(self.__class__.__name__,
                                  inspect_stack()[0][3]))
     oldest_index = self.indices.pop(0)
     l.debug('deleting %r', oldest_index)
     return self.es.delete(oldest_index)
 def my_noisy_method(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__,
                                    inspect_stack()[0][3]))
     for x in range(1, 10):
         l.info('noisy %d', x)
     l.error('a real warning message!!!')
 def my_method(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__, inspect_stack()[0][3]))
     l.debug('debug output in my_method')
     l.info('info input in my_method')
Exemple #30
0
def fns_log(output: str, level: int = 1) -> bool:
    _src = inspect_getfile(inspect_stack()[1][0])
    return Log(src_file=_src).write(output=output, level=level, system=True)
 def stats(self):
     l = getLogger("{}.{}".format(self.__class__.__name__, inspect_stack()[0][3]))
     if self._stats is None:
         self._stats = self.es.get('_stats')
         l.debug('stats: %r', self._stats)
     return self._stats
 def __init__(self):
     l = getLogger("{0}.{1}".format(self.__class__.__name__,
                                    inspect_stack()[0][3]))
     l.debug('A instantiated')
     l.info('A info')
     l.error('A exploding!!!')
 def my_interesting_method(self):
     super(B, self).my_interesting_method()
     l = getLogger('{0}.{1}'.format(self.__class__.__name__,
                                    inspect_stack()[0][3]))
     l.debug('debug output in my_interesting_method')
     l.info('info input in my_interesting_method')
 def delete_oldest_index(self):
     l = getLogger("{}.{}".format(self.__class__.__name__, inspect_stack()[0][3]))
     oldest_index = self.indices.pop(0)
     l.debug('deleting %r', oldest_index)
     return self.es.delete(oldest_index)