def main(): if len(sys.argv) != 3: print 'usage: %s generate_context.py tbl_file tags_file' % sys.argv[0] sys.exit(2) tbl_path = sys.argv[1] tags_path = sys.argv[2] tags = ctags.CTags(tags_path) entry = ctags.TagEntry() tbl_file = open(tbl_path, 'r') syscalls = [] for line in tbl_file: syscall = {'num': -1, 'entry': '', 'name': '', 'context':'', 'args': []} match = re.search(r'^(\w+)\t+\w+\t+(\w+)\t+(\w+)', line) if not match: continue num = match.group(1) name = match.group(2) entrypoint = match.group(3) # Rename stub_* entrypoints to sys_* entrypoint = re.sub(r'^stub_', r'sys_', entrypoint) if not tags.find(entry, entrypoint, ctags.TAG_FULLMATCH | ctags.TAG_OBSERVECASE): continue syscall['num'] = int(num) syscall['name'] = name syscall['entry'] = entrypoint found_prototype = False while not found_prototype: if(entry['kind'] == 'prototype'): found_prototype = True elif not tags.findNext(entry): break if not found_prototype: continue args = []; if(entry['signature'] != '(void)'): strargs = entry['signature'].strip('()').split(',') for strarg in strargs: strarg = strarg.strip() arg = {'sig': '', 'refcount': 0, 'context': ''} arg['sig'] = strarg arg['refcount'] = strarg.count('*') args.append(arg) syscall['args'] = args syscalls.append(syscall) tbl_file.close() print simplejson.dumps(syscalls, indent='\t')
def render_code(self, render_markup): should_use_ctags = current_app.should_use_ctags( self.context['repo'], self.context['commit']) if should_use_ctags: if ctags is None: raise ImportError( "Ctags enabled but python-ctags not installed") ctags_base_url = url_for(self.view_name, repo=self.context['repo'].name, rev=self.context['rev'], path='') ctags_tagsfile = CTAGS_CACHE.get_tagsfile( self.context['repo'].path, self.context['commit'].id) ctags_args = { 'ctags': ctags.CTags(ctags_tagsfile.encode( sys.getfilesystemencoding())), 'ctags_baseurl': ctags_base_url, } else: ctags_args = {} return highlight_or_render( force_unicode(self.context['blob_or_tree'].data), self.context['filename'], render_markup, **ctags_args)
def main(tags_file, search_expression): # read tags file tags = ctags.CTags(tags_file) status = tags.setSortType(ctags.TAG_SORTED) # search entry = ctags.TagEntry() found = tags.find(entry, search_expression, ctags.TAG_PARTIALMATCH) if not found: return # save entries entries = [] while True: file = entry['file'] pattern = entry['pattern'] entries.append({'file': file, 'pattern': pattern}) #print entry['pattern'] #print entry['file'] found = tags.findNext(entry) if not found: break for i, entry in enumerate(entries): file = entry['file'] pattern = entry['pattern'] print("{}:{}".format(file, pattern))
def __init__(self, **options): Formatter.__init__(self, **options) self.tagsfile = self._decodeifneeded(options.get('tagsfile', '')) self.tagurlformat = self._decodeifneeded( options.get('tagurlformat', '')) if self.tagsfile: if not ctags: raise RuntimeError('The "ctags" package must to be installed ' 'to be able to use the "tagsfile" feature.') self._ctags = ctags.CTags(self.tagsfile)
def __init__(self, **options): Formatter.__init__(self, **options) self.title = self._decodeifneeded(self.title) self.nowrap = get_bool_opt(options, 'nowrap', False) self.noclasses = get_bool_opt(options, 'noclasses', False) self.classprefix = options.get('classprefix', '') self.cssclass = self._decodeifneeded( options.get('cssclass', 'highlight')) self.cssstyles = self._decodeifneeded(options.get('cssstyles', '')) self.prestyles = self._decodeifneeded(options.get('prestyles', '')) self.cssfile = self._decodeifneeded(options.get('cssfile', '')) self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False) self.tagsfile = self._decodeifneeded(options.get('tagsfile', '')) self.tagurlformat = self._decodeifneeded( options.get('tagurlformat', '')) self.filename = self._decodeifneeded(options.get('filename', '')) self.wrapcode = get_bool_opt(options, 'wrapcode', False) self.span_element_openers = {} self.debug_token_types = get_bool_opt(options, 'debug_token_types', False) if self.tagsfile: if not ctags: raise RuntimeError('The "ctags" package must to be installed ' 'to be able to use the "tagsfile" feature.') self._ctags = ctags.CTags(self.tagsfile) linenos = options.get('linenos', False) if linenos == 'inline': self.linenos = 2 elif linenos: # compatibility with <= 0.7 self.linenos = 1 else: self.linenos = 0 self.linenostart = abs(get_int_opt(options, 'linenostart', 1)) self.linenostep = abs(get_int_opt(options, 'linenostep', 1)) self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0)) self.nobackground = get_bool_opt(options, 'nobackground', False) self.lineseparator = options.get('lineseparator', '\n') self.lineanchors = options.get('lineanchors', '') self.linespans = options.get('linespans', '') self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False) self.hl_lines = set() for lineno in get_list_opt(options, 'hl_lines', []): try: self.hl_lines.add(int(lineno)) except ValueError: pass self._create_stylesheet()
def readFromFile(self, filename, groupByFile, ignorePaths): self.__entries = [] e = ctags.TagEntry() tf = ctags.CTags(filename) while tf.next(e): self.__addFromTagEntry(e, ignorePaths) if groupByFile: self.__entries = self.__sortByFile(self.__entries) else: self.__entries = self.__sortByScope(self.__entries, None) self.reset()
def render_code(self, render_markup): should_use_ctags = current_app.should_use_ctags( self.context['repo'], self.context['commit']) if should_use_ctags: ctags_base_url = url_for(self.view_name, repo=self.context['repo'].name, rev=self.context['rev'], path='') ctags_tagsfile = CTAGS_CACHE.get_tagsfile( self.context['repo'].path, self.context['commit'].id) ctags_args = { 'ctags': ctags.CTags(ctags_tagsfile), 'ctags_baseurl': ctags_base_url, } else: ctags_args = {} return pygmentize(force_unicode(self.context['blob_or_tree'].data), self.context['filename'], render_markup, **ctags_args)
def utility_processor(): _ctags = ctags.CTags(os.path.join(SPEC_DIR, 'spectags')) kinds = { 't': 'type', 's': 'struct', 'm': 'msg', } # Shorthand for getting a site url def get_site_url(path=None, external=False): lang = 'en' if hasattr(g, 'lang') and g.lang: lang = g.lang if path: return url_for('site_show', lang=lang, page=path, _external=external) else: return url_for('site_show', lang=lang, _external=external) def get_spec_url(name): url = url_for('spec_show', name=name, _external=True) # Remove ?lang=xx if '?' in url: url = url[:url.index('?')] return url def get_proposal_url(identifier): name = None for f in os.listdir(PROPOSAL_DIR): if f.startswith(identifier): name = f[:-4] break if not name: return '' url = url_for('proposal_show', name=name, _external=True) # Remove ?lang=xx if '?' in url: url = url[:url.index('?')] return url def get_ctags_url(value): filename, kind = _lookup_ctag(value) # Handle message types if not kind and value.endswith('Message'): value = value[:-7] filename, kind = _lookup_ctag(value) if kind: specname, _ = os.path.splitext(filename) url = get_spec_url(specname) return '%s#%s-%s' % \ (url, kinds[kind], value.lower()) else: return '' def _lookup_ctag(token): entry = ctags.TagEntry() if _ctags.find(entry, token, 0): return entry['file'], entry['kind'] else: return None, None # Shorthand for getting a language-specific url def get_url_with_lang(endpoint, **args): lang = 'en' if hasattr(g, 'lang') and g.lang: lang = g.lang return url_for(endpoint, lang=lang, **args) def is_rtl_lang(lang=None): if not lang: if hasattr(g, 'lang') and g.lang: lang = g.lang else: lang = 'en' return lang in RTL_LANGS # Get a specific language flag, or the flag for the current language def get_flag(lang=None): if not lang: if hasattr(g, 'lang') and g.lang: lang = g.lang else: lang = 'en' return url_for('static', filename='images/flags/'+lang+'.png') # Provide the canonical link to the current page def get_canonical_link(): # Canonical domain should always be HTTPS return 'https://' + CANONICAL_DOMAIN + request.path # Convert an I2P url to an equivalent clearnet one def convert_url_to_clearnet(value): if not value.endswith('.i2p'): # The url being passed in isn't an I2P url, so just return it return value if request.headers.get('X-I2P-Desthash') and not request.headers.get('X-Forwarded-Server'): # The request is from within I2P, so use I2P url return value # The request is either directly from clearnet or through an inproxy try: # Return the known clearnet url corresponding to the I2P url return I2P_TO_CLEAR[value] except KeyError: # The I2P site has no known clearnet address, so use an inproxy return value + INPROXY # Convert an I2P url to an equivalent clearnet one, including HTTPS if necessary def convert_url_to_clearnet_inc_https(value): parts = urlsplit(value) if not parts[1].endswith('.i2p'): # The url being passed in isn't an I2P url, so just return it return value if request.headers.get('X-I2P-Desthash') and not request.headers.get('X-Forwarded-Server'): # The request is from within I2P, so use I2P url return value # The request is either directly from clearnet or through an inproxy try: # Return the known clearnet url corresponding to the I2P url scheme = parts[0] host = I2P_TO_CLEAR[parts[1]] if scheme == 'http' and host not in CLEAR_HTTP: scheme = 'https' return urlunsplit([scheme, host] + list(parts[2:])) except KeyError: # The I2P site has no known clearnet address, so use an inproxy return urlunsplit([parts[0], parts[1] + INPROXY] + list(parts[2:])) # Convert a paginated URL to that of another page def url_for_other_page(page): args = request.view_args.copy() args['page'] = page return url_for(request.endpoint, **args) # Change the theme of the current page def change_theme(theme): args = {} if request.view_args: args = request.view_args.copy() args['theme'] = theme if request.endpoint: return url_for(request.endpoint, **args) # Probably a 404 error page return url_for('main_index', **args) # Shorthand for getting the logo for the current theme def get_logo_for_theme(): logo = 'styles/' + g.theme + '/images/i2plogo.png' if not os.path.isfile(safe_join(STATIC_DIR, logo)): logo = 'images/i2plogo.png' return logo def get_current_version(string=None, ver=None): if string: if ver: return string % ver return string % CURRENT_I2P_VERSION return CURRENT_I2P_VERSION return dict(i2pconv=convert_url_to_clearnet, i2pclr=convert_url_to_clearnet_inc_https, url_for_other_page=url_for_other_page, change_theme=change_theme, logo_url=get_logo_for_theme, site_url=get_site_url, spec_url=get_spec_url, proposal_url=get_proposal_url, ctags_url=get_ctags_url, get_url=get_url_with_lang, is_rtl=is_rtl_lang, get_flag=get_flag, ver=get_current_version, canonical=get_canonical_link, supported_langs=SUPPORTED_LANGS, lang_names=SUPPORTED_LANG_NAMES)
def __init__(self, **options): Formatter.__init__(self, **options) self.title = self._decodeifneeded(self.title) # 语言 self.language = self._decodeifneeded(options.get('language', '')) self.nowrap = get_bool_opt(options, 'nowrap', False) self.noclasses = get_bool_opt(options, 'noclasses', False) self.classprefix = options.get('classprefix', '') self.cssclass = self._decodeifneeded( options.get('cssclass', 'highlight')) self.cssstyles = self._decodeifneeded(options.get('cssstyles', '')) self.prestyles = self._decodeifneeded(options.get('prestyles', '')) self.cssfile = self._decodeifneeded(options.get('cssfile', '')) self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False) self.tagsfile = self._decodeifneeded(options.get('tagsfile', '')) self.tagurlformat = self._decodeifneeded( options.get('tagurlformat', '')) self.filename = self._decodeifneeded(options.get('filename', '')) # 关于ul和li添加的属性 # 一开始就显示行号 self.shownum = get_bool_opt(options, 'shownum', True) # 代码是否折行,默认为True self.linefeed = get_bool_opt(options, 'linefeed', True) # ul自定义的class,默认为ul-wrap self.ulclass = self._decodeifneeded( options.get('ulclass', 'highlight-ul')) # ul自定义的style self.ulstyles = self._decodeifneeded(options.get('ulstyles', '')) # 需要行号的ul的class,默认为numbered self.ulnumclass = self._decodeifneeded( options.get('ulnumclass', 'numbered')) # li自定义的class,默认为li-wrap self.liclass = self._decodeifneeded(options.get('liclass', 'li-wrap')) # li自定义的style self.listyles = self._decodeifneeded(options.get('listyles', '')) # 需要行号的li的class,默认为numbered,该选项用在并不是所有行都需要行号的情况下 self.linumclass = self._decodeifneeded( options.get('linumclass', 'numbered')) # 检查ctags相关,这里的ctags是import的 if self.tagsfile: if not ctags: raise RuntimeError('The "ctags" package must to be installed ' 'to be able to use the "tagsfile" feature.') # 生成ctags self._ctags = ctags.CTags(self.tagsfile) # 行号模式 linenos = 'ul' # options.get('linenos', 'ul') if linenos == 'inline': self.linenos = 2 # inline为2 elif linenos == 'ul': self.linenos = 3 # ul为3 elif linenos == 'table': # compatibility with <= 0.7 self.linenos = 1 else: self.linenos = 0 # 如果没有指定默认为0 # 获取各种选项 self.linenostart = abs(get_int_opt(options, 'linenostart', 1)) self.linenostep = abs(get_int_opt(options, 'linenostep', 1)) self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0)) self.nobackground = get_bool_opt(options, 'nobackground', False) self.lineseparator = options.get('lineseparator', '\n') self.lineanchors = options.get('lineanchors', '') self.linespans = options.get('linespans', '') self.anchorlinenos = options.get('anchorlinenos', False) self.hl_lines = set() # 高亮行号存起来 hl_lines = self._decodeifneeded(options.get('hl_lines', '{}')) self.hl_line_nums = [] try: self.hl_lines = json.loads(hl_lines) for index, color in self.hl_lines.items(): try: self.hl_line_nums.append(int(index)) except ValueError: pass except Exception: if isinstance(hl_lines, (str, )): self.hl_lines = hl_lines.split() elif isinstance(hl_lines, (list, tuple)): self.hl_lines = list(hl_lines) else: raise ValueError( 'Invalid type `hl_lines`; you must give a list or json serialize value' ) self.hl_line_nums = [int(x) for x in self.hl_lines] # 生成style样式 self._create_stylesheet()
def setUp(self): file_path = os.path.join(src_dir, "examples", "tags") self.ctags = ctags.CTags(file_path.encode(sys.getfilesystemencoding()))
def setUp(self): file_path = os.path.join(src_dir, "examples", "tags") self.ctags = ctags.CTags(file_path)