-
-
Save Poslovitch/e61a5aa54689ce623bc759b376fd60e0 to your computer and use it in GitHub Desktop.
First try at creating a BentoBox "source code crawler" that'll generate a csv file containing information about the commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Script to run through BentoBox source code and gather data that will be printed to a .md file. | |
Made by Poslovitch on 09/02/2019. | |
Local address: | |
C:\\Users\\Poslovitch\\Documents\\Code\\BentoBox\\src | |
""" | |
import os | |
import re | |
import csv | |
from javaobj import JavaClass | |
srcDirectory = input("Directory (src): ") | |
commands = [] | |
JavaClass.main_package = f'{srcDirectory}\\main\\java' | |
for files in os.walk(JavaClass.main_package): | |
# we get an array with directory, subdirectories and files | |
# so, run through all the files we found | |
for file in files[2]: | |
fileName, fileExtension = os.path.splitext(file) | |
# only continue if this is java code files | |
if fileExtension != ".java": | |
continue | |
java = JavaClass(files[0], fileName) | |
if not java.is_public(): | |
continue | |
if java.is_extends('CompositeCommand') or java.is_extends('ConfirmableCommand'): | |
# get the initialisation info (label, aliases) from the constructor | |
initInfo = re.search('super\(.*?\);', java.content) | |
label = None | |
aliases = [] | |
if initInfo: | |
params = initInfo.group(0).replace(' ', '').replace(');', '').split(',')[1:] | |
label = params[0].replace('"', '') | |
for alias in params[1:]: | |
aliases.append(alias.replace('"', '')) | |
# get the permission | |
permission = None | |
permissionInfo = re.search('setPermission\(.*?\);', java.content) | |
if permissionInfo: | |
permission = "[prefix]." + permissionInfo.group(0).split('"')[1] | |
commands.append({'file': fileName, | |
'label': label, | |
'aliases': ';'.join(aliases), | |
'permission': permission, | |
'author': java.javadoc['author'], | |
'since': java.javadoc['since']}) | |
with open('commands.csv', 'w', newline='') as outputFile: | |
fieldNames = ['file', 'label', 'aliases', 'permission', 'author', 'since'] | |
writer = csv.DictWriter(outputFile, fieldNames) | |
writer.writeheader() | |
writer.writerows(commands) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class JavaClass: | |
main_package = '' | |
def __init__(self, directory, filename): | |
self.directory = directory | |
self.filename = filename | |
self.fullname = f'{directory}\\{filename}.java' | |
self.package = directory.replace(JavaClass.main_package + '\\', '') | |
with open(self.fullname, 'r') as source: | |
self.content = source.read() | |
self.javadoc = self.__read_javadoc() | |
self.methods = [] | |
# private method to extract data from the class's javadoc | |
def __read_javadoc(self): | |
# default result: | |
author = None | |
since = None | |
authorInfo = re.search('@author.*?\n', self.content) | |
if authorInfo: | |
author = authorInfo.group(0).replace('@author', '').strip() | |
sinceInfo = re.search('@since.*?\n', self.content) | |
if sinceInfo: | |
since = sinceInfo.group(0).replace('@since', '').strip() | |
result = {'author': author, 'since': since} | |
return result | |
def is_public(self): | |
if 'public class' in self.content: | |
return True | |
else: | |
return False | |
def is_extends(self, parent): | |
if f'extends {parent}' in self.content: | |
return True | |
else: | |
return False | |
class Command(JavaClass): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment