Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enum instead of hard code #45

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions unifree/free.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import unifree
from unifree import utils, log
from unifree.log import ExitCode


def run_migration(
Expand All @@ -32,15 +33,15 @@ def run_migration(

except Exception as e:
log.error(f"Unable to start: {config} is invalid: {e}", exc_info=e)
return 78 # os.EX_CONFIG
return ExitCode.EX_CONFIG.value

if not os.path.exists(source):
log.error(f"Unable to start: source folder does not exist ('{source}')")
return 78 # os.EX_CONFIG
return ExitCode.EX_CONFIG.value

if not os.path.isdir(source):
log.error(f"Unable to start: source folder is not a folder ('{source}')")
return 78 # os.EX_CONFIG
return ExitCode.EX_CONFIG.value

if os.path.exists(destination):
log.info(f"Using existing destination folder '{destination}'")
Expand All @@ -51,7 +52,7 @@ def run_migration(
os.makedirs(destination)
except Exception as e:
log.error(f"Unable to create destination folder '{destination}': {e}", exc_info=e)
return 74 # os.EX_IOERR
return ExitCode.EX_IOERR.value

from unifree.project_migration_strategies import CreateMigrations, ExecuteMigrations

Expand All @@ -67,7 +68,7 @@ def run_migration(
return os.EX_OK
except Exception as e:
log.error(f"Unable to migrate: {e}", exc_info=e)
return 70 # os.EX_SOFTWARE
return ExitCode.EX_SOFTWARE.value


def migrate():
Expand Down Expand Up @@ -113,7 +114,7 @@ def migrate():
args, _ = args_parser.parse_known_args()
except SystemExit:
args_parser.print_usage()
sys.exit(78) # os.EX_CONFIG
sys.exit(ExitCode.EX_CONFIG.value)

args = vars(args)

Expand Down
6 changes: 6 additions & 0 deletions unifree/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import traceback

import unifree
from enum import Enum

class ExitCode(Enum):
EX_SOFTWARE = 70
EX_IOERR = 74
EX_CONFIG = 78

py_logger = logging.getLogger("lion")

Expand Down
31 changes: 22 additions & 9 deletions unifree/source_code_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class CSharpCodeParser:
_parser: tree_sitter.Parser
_config: Dict
_config: Dict[str, any]

def __init__(self, config: Dict) -> None:
self._config = config
Expand Down Expand Up @@ -68,13 +68,26 @@ def _c_sharp_library_path(cls) -> str:
c_sharp_library_folder_path = os.path.join(unifree.project_root, 'vendor', 'build', 'libraries')
return os.path.join(c_sharp_library_folder_path, 'c-sharp.so')

def _replace_macros_with_comments(self, source_str):
result = ''
for line in source_str.split("\n"):
stripped_line = line.rstrip().lstrip()
if stripped_line.startswith("#"):
result += "// " + line + "\n"
@classmethod
def _find_vendor_folder(cls, current_folder_path: str, remaining_depth: int) -> str:
if remaining_depth <= 0:
log.error("Unable to locate 'vendor' folder that is necessary for reading the code. Please make sure your project is setup correctly")
raise RuntimeError(f"Can't locate 'vendor' folder")

if os.path.isdir(current_folder_path):
for file_name in os.listdir(current_folder_path):
if os.path.isdir(os.path.join(current_folder_path, file_name)) and file_name == 'vendor':
return current_folder_path

return cls._find_vendor_folder(os.path.dirname(current_folder_path), remaining_depth - 1)

def _replace_macros_with_comments(self, source_str : str) -> str:
result = []
lines = [line.strip() for line in source_str.split("\n") if line.strip() != '']
for line in lines:
if line.startswith("#"):
result.append("// " + line)
else:
result += line + "\n"
result.append(line)

return result
return "\n".join(result)