<commit_before>from __future__ import print_function
import os
import cPickle as pickle
from importlib import import_module
from time import time
from weakref import WeakKeyDictionary
from email.utils import mktime_tz, parsedate_tz
from w3lib.http import headers_raw_to_dict, headers_dict_to_raw
from scrapy.http import Headers
from scrapy.responsetypes import responsetypes
from scrapy.utils.request import request_fingerprint
from scrapy.utils.project import data_path
from scrapy.utils.httpobj import urlparse_cached

class DummyPolicy(object):

    def __init__(self, settings):
        self.ignore_schemes = settings.getlist('HTTPCACHE_IGNORE_SCHEMES')
        self.ignore_http_codes = [int(x) for x in settings.getlist('HTTPCACHE_IGNORE_HTTP_CODES')]

    def should_cache_request(self, request):
        return urlparse_cached(request).scheme not in self.ignore_schemes

    def should_cache_response(self, response, request):
        return response.status not in self.ignore_http_codes

    def is_cached_response_fresh(self, response, request):
        return True

    def is_cached_response_valid(self, cachedresponse, response, request):
        return True

class RFC2616Policy(object):

    MAXAGE = 3600 * 24 * 365  # one year

    def __init__(self, settings):
        self.ignore_schemes = settings.getlist('HTTPCACHE_IGNORE_SCHEMES')
        self._cc_parsed = WeakKeyDictionary()

    def _parse_cachecontrol(self, r):
        if r not in self._cc_parsed:
            cch = r.headers.get('Cache-Control', '')
            self._cc_parsed[r] = parse_cachecontrol(cch)
        return self._cc_parsed[r]

    def should_cache_request(self, request):
        if urlparse_cached(request).scheme in self.ignore_schemes:
            return False
        cc = self._parse_cachecontrol(request)
        # obey user-agent directive "Cache-Control: no-store"
        if 'no-store' in cc:
            return False
        # Any other is eligible for caching
        return True

    def should_cache_response(self, response, request):
        # What is cacheable - http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec14.9.1
        # Response cacheability - http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
        # Status code 206 is not included because cache can not deal with partial contents
        cc = self._parse_cachecontrol(response)
        # obey directive "Cache-Control: no-store"
        if 'no-store' in cc:
            return False
        # Never cache 304 (Not Modified) responses
        elif response.status == 304:
            return False
        # Any hint on response expiration is good
        elif 'max-age' in cc or 'Expires' in response.headers:
            return True
        # Firefox fallbacks this statuses to one year expiration if none is set
        elif response.status in (300, 301, 308):
            return True
        # Other statuses without expiration requires at least one validator
        elif response.status in (200, 203, 401):
            return 'Last-Modified' in response.headers or 'ETag' in response.headers
        # Any other is probably not eligible for caching
        # Makes no sense to cache responses that does not contain expiration
        # info and can not be revalidated
        else:
            return False

    def is_cached_response_fresh(self, cachedresponse, request):
        cc = self._parse_cachecontrol(cachedresponse)
        ccreq = self._parse_cachecontrol(request)
        if 'no-cache' in cc or 'no-cache' in ccreq:
            return False

        now = time()
        freshnesslifetime = self._compute_freshness_lifetime(cachedresponse, request, now)
        currentage = self._compute_current_age(cachedresponse, request, now)
        if currentage < freshnesslifetime:
            return True
        # Cached response is stale, try to set validators if any
        self._set_conditional_validators(request, cachedresponse)
        return False

    def is_cached_response_valid(self, cachedresponse, response, request):
        return response.status == 304

    def _set_conditional_validators(self, request, cachedresponse):
        if 'Last-Modified' in cachedresponse.headers:
            request.headers['If-Modified-Since'] = cachedresponse.headers['Last-Modified']

        if 'ETag' in cachedresponse.headers:
            request.headers['If-None-Match'] = cachedresponse.headers['ETag']

    def _compute_freshness_lifetime(self, response, request, now):
        # Reference nsHttpResponseHead::ComputeFresshnessLifetime
        # http://dxr.mozilla.org/mozilla-central/netwerk/protocol/http/nsHttpResponseHead.cpp.html#l259
        cc = self._parse_cachecontrol(response)
        if 'max-age' in cc:
            try:
                return max(0, int(cc['max-age']))
            except ValueError:
                pass

        # Parse date header or synthesize it if none exists
        date = rfc1123_to_epoch(response.headers.get('Date')) or now

        # Try HTTP/1.0 Expires header
        if 'Expires' in response.headers:
            expires = rfc1123_to_epoch(response.headers['Expires'])
            # When parsing Expires header fails RFC 2616 section 14.21 says we
            # should treat this as an expiration time in the past.
            return max(0, expires - date) if expires else 0

        # Fallback to heuristic using last-modified header
        # This is not in RFC but on Firefox caching implementation
        lastmodified = rfc1123_to_epoch(response.headers.get('Last-Modified'))
        if lastmodified and lastmodified <= date:
            return (date - lastmodified) / 10

        # This request can be cached indefinitely
        if response.status in (300, 301, 308):
            return self.MAXAGE

        # Insufficient information to compute fresshness lifetime
        return 0

    def _compute_current_age(self, response, request, now):
        # Reference nsHttpResponseHead::ComputeCurrentAge
        # http://dxr.mozilla.org/mozilla-central/netwerk/protocol/http/nsHttpResponseHead.cpp.html
        currentage = 0
        # If Date header is not set we assume it is a fast connection, and
        # clock is in sync with the server
        date = rfc1123_to_epoch(response.headers.get('Date')) or now
        if now > date:
            currentage = now - date

        if 'Age' in response.headers:
            try:
                age = int(response.headers['Age'])
                currentage = max(currentage, age)
            except ValueError:
                pass

        return currentage

class DbmCacheStorage(object):

    def __init__(self, settings):
        self.cachedir = data_path(settings['HTTPCACHE_DIR'], createdir=True)
        self.expiration_secs = settings.getint('HTTPCACHE_EXPIRATION_SECS')
        self.dbmodule = import_module(settings['HTTPCACHE_DBM_MODULE'])
        self.db = None

    def open_spider(self, spider):
        dbpath = os.path.join(self.cachedir, '%s.db' % spider.name)
        self.db = self.dbmodule.open(dbpath, 'c')

    def close_spider(self, spider):
        self.db.close()

    def retrieve_response(self, spider, request):
        data = self._read_data(spider, request)
        if data is None:
            return  # not cached
        url = data['url']
        status = data['status']
        headers = Headers(data['headers'])
        body = data['body']
        respcls = responsetypes.from_args(headers=headers, url=url)
        response = respcls(url=url, headers=headers, status=status, body=body)
        return response

    def store_response(self, spider, request, response):
        key = self._request_key(request)
        data = {
            'status': response.status,
            'url': response.url,
            'headers': dict(response.headers),
            'body': response.body,
        }
        self.db['%s_data' % key] = pickle.dumps(data, protocol=2)
        self.db['%s_time' % key] = str(time())

    def _read_data(self, spider, request):
        key = self._request_key(request)
        db = self.db
        tkey = '%s_time' % key
        if tkey not in db:
            return  # not found

        ts = db[tkey]
        if 0 < self.expiration_secs < time() - float(ts):
            return  # expired

        return pickle.loads(db['%s_data' % key])

    def _request_key(self, request):
        return request_fingerprint(request)

class FilesystemCacheStorage(object):

    def __init__(self, settings):
        self.cachedir = data_path(settings['HTTPCACHE_DIR'])
        self.expiration_secs = settings.getint('HTTPCACHE_EXPIRATION_SECS')

    def open_spider(self, spider):
        pass

    def close_spider(self, spider):
        pass

    def retrieve_response(self, spider, request):
        """Return response if present in cache, or None otherwise."""
        metadata = self._read_meta(spider, request)
        if metadata is None:
            return  # not cached
        rpath = self._get_request_path(spider, request)
        with open(os.path.join(rpath, 'response_body'), 'rb') as f:
            body = f.read()
        with open(os.path.join(rpath, 'response_headers'), 'rb') as f:
            rawheaders = f.read()
        url = metadata.get('response_url')
        status = metadata['status']
        headers = Headers(headers_raw_to_dict(rawheaders))
        respcls = responsetypes.from_args(headers=headers, url=url)
        response = respcls(url=url, headers=headers, status=status, body=body)
        return response

    def store_response(self, spider, request, response):
        """Store the given response in the cache."""
        rpath = self._get_request_path(spider, request)
        if not os.path.exists(rpath):
            os.makedirs(rpath)
        metadata = {
            'url': request.url,
            'method': request.method,
            'status': response.status,
            'response_url': response.url,
            'timestamp': time(),
        }
        with open(os.path.join(rpath, 'meta'), 'wb') as f:
            f.write(repr(metadata))
        with open(os.path.join(rpath, 'pickled_meta'), 'wb') as f:
            pickle.dump(metadata, f, protocol=2)
        with open(os.path.join(rpath, 'response_headers'), 'wb') as f:
            f.write(headers_dict_to_raw(response.headers))
        with open(os.path.join(rpath, 'response_body'), 'wb') as f:
            f.write(response.body)
        with open(os.path.join(rpath, 'request_headers'), 'wb') as f:
            f.write(headers_dict_to_raw(request.headers))
        with open(os.path.join(rpath, 'request_body'), 'wb') as f:
            f.write(request.body)

    def _get_request_path(self, spider, request):
        key = request_fingerprint(request)
        return os.path.join(self.cachedir, spider.name, key[0:2], key)

    def _read_meta(self, spider, request):
        rpath = self._get_request_path(spider, request)
        metapath = os.path.join(rpath, 'pickled_meta')
        if not os.path.exists(metapath):
            return  # not found
        mtime = os.stat(rpath).st_mtime
        if 0 < self.expiration_secs < time() - mtime:
            return  # expired
        with open(metapath, 'rb') as f:
            return pickle.load(f)

