Skip to content

Commit

Permalink
Mega refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
centaurialpha committed Feb 29, 2020
1 parent c07890b commit e2d5def
Show file tree
Hide file tree
Showing 38 changed files with 1,321 additions and 1,412 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Expand Up @@ -92,8 +92,5 @@ jobs:
name: coverage
before_install:
- pip install pip --upgrade
- pip install pytest
- pip install pytest-cov
- pip install coveralls
- pip install ordered-set
- pip install pytest pytest-cov coveralls ordered-set
script: coveralls
1 change: 0 additions & 1 deletion README.md
@@ -1,6 +1,5 @@

[![Build Status](https://travis-ci.org/centaurialpha/pireal.svg?branch=develop)](https://travis-ci.org/centaurialpha/pireal)
[![Workflow](https://github.com/centaurialpha/pireal/workflows/Main%20Workflow/badge.svg)](https://github.com/centaurialpha/pireal)
[![Coverage Status](https://coveralls.io/repos/github/centaurialpha/pireal/badge.svg?branch=develop)](https://coveralls.io/github/centaurialpha/pireal?branch=develop)
---

Expand Down
6 changes: 3 additions & 3 deletions bin/pireal
@@ -1,7 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2015-2019 Gabriel Acosta <acostadariogabriel@gmail.com>
# Copyright 2015-2020 Gabriel Acosta <acostadariogabriel@gmail.com>
#
# This file is part of Pireal.
#
Expand Down Expand Up @@ -60,7 +60,7 @@ if args.version:
print(__version__)
sys.exit(0)

# Creo los dirs antes de leer logs see #84
# Creo los dirs antes de leer logs. see #84
create_dirs()

from pireal.core import logger # noqa
Expand All @@ -69,4 +69,4 @@ from pireal import resources # noqa
# Set up logger
logger.set_up(args.verbose)

main.start_pireal()
main.start_pireal(args)
1 change: 1 addition & 0 deletions pireal/core/cliparser.py
Expand Up @@ -25,4 +25,5 @@ def get_cli():
parser.add_argument('-d', '--database', help='Database file')
parser.add_argument('-v', '--verbose', action='store_true', help='Verbose')
parser.add_argument('--version', action='store_true', help='Version')
parser.add_argument('--no-check-updates', action='store_true', help='Disable check updates')
return parser
47 changes: 14 additions & 33 deletions pireal/core/db.py
Expand Up @@ -16,18 +16,16 @@
#
# You should have received a copy of the GNU General Public License
# along with Pireal; If not, see <http://www.gnu.org/licenses/>.
import os
import logging

from pireal.core.file_manager import (
get_basename_with_extension,
generate_database,
parse_database_content,
detect_encoding
)
from pireal.core.relation import Relation
from pireal.core.file_manager import File

logger = logging.getLogger(__name__)
logger = logging.getLogger('core.db')


class DBError(Exception):
Expand All @@ -42,8 +40,10 @@ class DB:

def __init__(self, path=None):
self._dirty = False
self._path = path
self._relations = {}
self.file = File(path=path)
if path is not None:
self._dirty = True

@property
def relations(self) -> tuple:
Expand All @@ -58,24 +58,18 @@ def give_relation(self, name: str) -> Relation:
raise NameError(f'Relation {name} not found')
return self._relations[name]

def display_name(self) -> str:
return self.file.display_name

def file_path(self) -> str:
return self._path
return self.file.path

def display_name(self) -> str:
if self.is_new():
return 'new'
return get_basename_with_extension(self._path)
def is_new(self) -> bool:
return self.file.is_new

def is_dirty(self) -> bool:
return self._dirty

def is_new(self) -> bool:
new = True
if self._path is not None:
if os.path.exists(self._path):
new = False
return new

def add(self, relation: Relation):
if relation.name in self._relations:
raise NameError(f'Relation {relation.name} alredy exist')
Expand All @@ -91,26 +85,13 @@ def remove_from_name(self, name: str):
self._dirty = True

def save(self, file_path=None):
if file_path is not None:
self._path = file_path
logger.debug('Generating database in %s', self._path)
logger.debug('Generating database in %s', self.file.path)
db_content = generate_database(self._relations)
with open(self._path, 'w') as fp:
fp.write(db_content)
self.file.save(db_content, path=file_path)
self._dirty = False

def load(self, file_path=None):
if file_path is not None:
self._path = file_path
try:
with open(self._path, 'rb') as fp:
content = fp.read()
encoding = detect_encoding(content)
content = content.decode(encoding)
except (IOError, UnicodeDecodeError) as reason:
logger.exception('Could not open file: %s', self._path)
raise DBIOError(reason)
db_content = parse_database_content(content)
db_content = parse_database_content(self.file.read(file_path))
self._load_relations_from_content(db_content)

def _load_relations_from_content(self, content: list):
Expand Down
50 changes: 50 additions & 0 deletions pireal/core/file_manager.py
Expand Up @@ -26,6 +26,56 @@ class DBParserSyntaxError(Exception):
pass


class FileIOError(Exception):
pass


class File:
"""File representation"""

def __init__(self, path=None):
self._path = path

@property
def path(self) -> str:
return self._path

@property
def display_name(self) -> str:
if self._path is None:
return 'Untitled'
return get_basename_with_extension(self._path)

@property
def filename(self) -> str:
return get_basename_with_extension(self._path)

@property
def is_new(self) -> bool:
file_is_new = True
if self._path is not None and os.path.exists(self._path):
file_is_new = False
return file_is_new

def save(self, content: str, *, path=None):
if path is not None:
self._path = path
with open(self._path, 'w') as fh:
fh.write(content)

def read(self, path=None) -> str:
open_path = path and path or self._path
self._path = open_path
try:
with open(self._path, 'rb') as fh:
content = fh.read()
encoding = detect_encoding(content)
content = content.decode(encoding)
except (IOError, UnicodeDecodeError) as reason:
raise FileIOError(reason)
return content


def get_extension(filename):
""" This function returns the extension of filename
Expand Down
4 changes: 3 additions & 1 deletion pireal/core/relation.py
Expand Up @@ -351,4 +351,6 @@ def __str__(self):
return header + content

def __repr__(self):
return self.__str__()
return (f'Relation(name={self.name}, '
f'degree={self.degree()}, '
f'cardinality={self.cardinality()})')

0 comments on commit e2d5def

Please sign in to comment.