<commit_before>#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ansible.module_utils.basic import *
from ansible.module_utils.dci_common import *

import os

try:
    from dciclient.v1.api import component as dci_component
    from dciclient.v1.api import topic as dci_topic
except ImportError:
    dciclient_found = False
else:
    dciclient_found = True

DOCUMENTATION = '''
---
module: dci_component
short_description: module to interact with the components endpoint of DCI
description:
  - DCI module to manage the component resources
version_added: 2.2
options:
  state:
    required: false
    default: present
    description: Desired state of the resource
  dci_login:
    required: false
    description: User's DCI login
  dci_password:
    required: false
    description: User's DCI password
  dci_cs_url:
    required: false
    description: DCI Control Server URL
  dest:
    required: true
    description: Path where to drop the retrieved component
  active:
    required: false
    description: Wether of not the resource should be active
  embed:
    required: false
    description:
      - List of field to embed within the retrieved resource
'''

EXAMPLES = '''
- name: Download a component
  dci_component:
    id: {{ component_id }}
    dest: /srv/dci/components/{{ component_id }}

- name: Retrieve component informations
  dci_component:
    id: {{ component_id }}

- name: Remove component
  dci_component:
    state: absent
    id: {{ component_id }}
'''

# TODO
RETURN = '''
'''

def download_file(module, ctx):
    if os.path.isdir(module.params['dest']):
        dest_file = os.path.join(
            module.params['dest'],
            module.params['id'] + '.tar')
    else:
        dest_file = module.params['dest']

    if os.path.isfile(dest_file):
        module.exit_json(changed=False)
    else:
        try:
            component_files = dci_component.file_list(
                ctx,
                module.params['id']).json()['component_files']
            component_file_id = component_files[0]['id']
        except (KeyError, IndexError):
            module.fail_json(
                msg='Failed to get the component_files from the server.')
        return dci_component.file_download(
            ctx,
            module.params['id'],
            component_file_id,
            dest_file)

