Skip to content

Commit 2b1fec5

Browse files
committed
bug fixes
1 parent c8ec1a2 commit 2b1fec5

8 files changed

Lines changed: 81 additions & 89 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
# Created by https://www.gitignore.io/api/macos,linux,python,eclipse,pycharm,windows,sublimetext,visualstudio
33

4+
### Python Setup ###
5+
6+
pyBackup.egg-info/
7+
dist/
8+
build/
9+
10+
411
### Eclipse ###
512

613
.metadata

HISTORY.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#V 0.0.1
2+
- Initial commit
3+
4+
# V0.0.2
5+
- download via <pip>
6+
- Email System
7+
- Backup Log
8+
- Cleaned Code
9+
- Minor Fixes
10+
11+
# V0.0.3
12+
- Import errors
13+
- Script/Module mode availability
14+
- Reorganized codes
15+
- Reorganized example
16+
- History File
17+
- Changed name to pyBackup (Easier to import)
18+
19+
#V0.0.4 [IMPORTANT CHANGES]
20+
- Changed atributes name to meet good practices
21+
- <backup_itens> cannot be empty anymore [IMPORTANT]
22+
- minor fixes for importing <settings> errors

example/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
'tls' : True,
3232
}
3333

34-
mail_subject = 'Backup System. {0}'
34+
mail_subject = 'Backup. {0}'
3535

3636
mail_body = '''
3737
Backup System

pybackup/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.2'
1+
__version__ = '0.0.4'

pybackup/backup.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
'''
2525

2626
from os import path, makedirs
27-
from sys import argv
2827
from shutil import copy, copytree, ignore_patterns
2928
from errno import ENOTDIR
3029
from . import utils
@@ -36,42 +35,39 @@ class Backup():
3635
'''
3736

3837
def __init__(self, verbose=False):
39-
40-
# Initial attributes
41-
try:
42-
self.verbose = verbose
43-
self.log = utils.LogSystem(verbose=verbose)
44-
self.backup_list = []
45-
46-
except BaseException as e:
47-
self.log.update_log(e)
48-
raise BaseException(e)
38+
39+
self.verbose = verbose
40+
self.log = utils.LogSystem(verbose=verbose)
41+
self.backup_list = []
42+
4943

5044
# Check if settings.py is properly configured and import it
5145
try:
5246
from settings import backup_items, sub_folder_name, target_folder, ignore_extensions, now, mailing_list, send_mail, mail_body, mail_subject, mail_settings
5347

54-
except ImportError as e:
55-
self.log.update_log('Settings file or attributes not found')
56-
raise ImportError(e)
48+
except:
49+
msg = 'Check if <settings.py> is in your current folder'
50+
self.log.update_log(msg)
51+
raise BaseException(msg) from None
52+
5753

5854
else:
59-
self.backupItems = backup_items
60-
self.subfolderName = sub_folder_name
61-
self.targetFolder = target_folder
62-
self.ignoredExtensions = ignore_extensions
55+
self.backup_itens = backup_items
56+
self.sub_folder_name = sub_folder_name
57+
self.target_folder = target_folder
58+
self.ignored_extensions = ignore_extensions
6359
self.now = now
64-
self.sendMail = send_mail
60+
self.send_mail = send_mail
6561

6662
# Only import if <send_mail> is set to True
6763
if send_mail:
68-
self.mailSubject = mail_subject
69-
self.mailBody = mail_body
64+
self.mail_subject = mail_subject
65+
self.mail_body = mail_body
7066
self.mail = utils.MailSystem(*mailing_list, **mail_settings)
7167

7268
# Check mailing list
73-
if len(self.mail.excludedMails) > 0:
74-
self.log.update_log('Invalid mail addresses detected: %s' % self.mail.excludedMails, 'INFO')
69+
if len(self.mail.excluded_list) > 0:
70+
self.log.update_log('Invalid mail addresses detected: %s' % self.mail.excluded_list, 'INFO')
7571

7672

7773
def clean_list(self):
@@ -80,14 +76,14 @@ def clean_list(self):
8076
to backup
8177
'''
8278

83-
# Case <backupItems> is empty
84-
if self.backupItems == []:
85-
self.backup_list.append(path.abspath(path.dirname(argv[0])))
86-
87-
return None
79+
# Case <backup_itens> is empty
80+
if self.backup_itens == []:
81+
msg = "After version 0.0.4 <backup_itens> cannot be empty"
82+
self.log.update_log(msg)
83+
raise BaseException(msg) from None
8884

