Skip to content

Commit 8d22c1f

Browse files
committed
Place constants in own file and utils function in utils
1 parent 3691c8b commit 8d22c1f

File tree

7 files changed

+54
-34
lines changed

7 files changed

+54
-34
lines changed

pycloud/app.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@
2222
import os
2323
import signal
2424
from time import sleep
25-
from .session import SESSION_DIR, DATA_DIR
25+
from .constants import SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, FILE_LOG, RUN_FOLDER, PID_FILE
2626
from .handlers import CreateMessaging, StatusMessaging, RemoveMessaging, RankMessaging
27-
from .utils import remove, default_val
28-
from .cloud import Cloud, LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, FILE_LOG
27+
from .utils import remove, default_val, required_paths
28+
from .cloud import Cloud
2929
from redis import Redis
3030
from redis.exceptions import ConnectionError
3131
import yaml
3232

33-
RUN_FOLDER = '/var/run/year4000/pycloud/'
34-
PID_FILE = RUN_FOLDER + 'pycloud.pid'
35-
3633

3734
def start_daemon(nodes=None):
3835
""" Spins off a process that runs as a daemon """
@@ -77,7 +74,6 @@ def shutdown_daemon(*args):
7774
os.remove(PID_FILE)
7875
raise SystemExit('Terminating on signal number {0}'.format(args[0]))
7976

80-
8177
def main(nodes=None):
8278
""" Deploy all the needed threads """
8379
nodes = int(0 if nodes is None else nodes)
@@ -152,15 +148,6 @@ def daemon_thread(target, name=None):
152148
thread.start()
153149
return thread
154150

155-
def required_paths():
156-
""" Make sure the needed folders exist """
157-
for folder in (SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER):
158-
if not os.path.exists(folder):
159-
try:
160-
os.makedirs(folder)
161-
finally:
162-
os.chmod(folder, 0o777) # hack fix for an existing bug
163-
164151

165152
if __name__ == '__main__':
166153
_log = logging.getLogger('pycloud')

pycloud/cloud.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@
1818

1919
import os
2020
import sys
21-
import datetime
2221
from threading import Lock
2322
from json import JSONEncoder
2423
from time import time
24+
from .constants import CONFIG_PATH, CONFIG_FILE, LOG_FOLDER, FILE_LOG
2525
from .session import Session
2626
from .utils import generate_id, check_not_none
2727
import psutil
2828

2929

30-
CONFIG_PATH = '/etc/year4000/pycloud/'
31-
CONFIG_FILE = CONFIG_PATH + 'settings.yml'
32-
LOG_FOLDER = '/var/log/year4000/pycloud/'
33-
FILE_LOG = LOG_FOLDER + str(datetime.date.today()) + ".log"
34-
35-
3630
class Cloud:
3731
""" The cloud instance that stores the sessions that are running """
3832

pycloud/constants.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
# Copyright 2016 Year4000.
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
""" Constants needed for the program and setuptools """
18+
19+
import datetime
20+
21+
RUN_FOLDER = '/var/run/year4000/pycloud/'
22+
PID_FILE = RUN_FOLDER + 'pycloud.pid'
23+
24+
CONFIG_PATH = '/etc/year4000/pycloud/'
25+
CONFIG_FILE = CONFIG_PATH + 'settings.yml'
26+
LOG_FOLDER = '/var/log/year4000/pycloud/'
27+
FILE_LOG = LOG_FOLDER + str(datetime.date.today()) + ".log"
28+
29+
CREATE_CHANNEL = 'year4000.pycloud.create'
30+
STATUS_CHANNEL = 'year4000.pycloud.status'
31+
REMOVE_CHANNEL = 'year4000.pycloud.remove'
32+
RANK_CHANNEL = 'year4000.pycloud.rank'
33+
34+
SESSION_DIR = '/var/run/year4000/pycloud/'
35+
DATA_DIR = '/var/lib/year4000/pycloud/'

pycloud/handlers.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020
from json import JSONDecoder
2121
import threading
2222
import logging
23+
from .constants import CREATE_CHANNEL, STATUS_CHANNEL, REMOVE_CHANNEL, RANK_CHANNEL
2324
from .cloud import Rank
2425
from .utils import check_not_none
2526
from redis.exceptions import RedisError
2627

2728

2829
_log = logging.getLogger('pycloud')
29-
CREATE_CHANNEL = 'year4000.pycloud.create'
30-
STATUS_CHANNEL = 'year4000.pycloud.status'
31-
REMOVE_CHANNEL = 'year4000.pycloud.remove'
32-
RANK_CHANNEL = 'year4000.pycloud.rank'
3330

3431

3532
class Messaging:

pycloud/session.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
import logging
2222
import socket
2323
from json import JSONEncoder
24+
from .constants import SESSION_DIR, DATA_DIR
2425
from .utils import check_not_none, generate_id, remove, default_val
2526

2627

2728
_log = logging.getLogger('pycloud')
28-
SESSION_DIR = '/var/run/year4000/pycloud/'
29-
DATA_DIR = '/var/lib/year4000/pycloud/'
3029

3130

3231
class Session:

pycloud/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import shutil
2323
import time
2424
import random
25+
from .constants import SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER
2526

2627

2728
FNULL = open(os.devnull, 'w')
@@ -102,3 +103,13 @@ def copy_update(old, new, can_update=False):
102103
except OSError:
103104
message = 'run again with --update' if can_update else 'you have update it yourself'
104105
print('ERROR: {0} installed, {1}'.format(new, message))
106+
107+
108+
def required_paths():
109+
""" Make sure the needed folders exist """
110+
for folder in (SESSION_DIR, DATA_DIR, LOG_FOLDER, CONFIG_PATH, RUN_FOLDER):
111+
if not os.path.exists(folder):
112+
try:
113+
os.makedirs(folder)
114+
finally:
115+
os.chmod(folder, 0o777) # hack fix for an existing bug

setup.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616

1717
import os
1818
import sys
19-
from pycloud.utils import install, is_root, copy_update
20-
from pycloud.cloud import LOG_FOLDER, CONFIG_PATH, CONFIG_FILE
21-
from pycloud.session import SESSION_DIR, DATA_DIR
22-
from pycloud.app import RUN_FOLDER, required_paths
23-
from pycloud.utils import is_root
19+
from pycloud.utils import install, is_root, copy_update, required_paths
20+
from pycloud.constants import LOG_FOLDER, CONFIG_PATH, CONFIG_FILE, SESSION_DIR, DATA_DIR, RUN_FOLDER
2421
from pycloud import __version__
2522
from setuptools import setup, find_packages
2623

0 commit comments

Comments
 (0)