def main():

    resource_argument_spec = dict(
        state=dict(
            default='present',
            choices=['present', 'absent'],
            type='str'),
        id=dict(type='str'),
        dest=dict(type='str'),
        name=dict(type='str'),
        type=dict(type='str'),
        canonical_project_name=dict(type='str'),
        url=dict(type='str'),
        data=dict(type='dict'),
        topic_id=dict(type='str'),
        path=dict(type='str'),
        active=dict(default=True, type='bool'),
        embed=dict(type='str'),
    )
    resource_argument_spec.update(authentication_argument_spec())

    module = AnsibleModule(
        argument_spec=resource_argument_spec,
        required_if=[['state', 'absent', ['id']]]
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    ctx = build_dci_context(module)

    # Action required: Delete the component matching the component id
    # Endpoint called: /components/<component_id> DELETE
    #                  via dci_component.delete()
    #
    # If the component exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_component.delete(ctx, module.params['id'])

    # Action required: Attach a file to a component
    # Endpoint called: /components/<component_id>/files/ POST
    #                  via dci_component.file_upload()
    #
    # Attach file to a component
    elif module.params['path']:
        res = dci_component.file_upload(
            ctx,
            module.params['id'],
            module.params['path'])

    # Action required: Download a component
    # Endpoint called: /components/<component_id>/files/<file_id>/content GET
    #          via dci_component.file_download()
    #
    # Download the component
    elif module.params['dest']:
        res = download_file(module, ctx)

    # Action required: Get component informations
    # Endpoint called: /components/<component_id> GET
    #                  via dci_component.get()
    #
    # Get component informations
    elif module.params['id']:
        kwargs = {}
        if module.params['embed']:
            kwargs['embed'] = module.params['embed']
        res = dci_component.get(ctx, module.params['id'], **kwargs)

    # Action required: Update an existing component
    # Endpoint called: /components/<component_id> PUT
    #                  via dci_component.update()
    #
    # Update the component with the specified parameters.
    elif module.params['id']:
        res = dci_component.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            updated_kwargs = {
                'id': module.params['id'],
                'etag': res.json()['component']['etag']
            }
            if module.params['active']:
                updated_kwargs['state'] = 'active'
            else:
                updated_kwargs['state'] = 'inactive'

            res = dci_component.update(ctx, **updated_kwargs)

    # Action required: Create a new component
    # Endpoint called: /component POST via dci_component.create()
    #
    # Create a new component
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be speficied')
        if not module.params['type']:
            module.fail_json(msg='type parameter must be speficied')

        kwargs = {
            'name': module.params['name'],
            'type': module.params['type'],
        }

        if module.params['canonical_project_name']:
            canonical_project_name = module.params['canonical_project_name']
            kwargs['canonical_project_name'] = canonical_project_name
        if module.params['url']:
            kwargs['url'] = module.params['url']
        if module.params['data']:
            kwargs['data'] = module.params['data']
        if module.params['topic_id']:
            kwargs['topic_id'] = module.params['topic_id']
        kwargs['state'] = 'active' if module.params['active'] else 'inactive'
        res = dci_component.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result = {
                'component': dci_topic.list_components(
                    ctx, module.params['topic_id'],
                    where='name:' + module.params['name']
                ).json()['components'][0],
            }
        if res.status_code in [400, 401, 409]:
            result['changed'] = False
        else:
            result['changed'] = True
    except Exception:
        result = {}
        result['changed'] = False

    module.exit_json(**result)

if __name__ == '__main__':
    main()
<commit_msg>add team_id parameter to component creation<commit_after>#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ansible.module_utils.basic import *
from ansible.module_utils.dci_common import *

import os

try:
    from dciclient.v1.api import component as dci_component
    from dciclient.v1.api import topic as dci_topic
except ImportError:
    dciclient_found = False
else:
    dciclient_found = True

DOCUMENTATION = '''
---
module: dci_component
short_description: module to interact with the components endpoint of DCI
description:
  - DCI module to manage the component resources
version_added: 2.2
options:
  state:
    required: false
    default: present
    description: Desired state of the resource
  dci_login:
    required: false
    description: User's DCI login
  dci_password:
    required: false
    description: User's DCI password
  dci_cs_url:
    required: false
    description: DCI Control Server URL
  dest:
    required: true
    description: Path where to drop the retrieved component
  active:
    required: false
    description: Wether of not the resource should be active
  embed:
    required: false
    description:
      - List of field to embed within the retrieved resource
'''

EXAMPLES = '''
- name: Download a component
  dci_component:
    id: {{ component_id }}
    dest: /srv/dci/components/{{ component_id }}

- name: Retrieve component informations
  dci_component:
    id: {{ component_id }}

- name: Remove component
  dci_component:
    state: absent
    id: {{ component_id }}
'''

# TODO
RETURN = '''
'''

def download_file(module, ctx):
    if os.path.isdir(module.params['dest']):
        dest_file = os.path.join(
            module.params['dest'],
            module.params['id'] + '.tar')
    else:
        dest_file = module.params['dest']

    if os.path.isfile(dest_file):
        module.exit_json(changed=False)
    else:
        try:
            component_files = dci_component.file_list(
                ctx,
                module.params['id']).json()['component_files']
            component_file_id = component_files[0]['id']
        except (KeyError, IndexError):
            module.fail_json(
                msg='Failed to get the component_files from the server.')
        return dci_component.file_download(
            ctx,
            module.params['id'],
            component_file_id,
            dest_file)

def main():

    resource_argument_spec = dict(
        state=dict(
            default='present',
            choices=['present', 'absent'],
            type='str'),
        id=dict(type='str'),
        dest=dict(type='str'),
        name=dict(type='str'),
        type=dict(type='str'),
        canonical_project_name=dict(type='str'),
        url=dict(type='str'),
        data=dict(type='dict'),
        topic_id=dict(type='str'),
        path=dict(type='str'),
        active=dict(default=True, type='bool'),
        embed=dict(type='str'),
    )
    resource_argument_spec.update(authentication_argument_spec())

    module = AnsibleModule(
        argument_spec=resource_argument_spec,
        required_if=[['state', 'absent', ['id']]]
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    ctx = build_dci_context(module)

    # Action required: Delete the component matching the component id
    # Endpoint called: /components/<component_id> DELETE
    #                  via dci_component.delete()
    #
    # If the component exist and it has been succesfully deleted the changed is
    # set to true, else if the file does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_component.delete(ctx, module.params['id'])

    # Action required: Attach a file to a component
    # Endpoint called: /components/<component_id>/files/ POST
    #                  via dci_component.file_upload()
    #
    # Attach file to a component
    elif module.params['path']:
        res = dci_component.file_upload(
            ctx,
            module.params['id'],
            module.params['path'])

    # Action required: Download a component
    # Endpoint called: /components/<component_id>/files/<file_id>/content GET
    #          via dci_component.file_download()
    #
    # Download the component
    elif module.params['dest']:
        res = download_file(module, ctx)

    # Action required: Get component informations
    # Endpoint called: /components/<component_id> GET
    #                  via dci_component.get()
    #
    # Get component informations
    elif module.params['id']:
        kwargs = {}
        if module.params['embed']:
            kwargs['embed'] = module.params['embed']
        res = dci_component.get(ctx, module.params['id'], **kwargs)

    # Action required: Update an existing component
    # Endpoint called: /components/<component_id> PUT
    #                  via dci_component.update()
    #
    # Update the component with the specified parameters.
    elif module.params['id']:
        res = dci_component.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            updated_kwargs = {
                'id': module.params['id'],
                'etag': res.json()['component']['etag']
            }
            if module.params['active']:
                updated_kwargs['state'] = 'active'
            else:
                updated_kwargs['state'] = 'inactive'

            res = dci_component.update(ctx, **updated_kwargs)

    # Action required: Create a new component
    # Endpoint called: /component POST via dci_component.create()
    #
    # Create a new component
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be speficied')
        if not module.params['type']:
            module.fail_json(msg='type parameter must be speficied')

        kwargs = {
            'name': module.params['name'],
            'type': module.params['type'],
        }

        if module.params['canonical_project_name']:
            canonical_project_name = module.params['canonical_project_name']
            kwargs['canonical_project_name'] = canonical_project_name
        if module.params['url']:
            kwargs['url'] = module.params['url']
        if module.params['data']:
            kwargs['data'] = module.params['data']
        if module.params['topic_id']:
            kwargs['topic_id'] = module.params['topic_id']
        if module.params['team_id']:
            kwargs['team_id'] = module.params['team_id']
        kwargs['state'] = 'active' if module.params['active'] else 'inactive'
        res = dci_component.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result = {
                'component': dci_topic.list_components(
                    ctx, module.params['topic_id'],
                    where='name:' + module.params['name']
                ).json()['components'][0],
            }
        if res.status_code in [400, 401, 409]:
            result['changed'] = False
        else:
            result['changed'] = True
    except Exception:
        result = {}
        result['changed'] = False

    module.exit_json(**result)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from django.core.management import setup_environ
#from hir import settings
import settings
setup_environ(settings)

from mezzanine.forms.models import *

form_slug = 'organizationspace'
single_form = Form.objects.get(slug=form_slug)
formentry_list=FormEntry.objects.filter(form_id=single_form.id)
label_list = [ ]
field_list = Field.objects.filter(form_id=single_form.id)

print "Fields in form titled '%s' " % single_form.title
for field in field_list:
    print field.id, field.label

print

entrylist = [ ]

# I want a list of formentries?
# now I build a list of data entries
for formentry in formentry_list:
    fieldentry_list = FieldEntry.objects.filter(entry_id=formentry.id)
    for fieldentry in fieldentry_list:
        entrylist.append(fieldentry)

for entry in entrylist:
    print dir(entry)

print entrylist

<commit_msg>slight addition to test program<commit_after>from django.core.management import setup_environ
#from hir import settings
import settings
setup_environ(settings)

from mezzanine.forms.models import *

form_slug = 'organizationspace'
single_form = Form.objects.get(slug=form_slug)
formentry_list=FormEntry.objects.filter(form_id=single_form.id)
label_list = [ ]
field_list = Field.objects.filter(form_id=single_form.id)

print "Fields in form titled '%s' " % single_form.title
for field in field_list:
    print field.id, field.label

print

entrylist = [ ]

# I want a list of formentries?
# now I build a list of data entries
for formentry in formentry_list:
    fieldentry_list = FieldEntry.objects.filter(entry_id=formentry.id)
    for fieldentry in fieldentry_list:
        entrylist.append(fieldentry)

for entry in entrylist:
    print dir(entry)

print entrylist

"""
Next steps...

routine: pass a form name or slug, get a list of form_entry objects back?

put that in a view.

"""
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2010-2011 Roger Philibert
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

from weboob.deprecated.browser import Page
from weboob.tools.html import html2text
from weboob.tools.capabilities.thumbnail import Thumbnail

from datetime import datetime
import re

from .gallery import EHentaiGallery

class LoginPage(Page):
    def is_logged(self):
        success_p = self.document.xpath(
                '//p[text() = "Login Successful. You will be returned momentarily."]')
        if len(success_p):
            return True
        else:
            return False

class HomePage(Page):
    pass

class IndexPage(Page):
    def iter_galleries(self):
        lines = self.document.xpath('//table[@class="itg"]//tr[@class="gtr0" or @class="gtr1"]')
        for line in lines:
            a = line.xpath('.//div[@class="it3"]/a')[-1]
            url = a.attrib["href"]
            title = a.text.strip()
            yield EHentaiGallery(re.search('(?<=/g/)\d+/[\dabcdef]+', url).group(0), title=title)

class GalleryPage(Page):
    def image_pages(self):
        return self.document.xpath('//div[@class="gdtm"]//a/attribute::href')

    def _page_numbers(self):
        return [n for n in self.document.xpath("(//table[@class='ptt'])[1]//td/text()") if re.match(r"\d+", n)]

    def gallery_exists(self, gallery):
        if self.document.xpath("//h1"):
            return True
        else:
            return False

    def fill_gallery(self, gallery):
        gallery.title = self.document.xpath("//h1[@id='gn']/text()")[0]
        cardinality_string = self.document.xpath("//div[@id='gdd']//tr[td[@class='gdt1']/text()='Length:']/td[@class='gdt2']/text()")[0]
        gallery.cardinality = int(re.match(r"\d+", cardinality_string).group(0))
        date_string = self.document.xpath("//div[@id='gdd']//tr[td[@class='gdt1']/text()='Posted:']/td[@class='gdt2']/text()")[0]
        gallery.date = datetime.strptime(date_string, "%Y-%m-%d %H:%M")
        rating_string = self.document.xpath("//td[@id='rating_label']/text()")[0]
        rating_match = re.search(r"\d+\.\d+", rating_string)
        if rating_match is None:
            gallery.rating = None
        else:
            gallery.rating = float(rating_match.group(0))

        gallery.rating_max = 5

        try:
            thumbnail_url = self.document.xpath("//div[@class='gdtm']/a/img/attribute::src")[0]
        except IndexError:
            thumbnail_style = self.document.xpath("//div[@class='gdtm']/div/attribute::style")[0]
            thumbnail_url = re.search(r"background:[^;]+url\((.+?)\)", thumbnail_style).group(1)

        gallery.thumbnail = Thumbnail(thumbnail_url)

class ImagePage(Page):
    def get_url(self):
        return self.document.xpath('//div[@class="sni"]/a/img/attribute::src')[0]
<commit_msg>remove unused import<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2010-2011 Roger Philibert
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

from weboob.deprecated.browser import Page
from weboob.tools.capabilities.thumbnail import Thumbnail

from datetime import datetime
import re

from .gallery import EHentaiGallery

class LoginPage(Page):
    def is_logged(self):
        success_p = self.document.xpath(
                '//p[text() = "Login Successful. You will be returned momentarily."]')
        if len(success_p):
            return True
        else:
            return False

class HomePage(Page):
    pass

class IndexPage(Page):
    def iter_galleries(self):
        lines = self.document.xpath('//table[@class="itg"]//tr[@class="gtr0" or @class="gtr1"]')
        for line in lines:
            a = line.xpath('.//div[@class="it3"]/a')[-1]
            url = a.attrib["href"]
            title = a.text.strip()
            yield EHentaiGallery(re.search('(?<=/g/)\d+/[\dabcdef]+', url).group(0), title=title)

class GalleryPage(Page):
    def image_pages(self):
        return self.document.xpath('//div[@class="gdtm"]//a/attribute::href')

    def _page_numbers(self):
        return [n for n in self.document.xpath("(//table[@class='ptt'])[1]//td/text()") if re.match(r"\d+", n)]

    def gallery_exists(self, gallery):
        if self.document.xpath("//h1"):
            return True
        else:
            return False

    def fill_gallery(self, gallery):
        gallery.title = self.document.xpath("//h1[@id='gn']/text()")[0]
        cardinality_string = self.document.xpath("//div[@id='gdd']//tr[td[@class='gdt1']/text()='Length:']/td[@class='gdt2']/text()")[0]
        gallery.cardinality = int(re.match(r"\d+", cardinality_string).group(0))
        date_string = self.document.xpath("//div[@id='gdd']//tr[td[@class='gdt1']/text()='Posted:']/td[@class='gdt2']/text()")[0]
        gallery.date = datetime.strptime(date_string, "%Y-%m-%d %H:%M")
        rating_string = self.document.xpath("//td[@id='rating_label']/text()")[0]
        rating_match = re.search(r"\d+\.\d+", rating_string)
        if rating_match is None:
            gallery.rating = None
        else:
            gallery.rating = float(rating_match.group(0))

        gallery.rating_max = 5

        try:
            thumbnail_url = self.document.xpath("//div[@class='gdtm']/a/img/attribute::src")[0]
        except IndexError:
            thumbnail_style = self.document.xpath("//div[@class='gdtm']/div/attribute::style")[0]
            thumbnail_url = re.search(r"background:[^;]+url\((.+?)\)", thumbnail_style).group(1)

        gallery.thumbnail = Thumbnail(thumbnail_url)

class ImagePage(Page):
    def get_url(self):
        return self.document.xpath('//div[@class="sni"]/a/img/attribute::src')[0]
<|endoftext|>


<commit_before>#!/usr/bin/env python
import time
import random
import re
import subprocess
import signal
import sys
import os

def run(cmd, verbose=False, **kwargs):
    if verbose:
        print " [s] Running %r" % (cmd,)
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.PIPE,
                         **kwargs)
    p.wait()

    if verbose:
        for line in p.stdout:
            line = p.stdout.readline().strip()
            if line:
                print ' [s]  %s' % (line,)
        print " [s] Done"
    time.sleep(0.1)

def spawn(cmd, verbose=False, **kwargs):
    if verbose:
        print " [r] Waiting for %r" % (cmd,)
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **kwargs)
    time.sleep(0.4)
    return p

def wait(p, match, verbose=False):
    os.kill(p.pid, signal.SIGINT)
    p.wait()
    r = False
    for line in p.stdout:
        if re.search(match, line):
            r = True
        if verbose:
            print " [r]  %s" % (line.strip(),)
    if verbose:
        print " [r] Done"
    return r

def gen(prog, arg="", **kwargs):
    Prog = ''.join([w.capitalize() for w in prog.split('_')])
    ctx = {
        'python': kwargs.get('python', prog),
        'erlang': kwargs.get('erlang', prog),
        'java': kwargs.get('java', Prog),
        'dotnet': kwargs.get('dotnet', Prog),
        'ruby': kwargs.get('ruby', prog),
        'php': kwargs.get('php', prog),
        'arg': arg,
        'rubyver': os.environ.get('RUBYVER', '1.8'),
        'python-puka': kwargs.get('python-puka', prog),
        }
    return [
        ('python', './venv/bin/python %(python)s.py %(arg)s' % ctx),
        ('erlang', './%(python)s.erl %(arg)s' % ctx),
        ('java', 'java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:'
             'rabbitmq-client.jar %(java)s %(arg)s' % ctx),
        ('dotnet', 'env MONO_PATH=lib/bin mono %(dotnet)s.exe %(arg)s' % ctx),
        ('ruby', 'env RUBYOPT=-rubygems GEM_HOME=gems/gems RUBYLIB=gems/lib '
             'ruby%(rubyver)s %(ruby)s.rb %(arg)s' % ctx),
        ('php', 'php %(php)s.php %(arg)s' % ctx),
        ('python-puka', './venv/bin/python %(python-puka)s.py %(arg)s' % ctx),
        ]