8985
# Add items
90-
for item in self.backupItems:
86+
for item in self.backup_itens:
9187
if path.isfile(path.abspath(item)) or path.isdir(path.abspath(item)):
9288
self.backup_list.append(path.abspath(item))
9389
else:
@@ -99,7 +95,7 @@ def process_item(self, source, destination):
9995
Backup a single item
10096
'''
10197
try:
102-
copytree(source, destination, ignore=ignore_patterns(*self.ignoredExtensions))
98+
copytree(source, destination, ignore=ignore_patterns(*self.ignored_extensions))
10399

104100
except OSError as e:
105101
if e.errno == ENOTDIR:
@@ -120,7 +116,7 @@ def process_list(self):
120116
'''
121117
Process every item from a <backup_list>
122118
'''
123-
default_dest = path.abspath(path.join(self.targetFolder, self.subfolderName))
119+
default_dest = path.abspath(path.join(self.target_folder, self.sub_folder_name))
124120

125121
for item in self.backup_list:
126122
if path.isdir(item):
@@ -142,9 +138,9 @@ def run(self):
142138
self.process_list()
143139

144140
# Send email if configured to do so
145-
if self.sendMail:
141+
if self.send_mail:
146142

147-
subject = self.mailSubject.format(self.now)
148-
body = self.mailBody.format(self.now, self.backupItems, self.targetFolder)
143+
subject = self.mail_subject.format(self.now)
144+
body = self.mail_body.format(self.now, self.backup_itens, self.target_folder)
149145

150146
self.mail.send(subject, body)

pybackup/utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from time import localtime, strftime
66
from re import match, IGNORECASE
77

8-
def getDateTime():
8+
def get_date_time():
99
return strftime("%Y-%m-%d-%H:%M:%S", localtime())
1010

1111
class LogSystem():
@@ -19,7 +19,7 @@ def __init__(self, verbose=False):
1919
logging.basicConfig(filename='backup.log', level=logging.INFO)
2020

2121
def update_time(self):
22-
self.now = getDateTime()
22+
self.now = get_date_time()
2323

2424
def update_log(self, exception, type='ERROR'):
2525
self.update_time()
@@ -47,15 +47,15 @@ def __init__(self, *mail_list, **config):
4747

4848

4949
# Mailing List
50-
self.mailingList = []
51-
self.excludedMails = []
50+
self.mailing_list = []
51+
self.excluded_list = []
5252

5353
for item in self.mailing_list:
5454
if match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", item, IGNORECASE):
55-
self.mailingList.append(item)
55+
self.mailing_list.append(item)
5656

5757
else:
58-
self.excludedMails.append(item)
58+
self.excluded_list.append(item)
5959

6060
# Create SMTP object
6161

@@ -64,29 +64,29 @@ def connect(self):
6464
connect to SMTP server an return the SMTP object
6565
'''
6666
try:
67-
SMTP_obj = SMTP(self.smtp_server, self.smtp_port)
67+
stmp_obj = SMTP(self.smtp_server, self.smtp_port)
6868

6969
except SMTPException as e:
7070
raise "Could not create SMTP object: %s" % e
7171

7272
# StartTLS
7373
if self.tls:
7474
try:
75-
SMTP_obj.ehlo()
76-
SMTP_obj.starttls()
75+
stmp_obj.ehlo()
76+
stmp_obj.starttls()
7777

7878
except SMTPException as e:
7979
print("Could not open a TLS connection: %s" % e)
8080

8181
# login to SMTP
8282
try:
83-
SMTP_obj.ehlo()
84-
SMTP_obj.login(self.username, self.pasword)
83+
stmp_obj.ehlo()
84+
stmp_obj.login(self.username, self.pasword)
8585

8686
except SMTPException as e:
8787
print("Could not logon: %s" % e)
8888

89-
return SMTP_obj
89+
return stmp_obj
9090

9191
def send(self, subject, message):
9292
'''
@@ -96,12 +96,12 @@ def send(self, subject, message):
9696
smtp = self.connect()
9797

9898
header = 'from: %s\n' % self.mail_address
99-
header += 'to: %s\n' % ','.join(self.mailingList)
99+
header += 'to: %s\n' % ','.join(self.mailing_list)
100100
header += 'subject: %s\n' % subject
101101
message = header + '\r\n\r\n' + message
102102

103103
try:
104-
smtp.sendmail(self.mail_address, self.mailingList, message)
104+
smtp.sendmail(self.mail_address, self.mailing_list, message)
105105
smtp.quit()
106106

107107
except SMTPException as e:

settings.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
with open('README.md') as readme_file:
99
readme = readme_file.read()
1010

11+
with open('HISTORY.md') as history_file:
12+
history = history_file.read()
13+
1114
requirements = ['']
1215

1316
test_requirements = [
1417
# TODO: put package test requirements here
1518
]
1619

1720
setup(
18-
name='PyBackup',
19-
version='0.0.2',
21+
name='pyBackup',
22+
version='0.0.4',
2023
description=('Python module for Backup routines'
2124
'of files and folders'),
22-
long_description=readme,
25+
long_description=readme + '\n\n' + history,
2326
author="Fabricio Roberto reinert",
2427
author_email='fabricio.reinert@live.com',
2528
url='https://github.com/FRReinert/PyBackup',

0 commit comments

Comments
 (0)