xref: /Universal-ctags/parsers/pythonloggingconfig.c (revision 6b1a862e526d5017f9f212a321f59d67c859d521)
1b041a632SMasatake YAMATO /*
2b041a632SMasatake YAMATO  *   pythonLoggingConfig.c
3b041a632SMasatake YAMATO  *
4b041a632SMasatake YAMATO  *   Copyright (c) 2016, Masatake YAMATO <yamato@redhat.com>
5b041a632SMasatake YAMATO  *   Copyright (c) 2016, Red Hat, K.K.
6b041a632SMasatake YAMATO  *
7b041a632SMasatake YAMATO  *   This source code is released for free distribution under the terms of the
8b041a632SMasatake YAMATO  *   GNU General Public License version 2 or (at your option) any later version.
9b041a632SMasatake YAMATO  *
10b041a632SMasatake YAMATO  *   This module contains functions for generating tags for config files of
11b041a632SMasatake YAMATO  *   python's logging.config module.
12b041a632SMasatake YAMATO  *
13b041a632SMasatake YAMATO  *   https://docs.python.org/2/library/logging.config.html
14b041a632SMasatake YAMATO  *
15b041a632SMasatake YAMATO  */
16b041a632SMasatake YAMATO 
17b041a632SMasatake YAMATO /*
18b041a632SMasatake YAMATO *   INCLUDE FILES
19b041a632SMasatake YAMATO */
20b041a632SMasatake YAMATO #include "general.h"  /* must always come first */
21b041a632SMasatake YAMATO 
22b041a632SMasatake YAMATO #include "entry.h"
2387214e15SMasatake YAMATO #include "iniconf.h"
24b041a632SMasatake YAMATO #include "kind.h"
250d502ef0SMasatake YAMATO #include "parse.h"
26b041a632SMasatake YAMATO #include "read.h"
27b041a632SMasatake YAMATO #include "routines.h"
28b041a632SMasatake YAMATO #include <string.h>
29b041a632SMasatake YAMATO 
30b041a632SMasatake YAMATO /*
31b041a632SMasatake YAMATO *   DATA DEFINITIONS
32b041a632SMasatake YAMATO */
33b041a632SMasatake YAMATO typedef enum {
34b041a632SMasatake YAMATO 	K_LOGGER_SECTION,
35b041a632SMasatake YAMATO 	K_LOGGER_QUALNAME,
36b041a632SMasatake YAMATO } pythonLoggingConfigKind;
37b041a632SMasatake YAMATO 
38e112e8abSMasatake YAMATO static kindDefinition PythonLoggingConfigKinds [] = {
39ce990805SThomas Braun 	{ true, 'L', "loggerSection", "logger sections" },
40ce990805SThomas Braun 	{ true, 'q', "qualname",      "logger qualnames" },
41b041a632SMasatake YAMATO };
42b041a632SMasatake YAMATO 
43b041a632SMasatake YAMATO #define LOGGER_PREFIX "logger_"
44b041a632SMasatake YAMATO #define LOGGER_LEN (sizeof("logger_") - 1)
45b041a632SMasatake YAMATO 
46ed9b023fSMasatake YAMATO struct sPythonLoggingConfigSubparser {
47ed9b023fSMasatake YAMATO 	iniconfSubparser iniconf;
48ed9b023fSMasatake YAMATO 	int index;
49ed9b023fSMasatake YAMATO };
50b041a632SMasatake YAMATO 
newDataCallback(iniconfSubparser * iniconf,const char * section,const char * key,const char * value)51ed9b023fSMasatake YAMATO static void newDataCallback (iniconfSubparser *iniconf,
52ed9b023fSMasatake YAMATO 							 const char *section, const char *key, const char *value)
53b041a632SMasatake YAMATO {
54b041a632SMasatake YAMATO 	tagEntryInfo e;
55b041a632SMasatake YAMATO 
56b041a632SMasatake YAMATO 	if (section && (strncmp (LOGGER_PREFIX, section, LOGGER_LEN) == 0))
57b041a632SMasatake YAMATO 	{
58b041a632SMasatake YAMATO 		if (key == NULL && value == NULL)
59b041a632SMasatake YAMATO 		{
60b041a632SMasatake YAMATO 			const char *logger = section + LOGGER_LEN;
61b041a632SMasatake YAMATO 			if (logger [0] == '\0')
62b041a632SMasatake YAMATO 				goto out;
63b041a632SMasatake YAMATO 
6416a2541cSMasatake YAMATO 			initTagEntry (&e, logger, K_LOGGER_SECTION);
65ed9b023fSMasatake YAMATO 			((struct sPythonLoggingConfigSubparser *)iniconf)->index = makeTagEntry (&e);
66b041a632SMasatake YAMATO 		}
67b041a632SMasatake YAMATO 		else if (key && (strcmp (key, "qualname") == 0)
68b041a632SMasatake YAMATO 			 && value && value[0] != '\0')
69b041a632SMasatake YAMATO 		{
7016a2541cSMasatake YAMATO 			initTagEntry (&e, value, K_LOGGER_QUALNAME);
71ed9b023fSMasatake YAMATO 			e.extensionFields.scopeIndex = ((struct sPythonLoggingConfigSubparser*)iniconf)->index;
72b041a632SMasatake YAMATO 			makeTagEntry (&e);
73b041a632SMasatake YAMATO 		}
74b041a632SMasatake YAMATO 	}
75b041a632SMasatake YAMATO 
76b041a632SMasatake YAMATO out:
77b041a632SMasatake YAMATO 	return;
78b041a632SMasatake YAMATO }
79b041a632SMasatake YAMATO 
probeLanguage(const char * section,const char * key CTAGS_ATTR_UNUSED,const char * value CTAGS_ATTR_UNUSED)80e4d16241SMasatake YAMATO static bool probeLanguage (const char *section, const char *key CTAGS_ATTR_UNUSED, const char *value CTAGS_ATTR_UNUSED)
81b041a632SMasatake YAMATO {
82b041a632SMasatake YAMATO 	if (section && (strncmp (LOGGER_PREFIX, section, LOGGER_LEN) == 0))
83ce990805SThomas Braun 		return true;
84b041a632SMasatake YAMATO 	else
85ce990805SThomas Braun 		return false;
86b041a632SMasatake YAMATO }
87b041a632SMasatake YAMATO 
88ed9b023fSMasatake YAMATO 
exclusiveSubparserChosenCallback(subparser * s,void * data CTAGS_ATTR_UNUSED)892082dbbfSColomban Wendling static void exclusiveSubparserChosenCallback (subparser *s, void *data CTAGS_ATTR_UNUSED)
90ed9b023fSMasatake YAMATO {
91ed9b023fSMasatake YAMATO 	((struct sPythonLoggingConfigSubparser *)s)->index = CORK_NIL;
92ed9b023fSMasatake YAMATO }
93b041a632SMasatake YAMATO 
findPythonLoggingConfigTags(void)94b041a632SMasatake YAMATO static void findPythonLoggingConfigTags (void)
95b041a632SMasatake YAMATO {
96ed9b023fSMasatake YAMATO 	scheduleRunningBaseparser (0);
97b041a632SMasatake YAMATO }
98b041a632SMasatake YAMATO 
PythonLoggingConfigParser(void)99b041a632SMasatake YAMATO extern parserDefinition* PythonLoggingConfigParser (void)
100b041a632SMasatake YAMATO {
101ed9b023fSMasatake YAMATO 	static struct sPythonLoggingConfigSubparser pythonLoggingConfigSubparser = {
102ed9b023fSMasatake YAMATO 		.iniconf = {
103ed9b023fSMasatake YAMATO 			.subparser = {
104ed9b023fSMasatake YAMATO 				.direction = SUBPARSER_BI_DIRECTION,
105ed9b023fSMasatake YAMATO 				.exclusiveSubparserChosenNotify = exclusiveSubparserChosenCallback,
106ed9b023fSMasatake YAMATO 			},
107ed9b023fSMasatake YAMATO 			.probeLanguage = probeLanguage,
108ed9b023fSMasatake YAMATO 			.newDataNotify = newDataCallback,
109ed9b023fSMasatake YAMATO 		},
110ed9b023fSMasatake YAMATO 	};
111b041a632SMasatake YAMATO 	static parserDependency dependencies [] = {
112ed9b023fSMasatake YAMATO 		[0] = { DEPTYPE_SUBPARSER, "Iniconf", &pythonLoggingConfigSubparser },
113b041a632SMasatake YAMATO 	};
114b041a632SMasatake YAMATO 
115ed9b023fSMasatake YAMATO 	parserDefinition* const def = parserNew ("PythonLoggingConfig");
116b041a632SMasatake YAMATO 	def->dependencies = dependencies;
117b041a632SMasatake YAMATO 	def->dependencyCount = ARRAY_SIZE (dependencies);
118b041a632SMasatake YAMATO 
11909ae690fSMasatake YAMATO 	def->kindTable      = PythonLoggingConfigKinds;
120b041a632SMasatake YAMATO 	def->kindCount  = ARRAY_SIZE (PythonLoggingConfigKinds);
121b041a632SMasatake YAMATO 	def->parser     = findPythonLoggingConfigTags;
122*6b1a862eSMasatake YAMATO 	def->useCork    = CORK_QUEUE;
123b041a632SMasatake YAMATO 
124b041a632SMasatake YAMATO 	return def;
125b041a632SMasatake YAMATO }
126