tests = {
    'tut1': (gen('send'), gen('receive', java='Recv'), 'Hello World!'),
    'tut2': (gen('new_task', arg='%(arg)s'), gen('worker'), '%(arg)s'),
    'tut3': (gen('emit_log', arg='%(arg)s'), gen('receive_logs'), '%(arg)s'),
    }

verbose = len(sys.argv) > 1
errors = 0

for test in sorted(tests.keys()):
    (send_progs, recv_progs, output_mask) = tests[test]
    for scwd, send_cmd in send_progs:
        for rcwd, recv_cmd in recv_progs:
            ctx = {
                'arg': 'rand_%s' % (random.randint(1,100),)
                }
            rcmd = recv_cmd % ctx
            scmd = send_cmd % ctx
            mask = output_mask % ctx
            p = spawn(rcmd, verbose=verbose, cwd=rcwd)
            run(scmd, verbose=verbose, cwd=scwd)
            if wait(p, mask, verbose=verbose):
                print " [+] %s %-20s  ok" % (test, scwd+'/'+rcwd)
            else:
                print " [!] %s %-20s  FAILED %r %r" % \
                    (test, scwd+'/'+rcwd, rcmd, scmd)
                errors += 1

if errors:
    print " [!] %s tests failed" % (errors,)

sys.exit(errors)

<commit_msg>Automated testing of 4,5 and 6 tutorials. Php and erlang skipped.<commit_after>#!/usr/bin/env python
import time
import random
import re
import subprocess
import signal
import sys
import os

def run(cmd, verbose=False, **kwargs):
    if verbose:
        print " [s] Running %r" % (cmd,)
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.PIPE,
                         **kwargs)
    p.wait()

    if verbose:
        for line in p.stdout:
            line = line.strip()
            if line:
                print ' [s]  %s' % (line,)
        print " [s] Done"
    time.sleep(0.1)
    return p.returncode

def spawn(cmd, verbose=False, **kwargs):
    if verbose:
        print " [r] Waiting for %r" % (cmd,)
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **kwargs)
    time.sleep(0.4)
    return p

def wait(p, match, verbose=False):
    os.kill(p.pid, signal.SIGINT)
    p.wait()
    r = False
    for line in p.stdout:
        line = line.strip()
        if re.search(match, line):
            r = True
        if verbose:
            print " [r]  %s" % (line,)
    if verbose:
        print " [r] Done"
    return r

def gen(prog, arg="", **kwargs):
    Prog = ''.join([w.capitalize() for w in prog.split('_')])
    ctx = {
        'prog': prog,
        'Prog': Prog,
        'rubyver': os.environ.get('RUBYVER', '1.8'),
        'arg': arg,
        'java': kwargs.get('java', Prog),
        'dotnet': kwargs.get('dotnet', Prog),
        }
    return [
        ('python', './venv/bin/python %(prog)s.py %(arg)s' % ctx),
        ('erlang', './%(prog)s.erl %(arg)s' % ctx),
        ('java', 'java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:'
             'rabbitmq-client.jar %(java)s %(arg)s' % ctx),
        ('dotnet', 'env MONO_PATH=lib/bin mono %(dotnet)s.exe %(arg)s' % ctx),
        ('ruby', 'env RUBYOPT=-rubygems GEM_HOME=gems/gems RUBYLIB=gems/lib '
             'ruby%(rubyver)s %(prog)s.rb %(arg)s' % ctx),
        ('php', 'php %(prog)s.php %(arg)s' % ctx),
        ('python-puka', './venv/bin/python %(prog)s.py %(arg)s' % ctx),
        ]

def skip(cwd_cmd, to_skip):
    return [(cwd,cmd) for cwd, cmd in cwd_cmd if cwd not in to_skip]

tests = {
    'tut1': (gen('send'), gen('receive', java='Recv'), 'Hello World!'),
    'tut2': (gen('new_task', arg='%(arg)s'), gen('worker'), '%(arg)s'),
    'tut3': (gen('emit_log', arg='%(arg)s'), gen('receive_logs'), '%(arg)s'),
    'tut4': (skip(gen('emit_log_direct', arg='%(arg)s %(arg2)s'),
                  ['erlang', 'php']),
             skip(gen('receive_logs_direct', arg='%(arg)s'),
                  ['erlang', 'php']),
             '%(arg2)s'),
    'tut5': (skip(gen('emit_log_topic', arg='%(arg)s.foo %(arg2)s'),
                  ['erlang', 'php']),
             skip(gen('receive_logs_topic', arg='%(arg)s.*'),
                  ['erlang', 'php']),
             '%(arg2)s'),
    'tut6': (skip(gen('rpc_client', java='RPCClient', dotnet='RPCClient'),
                  ['erlang', 'php']),
             skip(gen('rpc_server', java='RPCServer', dotnet='RPCServer'),
                  ['erlang', 'php']),
             'fib[(]30[)]'),
    }

verbose = len(sys.argv) > 1
errors = 0

for test in sorted(tests.keys()):
    (send_progs, recv_progs, output_mask) = tests[test]
    for scwd, send_cmd in send_progs:
        for rcwd, recv_cmd in recv_progs:
            ctx = {
                'arg':  'rand_%s' % (random.randint(1,100),),
                'arg2': 'rand_%s' % (random.randint(1,100),),
                }
            rcmd = recv_cmd % ctx
            scmd = send_cmd % ctx
            mask = output_mask % ctx
            p = spawn(rcmd, verbose=verbose, cwd=rcwd)
            e = run(scmd, verbose=verbose, cwd=scwd)
            if wait(p, mask, verbose=verbose) and e == 0:
                print " [+] %s %-30s ok" % (test, scwd+'/'+rcwd)
            else:
                print " [!] %s %-30s FAILED %r %r (error=%r)" % \
                    (test, scwd+'/'+rcwd, rcmd, scmd, e)
                errors += 1

if errors:
    print " [!] %s tests failed" % (errors,)

sys.exit(errors)
<|endoftext|>


<commit_before>import ciso8601
import logging
import xml.etree.cElementTree as ET
from io import StringIO
from requests.exceptions import RequestException
from django.contrib.gis.geos import Point
from django.db.models import Q
from isodate import parse_duration
from busstops.models import Operator, Service, Locality, DataSource
from ..import_live_vehicles import ImportLiveVehiclesCommand
from ...models import Vehicle, VehicleLocation, VehicleJourney, JourneyCode

logger = logging.getLogger(__name__)
NS = {'siri': 'http://www.siri.org.uk/siri'}

def get_latlong(mvj):
    vl = mvj.find('siri:VehicleLocation', NS)
    lat = vl.find('siri:Latitude', NS).text
    lon = vl.find('siri:Longitude', NS).text
    return Point(float(lon), float(lat))

def items_from_response(response):
    try:
        iterator = ET.iterparse(StringIO(response.text))
    except ET.ParseError as e:
        logger.error(e, exc_info=True)
        print(response)
        return ()
    for _, element in iterator:
        if element.tag[-15:] == 'VehicleActivity':
            yield element
            element.clear()