def parse_cachecontrol(header):
    """Parse Cache-Control header

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9

    >>> parse_cachecontrol('public, max-age=3600')
    {'public': None, 'max-age': '3600'}
    >>> parse_cachecontrol('')
    {}

    """
    directives = {}
    for directive in header.split(','):
        key, sep, val = directive.strip().partition('=')
        if key:
            directives[key.lower()] = val if sep else None
    return directives

def rfc1123_to_epoch(date_str):
    try:
        return mktime_tz(parsedate_tz(date_str))
    except Exception:
        return None
<commit_msg>Fix httpcache doctest that assumed dictionary order<commit_after>from __future__ import print_function
import os
import cPickle as pickle
from importlib import import_module
from time import time
from weakref import WeakKeyDictionary
from email.utils import mktime_tz, parsedate_tz
from w3lib.http import headers_raw_to_dict, headers_dict_to_raw
from scrapy.http import Headers
from scrapy.responsetypes import responsetypes
from scrapy.utils.request import request_fingerprint
from scrapy.utils.project import data_path
from scrapy.utils.httpobj import urlparse_cached

class DummyPolicy(object):

    def __init__(self, settings):
        self.ignore_schemes = settings.getlist('HTTPCACHE_IGNORE_SCHEMES')
        self.ignore_http_codes = [int(x) for x in settings.getlist('HTTPCACHE_IGNORE_HTTP_CODES')]

    def should_cache_request(self, request):
        return urlparse_cached(request).scheme not in self.ignore_schemes

    def should_cache_response(self, response, request):
        return response.status not in self.ignore_http_codes

    def is_cached_response_fresh(self, response, request):
        return True

    def is_cached_response_valid(self, cachedresponse, response, request):
        return True

class RFC2616Policy(object):

    MAXAGE = 3600 * 24 * 365  # one year

    def __init__(self, settings):
        self.ignore_schemes = settings.getlist('HTTPCACHE_IGNORE_SCHEMES')
        self._cc_parsed = WeakKeyDictionary()

    def _parse_cachecontrol(self, r):
        if r not in self._cc_parsed:
            cch = r.headers.get('Cache-Control', '')
            self._cc_parsed[r] = parse_cachecontrol(cch)
        return self._cc_parsed[r]

    def should_cache_request(self, request):
        if urlparse_cached(request).scheme in self.ignore_schemes:
            return False
        cc = self._parse_cachecontrol(request)
        # obey user-agent directive "Cache-Control: no-store"
        if 'no-store' in cc:
            return False
        # Any other is eligible for caching
        return True

    def should_cache_response(self, response, request):
        # What is cacheable - http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec14.9.1
        # Response cacheability - http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
        # Status code 206 is not included because cache can not deal with partial contents
        cc = self._parse_cachecontrol(response)
        # obey directive "Cache-Control: no-store"
        if 'no-store' in cc:
            return False
        # Never cache 304 (Not Modified) responses
        elif response.status == 304:
            return False
        # Any hint on response expiration is good
        elif 'max-age' in cc or 'Expires' in response.headers:
            return True
        # Firefox fallbacks this statuses to one year expiration if none is set
        elif response.status in (300, 301, 308):
            return True
        # Other statuses without expiration requires at least one validator
        elif response.status in (200, 203, 401):
            return 'Last-Modified' in response.headers or 'ETag' in response.headers
        # Any other is probably not eligible for caching
        # Makes no sense to cache responses that does not contain expiration
        # info and can not be revalidated
        else:
            return False

    def is_cached_response_fresh(self, cachedresponse, request):
        cc = self._parse_cachecontrol(cachedresponse)
        ccreq = self._parse_cachecontrol(request)
        if 'no-cache' in cc or 'no-cache' in ccreq:
            return False

        now = time()
        freshnesslifetime = self._compute_freshness_lifetime(cachedresponse, request, now)
        currentage = self._compute_current_age(cachedresponse, request, now)
        if currentage < freshnesslifetime:
            return True
        # Cached response is stale, try to set validators if any
        self._set_conditional_validators(request, cachedresponse)
        return False

    def is_cached_response_valid(self, cachedresponse, response, request):
        return response.status == 304

    def _set_conditional_validators(self, request, cachedresponse):
        if 'Last-Modified' in cachedresponse.headers:
            request.headers['If-Modified-Since'] = cachedresponse.headers['Last-Modified']

        if 'ETag' in cachedresponse.headers:
            request.headers['If-None-Match'] = cachedresponse.headers['ETag']

    def _compute_freshness_lifetime(self, response, request, now):
        # Reference nsHttpResponseHead::ComputeFresshnessLifetime
        # http://dxr.mozilla.org/mozilla-central/netwerk/protocol/http/nsHttpResponseHead.cpp.html#l259
        cc = self._parse_cachecontrol(response)
        if 'max-age' in cc:
            try:
                return max(0, int(cc['max-age']))
            except ValueError:
                pass

        # Parse date header or synthesize it if none exists
        date = rfc1123_to_epoch(response.headers.get('Date')) or now

        # Try HTTP/1.0 Expires header
        if 'Expires' in response.headers:
            expires = rfc1123_to_epoch(response.headers['Expires'])
            # When parsing Expires header fails RFC 2616 section 14.21 says we
            # should treat this as an expiration time in the past.
            return max(0, expires - date) if expires else 0

        # Fallback to heuristic using last-modified header
        # This is not in RFC but on Firefox caching implementation
        lastmodified = rfc1123_to_epoch(response.headers.get('Last-Modified'))
        if lastmodified and lastmodified <= date:
            return (date - lastmodified) / 10

        # This request can be cached indefinitely
        if response.status in (300, 301, 308):
            return self.MAXAGE

        # Insufficient information to compute fresshness lifetime
        return 0

    def _compute_current_age(self, response, request, now):
        # Reference nsHttpResponseHead::ComputeCurrentAge
        # http://dxr.mozilla.org/mozilla-central/netwerk/protocol/http/nsHttpResponseHead.cpp.html
        currentage = 0
        # If Date header is not set we assume it is a fast connection, and
        # clock is in sync with the server
        date = rfc1123_to_epoch(response.headers.get('Date')) or now
        if now > date:
            currentage = now - date

        if 'Age' in response.headers:
            try:
                age = int(response.headers['Age'])
                currentage = max(currentage, age)
            except ValueError:
                pass

        return currentage

class DbmCacheStorage(object):

    def __init__(self, settings):
        self.cachedir = data_path(settings['HTTPCACHE_DIR'], createdir=True)
        self.expiration_secs = settings.getint('HTTPCACHE_EXPIRATION_SECS')
        self.dbmodule = import_module(settings['HTTPCACHE_DBM_MODULE'])
        self.db = None

    def open_spider(self, spider):
        dbpath = os.path.join(self.cachedir, '%s.db' % spider.name)
        self.db = self.dbmodule.open(dbpath, 'c')

    def close_spider(self, spider):
        self.db.close()

    def retrieve_response(self, spider, request):
        data = self._read_data(spider, request)
        if data is None:
            return  # not cached
        url = data['url']
        status = data['status']
        headers = Headers(data['headers'])
        body = data['body']
        respcls = responsetypes.from_args(headers=headers, url=url)
        response = respcls(url=url, headers=headers, status=status, body=body)
        return response

    def store_response(self, spider, request, response):
        key = self._request_key(request)
        data = {
            'status': response.status,
            'url': response.url,
            'headers': dict(response.headers),
            'body': response.body,
        }
        self.db['%s_data' % key] = pickle.dumps(data, protocol=2)
        self.db['%s_time' % key] = str(time())

    def _read_data(self, spider, request):
        key = self._request_key(request)
        db = self.db
        tkey = '%s_time' % key
        if tkey not in db:
            return  # not found

        ts = db[tkey]
        if 0 < self.expiration_secs < time() - float(ts):
            return  # expired

        return pickle.loads(db['%s_data' % key])

    def _request_key(self, request):
        return request_fingerprint(request)

