diff --git a/README.md b/README.md index 950d04e..6f00e2b 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,8 @@ The encoding part is delegated to `ffmpeg`, but the code is really modular so changing this is a breeze. To be quicker and avoid the quality issues related to reencoding, the codec "copy" is used: this means that input and output must have the same format. + +testing +----------- + +unit tests can be run with `python setup.py test` diff --git a/server/__init__.py b/server/__init__.py index 4d4ce6f..d3bad95 100644 --- a/server/__init__.py +++ b/server/__init__.py @@ -1,2 +1,2 @@ -import server -import cli +from . import server +from . import cli diff --git a/server/cli.py b/server/cli.py index e2badb9..7435ff3 100644 --- a/server/cli.py +++ b/server/cli.py @@ -9,10 +9,10 @@ logger = logging.getLogger('cli') CWD = os.getcwd() -import forge -import maint -from config_manager import get_config -import server +from . import forge +from . import maint +from .config_manager import get_config +from . import server def pre_check_permissions(): @@ -84,6 +84,7 @@ def common_pre(): def main(): parser = ArgumentParser(description='creates mp3 from live recordings') parser.add_argument('--verbose', '-v', action='count', + default=0, help='Increase verbosity; can be used multiple times') parser.add_argument('--pretend', '-p', action='store_true', default=False, help='Only pretend; no real action will be done') diff --git a/server/techrec.py b/server/db.py similarity index 95% rename from server/techrec.py rename to server/db.py index e5c2ffd..00ff634 100644 --- a/server/techrec.py +++ b/server/db.py @@ -1,6 +1,7 @@ ''' This module contains DB logic ''' +from __future__ import print_function import logging from datetime import datetime, timedelta @@ -11,7 +12,7 @@ from sqlalchemy import create_engine, Column, Integer, String, DateTime, inspect from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base -from config_manager import get_config +from .config_manager import get_config PAGESIZE = 10 @@ -218,7 +219,7 @@ class RecDB: return ret def get_err(self): - print "DB error: %s" % (self.err) + print("DB error: %s" % (self.err)) t = self.err self.err = "" return t @@ -227,25 +228,25 @@ class RecDB: if __name__ == "__main__": def printall(queryres): for record in queryres: - print "Record: %s" % record + print("Record: %s" % record) db = RecDB() - _mytime = datetime(2014, 05, 23, 15, 12, 17) - _endtime = datetime(2014, 05, 24, 17, 45, 17) + _mytime = datetime(2014, 5, 23, 15, 12, 17) + _endtime = datetime(2014, 5, 24, 17, 45, 17) a = Rec(name="Mimmo1", starttime=_mytime, endtime=_endtime) printall(db._search()) sys.exit("End test job") # a = Rec(name="Mimmo1", starttime=_mytime, endtime=None) - print "Aggiunto", db.add(a) + print("Aggiunto", db.add(a)) printall(db.get_all(page_size=5, page=0)) - print "Mimmo " + print("Mimmo ") printall(db._search(name="Mimmo1")) - print "Search" + print("Search") printall(db._search(name="Mimmo1", - starttime=datetime(2014, 05, 24, 15, 16, 1) )) + starttime=datetime(2014, 5, 24, 15, 16, 1) )) a = db.get_by_id(5) a.start() db.delete(1) diff --git a/server/default_config.py b/server/default_config.py index 6a7f6ca..2ac5eb9 100644 --- a/server/default_config.py +++ b/server/default_config.py @@ -33,7 +33,5 @@ try: if resource_isdir('techrec', 'pages'): STATIC_PAGES = resource_filename('techrec', 'pages') STATIC_FILES = resource_filename('techrec', 'static') - else: - print "invece dice", resource_filename('techrec', 'static') except ImportError: logging.exception("Error loading resources from installed part") diff --git a/server/forge.py b/server/forge.py index aaf1179..c174f73 100644 --- a/server/forge.py +++ b/server/forge.py @@ -4,7 +4,7 @@ import os from subprocess import Popen import logging -from config_manager import get_config +from .config_manager import get_config def get_timefile_exact(time): diff --git a/server/maint.py b/server/maint.py index 688a73e..3eb7d2d 100644 --- a/server/maint.py +++ b/server/maint.py @@ -1,10 +1,11 @@ +from __future__ import print_function import sys import logging from sqlalchemy import inspect -from config_manager import get_config -from techrec import RecDB +from .config_manager import get_config +from .db import RecDB def cleanold_cmd(options): log = logging.getLogger('cleanold') @@ -14,7 +15,7 @@ def cleanold_cmd(options): count = len(res) if options.pretend: for rec in res: - print rec + print(rec) else: for rec in res: logging.info("Deleting " + str(rec)) diff --git a/server/processqueue.py b/server/processqueue.py index cf55bd7..d5bbcf4 100644 --- a/server/processqueue.py +++ b/server/processqueue.py @@ -40,11 +40,11 @@ class JobQueue(object): def simulate_long_job(recid=None, starttime=None, endtime=None, name='', filename=None): from time import sleep - print "evviva " + name + print("evviva " + name) sleep(2) - print "lavoro su " + name + print("lavoro su " + name) sleep(2) - print "done su " + name + print("done su " + name) _queue = None @@ -60,15 +60,15 @@ if __name__ == '__main__': def sleep(n): import time - print "Inizio %d" % n + print("Inizio %d" % n) time.sleep(n) - print "Finisco %d" % n + print("Finisco %d" % n) return n get_process_queue().submit(sleep, 3) get_process_queue().submit(sleep, 3) get_process_queue().join() - print get_process_queue().jobs + print(get_process_queue().jobs) delta = (datetime.now() - n).total_seconds() - print delta + print(delta) assert 5 < delta < 7 diff --git a/server/server.py b/server/server.py index 7581c9d..44c637e 100644 --- a/server/server.py +++ b/server/server.py @@ -14,10 +14,10 @@ botlog.setLevel(logging.INFO) botlog.addHandler(logging.StreamHandler(sys.stdout)) bottle._stderr = lambda x: botlog.info(x.strip()) -from techrec import Rec, RecDB -from processqueue import get_process_queue -from forge import create_mp3 -from config_manager import get_config +from .db import Rec, RecDB +from .processqueue import get_process_queue +from .forge import create_mp3 +from .config_manager import get_config def date_read(s): @@ -89,13 +89,13 @@ class RecAPI(Bottle): self.get('/jobs/', callback=self.check_job) def create(self): - req = dict(request.POST.allitems()) + req = dict(request.POST.decode().allitems()) ret = {} logger.debug("Create request %s " % req) now = datetime.now() start = date_read(req['starttime']) if 'starttime' in req else now - name = req['name'].decode('utf8') if 'name' in req else u"" + name = req['name'] if 'name' in req else u"" end = date_read(req['endtime']) if 'endtime' in req else now rec = Rec(name=name, @@ -107,7 +107,7 @@ class RecAPI(Bottle): rec=rec_sanitize(rec)) def delete(self): - req = dict(request.POST.allitems()) + req = dict(request.POST.decode().allitems()) logging.info("Server: request delete %s " % (req)) if 'id' not in req: return self.rec_err("No valid ID") @@ -118,7 +118,7 @@ class RecAPI(Bottle): return self.rec_err("DELETE error: %s" % (self.db.get_err())) def update(self, recid): - req = dict(request.POST.allitems()) + req = dict(request.POST.decode().allitems()) newrec = {} now = datetime.now() @@ -131,7 +131,7 @@ class RecAPI(Bottle): else: newrec['endtime'] = date_read(req['endtime']) if 'name' in req: - newrec["name"] = req['name'].decode('utf8') + newrec["name"] = req['name'] try: logger.info("prima di update") @@ -144,7 +144,7 @@ class RecAPI(Bottle): def generate(self): # prendiamo la rec in causa - recid = dict(request.POST.allitems())['id'] + recid = dict(request.POST.decode().allitems())['id'] rec = self.db._search(_id=recid)[0] if rec.filename is not None and os.path.exists(rec.filename): return {'status': 'ready', @@ -166,8 +166,8 @@ class RecAPI(Bottle): 'endtime': rec.endtime.strftime('%H%M'), 'startdt': rec.starttime.strftime('%y%m%d_%H%M'), 'enddt': rec.endtime.strftime('%y%m%d_%H%M'), - 'name': filter(lambda c: c.isalpha(), - unicodedata.normalize('NFKD', rec.name).encode('ascii', 'ignore')), + 'name': ''.join(filter(lambda c: c.isalpha(), + unicodedata.normalize('NFKD', rec.name).encode('ascii', 'ignore').decode('ascii'))), } self.db.get_session(rec).commit() job_id = self._app.pq.submit( diff --git a/server/test_forge.py b/server/test_forge.py index 9fd6225..4839750 100644 --- a/server/test_forge.py +++ b/server/test_forge.py @@ -2,9 +2,9 @@ from datetime import datetime, timedelta from nose.tools import raises, eq_ -from forge import get_files_and_intervals, get_timefile_exact, round_timefile,\ +from .forge import get_files_and_intervals, get_timefile_exact, round_timefile,\ get_timefile, mp3_join -from config_manager import get_config +from .config_manager import get_config eight = datetime(2014, 5, 30, 20) nine = datetime(2014, 5, 30, 21) diff --git a/setup.py b/setup.py index 06a1d3f..c9ef907 100644 --- a/setup.py +++ b/setup.py @@ -13,15 +13,13 @@ setup( author_email="piuttosto@logorroici.org", packages=["techrec"], package_dir={"techrec": "server"}, - install_requires=[ - "Paste==1.7.5.1", - "SQLAlchemy==0.8.3", - "bottle==0.11.6", - "wsgiref==0.1.2", - ], + install_requires=["Paste~=3.2", "SQLAlchemy==0.8.3", "bottle~=0.12"], classifiers=["Programming Language :: Python :: 2.7"], entry_points={"console_scripts": ["techrec = techrec.cli:main"]}, zip_safe=False, install_package_data=True, package_data={"techrec": ["static/**/*", "pages/*.html"]}, + test_suite="nose.collector", + setup_requires=["nose>=1.0"], + tests_requires=["nose>=1.0"], )