class Command(ImportLiveVehiclesCommand):
    source_name = 'sirivm'
    url = 'sslink/SSLinkHTTP'

    operators_options = {
        'SQ': ('BLUS', 'SVCT', 'UNIL', 'SWWD', 'DAMY', 'TDTR', 'TOUR', 'WDBC'),
        'RB': ('RBUS', 'GLRB'),
        'TV': ('THVB', 'CTNY'),
        'SCHI': ('SINV', 'SCOR'),
        'SCFI': ('SCFI', 'SSPH', 'SSTY'),
        'SCSO': ('SCHM', 'SCCO', 'SMSO', 'SCHW'),
        'SCNH': ('SCNH', 'SCWW'),
        'CBLE': ('CBBH', 'CBNL'),
        'CSLB': ('CSLB', 'OXBC'),
        'RED': ('RRTR', 'RLNE', 'REDE'),
        'SCCM': ('SCCM', 'SCPB', 'SCHU', 'SCBD'),
        'ATS': ('ASES', 'ARBB', 'GLAR'),
        'ASC': ('ARHE', 'GLAR'),
        'NXB': ('TNXB', 'TCVW'),
    }
    operators = {}

    @staticmethod
    def get_datetime(item):
        datetime = item.find('siri:RecordedAtTime', NS).text
        return ciso8601.parse_datetime(datetime)

    def get_response(self, url, xml):
        try:
            return self.session.post(url, data=xml, timeout=10)
        except RequestException as e:
            print(e)
            return

    def get_items(self):
        now = self.source.datetime

        for source in DataSource.objects.filter(name__endswith=' SIRI'):
            if source.settings and source.settings['RequestorRef']:
                requestor_ref = source.settings['RequestorRef']
                requestor_ref = f'<RequestorRef>{requestor_ref}</RequestorRef>'
            else:
                requestor_ref = ''
            data = f"""<Siri xmlns="http://www.siri.org.uk/siri">
<ServiceRequest>{requestor_ref}<VehicleMonitoringRequest/></ServiceRequest>
</Siri>"""
            response = self.get_response(source.url, data)
            if response and response.text:
                source.datetime = now
                self.source = source
                for item in items_from_response(response):
                    yield item
                source.save(update_fields=['datetime'])

    def get_operator(self, operator_ref):
        operator_options = self.operators_options.get(operator_ref)
        if operator_ref in self.operators:
            return self.operators[operator_ref], operator_options
        if operator_options:
            operator = Operator.objects.get(id=operator_options[0])
        else:
            try:
                try:
                    operator = Operator.objects.get(operatorcode__source=self.source,
                                                    operatorcode__code=operator_ref)
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    operator = Operator.objects.get(id=operator_ref)
            except (Operator.MultipleObjectsReturned, Operator.DoesNotExist) as e:
                logger.error(e, exc_info=True)
                operator = None
        if operator:
            self.operators[operator_ref] = operator
        return operator, operator_options

    def get_vehicle(self, item):
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        vehicle_code = mvj.find('siri:VehicleRef', NS).text
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        if operator_ref and operator_ref != 'CSLB':
            while vehicle_code.startswith(operator_ref + '-'):
                if operator_ref == 'SQ' and vehicle_code[:6] != 'SQ-SQ-':
                    break
                vehicle_code = vehicle_code[len(operator_ref) + 1:]

        if vehicle_code[:4] == 'ASC-' and vehicle_code[4:].isdigit():
            vehicle_code = vehicle_code[4:]
        if vehicle_code[:5] == 'CTNY-':
            vehicle_code = vehicle_code[5:]

        operator, operator_options = self.get_operator(operator_ref)

        if operator:
            if operator.id in {'THVB', 'RBUS', 'CTNY'}:
                operator_options = ('RBUS', 'CTNY')
                if operator.id == 'THVB':
                    operator = Operator.objects.get(id='RBUS')
            elif operator.id == 'SESX':
                operator_options = ('SESX', 'NIBS', 'GECL')
            elif operator.id == 'FBRI':
                if len(vehicle_code) < 4:
                    return None, None
                if not vehicle_code.isdigit():
                    operator = Operator.objects.get(id='ABUS')

        defaults = {
            'source': self.source,
            'operator': operator
        }
        if vehicle_code.isdigit():
            defaults['fleet_number'] = vehicle_code
            if operator_ref == 'ATS' and int(vehicle_code) > 8000:
                return None, None
            if operator_ref == 'CTNY':
                return None, None
        elif '-' in vehicle_code:
            parts = vehicle_code.split('-')
            if len(parts) == 2 and parts[0].isalpha() and parts[0].isupper() and parts[1].isdigit():
                defaults['fleet_number'] = parts[1]

            if parts[0] == 'GOEA' or parts[0] == 'CSLB':
                return self.vehicles.get_or_create(
                    defaults,
                    code=vehicle_code,
                )

        if not operator_options:
            operator_options = (operator,)
        try:
            if operator and operator.parent:
                if '-' in vehicle_code:
                    fleet_number = vehicle_code.split('-', 1)[-1]
                    if fleet_number.isdigit():
                        defaults['fleet_number'] = fleet_number
                        if operator.parent in {'Stagecoach', 'Plymouth Citybus'}:
                            vehicle_code = fleet_number
                        return self.vehicles.filter(
                            operator__parent=operator.parent
                        ).get_or_create(defaults, fleet_number=fleet_number)

                return self.vehicles.filter(
                    operator__parent=operator.parent
                ).get_or_create(defaults, code=vehicle_code)

            if operator_ref == 'ATS' and vehicle_code.isdigit():
                defaults['code'] = vehicle_code
                return self.vehicles.get_or_create(
                    defaults,
                    operator__in=operator_options,
                    fleet_number=vehicle_code,
                )
            return self.vehicles.get_or_create(
                defaults,
                operator__in=operator_options,
                code=vehicle_code,
            )
        except Vehicle.MultipleObjectsReturned as e:
            logger.error(e, exc_info=True)
            return self.vehicles.filter(
                operator__in=operator_options,
                code=vehicle_code
            ).first(), False

    def get_journey(self, item, vehicle):
        journey = VehicleJourney()

        journey.text = ET.tostring(item).decode()

        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        service = mvj.find('siri:LineRef', NS).text

        journey_code = mvj.find('siri:FramedVehicleJourneyRef/siri:DatedVehicleJourneyRef', NS)
        if operator_ref != 'GOCH':
            if journey_code is not None:
                journey.code = journey_code.text

        departure_time = mvj.find('siri:OriginAimedDepartureTime', NS)
        if departure_time is not None:
            journey.datetime = ciso8601.parse_datetime(departure_time.text)

        if service is None and operator_ref == 'TG':
            service = 'Colchester Park & Ride'

        if service:
            journey.route_name = service

        if vehicle.latest_location and vehicle.latest_location.journey.code == journey.code and (
                                       vehicle.latest_location.journey.route_name == journey.route_name
        ):

            journey.service = vehicle.latest_location.journey.service
            if vehicle.latest_location.journey.destination:
                journey.destination = vehicle.latest_location.journey.destination
            return journey

        destination_ref = mvj.find('siri:DestinationRef', NS)
        if destination_ref is not None:
            destination_ref = destination_ref.text

        if destination_ref:
            try:
                journey.destination = Locality.objects.get(stoppoint=destination_ref).name
            except Locality.DoesNotExist:
                pass
        if not journey.destination:
            destination_name = mvj.find('siri:DestinationName', NS)
            if destination_name is not None:
                journey.destination = destination_name.text

        services = Service.objects.filter(current=True)

        services = services.filter(Q(line_name__iexact=service)
                                   | Q(servicecode__scheme__endswith=' SIRI', servicecode__code=service))

        operator, operator_options = self.get_operator(operator_ref)

        if operator_options:
            services = services.filter(operator__in=operator_options).distinct()
        elif operator:
            if operator.id == 'FBRI' and vehicle.operator_id == 'ABUS':
                operator = vehicle.operator
            services = services.filter(operator=operator)
        else:
            return journey

        latlong = get_latlong(mvj)

        if operator_ref != 'OFJ':  # not a Gatwick Airport shuttle

            try:
                journey.service = self.get_service(services, latlong)
            except Service.DoesNotExist:
                pass

            if not journey.service:
                origin_ref = mvj.find('siri:OriginRef', NS)
                destination_ref = mvj.find('siri:DestinationRef', NS)
                if origin_ref is not None:
                    origin_ref = origin_ref.text
                if destination_ref is not None:
                    destination_ref = destination_ref.text

                if origin_ref and destination_ref:
                    for queryset in (
                        services.filter(stops=origin_ref).filter(route__trip__destination=destination_ref),
                        services.filter(stops=origin_ref).filter(stops=destination_ref),
                        services.filter(Q(stops=origin_ref) | Q(stops=destination_ref)),
                    ):
                        if queryset.exists():
                            services = queryset.distinct()
                            break
                try:
                    journey.service = self.get_service(services, latlong)
                    if not journey.service:
                        journey.service = services.first()
                except Service.DoesNotExist as e:
                    print(e, operator, service)

        if not journey.destination and journey.code and journey.service:
            try:
                journey_code = journey.service.journeycode_set.get(code=journey.code)
                journey.destination = journey_code.destination
            except (JourneyCode.DoesNotExist, JourneyCode.MultipleObjectsReturned):
                pass

        if operator_options and operator and journey.service and operator.id == operator_options[0]:
            if operator.id not in {'RBUS', 'ARBB', 'ARHE'}:
                try:
                    operator = journey.service.operator.get()
                    vehicle.maybe_change_operator(operator)
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    pass

        return journey

    def create_vehicle_location(self, item):
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        latlong = get_latlong(mvj)
        heading = mvj.find('siri:Bearing', NS)
        if heading is not None:
            heading = int(heading.text)
            if heading == -1 or heading == 0:
                heading = None
            elif heading == 0:
                operator_ref = mvj.find('siri:OperatorRef', NS).text
                if operator_ref == 'ATS':
                    heading = None
        delay = mvj.find('siri:Delay', NS)
        if (delay is not None) and delay.text:
            try:
                delay = parse_duration(delay.text)
                delay = round(delay.total_seconds()/60)
                early = -delay
            except ValueError:
                delay = None
                early = None
        else:
            delay = None
            early = None
        return VehicleLocation(
            latlong=latlong,
            heading=heading,
            delay=delay,
            early=early
        )
<commit_msg>cope with null destination in SIRI-VM<commit_after>import ciso8601
import logging
import xml.etree.cElementTree as ET
from io import StringIO
from requests.exceptions import RequestException
from django.contrib.gis.geos import Point
from django.db.models import Q
from isodate import parse_duration
from busstops.models import Operator, Service, Locality, DataSource
from ..import_live_vehicles import ImportLiveVehiclesCommand
from ...models import Vehicle, VehicleLocation, VehicleJourney, JourneyCode

logger = logging.getLogger(__name__)
NS = {'siri': 'http://www.siri.org.uk/siri'}

def get_latlong(mvj):
    vl = mvj.find('siri:VehicleLocation', NS)
    lat = vl.find('siri:Latitude', NS).text
    lon = vl.find('siri:Longitude', NS).text
    return Point(float(lon), float(lat))

def items_from_response(response):
    try:
        iterator = ET.iterparse(StringIO(response.text))
    except ET.ParseError as e:
        logger.error(e, exc_info=True)
        print(response)
        return ()
    for _, element in iterator:
        if element.tag[-15:] == 'VehicleActivity':
            yield element
            element.clear()