class FilesystemCacheStorage(object):

    def __init__(self, settings):
        self.cachedir = data_path(settings['HTTPCACHE_DIR'])
        self.expiration_secs = settings.getint('HTTPCACHE_EXPIRATION_SECS')

    def open_spider(self, spider):
        pass

    def close_spider(self, spider):
        pass

    def retrieve_response(self, spider, request):
        """Return response if present in cache, or None otherwise."""
        metadata = self._read_meta(spider, request)
        if metadata is None:
            return  # not cached
        rpath = self._get_request_path(spider, request)
        with open(os.path.join(rpath, 'response_body'), 'rb') as f:
            body = f.read()
        with open(os.path.join(rpath, 'response_headers'), 'rb') as f:
            rawheaders = f.read()
        url = metadata.get('response_url')
        status = metadata['status']
        headers = Headers(headers_raw_to_dict(rawheaders))
        respcls = responsetypes.from_args(headers=headers, url=url)
        response = respcls(url=url, headers=headers, status=status, body=body)
        return response

    def store_response(self, spider, request, response):
        """Store the given response in the cache."""
        rpath = self._get_request_path(spider, request)
        if not os.path.exists(rpath):
            os.makedirs(rpath)
        metadata = {
            'url': request.url,
            'method': request.method,
            'status': response.status,
            'response_url': response.url,
            'timestamp': time(),
        }
        with open(os.path.join(rpath, 'meta'), 'wb') as f:
            f.write(repr(metadata))
        with open(os.path.join(rpath, 'pickled_meta'), 'wb') as f:
            pickle.dump(metadata, f, protocol=2)
        with open(os.path.join(rpath, 'response_headers'), 'wb') as f:
            f.write(headers_dict_to_raw(response.headers))
        with open(os.path.join(rpath, 'response_body'), 'wb') as f:
            f.write(response.body)
        with open(os.path.join(rpath, 'request_headers'), 'wb') as f:
            f.write(headers_dict_to_raw(request.headers))
        with open(os.path.join(rpath, 'request_body'), 'wb') as f:
            f.write(request.body)

    def _get_request_path(self, spider, request):
        key = request_fingerprint(request)
        return os.path.join(self.cachedir, spider.name, key[0:2], key)

    def _read_meta(self, spider, request):
        rpath = self._get_request_path(spider, request)
        metapath = os.path.join(rpath, 'pickled_meta')
        if not os.path.exists(metapath):
            return  # not found
        mtime = os.stat(rpath).st_mtime
        if 0 < self.expiration_secs < time() - mtime:
            return  # expired
        with open(metapath, 'rb') as f:
            return pickle.load(f)

def parse_cachecontrol(header):
    """Parse Cache-Control header

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9

    >>> parse_cachecontrol('public, max-age=3600') == {'public': None,
    ...                                                'max-age': '3600'}
    True
    >>> parse_cachecontrol('') == {}
    True

    """
    directives = {}
    for directive in header.split(','):
        key, sep, val = directive.strip().partition('=')
        if key:
            directives[key.lower()] = val if sep else None
    return directives

def rfc1123_to_epoch(date_str):
    try:
        return mktime_tz(parsedate_tz(date_str))
    except Exception:
        return None
<|endoftext|>


<commit_before>from db import DB
<commit_msg>Root package is namespace<commit_after>try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

from db import DB
<|endoftext|>


<commit_before>from lcapy import *
from lcapy.phasor import PhasorDomainExpression
from lcapy.sexpr import LaplaceDomainVoltage
import unittest
import sympy as sym
from lcapy.sym import omega0sym

