Skip to content

Commit

Permalink
Delete analysis part (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbirillo authored Aug 27, 2021
1 parent 1442316 commit 1c3a75f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 158 deletions.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ radon==4.5.0
# extra libraries and frameworks
django==3.2
requests==2.25.1
argparse==1.4.0
pyyaml==5.4.1
argparse==1.4.0
148 changes: 2 additions & 146 deletions src/python/review/common/file_system.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import linecache
import os
import pickle
import re
import shutil
import tempfile
from contextlib import contextmanager
from enum import Enum, unique
from pathlib import Path
from typing import Any, Callable, List, Optional, Tuple, Union

import yaml
from typing import Callable, List, Tuple, Union


@unique
Expand All @@ -34,39 +29,13 @@ class Extension(Enum):
KT = '.kt'
JS = '.js'
KTS = '.kts'
XLSX = '.xlsx'
CSV = '.csv'
PICKLE = '.pickle'
JSON = '.json'
HTML = '.html'

# Image extensions
PNG = '.png'
JPG = '.jpg'
JPEG = '.jpeg'
WEBP = '.webp'
SVG = '.svg'
PDF = '.pdf'
EPS = '.eps'

# Not empty extensions are returned with a dot, for example, '.txt'
# If file has no extensions, an empty one ('') is returned
@classmethod
def get_extension_from_file(cls, file: str) -> 'Extension':
def get_extension_from_file(cls, file: Union[Path, str]) -> 'Extension':
return Extension(os.path.splitext(file)[1])

@classmethod
def get_image_extensions(cls) -> List['Extension']:
return [
Extension.PNG,
Extension.JPG,
Extension.JPEG,
Extension.WEBP,
Extension.SVG,
Extension.PDF,
Extension.EPS,
]


ItemCondition = Callable[[str], bool]

Expand All @@ -75,13 +44,6 @@ def all_items_condition(name: str) -> bool:
return True


def extension_file_condition(extension: Extension) -> ItemCondition:
def has_this_extension(name: str) -> bool:
return get_extension_from_file(name) == extension

return has_this_extension


# To get all files or subdirs (depends on the last parameter) from root that match item_condition
# Note that all subdirs or files already contain the full path for them
def get_all_file_system_items(
Expand All @@ -105,53 +67,6 @@ def get_all_file_system_items(
return items


def match_condition(regex: str) -> ItemCondition:
def does_name_match(name: str) -> bool:
return re.fullmatch(regex, name) is not None
return does_name_match


def serialize_data_and_write_to_file(path: Path, data: Any) -> None:
os.makedirs(get_parent_folder(path), exist_ok=True)
with open(path, 'wb') as f:
p = pickle.Pickler(f)
p.dump(data)


def deserialize_data_from_file(path: Path) -> Any:
with open(path, 'rb') as f:
u = pickle.Unpickler(f)
return u.load()


def parse_yaml(path: Union[Path, str]) -> Any:
with open(path) as file:
return yaml.safe_load(file)


# For getting name of the last folder or file
# For example, returns 'folder' for both 'path/data/folder' and 'path/data/folder/'
def get_name_from_path(path: Union[Path, str], with_extension: bool = True) -> str:
head, tail = os.path.split(path)
# Tail can be empty if '/' is at the end of the path
file_name = tail or os.path.basename(head)
if not with_extension:
file_name = os.path.splitext(file_name)[0]
elif get_extension_from_file(Path(file_name)) == Extension.EMPTY:
raise ValueError('Cannot get file name with extension, because the passed path does not contain it')
return file_name


def pair_in_and_out_files(in_files: List[Path], out_files: List[Path]) -> List[Tuple[Path, Path]]:
pairs = []
for in_file in in_files:
out_file = Path(re.sub(r'in(?=[^in]*$)', 'out', str(in_file)))
if out_file not in out_files:
raise ValueError(f'List of out files does not contain a file for {in_file}')
pairs.append((in_file, out_file))
return pairs


# TODO: Need testing
@contextmanager
def new_temp_dir() -> Path:
Expand All @@ -163,17 +78,6 @@ def new_temp_file(suffix: Extension = Extension.EMPTY) -> Tuple[str, str]:
yield tempfile.mkstemp(suffix=suffix.value)


# File should contain the full path and its extension.
# Create all parents if necessary
def create_file(file_path: Union[str, Path], content: str):
file_path = Path(file_path)

os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w+') as f:
f.writelines(content)
yield Path(file_path)


def get_file_line(path: Path, line_number: int):
return linecache.getline(
str(path),
Expand All @@ -188,54 +92,6 @@ def get_content_from_file(file_path: Path, encoding: str = Encoding.ISO_ENCODING
return content if not to_strip_nl else content.rstrip('\n')


# Not empty extensions are returned with a dot, for example, '.txt'
# If file has no extensions, an empty one ('') is returned
def get_extension_from_file(file: Union[Path, str]) -> Extension:
return Extension(os.path.splitext(file)[1])


def get_restricted_extension(file_path: Optional[Union[str, Path]] = None,
available_values: List[Extension] = None) -> Extension:
if file_path is None:
return Extension.EMPTY
ext = Extension.get_extension_from_file(file_path)
if available_values is not None and ext not in available_values:
raise ValueError(f'Invalid extension. '
f'Available values are: {list(map(lambda e: e.value, available_values))}.')
return ext


def remove_slash(path: str) -> str:
return path.rstrip('/')


def remove_directory(directory: Union[str, Path]) -> None:
if os.path.isdir(directory):
shutil.rmtree(directory, ignore_errors=True)


def add_slash(path: str) -> str:
if not path.endswith('/'):
path += '/'
return path


def get_parent_folder(path: Union[Path, str], to_add_slash: bool = False) -> Path:
path = remove_slash(str(path))
parent_folder = '/'.join(path.split('/')[:-1])
if to_add_slash:
parent_folder = add_slash(parent_folder)
return Path(parent_folder)


def copy_directory(source: Union[str, Path], destination: Union[str, Path], dirs_exist_ok: bool = True):
shutil.copytree(source, destination, dirs_exist_ok=dirs_exist_ok)


def copy_file(source: Union[str, Path], destination: Union[str, Path]):
shutil.copy(source, destination)


# Before using it, check that there are no line breaks in the string
def __is_line_empty(line: str) -> bool:
return len(line.strip()) == 0
Expand Down
4 changes: 2 additions & 2 deletions src/python/review/common/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List

from src.python.review.application_config import LanguageVersion
from src.python.review.common.file_system import Extension, get_extension_from_file
from src.python.review.common.file_system import Extension


@unique
Expand Down Expand Up @@ -51,7 +51,7 @@ def from_value(cls, value: str, default=None):


def guess_file_language(file_path: Path) -> Language:
return EXTENSION_TO_LANGUAGE.get(get_extension_from_file(file_path), Language.UNKNOWN)
return EXTENSION_TO_LANGUAGE.get(Extension.get_extension_from_file(file_path), Language.UNKNOWN)


def filter_paths_by_language(file_paths: List[Path], language: Language) -> List[Path]:
Expand Down
5 changes: 0 additions & 5 deletions src/python/review/common/subprocess_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,3 @@ def run_in_subprocess(command: List[str]) -> str:
logger.debug('%s\'s stderr:\n%s' % (command[0], stderr))

return stdout


def run_and_wait(command: List[str]) -> None:
process = subprocess.Popen(command)
process.wait()
6 changes: 3 additions & 3 deletions src/python/review/reviewers/utils/metadata_exploration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from typing import Dict, List, Set, Union

from src.python.review.common.file_system import get_all_file_system_items, get_extension_from_file
from src.python.review.common.file_system import Extension, get_all_file_system_items
from src.python.review.common.language import guess_file_language, Language


Expand All @@ -16,7 +16,7 @@ class FileMetadata:

@property
def extension(self) -> str:
return get_extension_from_file(self.path).value
return Extension.get_extension_from_file(self.path).value


@dataclass
Expand All @@ -39,7 +39,7 @@ def size_bytes(self) -> int:
def extension_to_files(self) -> Dict[str, List[FileMetadata]]:
extension_to_files = defaultdict(list)
for file in self.inner_files:
extension_to_files[get_extension_from_file(file.path)].append(file)
extension_to_files[Extension.get_extension_from_file(file.path)].append(file)
return extension_to_files

@property
Expand Down

0 comments on commit 1c3a75f

Please sign in to comment.