class Command(ImportLiveVehiclesCommand):
    source_name = 'sirivm'
    url = 'sslink/SSLinkHTTP'

    operators_options = {
        'SQ': ('BLUS', 'SVCT', 'UNIL', 'SWWD', 'DAMY', 'TDTR', 'TOUR', 'WDBC'),
        'RB': ('RBUS', 'GLRB'),
        'TV': ('THVB', 'CTNY'),
        'SCHI': ('SINV', 'SCOR'),
        'SCFI': ('SCFI', 'SSPH', 'SSTY'),
        'SCSO': ('SCHM', 'SCCO', 'SMSO', 'SCHW'),
        'SCNH': ('SCNH', 'SCWW'),
        'CBLE': ('CBBH', 'CBNL'),
        'CSLB': ('CSLB', 'OXBC'),
        'RED': ('RRTR', 'RLNE', 'REDE'),
        'SCCM': ('SCCM', 'SCPB', 'SCHU', 'SCBD'),
        'ATS': ('ASES', 'ARBB', 'GLAR'),
        'ASC': ('ARHE', 'GLAR'),
        'NXB': ('TNXB', 'TCVW'),
    }
    operators = {}

    @staticmethod
    def get_datetime(item):
        datetime = item.find('siri:RecordedAtTime', NS).text
        return ciso8601.parse_datetime(datetime)

    def get_response(self, url, xml):
        try:
            return self.session.post(url, data=xml, timeout=10)
        except RequestException as e:
            print(e)
            return

    def get_items(self):
        now = self.source.datetime

        for source in DataSource.objects.filter(name__endswith=' SIRI'):
            if source.settings and source.settings['RequestorRef']:
                requestor_ref = source.settings['RequestorRef']
                requestor_ref = f'<RequestorRef>{requestor_ref}</RequestorRef>'
            else:
                requestor_ref = ''
            data = f"""<Siri xmlns="http://www.siri.org.uk/siri">
<ServiceRequest>{requestor_ref}<VehicleMonitoringRequest/></ServiceRequest>
</Siri>"""
            response = self.get_response(source.url, data)
            if response and response.text:
                source.datetime = now
                self.source = source
                for item in items_from_response(response):
                    yield item
                source.save(update_fields=['datetime'])

    def get_operator(self, operator_ref):
        operator_options = self.operators_options.get(operator_ref)
        if operator_ref in self.operators:
            return self.operators[operator_ref], operator_options
        if operator_options:
            operator = Operator.objects.get(id=operator_options[0])
        else:
            try:
                try:
                    operator = Operator.objects.get(operatorcode__source=self.source,
                                                    operatorcode__code=operator_ref)
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    operator = Operator.objects.get(id=operator_ref)
            except (Operator.MultipleObjectsReturned, Operator.DoesNotExist) as e:
                logger.error(e, exc_info=True)
                operator = None
        if operator:
            self.operators[operator_ref] = operator
        return operator, operator_options

    def get_vehicle(self, item):
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        vehicle_code = mvj.find('siri:VehicleRef', NS).text
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        if operator_ref and operator_ref != 'CSLB':
            while vehicle_code.startswith(operator_ref + '-'):
                if operator_ref == 'SQ' and vehicle_code[:6] != 'SQ-SQ-':
                    break
                vehicle_code = vehicle_code[len(operator_ref) + 1:]

        if vehicle_code[:4] == 'ASC-' and vehicle_code[4:].isdigit():
            vehicle_code = vehicle_code[4:]
        if vehicle_code[:5] == 'CTNY-':
            vehicle_code = vehicle_code[5:]

        operator, operator_options = self.get_operator(operator_ref)

        if operator:
            if operator.id in {'THVB', 'RBUS', 'CTNY'}:
                operator_options = ('RBUS', 'CTNY')
                if operator.id == 'THVB':
                    operator = Operator.objects.get(id='RBUS')
            elif operator.id == 'SESX':
                operator_options = ('SESX', 'NIBS', 'GECL')
            elif operator.id == 'FBRI':
                if len(vehicle_code) < 4:
                    return None, None
                if not vehicle_code.isdigit():
                    operator = Operator.objects.get(id='ABUS')

        defaults = {
            'source': self.source,
            'operator': operator
        }
        if vehicle_code.isdigit():
            defaults['fleet_number'] = vehicle_code
            if operator_ref == 'ATS' and int(vehicle_code) > 8000:
                return None, None
            if operator_ref == 'CTNY':
                return None, None
        elif '-' in vehicle_code:
            parts = vehicle_code.split('-')
            if len(parts) == 2 and parts[0].isalpha() and parts[0].isupper() and parts[1].isdigit():
                defaults['fleet_number'] = parts[1]

            if parts[0] == 'GOEA' or parts[0] == 'CSLB':
                return self.vehicles.get_or_create(
                    defaults,
                    code=vehicle_code,
                )

        if not operator_options:
            operator_options = (operator,)
        try:
            if operator and operator.parent:
                if '-' in vehicle_code:
                    fleet_number = vehicle_code.split('-', 1)[-1]
                    if fleet_number.isdigit():
                        defaults['fleet_number'] = fleet_number
                        if operator.parent in {'Stagecoach', 'Plymouth Citybus'}:
                            vehicle_code = fleet_number
                        return self.vehicles.filter(
                            operator__parent=operator.parent
                        ).get_or_create(defaults, fleet_number=fleet_number)

                return self.vehicles.filter(
                    operator__parent=operator.parent
                ).get_or_create(defaults, code=vehicle_code)

            if operator_ref == 'ATS' and vehicle_code.isdigit():
                defaults['code'] = vehicle_code
                return self.vehicles.get_or_create(
                    defaults,
                    operator__in=operator_options,
                    fleet_number=vehicle_code,
                )
            return self.vehicles.get_or_create(
                defaults,
                operator__in=operator_options,
                code=vehicle_code,
            )
        except Vehicle.MultipleObjectsReturned as e:
            logger.error(e, exc_info=True)
            return self.vehicles.filter(
                operator__in=operator_options,
                code=vehicle_code
            ).first(), False

    def get_journey(self, item, vehicle):
        journey = VehicleJourney()

        journey.text = ET.tostring(item).decode()

        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        service = mvj.find('siri:LineRef', NS).text

        journey_code = mvj.find('siri:FramedVehicleJourneyRef/siri:DatedVehicleJourneyRef', NS)
        if operator_ref != 'GOCH':
            if journey_code is not None:
                journey.code = journey_code.text

        departure_time = mvj.find('siri:OriginAimedDepartureTime', NS)
        if departure_time is not None:
            journey.datetime = ciso8601.parse_datetime(departure_time.text)

        if service is None and operator_ref == 'TG':
            service = 'Colchester Park & Ride'

        if service:
            journey.route_name = service

        if vehicle.latest_location and vehicle.latest_location.journey.code == journey.code and (
                                       vehicle.latest_location.journey.route_name == journey.route_name
        ):

            journey.service = vehicle.latest_location.journey.service
            if vehicle.latest_location.journey.destination:
                journey.destination = vehicle.latest_location.journey.destination
            return journey

        destination_ref = mvj.find('siri:DestinationRef', NS)
        if destination_ref is not None:
            destination_ref = destination_ref.text

        if destination_ref:
            try:
                journey.destination = Locality.objects.get(stoppoint=destination_ref).name
            except Locality.DoesNotExist:
                pass
        if not journey.destination:
            destination_name = mvj.find('siri:DestinationName', NS)
            if destination_name is not None:
                journey.destination = destination_name.text or ''

        services = Service.objects.filter(current=True)

        services = services.filter(Q(line_name__iexact=service)
                                   | Q(servicecode__scheme__endswith=' SIRI', servicecode__code=service))

        operator, operator_options = self.get_operator(operator_ref)

        if operator_options:
            services = services.filter(operator__in=operator_options).distinct()
        elif operator:
            if operator.id == 'FBRI' and vehicle.operator_id == 'ABUS':
                operator = vehicle.operator
            services = services.filter(operator=operator)
        else:
            return journey

        latlong = get_latlong(mvj)

        if operator_ref != 'OFJ':  # not a Gatwick Airport shuttle

            try:
                journey.service = self.get_service(services, latlong)
            except Service.DoesNotExist:
                pass

            if not journey.service:
                origin_ref = mvj.find('siri:OriginRef', NS)
                destination_ref = mvj.find('siri:DestinationRef', NS)
                if origin_ref is not None:
                    origin_ref = origin_ref.text
                if destination_ref is not None:
                    destination_ref = destination_ref.text

                if origin_ref and destination_ref:
                    for queryset in (
                        services.filter(stops=origin_ref).filter(route__trip__destination=destination_ref),
                        services.filter(stops=origin_ref).filter(stops=destination_ref),
                        services.filter(Q(stops=origin_ref) | Q(stops=destination_ref)),
                    ):
                        if queryset.exists():
                            services = queryset.distinct()
                            break
                try:
                    journey.service = self.get_service(services, latlong)
                    if not journey.service:
                        journey.service = services.first()
                except Service.DoesNotExist as e:
                    print(e, operator, service)

        if not journey.destination and journey.code and journey.service:
            try:
                journey_code = journey.service.journeycode_set.get(code=journey.code)
                journey.destination = journey_code.destination
            except (JourneyCode.DoesNotExist, JourneyCode.MultipleObjectsReturned):
                pass

        if operator_options and operator and journey.service and operator.id == operator_options[0]:
            if operator.id not in {'RBUS', 'ARBB', 'ARHE'}:
                try:
                    operator = journey.service.operator.get()
                    vehicle.maybe_change_operator(operator)
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    pass

        return journey

    def create_vehicle_location(self, item):
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        latlong = get_latlong(mvj)
        heading = mvj.find('siri:Bearing', NS)
        if heading is not None:
            heading = int(heading.text)
            if heading == -1 or heading == 0:
                heading = None
            elif heading == 0:
                operator_ref = mvj.find('siri:OperatorRef', NS).text
                if operator_ref == 'ATS':
                    heading = None
        delay = mvj.find('siri:Delay', NS)
        if (delay is not None) and delay.text:
            try:
                delay = parse_duration(delay.text)
                delay = round(delay.total_seconds()/60)
                early = -delay
            except ValueError:
                delay = None
                early = None
        else:
            delay = None
            early = None
        return VehicleLocation(
            latlong=latlong,
            heading=heading,
            delay=delay,
            early=early
        )
<|endoftext|>


<commit_before>import unittest
import logging
from instabot import User
from instabot import API
from instabot import Bot