class LcapyTester(unittest.TestCase):

    """Unit tests for lcapy

    """

    def assertEqual2(self, ans1, ans2, comment):

        try:
            self.assertEqual(ans1, ans2, comment)
        except AssertionError as e:
            ans1.pprint()
            ans2.pprint()
            raise AssertionError(e)

    def test_R(self):
        """Lcapy: check R

        """
        a = R(10)
        self.assertEqual(a.Z, 10, "Z incorrect.")
        self.assertEqual(a.Y * 10, 1, "Y incorrect.")
        self.assertEqual2(a.Voc.s, 0, "Voc incorrect.")
        self.assertEqual2(a.Isc.s, 0, "Isc incorrect.")
        self.assertEqual2(a.voc, 0, "voc incorrect.")
        self.assertEqual2(a.isc, 0, "isc incorrect.")

    def test_L(self):
        """Lcapy: check L

        """
        a = L(10, 5)
        self.assertEqual2(a.Z(s), 10 * s, "Zs incorrect.")
        self.assertEqual2(a.Y(s), 1 / (10 * s), "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 10 * j * omega, "Z incorrect.")
        self.assertEqual2(a.Y(jw), 1 / (10 * j * omega), "Y incorrect.")
        self.assertEqual2(a.Voc.s, -10 * 5, "Voc incorrect.")
        self.assertEqual2(a.Isc.s, -5 / s, "Isc incorrect.")

    def test_C(self):
        """Lcapy: check C

        """
        a = C(10, 5)

        self.assertEqual2(a.Z(s), 1 / (10 * s), "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10 * s, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1 / (10 * j * omega), "Z incorrect.")
        self.assertEqual2(a.Y(jw), 10 * j * omega, "Y incorrect.")
        self.assertEqual2(a.Voc.s, 5 / s, "Voc incorrect.")
        self.assertEqual(a.Isc.s, 50, "Isc incorrect.")

    def test_R_series_R(self):
        """Lcapy: check R + R

        """
        a = R(10) + R(5)
        b = a.simplify()
        self.assertEqual2(b.R, 15, "R incorrect.")
        self.assertEqual2(b.Z, 15, "Z incorrect.")
        self.assertEqual(b.Voc.s, 0, "Voc incorrect.")
        self.assertEqual(b.Isc.s, 0, "Isc incorrect.")
        self.assertEqual2(type(b), R, "type incorrect.")

    def test_L_series_L(self):
        """Lcapy: check L + L

        """
        a = L(10) + L(5)
        b = a.simplify()
        self.assertEqual2(b.L, 15, "L incorrect.")
        self.assertEqual2(b.Z(s), 15 * s, "Zs incorrect.")
        self.assertEqual2(b.Z(jw), 15 * j * omega, "Z incorrect.")
        self.assertEqual2(type(b), L, "type incorrect.")

    def test_C_series_C(self):
        """Lcapy: check C + C

        """
        a = C(10) + C(15)
        b = a.simplify()
        self.assertEqual2(b.C, 6, "C incorrect.")
        self.assertEqual2(b.Z(s), 1 / (6 * s), "Zs incorrect.")
        self.assertEqual2(b.Z(jw), 1 / (6 * j * omega), "Z incorrect.")
        self.assertEqual2(type(b), C, "type incorrect.")

    def test_Vdc_series_Vdc(self):
        """Lcapy: check Vdc + Vdc

        """
        a = Vdc(10) + Vdc(5)
        b = a.simplify()
        self.assertEqual2(b.voc, Vdc(15).voc, "voc incorrect.")
        self.assertEqual(b.Voc.dc, 15, "Voc incorrect.")
        self.assertEqual2(type(b), Vdc, "type incorrect.")
        self.assertEqual2(b.voc, 15, "voc incorrect.")

    def test_Vdc_series_R(self):
        """Lcapy: check Vdc + R

        """
        a = Vdc(10) + R(5)
        self.assertEqual2(a.voc, Vdc(10).voc, "voc incorrect.")
        self.assertEqual(a.Voc.dc, 10, "Voc incorrect.")
        self.assertEqual(a.V.dc, 10, "Voc incorrect.")
        self.assertEqual(a.I, 0, "I incorrect.")
        self.assertEqual(a.v(0), 10, "Voc incorrect.")
        self.assertEqual(a.i(0), 0, "I incorrect.")

        b = a.norton()
        self.assertEqual2(b.voc, Vdc(10).voc, "voc incorrect.")
        self.assertEqual(b.Voc.dc, 10, "Voc incorrect.")
        self.assertEqual(b.V.dc, 10, "Voc incorrect.")
        self.assertEqual(b.I, 0, "I incorrect.")
        self.assertEqual(b.v(0), 10, "Voc incorrect.")
        self.assertEqual(b.i(0), 0, "I incorrect.")

    def test_Vac_series_R(self):
        """Lcapy: check Vac + R

        """
        a = Vac(10) + R(5)
        self.assertEqual2(a.voc, Vac(10).voc, "voc incorrect.")
        self.assertEqual(a.Voc.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(a.V.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(a.I, 0, "I incorrect.")
        self.assertEqual(a.v(0), 10, "Voc incorrect.")
        self.assertEqual(a.i(0), 0, "I incorrect.")

        b = a.norton()
        self.assertEqual2(b.voc, Vac(10).voc, "voc incorrect.")
        self.assertEqual(b.Voc.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(b.V.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(b.I, 0, "I incorrect.")
        self.assertEqual(b.v(0), 10, "Voc incorrect.")
        self.assertEqual(b.i(0), 0, "I incorrect.")

    def test_R_series_L(self):
        """Lcapy: check R + L

        """
        a = R(10) + L(5)
        self.assertEqual2(a.Z(s), 5 * s + 10, "Zs incorrect.")

    def test_R_parallel_R(self):
        """Lcapy: check R | R

        """
        a = R(10) | R(15)
        b = a.simplify()
        self.assertEqual2(b.R, 6, "R incorrect.")
        self.assertEqual2(b.Z, 6, "Z incorrect.")
        self.assertEqual2(type(b), R, "type incorrect.")

    def test_L_parallel_L(self):
        """Lcapy: check L | L

        """
        a = L(10) | L(15)
        b = a.simplify()
        self.assertEqual2(b.L, 6, "L incorrect.")
        self.assertEqual2(b.Z(s), 6 * s, "Zs incorrect.")
        self.assertEqual2(type(b), L, "type incorrect.")

    def test_C_parallel_C(self):
        """Lcapy: check C | C

        """
        a = C(10) | C(15)
        b = a.simplify()
        self.assertEqual2(b.C, 25, "C incorrect.")
        self.assertEqual2(b.Z(s), 1 / (25 * s), "Zs incorrect.")
        self.assertEqual2(type(b), C, "type incorrect.")

    def test_I_parallel_I(self):
        """Lcapy: check I | I

        """
        a = Idc(10) | Idc(5)
        b = a.simplify()
        self.assertEqual2(b.isc, Idc(15).isc, "isc incorrect.")
        self.assertEqual2(b.Isc.dc, 15, "Isc incorrect.")
        self.assertEqual2(type(b), Idc, "type incorrect.")
        self.assertEqual2(b.isc, 15, "isc incorrect.")

    def test_load(self):
        """Lcapy: check load

        """

        a = Shunt(R(10) + Vdc(5))
        b = a.load(R(30))

        self.assertEqual2(type(b), Ser, "type incorrect.")
        self.assertEqual2(b.Z, 7.5, "Shunt loaded R incorrect Z.")

    def test_open_circuit(self):
        """Lcapy: check open_circuit

        """

        a = Shunt(R(10) + Vdc(5))
        b = a.open_circuit()

        self.assertEqual2(b.Z, 10, "incorrect Z.")
        self.assertEqual2(b.Voc.laplace(), LaplaceDomainVoltage(5) / s, "incorrect V.")

    def test_short_circuit(self):
        """Lcapy: check short_circuit

        """

        a = Series(R(10) + Vdc(5))
        b = a.short_circuit()

        self.assertEqual2(b.Z, 10, "incorrect Z.")
        self.assertEqual2(b.Voc.laplace(), LaplaceDomainVoltage(5) / s, "incorrect V.")

    def test_v(self):
        """Lcapy: check inverse Laplace for voltage sources"""

        omega0 = symbol('omega_0', real=True)

        a = Vdc(10)
        self.assertEqual(a.voc, 10, "DC incorrect.")

        a = Vstep(10)
        self.assertEqual2(a.voc, 10 * Heaviside(t), "Step incorrect.")

        a = Vac(10)
        self.assertEqual2(a.voc, 10 * cos(omega0 * t), "AC incorrect.")

    def test_i(self):
        """Lcapy: check inverse Laplace for current sources"""

        omega0 = symbol('omega_0', real=True)

        a = Idc(10)
        self.assertEqual(a.isc, 10, "DC incorrect.")

        a = Istep(10)
        self.assertEqual2(a.isc, 10 * Heaviside(t), "Step incorrect.")

        a = Iac(10)
        self.assertEqual2(a.isc, 10 * cos(omega0 * t), "AC incorrect.")

    def test_CPE(self):
        """Lcapy: check CPE

        """
        a = CPE(10, 1)

        self.assertEqual2(a.Z(s), 1 / (10 * s), "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10 * s, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1 / (10 * j * omega), "Zw incorrect.")
        self.assertEqual2(a.Y(jw), 10 * j * omega, "Yw incorrect.")

        a = CPE(10, 0)

        self.assertEqual2(a.Z(s), 1.0 / 10, "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1.0 / 10, "Zw incorrect.")
        self.assertEqual2(a.Y(jw), 10, "Yw incorrect.")

        a = CPE(10, 0.5)

        self.assertEqual2(a.Y(s), 10 * sqrt(s), "Ys incorrect.")

    def simplify1(self):

        a = I(0) + Y(1)
        b = a.simplify()

        self.assertEqual2(b._Z, None, "Z incorrect.")
        self.assertEqual2(b._Y, 1, "Y incorrect.")

    def test_cpt(self):

        self.assertEqual2(type(voltage(2).cpt()), Vdc , "-> Vdc")
        self.assertEqual2(type(voltage(cos(t)).cpt()), Vac, "-> Vac")

        self.assertEqual2(type(current(2).cpt()), Idc, "-> Idc")
        self.assertEqual2(type(current(cos(t)).cpt()), Iac, "-> Iac")

        self.assertEqual2(type(impedance(2).cpt()), R , "-> R")
        self.assertEqual2(type(impedance(2 * s).cpt()), L, "-> L")
        self.assertEqual2(type(impedance(2 / s).cpt()), C, "-> C")

        self.assertEqual2(type(admittance(2).cpt()), G, "-> G")
        self.assertEqual2(type(admittance(2 * s).cpt()), C, "-> C")
        self.assertEqual2(type(admittance(2 / s).cpt()), L, "-> L")

<commit_msg>Defer ac cpt tests<commit_after>from lcapy import *
from lcapy.phasor import PhasorDomainExpression
from lcapy.sexpr import LaplaceDomainVoltage
import unittest
import sympy as sym
from lcapy.sym import omega0sym

class LcapyTester(unittest.TestCase):

    """Unit tests for lcapy

    """

    def assertEqual2(self, ans1, ans2, comment):

        try:
            self.assertEqual(ans1, ans2, comment)
        except AssertionError as e:
            ans1.pprint()
            ans2.pprint()
            raise AssertionError(e)

    def test_R(self):
        """Lcapy: check R

        """
        a = R(10)
        self.assertEqual(a.Z, 10, "Z incorrect.")
        self.assertEqual(a.Y * 10, 1, "Y incorrect.")
        self.assertEqual2(a.Voc.s, 0, "Voc incorrect.")
        self.assertEqual2(a.Isc.s, 0, "Isc incorrect.")
        self.assertEqual2(a.voc, 0, "voc incorrect.")
        self.assertEqual2(a.isc, 0, "isc incorrect.")

    def test_L(self):
        """Lcapy: check L

        """
        a = L(10, 5)
        self.assertEqual2(a.Z(s), 10 * s, "Zs incorrect.")
        self.assertEqual2(a.Y(s), 1 / (10 * s), "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 10 * j * omega, "Z incorrect.")
        self.assertEqual2(a.Y(jw), 1 / (10 * j * omega), "Y incorrect.")
        self.assertEqual2(a.Voc.s, -10 * 5, "Voc incorrect.")
        self.assertEqual2(a.Isc.s, -5 / s, "Isc incorrect.")

    def test_C(self):
        """Lcapy: check C

        """
        a = C(10, 5)

        self.assertEqual2(a.Z(s), 1 / (10 * s), "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10 * s, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1 / (10 * j * omega), "Z incorrect.")
        self.assertEqual2(a.Y(jw), 10 * j * omega, "Y incorrect.")
        self.assertEqual2(a.Voc.s, 5 / s, "Voc incorrect.")
        self.assertEqual(a.Isc.s, 50, "Isc incorrect.")

    def test_R_series_R(self):
        """Lcapy: check R + R

        """
        a = R(10) + R(5)
        b = a.simplify()
        self.assertEqual2(b.R, 15, "R incorrect.")
        self.assertEqual2(b.Z, 15, "Z incorrect.")
        self.assertEqual(b.Voc.s, 0, "Voc incorrect.")
        self.assertEqual(b.Isc.s, 0, "Isc incorrect.")
        self.assertEqual2(type(b), R, "type incorrect.")

    def test_L_series_L(self):
        """Lcapy: check L + L

        """
        a = L(10) + L(5)
        b = a.simplify()
        self.assertEqual2(b.L, 15, "L incorrect.")
        self.assertEqual2(b.Z(s), 15 * s, "Zs incorrect.")
        self.assertEqual2(b.Z(jw), 15 * j * omega, "Z incorrect.")
        self.assertEqual2(type(b), L, "type incorrect.")

    def test_C_series_C(self):
        """Lcapy: check C + C

        """
        a = C(10) + C(15)
        b = a.simplify()
        self.assertEqual2(b.C, 6, "C incorrect.")
        self.assertEqual2(b.Z(s), 1 / (6 * s), "Zs incorrect.")
        self.assertEqual2(b.Z(jw), 1 / (6 * j * omega), "Z incorrect.")
        self.assertEqual2(type(b), C, "type incorrect.")

    def test_Vdc_series_Vdc(self):
        """Lcapy: check Vdc + Vdc

        """
        a = Vdc(10) + Vdc(5)
        b = a.simplify()
        self.assertEqual2(b.voc, Vdc(15).voc, "voc incorrect.")
        self.assertEqual(b.Voc.dc, 15, "Voc incorrect.")
        self.assertEqual2(type(b), Vdc, "type incorrect.")
        self.assertEqual2(b.voc, 15, "voc incorrect.")

    def test_Vdc_series_R(self):
        """Lcapy: check Vdc + R

        """
        a = Vdc(10) + R(5)
        self.assertEqual2(a.voc, Vdc(10).voc, "voc incorrect.")
        self.assertEqual(a.Voc.dc, 10, "Voc incorrect.")
        self.assertEqual(a.V.dc, 10, "Voc incorrect.")
        self.assertEqual(a.I, 0, "I incorrect.")
        self.assertEqual(a.v(0), 10, "Voc incorrect.")
        self.assertEqual(a.i(0), 0, "I incorrect.")

        b = a.norton()
        self.assertEqual2(b.voc, Vdc(10).voc, "voc incorrect.")
        self.assertEqual(b.Voc.dc, 10, "Voc incorrect.")
        self.assertEqual(b.V.dc, 10, "Voc incorrect.")
        self.assertEqual(b.I, 0, "I incorrect.")
        self.assertEqual(b.v(0), 10, "Voc incorrect.")
        self.assertEqual(b.i(0), 0, "I incorrect.")

    def test_Vac_series_R(self):
        """Lcapy: check Vac + R

        """
        a = Vac(10) + R(5)
        self.assertEqual2(a.voc, Vac(10).voc, "voc incorrect.")
        self.assertEqual(a.Voc.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(a.V.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(a.I, 0, "I incorrect.")
        self.assertEqual(a.v(0), 10, "Voc incorrect.")
        self.assertEqual(a.i(0), 0, "I incorrect.")

        b = a.norton()
        self.assertEqual2(b.voc, Vac(10).voc, "voc incorrect.")
        self.assertEqual(b.Voc.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(b.V.ac[omega0sym], PhasorDomainExpression(10), "Voc incorrect.")
        self.assertEqual(b.I, 0, "I incorrect.")
        self.assertEqual(b.v(0), 10, "Voc incorrect.")
        self.assertEqual(b.i(0), 0, "I incorrect.")

    def test_R_series_L(self):
        """Lcapy: check R + L

        """
        a = R(10) + L(5)
        self.assertEqual2(a.Z(s), 5 * s + 10, "Zs incorrect.")

    def test_R_parallel_R(self):
        """Lcapy: check R | R

        """
        a = R(10) | R(15)
        b = a.simplify()
        self.assertEqual2(b.R, 6, "R incorrect.")
        self.assertEqual2(b.Z, 6, "Z incorrect.")
        self.assertEqual2(type(b), R, "type incorrect.")

    def test_L_parallel_L(self):
        """Lcapy: check L | L

        """
        a = L(10) | L(15)
        b = a.simplify()
        self.assertEqual2(b.L, 6, "L incorrect.")
        self.assertEqual2(b.Z(s), 6 * s, "Zs incorrect.")
        self.assertEqual2(type(b), L, "type incorrect.")

    def test_C_parallel_C(self):
        """Lcapy: check C | C

        """
        a = C(10) | C(15)
        b = a.simplify()
        self.assertEqual2(b.C, 25, "C incorrect.")
        self.assertEqual2(b.Z(s), 1 / (25 * s), "Zs incorrect.")
        self.assertEqual2(type(b), C, "type incorrect.")

    def test_I_parallel_I(self):
        """Lcapy: check I | I

        """
        a = Idc(10) | Idc(5)
        b = a.simplify()
        self.assertEqual2(b.isc, Idc(15).isc, "isc incorrect.")
        self.assertEqual2(b.Isc.dc, 15, "Isc incorrect.")
        self.assertEqual2(type(b), Idc, "type incorrect.")
        self.assertEqual2(b.isc, 15, "isc incorrect.")

    def test_load(self):
        """Lcapy: check load

        """

        a = Shunt(R(10) + Vdc(5))
        b = a.load(R(30))

        self.assertEqual2(type(b), Ser, "type incorrect.")
        self.assertEqual2(b.Z, 7.5, "Shunt loaded R incorrect Z.")

    def test_open_circuit(self):
        """Lcapy: check open_circuit

        """

        a = Shunt(R(10) + Vdc(5))
        b = a.open_circuit()

        self.assertEqual2(b.Z, 10, "incorrect Z.")
        self.assertEqual2(b.Voc.laplace(), LaplaceDomainVoltage(5) / s, "incorrect V.")

    def test_short_circuit(self):
        """Lcapy: check short_circuit

        """

        a = Series(R(10) + Vdc(5))
        b = a.short_circuit()

        self.assertEqual2(b.Z, 10, "incorrect Z.")
        self.assertEqual2(b.Voc.laplace(), LaplaceDomainVoltage(5) / s, "incorrect V.")

    def test_v(self):
        """Lcapy: check inverse Laplace for voltage sources"""

        omega0 = symbol('omega_0', real=True)

        a = Vdc(10)
        self.assertEqual(a.voc, 10, "DC incorrect.")

        a = Vstep(10)
        self.assertEqual2(a.voc, 10 * Heaviside(t), "Step incorrect.")

        a = Vac(10)
        self.assertEqual2(a.voc, 10 * cos(omega0 * t), "AC incorrect.")

    def test_i(self):
        """Lcapy: check inverse Laplace for current sources"""

        omega0 = symbol('omega_0', real=True)

        a = Idc(10)
        self.assertEqual(a.isc, 10, "DC incorrect.")

        a = Istep(10)
        self.assertEqual2(a.isc, 10 * Heaviside(t), "Step incorrect.")

        a = Iac(10)
        self.assertEqual2(a.isc, 10 * cos(omega0 * t), "AC incorrect.")

    def test_CPE(self):
        """Lcapy: check CPE

        """
        a = CPE(10, 1)

        self.assertEqual2(a.Z(s), 1 / (10 * s), "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10 * s, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1 / (10 * j * omega), "Zw incorrect.")
        self.assertEqual2(a.Y(jw), 10 * j * omega, "Yw incorrect.")

        a = CPE(10, 0)

        self.assertEqual2(a.Z(s), 1.0 / 10, "Zs incorrect.")
        self.assertEqual2(a.Y(s), 10, "Ys incorrect.")
        self.assertEqual2(a.Z(jw), 1.0 / 10, "Zw incorrect.")
        self.assertEqual2(a.Y(jw), 10, "Yw incorrect.")

        a = CPE(10, 0.5)

        self.assertEqual2(a.Y(s), 10 * sqrt(s), "Ys incorrect.")

    def simplify1(self):

        a = I(0) + Y(1)
        b = a.simplify()

        self.assertEqual2(b._Z, None, "Z incorrect.")
        self.assertEqual2(b._Y, 1, "Y incorrect.")

    def test_cpt(self):

        self.assertEqual(type(voltage(2).cpt()), Vdc , "-> Vdc")
        #self.assertEqual(type(voltage(cos(t)).cpt()), Vac, "-> Vac")

        self.assertEqual(type(current(2).cpt()), Idc, "-> Idc")
        #self.assertEqual(type(current(cos(t)).cpt()), Iac, "-> Iac")

        self.assertEqual(type(impedance(2).cpt()), R , "-> R")
        self.assertEqual(type(impedance(2 * s).cpt()), L, "-> L")
        self.assertEqual(type(impedance(2 / s).cpt()), C, "-> C")

        self.assertEqual(type(admittance(2).cpt()), G, "-> G")
        self.assertEqual(type(admittance(2 * s).cpt()), C, "-> C")
        self.assertEqual(type(admittance(2 / s).cpt()), L, "-> L")

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Django settings for semillas_backend project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
from __future__ import absolute_import, unicode_literals

import environ

ROOT_DIR = environ.Path(__file__) - 3  # (semillas_backend/config/settings/common.py - 3 = semillas_backend/)
APPS_DIR = ROOT_DIR.path('semillas_backend')

env = environ.Env()

# APP CONFIGURATION
# ------------------------------------------------------------------------------
DJANGO_APPS = (
    # Default Django apps:
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Useful template tags:
    # 'django.contrib.humanize',

    # Admin
    'django.contrib.admin',
)
THIRD_PARTY_APPS = (
    'crispy_forms',  # Form layouts
    'allauth',  # registration
    'allauth.account',  # registration
    'allauth.socialaccount',  # registration
    'rest_framework',
    'rest_framework_swagger', # Rest-api web documentation
)

# Apps specific for this project go here.
LOCAL_APPS = (
    # custom users app
    'semillas_backend.users.apps.UsersConfig',
    'landing',
    'board',
    'swagger',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

# MIDDLEWARE CONFIGURATION
# ------------------------------------------------------------------------------
MIDDLEWARE_CLASSES = (
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

# MIGRATIONS CONFIGURATION
# ------------------------------------------------------------------------------
MIGRATION_MODULES = {
    'sites': 'semillas_backend.contrib.sites.migrations'
}

# DEBUG
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = env.bool('DJANGO_DEBUG', False)

# FIXTURE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (
    str(APPS_DIR.path('fixtures')),
)

# EMAIL CONFIGURATION
# ------------------------------------------------------------------------------
EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend')

# MANAGER CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
    ("""Ismael de Esteban""", 'ismael.deesteban@gmail.com'),
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS

# DATABASE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    'default': env.db('DATABASE_URL', default='postgres:///semillas_backend'),
}
DATABASES['default']['ATOMIC_REQUESTS'] = True
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

# GENERAL CONFIGURATION
# ------------------------------------------------------------------------------
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'UTC'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'es'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True

# TEMPLATE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#templates
TEMPLATES = [
    {
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
            'debug': DEBUG,
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
            # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                # Your stuff: custom template context processors go here
            ],
        },
    },
]

# See: http://django-crispy-forms.readthedocs.io/en/latest/install.html#template-packs
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    str(APPS_DIR.path('static')),
)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR('media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'

# URL Configuration
# ------------------------------------------------------------------------------
ROOT_URLCONF = 'config.urls'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = 'config.wsgi.application'

# AUTHENTICATION CONFIGURATION
# ------------------------------------------------------------------------------
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

# Some really nice defaults
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

ACCOUNT_ALLOW_REGISTRATION = env.bool('DJANGO_ACCOUNT_ALLOW_REGISTRATION', True)
ACCOUNT_ADAPTER = 'semillas_backend.users.adapters.AccountAdapter'
SOCIALACCOUNT_ADAPTER = 'semillas_backend.users.adapters.SocialAccountAdapter'

# Custom user app defaults
# Select the correct user model
AUTH_USER_MODEL = 'users.User'
LOGIN_REDIRECT_URL = 'users:redirect'
LOGIN_URL = 'account_login'

# SLUGLIFIER
AUTOSLUG_SLUGIFY_FUNCTION = 'slugify.slugify'

# django-compressor
# ------------------------------------------------------------------------------

# Location of root django.contrib.admin URL, use {% url 'admin:index' %}
ADMIN_URL = r'^admin/'

# Your common stuff: Below this line define 3rd party library settings

from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
  ('es', _('Spanish')),
  ('en', _('English')),
]

LOCALE_PATHS = (
    'locale',
)

GEOIP_PATH = '/opt/maxmind/'

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',

    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
    ),

}
<commit_msg>Remove email account confirmation<commit_after># -*- coding: utf-8 -*-
"""
Django settings for semillas_backend project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
from __future__ import absolute_import, unicode_literals

import environ

ROOT_DIR = environ.Path(__file__) - 3  # (semillas_backend/config/settings/common.py - 3 = semillas_backend/)
APPS_DIR = ROOT_DIR.path('semillas_backend')

env = environ.Env()

# APP CONFIGURATION
# ------------------------------------------------------------------------------
DJANGO_APPS = (
    # Default Django apps:
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Useful template tags:
    # 'django.contrib.humanize',

    # Admin
    'django.contrib.admin',
)
THIRD_PARTY_APPS = (
    'crispy_forms',  # Form layouts
    'allauth',  # registration
    'allauth.account',  # registration
    'allauth.socialaccount',  # registration
    'rest_framework',
    'rest_framework_swagger', # Rest-api web documentation
)

# Apps specific for this project go here.
LOCAL_APPS = (
    # custom users app
    'semillas_backend.users.apps.UsersConfig',
    'landing',
    'board',
    'swagger',
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

# MIDDLEWARE CONFIGURATION
# ------------------------------------------------------------------------------
MIDDLEWARE_CLASSES = (
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

# MIGRATIONS CONFIGURATION
# ------------------------------------------------------------------------------
MIGRATION_MODULES = {
    'sites': 'semillas_backend.contrib.sites.migrations'
}

# DEBUG
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = env.bool('DJANGO_DEBUG', False)

# FIXTURE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (
    str(APPS_DIR.path('fixtures')),
)

# EMAIL CONFIGURATION
# ------------------------------------------------------------------------------
EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend')

# MANAGER CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
    ("""Ismael de Esteban""", 'ismael.deesteban@gmail.com'),
)

# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS

# DATABASE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    # Raises ImproperlyConfigured exception if DATABASE_URL not in os.environ
    'default': env.db('DATABASE_URL', default='postgres:///semillas_backend'),
}
DATABASES['default']['ATOMIC_REQUESTS'] = True
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

# GENERAL CONFIGURATION
# ------------------------------------------------------------------------------
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'UTC'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'es'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True

# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True

# TEMPLATE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#templates
TEMPLATES = [
    {
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
            'debug': DEBUG,
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
            # https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                # Your stuff: custom template context processors go here
            ],
        },
    },
]

# See: http://django-crispy-forms.readthedocs.io/en/latest/install.html#template-packs
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    str(APPS_DIR.path('static')),
)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR('media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'

# URL Configuration
# ------------------------------------------------------------------------------
ROOT_URLCONF = 'config.urls'

# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = 'config.wsgi.application'

# AUTHENTICATION CONFIGURATION
# ------------------------------------------------------------------------------
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

# Some really nice defaults
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

ACCOUNT_ALLOW_REGISTRATION = env.bool('DJANGO_ACCOUNT_ALLOW_REGISTRATION', True)
ACCOUNT_ADAPTER = 'semillas_backend.users.adapters.AccountAdapter'
SOCIALACCOUNT_ADAPTER = 'semillas_backend.users.adapters.SocialAccountAdapter'
ACCOUNT_EMAIL_VERIFICATION = "none"

# Custom user app defaults
# Select the correct user model
AUTH_USER_MODEL = 'users.User'
LOGIN_REDIRECT_URL = 'users:redirect'
LOGIN_URL = 'account_login'

# SLUGLIFIER
AUTOSLUG_SLUGIFY_FUNCTION = 'slugify.slugify'

# django-compressor
# ------------------------------------------------------------------------------

# Location of root django.contrib.admin URL, use {% url 'admin:index' %}
ADMIN_URL = r'^admin/'

# Your common stuff: Below this line define 3rd party library settings

from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
  ('es', _('Spanish')),
  ('en', _('English')),
]

LOCALE_PATHS = (
    'locale',
)

GEOIP_PATH = '/opt/maxmind/'

REST_FRAMEWORK = {
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',

    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
    ),

}
<|endoftext|>


<commit_before>import constants
import datetime
import json
import re
import uuid
from xml.dom import minidom

def amount_to_int(amt):
    scale = { 'T': 40, 't': 40, 'G': 30, 'g': 30, 'M': 20, 'm': 20, 'K': 10, 'k': 10 }

    m = re.match(r'(\d+)([GgMmKk])?$', amt)
    if not m:
        raise ValueError("invalid amount: " + amt)
    v = int(m.group(1))
    if m.group(2):
        v = v << scale[m.group(2)]
    return v

def get_bool_key(d, key, default):
    v = d.get(key, default)
    if isinstance(v, basestring):
        v = v.lower() in ('t', 'true', 'y', 'yes', '1')
    return v

class Platform(object):

    def __init__(self, data, defaults = False):
        if defaults or 'nx' in data:
            self.nx = 'true' if get_bool_key(data, 'nx', True) else 'false'
        if defaults or 'acpi' in data:
            self.acpi = '1' if get_bool_key(data, 'acpi', True) else '0'
        if defaults or 'apic' in data:
            self.apic = 'true' if get_bool_key(data, 'apic', True) else 'false'
        if defaults or 'pae' in data:
            self.pae = 'true' if get_bool_key(data, 'pae', True) else 'false'
        if defaults or 'hpet' in data:
            self.hpet = 'true' if get_bool_key(data, 'hpet', True) else 'false'
        if 'vga' in data:
            self.vga = data['vga']
        if 'videoram' in data:
            self.videoram = str(amount_to_int(data['videoram']) >> 20)
        if defaults or 'viridian' in data:
            self.viridian = 'true' if get_bool_key(data, 'viridian', True) else 'false'
        if 'device_id' in data:
            self.device_id = data['device_id']

    def getPlatform(self):
        return self.__dict__

    def update(self, new):
        self.__dict__.update(new.__dict__)

class OtherConfig(object):

    def __init__(self, data):
        self.mac_seed = data.get('mac_seed', str(uuid.uuid4()))
        if 'disks' in data:
            self.disks = DiskDevices(data['disks']).toXML()
        if 'other_config' in data:
            self.__dict__.update(data['other_config'])
        self.default_template = 'false' # cannot import with this set to 'true'

    def getOtherConfig(self):
        return self.__dict__

    def update(self, new):
        self.__dict__.update(new.__dict__)

class DiskDevices(object):

    def __init__(self, disks):
        self.disks = []
        for disk in disks:
            self.disks.append(Disk(disk['size'],
                                   disk.get('sr', ''),
                                   disk.get('bootable', True),
                                   disk.get('type', 'system')))

    def toXML(self):
        doc = minidom.Document()
        root = doc.createElement('provision')
        doc.appendChild(root)

        position = 0
        for disk in self.disks:
            entry = disk.getDiskEntry()
            entry.setAttribute('device', str(position))
            root.appendChild(entry)
            position += 1

        return doc.documentElement.toxml('utf-8')

class Disk(object):

    def __init__(self, size, sr, bootable, disk_type):
        self.size = amount_to_int(size)
        self.sr = sr
        self.bootable = 'true' if bootable else 'false'
        self.type = disk_type

    def getDiskEntry(self):
        doc = minidom.Document()
        entry = doc.createElement('disk')
        for element_name, element_value in self.__dict__.items():
            entry.setAttribute(element_name, str(element_value))

        return entry

# Template restrictions (added to recommendations field for clients, especially UI clients)
class Recommendations(object):

    def __init__(self, data, defaults = False):

        if 'max_memory' in data:
            self.memory_static_max = str(amount_to_int(data['max_memory']))
        if 'vcpus_max' in data:
            self.vcpus_max = str(data['vcpus_max'])
        if 'number_of_vbds' in data:
            self.number_of_vbds = str(data['number_of_vbds'])
        if 'number_of_vifs' in data:
            self.number_of_vifs = str(data['number_of_vifs'])
        if defaults or 'has_vendor_device' in data:
            self.has_vendor_device = 'true' if get_bool_key(data, 'has_vendor_device', False) else 'false'
        if 'allow_gpu_passthrough' in data:
            self.allow_gpu_passthrough = '1' if get_bool_key(data, 'allow_gpu_passthrough', False) else '0'
        if 'allow_vgpu' in data:
            self.allow_vgpu = '1' if get_bool_key(data, 'allow_vgpu', False) else '0'

    def toXML(self):

        doc = minidom.Document()
        root = doc.createElement('restrictions')
        doc.appendChild(root)

        for field, attr in (('memory-static-max', 'max'),
                            ('vcpus-max', 'max'),
                            ('has-vendor-device', 'value'),
                            ('allow-gpu-passthrough', 'value'),
                            ('allow-vgpu', 'value')):
            entry = doc.createElement('restriction')
            root.appendChild(entry)
            entry.setAttribute('field', field)
            entry.setAttribute(attr, self.__dict__.get(field.replace('-', '_'), ""))

        for prop in ('number-of-vbds', 'number-of-vifs'):
            entry = doc.createElement('restriction')
            entry.setAttribute('property', prop)
            entry.setAttribute('max', self.__dict__.get(prop.replace('-', '_'), ""))
            root.appendChild(entry)

        return doc.documentElement.toxml('utf-8')

    def update(self, new):
        self.__dict__.update(new.__dict__)

class BlankTemplate(object):
    """Collection of common template parameters"""

    def __init__(self):
        self.allowed_operations = []
        self.current_operations = {}
        self.power_state = constants.PowerStates.Halted
        self.user_version = 1
        self.is_a_template = True
        self.suspend_VDI = "OpaqueRef:NULL"
        self.resident_on = "OpaqueRef:NULL"
        self.affinity = "OpaqueRef:NULL"
        self.memory_overhead = "19922944"
        self.VCPUs_params = {}
        self.VCPUs_max = 1
        self.VCPUs_at_startup = 1
        self.actions_after_shutdown = constants.Actions.destroy
        self.actions_after_reboot = constants.Actions.restart
        self.actions_after_crash = constants.Actions.restart
        self.consoles = []
        self.VIFs = []
        self.VBDs = []
        self.crash_dumps = []
        self.VTPMs = []
        self.PCI_bus = ""
        self.domid = (-1)
        self.domarch = ""
        self.last_boot_CPU_flags = {}
        self.is_control_domain = False
        self.metrics = "OpaqueRef:NULL"
        self.guest_metrics = "OpaqueRef:NULL"
        self.last_booted_record = ""
        self.xenstore_data = {}
        self.ha_always_run = False
        self.ha_restart_priority = ""
        self.is_a_snapshot = False
        self.snapshot_of = "OpaqueRef:NULL"
        self.snapshots = []
        self.snapshot_time = datetime.datetime(1970, 1, 1)
        self.transportable_snapshot_id = ""
        self.blobs = {}
        self.tags = []
        self.blocked_operations = {}
        self.snapshot_info = {}
        self.snapshot_metadata = ""
        self.parent = "OpaqueRef:NULL"
        self.children = []
        self.bios_strings = {}
        self.protection_policy = "OpaqueRef:NULL"
        self.is_snapshot_from_vmpp = False
        self.appliance = "OpaqueRef:NULL"
        self.start_delay = 0
        self.shutdown_delay = 0
        self.order = 0
        self.VGPUs = []
        self.attached_PCIs = []
        self.suspend_SR = "OpaqueRef:NULL"
        self.version = 0
        self.generation_id = ""
        self.hardware_platform_version = 0
        self.has_vendor_device = False

    def createMember(self, doc, father, member_name):
        entry = doc.createElement('member')
        father.appendChild(entry)
        name = doc.createElement('name')
        entry.appendChild(name)
        name.appendChild(doc.createTextNode(member_name))
        value = doc.createElement('value')
        entry.appendChild(value)

        return value

    def toXML(self, version):

        record_dict = dict(self.__dict__)
        record_dict['platform'] = self.platform.getPlatform()
        record_dict['other_config'] = self.other_config.getOtherConfig()
        record_dict['recommendations'] = self.recommendations.toXML()

        doc = minidom.Document()
        root = doc.createElement('value')
        doc.appendChild(root)

        main_struct = doc.createElement('struct')
        root.appendChild(main_struct)

        ver_member = self.createMember(doc, main_struct, 'version')
        struct = doc.createElement('struct')
        ver_member.appendChild(struct)

        for n, v in version.items():
            value = self.createMember(doc, struct, n)
            value.appendChild(doc.createTextNode(v))

        obj_member = self.createMember(doc, main_struct, 'objects')
        obj_array = doc.createElement('array')
        obj_member.appendChild(obj_array)
        obj_data = doc.createElement('data')
        obj_array.appendChild(obj_data)
        obj_value = doc.createElement('value')
        obj_data.appendChild(obj_value)
        struct = doc.createElement('struct')
        obj_value.appendChild(struct)

        for n, v in (('class', 'VM'), ('id', 'Ref:0')):
            value = self.createMember(doc, struct, n)
            value.appendChild(doc.createTextNode(v))

        snapshot = self.createMember(doc, struct, 'snapshot')
        struct2 = doc.createElement('struct')
        snapshot.appendChild(struct2)

        for n2, v2 in record_dict.items():
            value = self.createMember(doc, struct2, n2)

            if isinstance(v2, basestring) and v2 != "":
                value.appendChild(doc.createTextNode(v2))

            elif isinstance(v2, datetime.datetime):
                date = doc.createElement('dateTime.iso8601')
                value.appendChild(date)
                date.appendChild(doc.createTextNode(v2.strftime("%Y%m%dT%H:%M:%SZ")))

            elif isinstance(v2, float):
                double = doc.createElement('double')
                value.appendChild(double)
                double.appendChild(doc.createTextNode(str(v2)))

            elif isinstance(v2, bool):
                boolean = doc.createElement('boolean')
                value.appendChild(boolean)
                boolean.appendChild(doc.createTextNode("%i" % v2))

            elif isinstance(v2, list):
                struct3 = doc.createElement('array')
                value.appendChild(struct3)
                data = doc.createElement('data')
                struct3.appendChild(data)
                for n3,v3 in v2:
                    value = self.createMember(doc, data, n3)
                    value.appendChild(doc.createTextNode(v3))

            elif isinstance(v2, dict):
                struct3 = doc.createElement('struct')
                value.appendChild(struct3)
                for n3,v3 in v2.items():
                    value = self.createMember(doc, struct3, n3)
                    value.appendChild(doc.createTextNode(v3))

            elif isinstance(v2, int):
                value.appendChild(doc.createTextNode(str(v2)))

        return doc.toprettyxml(indent = '   ')

    def update(self, template):
        self.__dict__.update(template)

class BaseTemplate(BlankTemplate):
    def __init__(self, template):

        super(BaseTemplate, self).__init__()
        self.platform = Platform(template)
        self.other_config = OtherConfig(template)
        self.recommendations = Recommendations(template)
        self.update(template, True)

    def update(self, template, defaults = False):

        if defaults or 'HVM_boot_params' in template:
            self.HVM_boot_params = template.get('HVM_boot_params', {})
        for k in ('HVM_boot_policy', 'PV_bootloader', 'PV_kernel', 'PV_ramdisk', 'PV_args',
                  'PV_bootloader_args', 'PV_legacy_args'):
            if defaults or k in template:
                self.__dict__[k] = template.get(k, '')

        blacklist = ( 'platform', 'other_config', 'recommendations', 'disks' )

        # apply template values over current values
        filtered_template = { k: v for k, v in template.iteritems() if k not in blacklist }
        super(BaseTemplate, self).update(filtered_template)

        if "min_memory" in template:
            self.memory_static_min = amount_to_int(template["min_memory"])
            self.memory_static_max = self.memory_static_min * 2
            self.memory_dynamic_min = self.memory_static_min * 2
            self.memory_dynamic_max = self.memory_static_min * 2
        if defaults or 'HVM_shadow_multiplier' in template:
            self.HVM_shadow_multiplier = template.get('HVM_shadow_multiplier', 1.0)

        # update contained objects
        self.platform.update(Platform(template, defaults))
        self.other_config.update(OtherConfig(template))
        self.recommendations.update(Recommendations(template, defaults))

def load_template(fname):
    """ Read JSON template and create a template object. If one template derives
    from another load that and apply changes upon that. """

    with open(fname) as templatefile:
        template = json.load(templatefile)

    if 'derived_from' in template:
        # load base template and overlay deltas
        ret = load_template(template['derived_from'])
        ret.update(template)
    else:
        ret = BaseTemplate(template)

    return ret
<commit_msg>Drop unneccessary parameter<commit_after>import constants
import datetime
import json
import re
import uuid
from xml.dom import minidom

def amount_to_int(amt):
    scale = { 'T': 40, 't': 40, 'G': 30, 'g': 30, 'M': 20, 'm': 20, 'K': 10, 'k': 10 }

    m = re.match(r'(\d+)([GgMmKk])?$', amt)
    if not m:
        raise ValueError("invalid amount: " + amt)
    v = int(m.group(1))
    if m.group(2):
        v = v << scale[m.group(2)]
    return v

def get_bool_key(d, key, default):
    v = d.get(key, default)
    if isinstance(v, basestring):
        v = v.lower() in ('t', 'true', 'y', 'yes', '1')
    return v

class Platform(object):

    def __init__(self, data, defaults = False):
        if defaults or 'nx' in data:
            self.nx = 'true' if get_bool_key(data, 'nx', True) else 'false'
        if defaults or 'acpi' in data:
            self.acpi = '1' if get_bool_key(data, 'acpi', True) else '0'
        if defaults or 'apic' in data:
            self.apic = 'true' if get_bool_key(data, 'apic', True) else 'false'
        if defaults or 'pae' in data:
            self.pae = 'true' if get_bool_key(data, 'pae', True) else 'false'
        if defaults or 'hpet' in data:
            self.hpet = 'true' if get_bool_key(data, 'hpet', True) else 'false'
        if 'vga' in data:
            self.vga = data['vga']
        if 'videoram' in data:
            self.videoram = str(amount_to_int(data['videoram']) >> 20)
        if defaults or 'viridian' in data:
            self.viridian = 'true' if get_bool_key(data, 'viridian', True) else 'false'
        if 'device_id' in data:
            self.device_id = data['device_id']

    def getPlatform(self):
        return self.__dict__

    def update(self, new):
        self.__dict__.update(new.__dict__)

class OtherConfig(object):

    def __init__(self, data):
        self.mac_seed = data.get('mac_seed', str(uuid.uuid4()))
        if 'disks' in data:
            self.disks = DiskDevices(data['disks']).toXML()
        if 'other_config' in data:
            self.__dict__.update(data['other_config'])
        self.default_template = 'false' # cannot import with this set to 'true'

    def getOtherConfig(self):
        return self.__dict__

    def update(self, new):
        self.__dict__.update(new.__dict__)

class DiskDevices(object):

    def __init__(self, disks):
        self.disks = []
        for disk in disks:
            self.disks.append(Disk(disk['size'],
                                   disk.get('sr', ''),
                                   disk.get('bootable', True),
                                   disk.get('type', 'system')))

    def toXML(self):
        doc = minidom.Document()
        root = doc.createElement('provision')
        doc.appendChild(root)

        position = 0
        for disk in self.disks:
            entry = disk.getDiskEntry()
            entry.setAttribute('device', str(position))
            root.appendChild(entry)
            position += 1

        return doc.documentElement.toxml('utf-8')

class Disk(object):

    def __init__(self, size, sr, bootable, disk_type):
        self.size = amount_to_int(size)
        self.sr = sr
        self.bootable = 'true' if bootable else 'false'
        self.type = disk_type

    def getDiskEntry(self):
        doc = minidom.Document()
        entry = doc.createElement('disk')
        for element_name, element_value in self.__dict__.items():
            entry.setAttribute(element_name, str(element_value))

        return entry

# Template restrictions (added to recommendations field for clients, especially UI clients)
class Recommendations(object):

    def __init__(self, data, defaults = False):

        if 'max_memory' in data:
            self.memory_static_max = str(amount_to_int(data['max_memory']))
        if 'vcpus_max' in data:
            self.vcpus_max = str(data['vcpus_max'])
        if 'number_of_vbds' in data:
            self.number_of_vbds = str(data['number_of_vbds'])
        if 'number_of_vifs' in data:
            self.number_of_vifs = str(data['number_of_vifs'])
        if defaults or 'has_vendor_device' in data:
            self.has_vendor_device = 'true' if get_bool_key(data, 'has_vendor_device', False) else 'false'
        if 'allow_gpu_passthrough' in data:
            self.allow_gpu_passthrough = '1' if get_bool_key(data, 'allow_gpu_passthrough', False) else '0'
        if 'allow_vgpu' in data:
            self.allow_vgpu = '1' if get_bool_key(data, 'allow_vgpu', False) else '0'

    def toXML(self):

        doc = minidom.Document()
        root = doc.createElement('restrictions')
        doc.appendChild(root)

        for field, attr in (('memory-static-max', 'max'),
                            ('vcpus-max', 'max'),
                            ('has-vendor-device', 'value'),
                            ('allow-gpu-passthrough', 'value'),
                            ('allow-vgpu', 'value')):
            entry = doc.createElement('restriction')
            root.appendChild(entry)
            entry.setAttribute('field', field)
            entry.setAttribute(attr, self.__dict__.get(field.replace('-', '_'), ""))

        for prop in ('number-of-vbds', 'number-of-vifs'):
            entry = doc.createElement('restriction')
            entry.setAttribute('property', prop)
            entry.setAttribute('max', self.__dict__.get(prop.replace('-', '_'), ""))
            root.appendChild(entry)

        return doc.documentElement.toxml('utf-8')

    def update(self, new):
        self.__dict__.update(new.__dict__)

class BlankTemplate(object):
    """Collection of common template parameters"""

    def __init__(self):
        self.allowed_operations = []
        self.current_operations = {}
        self.power_state = constants.PowerStates.Halted
        self.user_version = 1
        self.is_a_template = True
        self.suspend_VDI = "OpaqueRef:NULL"
        self.resident_on = "OpaqueRef:NULL"
        self.affinity = "OpaqueRef:NULL"
        self.VCPUs_params = {}
        self.VCPUs_max = 1
        self.VCPUs_at_startup = 1
        self.actions_after_shutdown = constants.Actions.destroy
        self.actions_after_reboot = constants.Actions.restart
        self.actions_after_crash = constants.Actions.restart
        self.consoles = []
        self.VIFs = []
        self.VBDs = []
        self.crash_dumps = []
        self.VTPMs = []
        self.PCI_bus = ""
        self.domid = (-1)
        self.domarch = ""
        self.last_boot_CPU_flags = {}
        self.is_control_domain = False
        self.metrics = "OpaqueRef:NULL"
        self.guest_metrics = "OpaqueRef:NULL"
        self.last_booted_record = ""
        self.xenstore_data = {}
        self.ha_always_run = False
        self.ha_restart_priority = ""
        self.is_a_snapshot = False
        self.snapshot_of = "OpaqueRef:NULL"
        self.snapshots = []
        self.snapshot_time = datetime.datetime(1970, 1, 1)
        self.transportable_snapshot_id = ""
        self.blobs = {}
        self.tags = []
        self.blocked_operations = {}
        self.snapshot_info = {}
        self.snapshot_metadata = ""
        self.parent = "OpaqueRef:NULL"
        self.children = []
        self.bios_strings = {}
        self.protection_policy = "OpaqueRef:NULL"
        self.is_snapshot_from_vmpp = False
        self.appliance = "OpaqueRef:NULL"
        self.start_delay = 0
        self.shutdown_delay = 0
        self.order = 0
        self.VGPUs = []
        self.attached_PCIs = []
        self.suspend_SR = "OpaqueRef:NULL"
        self.version = 0
        self.generation_id = ""
        self.hardware_platform_version = 0
        self.has_vendor_device = False

    def createMember(self, doc, father, member_name):
        entry = doc.createElement('member')
        father.appendChild(entry)
        name = doc.createElement('name')
        entry.appendChild(name)
        name.appendChild(doc.createTextNode(member_name))
        value = doc.createElement('value')
        entry.appendChild(value)

        return value

    def toXML(self, version):

        record_dict = dict(self.__dict__)
        record_dict['platform'] = self.platform.getPlatform()
        record_dict['other_config'] = self.other_config.getOtherConfig()
        record_dict['recommendations'] = self.recommendations.toXML()

        doc = minidom.Document()
        root = doc.createElement('value')
        doc.appendChild(root)

        main_struct = doc.createElement('struct')
        root.appendChild(main_struct)

        ver_member = self.createMember(doc, main_struct, 'version')
        struct = doc.createElement('struct')
        ver_member.appendChild(struct)

        for n, v in version.items():
            value = self.createMember(doc, struct, n)
            value.appendChild(doc.createTextNode(v))

        obj_member = self.createMember(doc, main_struct, 'objects')
        obj_array = doc.createElement('array')
        obj_member.appendChild(obj_array)
        obj_data = doc.createElement('data')
        obj_array.appendChild(obj_data)
        obj_value = doc.createElement('value')
        obj_data.appendChild(obj_value)
        struct = doc.createElement('struct')
        obj_value.appendChild(struct)

        for n, v in (('class', 'VM'), ('id', 'Ref:0')):
            value = self.createMember(doc, struct, n)
            value.appendChild(doc.createTextNode(v))

        snapshot = self.createMember(doc, struct, 'snapshot')
        struct2 = doc.createElement('struct')
        snapshot.appendChild(struct2)

        for n2, v2 in record_dict.items():
            value = self.createMember(doc, struct2, n2)

            if isinstance(v2, basestring) and v2 != "":
                value.appendChild(doc.createTextNode(v2))

            elif isinstance(v2, datetime.datetime):
                date = doc.createElement('dateTime.iso8601')
                value.appendChild(date)
                date.appendChild(doc.createTextNode(v2.strftime("%Y%m%dT%H:%M:%SZ")))

            elif isinstance(v2, float):
                double = doc.createElement('double')
                value.appendChild(double)
                double.appendChild(doc.createTextNode(str(v2)))

            elif isinstance(v2, bool):
                boolean = doc.createElement('boolean')
                value.appendChild(boolean)
                boolean.appendChild(doc.createTextNode("%i" % v2))

            elif isinstance(v2, list):
                struct3 = doc.createElement('array')
                value.appendChild(struct3)
                data = doc.createElement('data')
                struct3.appendChild(data)
                for n3,v3 in v2:
                    value = self.createMember(doc, data, n3)
                    value.appendChild(doc.createTextNode(v3))

            elif isinstance(v2, dict):
                struct3 = doc.createElement('struct')
                value.appendChild(struct3)
                for n3,v3 in v2.items():
                    value = self.createMember(doc, struct3, n3)
                    value.appendChild(doc.createTextNode(v3))

            elif isinstance(v2, int):
                value.appendChild(doc.createTextNode(str(v2)))

        return doc.toprettyxml(indent = '   ')

    def update(self, template):
        self.__dict__.update(template)

class BaseTemplate(BlankTemplate):
    def __init__(self, template):

        super(BaseTemplate, self).__init__()
        self.platform = Platform(template)
        self.other_config = OtherConfig(template)
        self.recommendations = Recommendations(template)
        self.update(template, True)

    def update(self, template, defaults = False):

        if defaults or 'HVM_boot_params' in template:
            self.HVM_boot_params = template.get('HVM_boot_params', {})
        for k in ('HVM_boot_policy', 'PV_bootloader', 'PV_kernel', 'PV_ramdisk', 'PV_args',
                  'PV_bootloader_args', 'PV_legacy_args'):
            if defaults or k in template:
                self.__dict__[k] = template.get(k, '')

        blacklist = ( 'platform', 'other_config', 'recommendations', 'disks' )

        # apply template values over current values
        filtered_template = { k: v for k, v in template.iteritems() if k not in blacklist }
        super(BaseTemplate, self).update(filtered_template)

        if "min_memory" in template:
            self.memory_static_min = amount_to_int(template["min_memory"])
            self.memory_static_max = self.memory_static_min * 2
            self.memory_dynamic_min = self.memory_static_min * 2
            self.memory_dynamic_max = self.memory_static_min * 2
        if defaults or 'HVM_shadow_multiplier' in template:
            self.HVM_shadow_multiplier = template.get('HVM_shadow_multiplier', 1.0)

        # update contained objects
        self.platform.update(Platform(template, defaults))
        self.other_config.update(OtherConfig(template))
        self.recommendations.update(Recommendations(template, defaults))

def load_template(fname):
    """ Read JSON template and create a template object. If one template derives
    from another load that and apply changes upon that. """

    with open(fname) as templatefile:
        template = json.load(templatefile)

    if 'derived_from' in template:
        # load base template and overlay deltas
        ret = load_template(template['derived_from'])
        ret.update(template)
    else:
        ret = BaseTemplate(template)

    return ret
<|endoftext|>