class TestUser(unittest.TestCase):

    def test_init(self):
        User.delete("test")
        usr = User("test", "testpass")
        usr.save()
        del usr
        usr = User.load("test")
        self.assertEqual(usr.username, "test")
        self.assertEqual(usr.password, "testpass")
        User.delete("test")

    def test_api(self):
        api = API("instabotproject")
        self.assertTrue(api.User.isLoggedIn)
        api.User.save()
        reqs = api.User.counters.requests
        self.assertTrue(api.follow(352300017))
        self.assertTrue(api.unfollow(352300017))
        self.assertEqual(reqs + 2, api.User.counters.requests)
        api.User.save()

    def test_bot(self):
        bot = Bot("instabotproject")
        self.assertTrue(isinstance(bot, Bot))
        reqs = bot.User.counters.requests
        self.assertEqual(bot.convert_to_user_id("ohld"), "352300017")
        self.assertEqual(reqs + 1, bot.User.counters.requests)
        bot.User.save()

def reset_user():
    User.delete("instabotproject")
    api = API("instabotproject")
    api.User.save()

if __name__ == '__main__':
    # reset_user()
    unittest.main()
<commit_msg>add tests for Parser<commit_after>import unittest
import logging
from instabot import User, API, Bot, Parser

class TestUser(unittest.TestCase):

    def test_init(self):
        User.delete("test")
        usr = User("test", "testpass")
        usr.save()
        del usr
        usr = User.load("test")
        self.assertEqual(usr.username, "test")
        self.assertEqual(usr.password, "testpass")
        User.delete("test")

    def test_api(self):
        api = API("instabotproject")
        self.assertTrue(api.User.isLoggedIn)
        api.User.save()
        reqs = api.User.counters.requests
        self.assertTrue(api.follow(352300017))
        self.assertTrue(api.unfollow(352300017))
        self.assertEqual(reqs + 2, api.User.counters.requests)
        api.User.save()

    def test_bot(self):
        bot = Bot("instabotproject")
        self.assertTrue(isinstance(bot, Bot))
        reqs = bot.User.counters.requests
        self.assertEqual(bot.convert_to_user_id("ohld"), "352300017")
        self.assertEqual(reqs + 1, bot.User.counters.requests)
        bot.User.save()

    def test_parser(self):
        prs = Parser()
        self.assertTrue(len(prs.users) > 0)

def reset_user():
    User.delete("instabotproject")
    api = API("instabotproject")
    api.User.save()

if __name__ == '__main__':
    # reset_user()
    unittest.main()
<|endoftext|>


<commit_before>import logging
import decimal
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.core.exceptions import MultipleObjectsReturned
from django.contrib.auth.decorators import login_required
from .models import Product, Order, Cash
from . import helper

# Create your views here.
def index(request):
    return HttpResponse("Hello World. This is the pos starting page")

@login_required
def order(request):
    product_list = Product.objects.all
    template = loader.get_template('pos/order.html')
    context = {
            'product_list': product_list,
    }
    return HttpResponse(template.render(context, request))

def addition(request, operation):
    cash, _ = Cash.objects.get_or_create(id=0)
    succesfully_payed = False
    payment_error = False
    amount_added = 0
    q = Order.objects.filter(order_user=request.user.username, order_done=False).order_by('order_lastChange')
    if q.count() >= 1:
        current_order = q[0]
    else:
        current_order = Order.objects.create(order_user=request.user.username)

    if operation:
        if operation.isdecimal():
            current_order_parsed_list = helper.parse_json_product_list(current_order.order_list)
            product_to_add = Product.objects.get(product_id=operation)
            current_order_parsed_list.append(product_to_add)
            current_order.order_list = helper.product_list_to_json(current_order_parsed_list)
            current_order.order_totalprice = ( decimal.Decimal(product_to_add.product_price) + current_order.order_totalprice ).quantize(decimal.Decimal('0.01'))
            current_order.save()

        elif operation == "reset":
            current_order.order_list = "[]"
            current_order.order_totalprice = 0
            current_order.save()

        elif operation == "cashpayment":
            for product in helper.parse_json_product_list(current_order.order_list):
                product_in_database = Product.objects.get(product_name=product.product_name)
                if product_in_database.product_stockApplies:
                    product_in_database.product_stock = productInDatabase.product_stock - 1
                    product_in_database.save()

                cash.cash_amount = cash.cash_amount + product.product_price
                amount_added = amount_added + product.product_price
                cash.save()

            current_order.order_done = True
            current_order.save()
            current_order = Order.objects.create(order_user=request.user.username)
            succesfully_payed = True

        elif operation == "cardpayment":
            for product in helper.parse_json_product_list(current_order.order_list):
                product_in_database = Product.objects.get(product_name=product.product_name)
                amount_added = amount_added + product.product_price
                if product_in_database.product_stockApplies:
                    product_in_database.product_stock = productInDatabase.product_stock - 1
                    product_in_database.save()

            current_order.order_done = True
            current_order.save()
            current_order = Order.objects.create(order_user=request.user.username)
            succesfully_payed = True

        else:
            product_in_database = Product.objects.filter(product_name = operation).first()
            if product_in_database is not None:
                parsed_json_list = helper.parse_json_product_list(current_order.order_list)

                i = parsed_json_list.index(product_in_database)
                del parsed_json_list[i]
                current_order.order_list = helper.product_list_to_json(parsed_json_list)
                current_order.order_totalprice = ( current_order.order_totalprice - product_in_database.product_price ).quantize(decimal.Decimal('0.01'))

                if current_order.order_totalprice < 0:
                    logging.warn("prices below 0! You might be running in to the 10 digit total order price limit")
                    current_order.order_totalprice = 0
                current_order.save()

    totalprice = current_order.order_totalprice
    order_list = helper.parse_json_product_list(current_order.order_list)
    template = loader.get_template('pos/addition.html')
    context = {
            'order_list': order_list,
            'totalprice': totalprice,
            'cash': cash,
            'succesfully_payed': succesfully_payed,
            'payment_error': payment_error,
            'amount_added': amount_added,
    }
    return HttpResponse(template.render(context, request))

def cash(request, amount):
    cash, _ = Cash.objects.get_or_create(id=0)
    cash.cash_amount = amount
    cash.save()
    return HttpResponse('')
<commit_msg>NameError name 'productInDatabase' is not defined (fixes #29)<commit_after>import logging
import decimal
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.core.exceptions import MultipleObjectsReturned
from django.contrib.auth.decorators import login_required
from .models import Product, Order, Cash
from . import helper

# Create your views here.
def index(request):
    return HttpResponse("Hello World. This is the pos starting page")

@login_required
def order(request):
    product_list = Product.objects.all
    template = loader.get_template('pos/order.html')
    context = {
            'product_list': product_list,
    }
    return HttpResponse(template.render(context, request))

def addition(request, operation):
    cash, _ = Cash.objects.get_or_create(id=0)
    succesfully_payed = False
    payment_error = False
    amount_added = 0
    q = Order.objects.filter(order_user=request.user.username, order_done=False).order_by('order_lastChange')
    if q.count() >= 1:
        current_order = q[0]
    else:
        current_order = Order.objects.create(order_user=request.user.username)

    if operation:
        if operation.isdecimal():
            current_order_parsed_list = helper.parse_json_product_list(current_order.order_list)
            product_to_add = Product.objects.get(product_id=operation)
            current_order_parsed_list.append(product_to_add)
            current_order.order_list = helper.product_list_to_json(current_order_parsed_list)
            current_order.order_totalprice = ( decimal.Decimal(product_to_add.product_price) + current_order.order_totalprice ).quantize(decimal.Decimal('0.01'))
            current_order.save()

        elif operation == "reset":
            current_order.order_list = "[]"
            current_order.order_totalprice = 0
            current_order.save()

        elif operation == "cashpayment":
            # when a customer just paid in cash
            for ordered_product in helper.parse_json_product_list(current_order.order_list):
                product = Product.objects.get(
                    product_name=ordered_product.product_name)
                if product.product_stockApplies:
                    product.product_stock -= 1
                    product.save()

                cash.cash_amount += ordered_product.product_price
                amount_added += ordered_product.product_price
                cash.save()

            current_order.order_done = True
            current_order.save()
            current_order = Order.objects.create(order_user=request.user.username)
            succesfully_payed = True

        elif operation == "cardpayment":
            for product in helper.parse_json_product_list(current_order.order_list):
                product_in_database = Product.objects.get(product_name=product.product_name)
                amount_added = amount_added + product.product_price
                if product_in_database.product_stockApplies:
                    product_in_database.product_stock = productInDatabase.product_stock - 1
                    product_in_database.save()

            current_order.order_done = True
            current_order.save()
            current_order = Order.objects.create(order_user=request.user.username)
            succesfully_payed = True

        else:
            product_in_database = Product.objects.filter(product_name = operation).first()
            if product_in_database is not None:
                parsed_json_list = helper.parse_json_product_list(current_order.order_list)

                i = parsed_json_list.index(product_in_database)
                del parsed_json_list[i]
                current_order.order_list = helper.product_list_to_json(parsed_json_list)
                current_order.order_totalprice = ( current_order.order_totalprice - product_in_database.product_price ).quantize(decimal.Decimal('0.01'))

                if current_order.order_totalprice < 0:
                    logging.warn("prices below 0! You might be running in to the 10 digit total order price limit")
                    current_order.order_totalprice = 0
                current_order.save()

    totalprice = current_order.order_totalprice
    order_list = helper.parse_json_product_list(current_order.order_list)
    template = loader.get_template('pos/addition.html')
    context = {
            'order_list': order_list,
            'totalprice': totalprice,
            'cash': cash,
            'succesfully_payed': succesfully_payed,
            'payment_error': payment_error,
            'amount_added': amount_added,
    }
    return HttpResponse(template.render(context, request))

def cash(request, amount):
    cash, _ = Cash.objects.get_or_create(id=0)
    cash.cash_amount = amount
    cash.save()
    return HttpResponse('')
<|endoftext|>


<commit_before>import ciso8601
import logging
import xml.etree.cElementTree as ET
from requests.exceptions import RequestException
from django.contrib.gis.geos import Point
from django.db.models import Q
from isodate import parse_duration
from busstops.models import Operator, Service
from ..import_live_vehicles import ImportLiveVehiclesCommand
from ...models import Vehicle, VehicleLocation, VehicleJourney

logger = logging.getLogger(__name__)
NS = {'siri': 'http://www.siri.org.uk/siri'}

def get_latlong(mvj):
    vl = mvj.find('siri:VehicleLocation', NS)
    lat = vl.find('siri:Latitude', NS).text
    lon = vl.find('siri:Longitude', NS).text
    return Point(float(lon), float(lat))

def items_from_response(response):
    if not (response and response.text):
        return ()
    try:
        items = ET.fromstring(response.text)
    except ET.ParseError as e:
        logger.error(e, exc_info=True)
        print(response)
        return ()
    return items.findall('siri:ServiceDelivery/siri:VehicleMonitoringDelivery/siri:VehicleActivity', NS)

class Command(ImportLiveVehiclesCommand):
    source_name = 'sirivm'
    url = 'sslink/SSLinkHTTP'

    operators = {
        'SQ': ('BLUS', 'SVCT', 'UNIL', 'SWWD', 'DAMY', 'TDTR', 'TOUR', 'WDBC'),
        'RB': ('RBUS', 'GLRB'),
        'SCHI': ('SINV', 'SCOR'),
        'SCFI': ('SCFI', 'SSPH', 'SSTY'),
        'SCSO': ('SCHM', 'SCCO', 'SMSO', 'SCHW'),
        'SCNH': ('SCNH', 'SCWW'),
        'CBLE': ('CBBH', 'CBNL'),
        'RED': ('RRTR', 'RLNE', 'REDE'),
        'SCCM': ('SCCM', 'SCPB', 'SCHU', 'SCBD'),
        'ATS': ('ARBB', 'ARHE', 'GLAR'),
        'ASC': ('ARBB', 'GLAR'),
    }

    def get_response(self, url, xml):
        try:
            return self.session.post(url, data=xml, timeout=10)
        except RequestException as e:
            print(e)
            return

    def get_items(self):
        url = 'http://{}.jmwrti.co.uk:8080/RTI-SIRI-Server/SIRIHandler'
        data = """
            <Siri xmlns="http://www.siri.org.uk/siri">
                <ServiceRequest><VehicleMonitoringRequest/></ServiceRequest>
            </Siri>
        """
        for subdomain in ('essex', 'southampton', 'slough', 'staffordshire'):
            response = self.get_response(url.format(subdomain), data)
            for item in items_from_response(response):
                yield item

        url = 'http://data.icarus.cloudamber.com/VehicleMonitoringRequest.ashx'
        data = """
            <Siri xmlns="http://www.siri.org.uk/siri">
                <ServiceRequest>
                    <RequestorRef>{}</RequestorRef>
                    <VehicleMonitoringRequest/>
                </ServiceRequest>
            </Siri>
        """
        requestor_ref = 'gatwick_app'
        response = self.get_response(url, data.format(requestor_ref))
        for item in items_from_response(response):
            yield item

    def get_journey(self, item):
        journey = VehicleJourney()

        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        operator = None
        operator_options = None

        service = mvj.find('siri:LineRef', NS).text

        try:
            if operator_ref == 'TD':  # Xplore Dundee
                return None, None
            elif operator_ref:
                operator_options = self.operators.get(operator_ref)
                if operator_options:
                    operator = Operator.objects.get(id=operator_options[0])
                else:
                    try:
                        operator = Operator.objects.get(operatorcode__source=self.source,
                                                        operatorcode__code=operator_ref)
                    except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                        operator = Operator.objects.get(id=operator_ref)
        except (Operator.MultipleObjectsReturned, Operator.DoesNotExist) as e:
            logger.error(e, exc_info=True)
            print(e, operator_ref, service)

        vehicle_code = mvj.find('siri:VehicleRef', NS).text
        if operator_ref and vehicle_code.startswith(operator_ref + '-'):
            if operator_ref != 'SQ' or vehicle_code.startswith('SQ-SQ-'):
                vehicle_code = vehicle_code[len(operator_ref) + 1:]

        defaults = {
            'source': self.source,
            'operator': operator
        }
        if vehicle_code.isdigit():
            defaults['fleet_number'] = vehicle_code
        journey.vehicle, vehicle_created = Vehicle.objects.get_or_create(
            defaults,
            operator__in=operator_options or (operator,),
            code=vehicle_code,
        )

        journey_code = mvj.find('siri:FramedVehicleJourneyRef/siri:DatedVehicleJourneyRef', NS)
        if journey_code is not None:
            journey.code = journey_code.text

        departure_time = mvj.find('siri:OriginAimedDepartureTime', NS)
        if departure_time is not None:
            journey.datetime = ciso8601.parse_datetime(departure_time.text)

        destination = mvj.find('siri:DestinationName', NS)
        if destination is not None:
            journey.destination = destination.text

        if service is None and operator_ref == 'TG':
            service = 'Colchester Park & Ride'
        elif service == 'm1' and operator_ref == 'FB':
            operator = Operator.objects.get(pk='NCTP')

        services = Service.objects.filter(current=True)
        services = services.filter(Q(line_name__iexact=service) | Q(servicecode__scheme__endswith=' SIRI',
                                                                    servicecode__code=service))
        if operator_options:
            services = services.filter(operator__in=operator_options).distinct()
        elif operator:
            services = services.filter(operator=operator)

        else:
            return journey, vehicle_created

        latlong = get_latlong(mvj)

        try:
            if operator and operator.id == 'TNXB' and service == '4':
                journey.service_id = 'cen_33-4-W-y11'
            elif operator_ref != 'OFJ':
                journey.service = self.get_service(services, latlong)
        except (Service.MultipleObjectsReturned, Service.DoesNotExist):
            origin_ref = mvj.find('siri:OriginRef', NS)
            destination_ref = mvj.find('siri:DestinationRef', NS)
            if origin_ref is not None:
                origin_ref = origin_ref.text
            if destination_ref is not None:
                destination_ref = destination_ref.text

            if origin_ref:
                for queryset in (
                    services.filter(journey__stopusageusage__stop=origin_ref, journey__destination=destination_ref,
                                    journey__stopusageusage__order=0, journey__datetime=journey.datetime),
                    services.filter(stops=origin_ref).filter(journey__destination=destination_ref),
                    services.filter(stops=origin_ref).filter(stops=destination_ref),
                    services.filter(Q(stops=origin_ref) | Q(stops=destination_ref)),
                ):
                    if queryset.exists():
                        services = queryset.distinct()
                        break
            try:
                journey.service = self.get_service(services, latlong)
            except (Service.MultipleObjectsReturned, Service.DoesNotExist) as e:
                logger.error(e)

        if operator_options and operator and journey.service and operator.id == operator_options[0]:
            if operator.id != 'RBUS' and operator.id != 'ARBB':
                try:
                    operator = journey.service.operator.get()
                    if journey.vehicle.operator_id != operator.id:
                        journey.vehicle.operator = operator
                        journey.vehicle.save()
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    pass

        return journey, vehicle_created

    def create_vehicle_location(self, item):
        datetime = item.find('siri:RecordedAtTime', NS).text
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        latlong = get_latlong(mvj)
        heading = mvj.find('siri:Bearing', NS)
        if heading is not None:
            heading = int(heading.text)
            if heading == -1:
                heading = None
        delay = mvj.find('siri:Delay', NS)
        if (delay is not None) and delay.text:
            try:
                delay = parse_duration(delay.text)
                early = -round(delay.total_seconds()/60)
            except ValueError:
                early = None
        else:
            early = None
        return VehicleLocation(
            datetime=ciso8601.parse_datetime(datetime),
            latlong=latlong,
            heading=heading,
            early=early
        )
<commit_msg>Use iterparse for SIRI-VM<commit_after>import ciso8601
import logging
import xml.etree.cElementTree as ET
from io import StringIO
from requests.exceptions import RequestException
from django.contrib.gis.geos import Point
from django.db.models import Q
from isodate import parse_duration
from busstops.models import Operator, Service
from ..import_live_vehicles import ImportLiveVehiclesCommand
from ...models import Vehicle, VehicleLocation, VehicleJourney

logger = logging.getLogger(__name__)
NS = {'siri': 'http://www.siri.org.uk/siri'}

def get_latlong(mvj):
    vl = mvj.find('siri:VehicleLocation', NS)
    lat = vl.find('siri:Latitude', NS).text
    lon = vl.find('siri:Longitude', NS).text
    return Point(float(lon), float(lat))

def items_from_response(response):
    if not (response and response.text):
        return ()
    try:
        iterator = ET.iterparse(StringIO(response.text))
    except ET.ParseError as e:
        logger.error(e, exc_info=True)
        print(response)
        return ()
    for _, element in iterator:
        if element.tag[29:] == 'VehicleActivity':
            yield element
            element.clear()

class Command(ImportLiveVehiclesCommand):
    source_name = 'sirivm'
    url = 'sslink/SSLinkHTTP'

    operators = {
        'SQ': ('BLUS', 'SVCT', 'UNIL', 'SWWD', 'DAMY', 'TDTR', 'TOUR', 'WDBC'),
        'RB': ('RBUS', 'GLRB'),
        'SCHI': ('SINV', 'SCOR'),
        'SCFI': ('SCFI', 'SSPH', 'SSTY'),
        'SCSO': ('SCHM', 'SCCO', 'SMSO', 'SCHW'),
        'SCNH': ('SCNH', 'SCWW'),
        'CBLE': ('CBBH', 'CBNL'),
        'RED': ('RRTR', 'RLNE', 'REDE'),
        'SCCM': ('SCCM', 'SCPB', 'SCHU', 'SCBD'),
        'ATS': ('ARBB', 'ARHE', 'GLAR'),
        'ASC': ('ARBB', 'GLAR'),
    }

    def get_response(self, url, xml):
        try:
            return self.session.post(url, data=xml, timeout=10)
        except RequestException as e:
            print(e)
            return

    def get_items(self):
        url = 'http://{}.jmwrti.co.uk:8080/RTI-SIRI-Server/SIRIHandler'
        data = """
            <Siri xmlns="http://www.siri.org.uk/siri">
                <ServiceRequest><VehicleMonitoringRequest/></ServiceRequest>
            </Siri>
        """
        for subdomain in ('essex', 'southampton', 'slough', 'staffordshire'):
            response = self.get_response(url.format(subdomain), data)
            for item in items_from_response(response):
                yield item

        url = 'http://data.icarus.cloudamber.com/VehicleMonitoringRequest.ashx'
        data = """
            <Siri xmlns="http://www.siri.org.uk/siri">
                <ServiceRequest>
                    <RequestorRef>{}</RequestorRef>
                    <VehicleMonitoringRequest/>
                </ServiceRequest>
            </Siri>
        """
        requestor_ref = 'gatwick_app'
        response = self.get_response(url, data.format(requestor_ref))
        for item in items_from_response(response):
            yield item

    def get_journey(self, item):
        journey = VehicleJourney()

        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        operator_ref = mvj.find('siri:OperatorRef', NS).text
        operator = None
        operator_options = None

        service = mvj.find('siri:LineRef', NS).text

        try:
            if operator_ref == 'TD':  # Xplore Dundee
                return None, None
            elif operator_ref:
                operator_options = self.operators.get(operator_ref)
                if operator_options:
                    operator = Operator.objects.get(id=operator_options[0])
                else:
                    try:
                        operator = Operator.objects.get(operatorcode__source=self.source,
                                                        operatorcode__code=operator_ref)
                    except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                        operator = Operator.objects.get(id=operator_ref)
        except (Operator.MultipleObjectsReturned, Operator.DoesNotExist) as e:
            logger.error(e, exc_info=True)
            print(e, operator_ref, service)

        vehicle_code = mvj.find('siri:VehicleRef', NS).text
        if operator_ref and vehicle_code.startswith(operator_ref + '-'):
            if operator_ref != 'SQ' or vehicle_code.startswith('SQ-SQ-'):
                vehicle_code = vehicle_code[len(operator_ref) + 1:]

        defaults = {
            'source': self.source,
            'operator': operator
        }
        if vehicle_code.isdigit():
            defaults['fleet_number'] = vehicle_code
        journey.vehicle, vehicle_created = Vehicle.objects.get_or_create(
            defaults,
            operator__in=operator_options or (operator,),
            code=vehicle_code,
        )

        journey_code = mvj.find('siri:FramedVehicleJourneyRef/siri:DatedVehicleJourneyRef', NS)
        if journey_code is not None:
            journey.code = journey_code.text

        departure_time = mvj.find('siri:OriginAimedDepartureTime', NS)
        if departure_time is not None:
            journey.datetime = ciso8601.parse_datetime(departure_time.text)

        destination = mvj.find('siri:DestinationName', NS)
        if destination is not None:
            journey.destination = destination.text

        if service is None and operator_ref == 'TG':
            service = 'Colchester Park & Ride'
        elif service == 'm1' and operator_ref == 'FB':
            operator = Operator.objects.get(pk='NCTP')

        services = Service.objects.filter(current=True)
        services = services.filter(Q(line_name__iexact=service) | Q(servicecode__scheme__endswith=' SIRI',
                                                                    servicecode__code=service))
        if operator_options:
            services = services.filter(operator__in=operator_options).distinct()
        elif operator:
            services = services.filter(operator=operator)

        else:
            return journey, vehicle_created

        latlong = get_latlong(mvj)

        try:
            if operator and operator.id == 'TNXB' and service == '4':
                journey.service_id = 'cen_33-4-W-y11'
            elif operator_ref != 'OFJ':
                journey.service = self.get_service(services, latlong)
        except (Service.MultipleObjectsReturned, Service.DoesNotExist):
            origin_ref = mvj.find('siri:OriginRef', NS)
            destination_ref = mvj.find('siri:DestinationRef', NS)
            if origin_ref is not None:
                origin_ref = origin_ref.text
            if destination_ref is not None:
                destination_ref = destination_ref.text

            if origin_ref:
                for queryset in (
                    services.filter(journey__stopusageusage__stop=origin_ref, journey__destination=destination_ref,
                                    journey__stopusageusage__order=0, journey__datetime=journey.datetime),
                    services.filter(stops=origin_ref).filter(journey__destination=destination_ref),
                    services.filter(stops=origin_ref).filter(stops=destination_ref),
                    services.filter(Q(stops=origin_ref) | Q(stops=destination_ref)),
                ):
                    if queryset.exists():
                        services = queryset.distinct()
                        break
            try:
                journey.service = self.get_service(services, latlong)
            except (Service.MultipleObjectsReturned, Service.DoesNotExist) as e:
                logger.error(e)

        if operator_options and operator and journey.service and operator.id == operator_options[0]:
            if operator.id != 'RBUS' and operator.id != 'ARBB':
                try:
                    operator = journey.service.operator.get()
                    if journey.vehicle.operator_id != operator.id:
                        journey.vehicle.operator = operator
                        journey.vehicle.save()
                except (Operator.MultipleObjectsReturned, Operator.DoesNotExist):
                    pass

        return journey, vehicle_created

    def create_vehicle_location(self, item):
        datetime = item.find('siri:RecordedAtTime', NS).text
        mvj = item.find('siri:MonitoredVehicleJourney', NS)
        latlong = get_latlong(mvj)
        heading = mvj.find('siri:Bearing', NS)
        if heading is not None:
            heading = int(heading.text)
            if heading == -1:
                heading = None
        delay = mvj.find('siri:Delay', NS)
        if (delay is not None) and delay.text:
            try:
                delay = parse_duration(delay.text)
                early = -round(delay.total_seconds()/60)
            except ValueError:
                early = None
        else:
            early = None
        return VehicleLocation(
            datetime=ciso8601.parse_datetime(datetime),
            latlong=latlong,
            heading=heading,
            early=early
        )
<|endoftext|>


<commit_before>##Logger daemon version 1

from time import gmtime, strftime
import os

def logger_daemon ( self ):
	if os.path.exists(self.config["log-path"]) == True:

		if len(self.msg) >= 4:

			if "353" in self.msg or "366" in self.msg or "412" in self.msg:
				return
			else:
				brackets = self.config["TimestampBrackets"].split(",")
				usertxt = ""
				chan = self.msg[2]

				for i in range(3, len(self.msg)):
					usertxt += self.msg[i] +" "

				if chan[0] == "#":
					log = self.config["log-path"]+chan+".log"
					logline = brackets[0]+strftime(self.config["timeformat"])+brackets[1] + " " + self.get_nick() + " @ " + chan + " " + usertxt

					with open(log, "a") as log:
						log.write(logline)
						log.flush()

	else:
		try:
			if self.config["debug"] == "true":
				print("Cannot find existing folder for logs, creating: "+self.config["log-path"])
			os.mkdir(self.config["log-path"])
		except Exception as e:
			if self.config["debug"] == "true":
				print(e)

	if os.path.exists(self.config["log-path"]) == True:
		seendb = self.config["log-path"]+"seendb.txt"
		timeformat = strftime(self.config["timeformat"])

		try:
			if self.get_nick() in open(seendb).read():
				with open(seendb, "w", encoding="UTF-8") as temp:
					for line in open(seendb):
						str = "{0}:{1}".format(timeformat,self.get_nick())
						temp.write(re.sub("^{0}:.*$".format(self.get_nick()), str, line))
					os.remove(seendb)
					os.rename(self.config["log-path"]+"temp.txt", seendb)
					temp.close()
				return(True)
			## If the nick doesn't exist in the file, append it in there
			else:
				with open(seendb, "a", encoding="UTF-8") as file:
					str = "\r\n{0}:{1}".format(timeformat,self.get_nick())
					file.write(str)
					file.close()
				return(True)
		except Exception as e:
			if self.config["debug"] == "true":
				print(e)
				print("Creating file")
				with open(seendb, "a") as db:
					db.close()

<commit_msg>Added seendb<commit_after>##Logger daemon version 1

from time import gmtime, strftime
import time
import os
import re

def logger_daemon ( self ):
	if os.path.exists(self.config["log-path"]) == True:

		if len(self.msg) >= 4:

			if "353" in self.msg or "366" in self.msg or "412" in self.msg:
				return
			else:
				brackets = self.config["TimestampBrackets"].split(",")
				usertxt = ""
				chan = self.msg[2]

				for i in range(3, len(self.msg)):
					usertxt += self.msg[i] +" "

				if chan[0] == "#":
					log = self.config["log-path"]+chan+".log"
					logline = brackets[0]+strftime(self.config["timeformat"])+brackets[1] + " " + self.get_nick() + " @ " + chan + " " + usertxt

					with open(log, "a") as log:
						log.write(logline)
						log.flush()

	else:
		try:
			if self.config["debug"] == "true":
				print("Cannot find existing folder for logs, creating: "+self.config["log-path"])
			os.mkdir(self.config["log-path"])
		except Exception as e:
			if self.config["debug"] == "true":
				print(e)

	if os.path.exists(self.config["log-path"]) == True:
		seendb = self.config["log-path"]+"seendb.txt"
		temp = self.config["log-path"]+"temp.txt"
		timestamp = int(time.time())
		nick = self.get_nick()

		try:
			if self.get_nick() in open(seendb).read():
				with open(temp, "w", encoding="UTF-8") as temp:
					for line in open(seendb):
						str = "{0}:{1}".format(nick,timestamp)
						temp.write(re.sub("^{0}:.*$".format(nick), str, line))
					os.remove(seendb)
					os.rename(self.config["log-path"]+"temp.txt", seendb)
					temp.close()
				return(True)
			## If the nick doesn't exist in the file, append it in there
			else:
				with open(seendb, "a", encoding="UTF-8") as file:
					str = "\r\n{0}:{1}".format(nick,timestamp)
					file.write(str)
					file.close()
				return(True)
		except Exception as e:
			if self.config["debug"] == "true":
				print(e)
				print("Creating file")
				with open(seendb, "a") as db:
					db.close()

<|endoftext|>


<commit_before>print "hello, world"
<commit_msg>Updated to danish<commit_after>print "Hej store verden"
<|endoftext|>