Exemple #1
0
def main(args):
    """Entry point if run on the command line"""
    with open(args.output, 'w') as f:
        if args.runners_hh:
            code = Runners(nodes + custom_nodes).header
        elif args.runners_cc:
            code = Runners(nodes).source
        elif args.dispatch_hh:
            code = Dispatch(terminals, nodes + custom_nodes).header
        elif args.dispatch_cc:
            code = Dispatch(terminals, nodes + custom_nodes).source
        elif args.expression_hh:
            code = Expressions(nodes).header
        elif args.expression_cc:
            code = Expressions(nodes).source
        elif args.numeric_hh:
            code = Numeric(nodes).header
        elif args.numeric_cc:
            code = Numeric(nodes).source
        elif args.exprenum_hh:
            code = ExprEnums(nodes).code
        elif args.exprenum_java:
            code = ExprEnums(nodes).java
        elif args.createexpr_cc:
            code = CreateExpressions(terminals + nodes + custom_nodes).source
        f.writelines(code)
Exemple #2
0
def renderDispatches():
    disp_dict = {}
    try:
        disp_list = os.listdir(os.getcwd() + '/dispatches')
    except Exception as e:
        print(e)
        print('creating dispatches directory...')
        os.mkdir(os.getcwd() + '/dispatches')
        disp_list = os.listdir(os.getcwd() + '/dispatches')
    if disp_list == []:
        d = Dispatch(0)
        disp_list = os.listdir('dispatches')
    disp_list = [i for i in disp_list if i not in ['0', 0]]
    for i in disp_list:
        disp_dict[i] = []
        d = Dispatch(i)
        d_id = d.id_
        d_interval = d.interval
        post_list = os.listdir('dispatches/%s/posts' % d_id)
        post_list = [i for i in post_list if i not in ['0', 0]]
        for p in post_list:
            p = Post(p, d_id)
            p = p.post_data
            disp_dict[i].append(p)
        disp_dict[i] = [d_interval] + disp_dict[i]
    print(disp_dict)
    return disp_dict
    def test_simple_procedure_dispatch(self):
        class Endpoint:
            def __init__(self):
                self.called = False

            def API_pippo(self):
                self.called = True

        target = Dispatch().register(Endpoint, 'API_')
        instance = Endpoint()
        target.dispatch(instance, 'pippo')
        self.assertTrue(instance.called)
Exemple #4
0
def test_alter_get():
    from dispatch import Dispatch
    from dispatch.profiles import profile_registry

    def foo_clock_skew():
        return 20

    profile_registry.get_profile("Arista").add_getter("ntp.client.skew", foo_clock_skew)

    dispatcher = Dispatch()

    device = dispatcher.connect("switch01")
    device.get("ntp.client.skew")
    def test_dispatch__with_param_and_return_value(self):
        class Endpoint:
            def __init__(self):
                self.name = None

            def API_set_name(self, name):
                self.name = name
                return 'done'

        target = Dispatch().register(Endpoint, 'API_')
        instance = Endpoint()

        result = target.dispatch(instance, 'set_name', {'name': 'john'})
        self.assertEqual('john', instance.name)
        self.assertEqual('done', result)
Exemple #6
0
 def solve(self):
     len_of_unsolved = len(self.t_dict_unsolved)
     # 贪心法遍历任务
     for t in self.t_dict_unsolved.itervalues():
         print 'tasks remain:', len_of_unsolved
         min_EMCT = {
             't_id': 0,
             'r_id': 0,
             'start_time': np.inf,
             'end_time': np.inf
         }
         # 找到该任务的最小完成时间
         t.EMCT['end_time'] = np.inf
         for r in self.r_dict.itervalues():
             # 找到任务t的最小完成时间
             ETC = expected_time_to_compute(t.t_id, r.r_id)
             ECT = ETC + r.RAT
             if ECT < t.EMCT['end_time']:
                 t.EMCT['r_id'] = r.r_id
                 t.EMCT['start_time'] = r.RAT
                 t.EMCT['end_time'] = ECT
         if t.EMCT['end_time'] < min_EMCT['end_time']:
             min_EMCT['start_time'] = t.EMCT['start_time']
             min_EMCT['end_time'] = t.EMCT['end_time']
             min_EMCT['r_id'] = t.EMCT['r_id']
             min_EMCT['t_id'] = t.t_id
         # 执行调度
         d = Dispatch(min_EMCT['t_id'], min_EMCT['r_id'],
                      min_EMCT['start_time'])
         self.d_list.append(d)
         self.r_dict[min_EMCT['r_id']].RAT = min_EMCT['end_time']
         len_of_unsolved -= 1
Exemple #7
0
 def solve(self):
     while self.t_dict_unsolved:
         print 'tasks remain:', len(self.t_dict_unsolved)
         max_EMCT = {'t_id': 0, 'r_id': 0, 'start_time': 0, 'end_time': 0}
         for t in self.t_dict_unsolved.itervalues():
             # 找到所有任务的最大完成时间
             t.EMCT['end_time'] = np.inf
             for r in self.r_dict.itervalues():
                 # 找到任务t的最小完成时间
                 ETC = expected_time_to_compute(t.t_id, r.r_id)
                 ECT = ETC + r.RAT
                 if ECT < t.EMCT['end_time']:
                     t.EMCT['r_id'] = r.r_id
                     t.EMCT['start_time'] = r.RAT
                     t.EMCT['end_time'] = ECT
             if t.EMCT['end_time'] > max_EMCT['end_time']:
                 max_EMCT['start_time'] = t.EMCT['start_time']
                 max_EMCT['end_time'] = t.EMCT['end_time']
                 max_EMCT['r_id'] = t.EMCT['r_id']
                 max_EMCT['t_id'] = t.t_id
         # 执行调度
         d = Dispatch(max_EMCT['t_id'], max_EMCT['r_id'],
                      max_EMCT['start_time'])
         self.d_list.append(d)
         self.r_dict[max_EMCT['r_id']].RAT = max_EMCT['end_time']
         # 删除任务
         self.t_dict_unsolved.pop(max_EMCT['t_id'])
    def test_dispatch__should_not_call_late_added_method_AND_raise_exception(
            self):
        self.called = False

        def call_API_late():
            self.called = True

        class Endpoint:
            pass

        target = Dispatch().register(Endpoint, 'API_')
        instance = Endpoint()
        setattr(instance, 'API_late', lambda: call_API_late())

        self.assertRaises(MethodNotRegistered,
                          lambda: target.dispatch(instance, 'late'))
        self.assertFalse(self.called)
    def test_register__should_register_only_prefix(self):
        class Endpoint:
            def no_register(self):
                pass

            def PREFIX_foo(self):
                pass

        target = Dispatch().register(Endpoint, 'PREFIX_')
        self.assertEqual(['foo'], list(target.registered.keys()))
Exemple #10
0
def push(host):
    """
    Connects to a dispatcher at ip address `host` and pushes the records to
    a ZMQ socket.
    """
    dispatcher = Dispatch(host)

    post(host)

    context = zmq.Context()
    zmq_socket = context.socket(zmq.PUSH)
    zmq_socket.bind('tcp://127.0.0.1:5560')

    for record in dispatcher:
        zmq_socket.send_pyobj((int(time.time()), record.raw))
def main():
    port = 8090
    print('Starting server web v1.1 on port %d...' % port)
    args = sys.argv[1:]
    image_directory = './test_files/flat_files' if len(args) == 0 else args[0]

    refreshable_metadata = RefreshableCache(lambda: Metadata.from_folder(image_directory))
    api_dispatch = Dispatch().register(WebApi, 'API_')
    httpd = HTTPServer(('', port), partial(RequestHandler
                                           , refreshable_metadata=refreshable_metadata
                                           , api_dispatch=api_dispatch
                                           , image_directory=image_directory
                                           , directory='./wwwroot'))
    httpd.timeout = 10

    print('serving...')
    Thread(target=httpd.serve_forever).start()
    changed = wait_file_changes(__file__)
    httpd.shutdown()
    sys.exit(4 if changed else 0)
Exemple #12
0
def main():
    script_dir = os.path.dirname(__file__)
    if script_dir != '':
        os.chdir(script_dir)
    port = 8090
    print('Starting server web v0.1 on port %d...' % port)

    print(Example1().API_execute())

    api_dispatch = Dispatch().register(Example1, 'API_')

    def handler(request: RequestHandler) -> bool:
        params, rpath = request.decode_request()
        api_name = rpath[1:]  # remove initial /
        print('api_name = ' + api_name)
        if api_name == 'list':
            request.send_json(list(api_dispatch.registered.keys()))
        elif api_name == '':
            request.serve_file(script_dir, "index.html")
        elif api_name == 'favicon.ico':
            request.send_response(404, '')
        else:
            instance = Example1()
            instance.request = request
            result = api_dispatch.dispatch(instance, api_name, params)
            if isinstance(result, str):
                print(result)
                request.send_string(result)
            elif isinstance(result, (list, set, dict, tuple)):
                request.send_json(result)
            else:
                request.send_json(result.__dict__)

        return True

    httpd = HTTPServer(('', port), partial(RequestHandler, handler=handler))
    httpd.timeout = 10

    print('serving...')
    httpd.serve_forever()
    exit(0)
Exemple #13
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.dir = os.path.dirname(__file__)
        self.ui = loadUi(os.path.join(self.dir, 'ui_mainWindow.ui'))
        self.ui.show()
        self.ui.setWindowTitle(Settings.appName + ' - ' + Settings.version)
        self.dlcommand = utils.findFile(
            r'\Program Files\Thinkbox\*\bin\deadlinecommandbg.exe')
        self.vnc = utils.findFile(
            r'\Program Files\RealVNC\VNC Viewer\vncviewer.exe')
        self.hostIPs = {}
        self.slaveInfo = []
        self.slaveReports = {}
        self.accepted = r'Result: "Connection Accepted.'
        self.softwareCmd = r'reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" /S /v DisplayName'
        self.winLicenseCmd = r'reg query ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion""'

        self.createFolderStructure()
        self.deadline = Dispatch()  # connect to Deadline Web Service
        self.startUp()
        self.connectUiSignals()
        self.setupFolderWatcher()
Exemple #14
0
# !usr/env/bin python3
# -*- coding: utf8 -*-

from dispatch import Dispatch
from post import Post
from sendDispatch import *

import random

if __name__ == '__main__':
    for k in range(0,10):
        d = Dispatch(0)
        p = Post(0, d.id_)
        p.addText('текст поста %s в рассылке %s'%(p.id_, d.id_))
        p.addLink(random.choice(['http://lenta.ru/', 'http://vk.com', 'http://python.org']))
        p = Post(0, d.id_)
        p.addText('текст поста %s в рассылке %s'%(p.id_, d.id_))
        p.addLink(random.choice(['http://lenta.ru/', 'http://vk.com', 'http://python.org']))
        p = Post(0, d.id_)
        p.addText('текст поста %s в рассылке %s'%(p.id_, d.id_))
        p.addLink(random.choice(['http://lenta.ru/', 'http://vk.com', 'http://python.org']))
        
Exemple #15
0
    def do_POST(self):
        path = self.path
        print(path)
        status = 404

        # Pulling in our postbody data
        postBody = self.getPOSTBody()
        responseBody = {}
        print(postBody)

        # Endpoint for when an order is submitted to dispatch a vehicle
        if '/supply/vehicles/req' in path:
            status = 200

            # First convert our string into the ServiceType enumerated type
            postBody['serviceType'] = ServiceType.translate(
                postBody['serviceType'])
            print(postBody['serviceType'])

            desiredVehicleAttributes = [
                VehicleStatus.INACTIVE.value,
                postBody['serviceType'].value,
            ]
            # print(desiredVehicleAttributes)
            # Query all vehicles whose status is INACTIVE and are a part of the fleet whose ServiceType is the
            # incoming order's service type
            # We need a switch here to know whether our dispatch should be inserted as QUEUED or RUNNING for the case
            # were there are no INACTIVE vehicles to pick up the order
            vehicleEntries = databaseutils.getCourierCandidates(
                desiredVehicleAttributes)

            needsToBeQueued = False
            # In the case that we have no inactive vehicles, we then ask for the active vehicles and later on,
            # instead of a running dispatch, it will be queued.
            if not vehicleEntries:
                needsToBeQueued = True
                desiredVehicleAttributes[0] = VehicleStatus.ACTIVE.value
                vehicleEntries = databaseutils.getCourierCandidates(
                    desiredVehicleAttributes)

            print(vehicleEntries)
            # Make a tuple containing all the returned vehicles' current lat, lon and vid
            allPostions = [(float(x[4]), float(x[5]), x[0])
                           for x in vehicleEntries]
            print(allPostions)
            # We are deepcopying so that we reuse and mutate components of our postBody, but also maintain the
            # postBody's immutability
            dispatchDict = deepcopy(postBody)

            # Turn the postBody's destination dictionary into a tupled pair
            destination = dispatchDict.pop('destination')
            dispatchDict['loc_f'] = (destination['lat'], destination['lon'])

            LAT_INDEX = 0
            LON_INDEX = 1
            # Create a dictionary where the keys are vids and the values are the derived ETAs based on the returned
            # cars' current position to the order's destination
            etas = {
                vid: getETA(startLat=lat,
                            startLon=lon,
                            endLat=dispatchDict['loc_f'][LAT_INDEX],
                            endLon=dispatchDict['loc_f'][LON_INDEX])
                for lat, lon, vid in allPostions
            }
            print(etas)
            # fastestVID = sorted(etas.items(), key=lambda x: x[1])[0][0]
            fastestVID, eta = sorted(etas.items(), key=lambda x: x[1])[0]
            print(fastestVID)
            print(eta)
            if needsToBeQueued:
                dispatchDict['status'] = DispatchStatus.QUEUED

            # For now we are just picking the vehicle whose vid was just determined to have the fastest ETA to the
            # incoming orders destination
            vehicle = [
                vehicle for vehicle in vehicleEntries
                if vehicle[0] == fastestVID
            ][0]
            print(vehicle)

            # Capture vehicle tuple into its separate variables
            vid, licensePlate, make, model, vLat, vLon = vehicle

            if not needsToBeQueued:
                # Now that a vehicle has been assigned their status will be updated, given that they weren't already
                # active
                databaseutils.updateVehicleStatus(vid)

            # Seeing if the unpacking worked d:
            print(vehicle)

            print(vid)
            print(licensePlate)
            print(make)
            print(model)
            print(vLon)
            print(vLat)

            # Need to convert to float as SQL's return type on floats are Decimal(...)
            vLat = float(vLat)
            vLon = float(vLon)

            # These are added here because until this point we have no idea which vehicle would carry out the order
            # Everything else in the post body was already almost already how it needed to be to translate directly to
            # a dispatch dictionary
            dispatchDict['loc_0'] = (vLat, vLon)
            dispatchDict['vid'] = vid

            print(dispatchDict)

            # Instantiating a dispatch instance
            dispatch = Dispatch(**dispatchDict)

            print(dispatch)

            dispatchData = (
                dispatch.vid,
                dispatch.custid,
                dispatch.orderid,
                dispatch.loc_0[LAT_INDEX],
                dispatch.loc_0[LON_INDEX],
                dispatch.loc_f[LAT_INDEX],
                dispatch.loc_f[LON_INDEX],
                dispatch.timeCreated,
                dispatch.status.value,
                dispatch.serviceType.value,
            )
            # Now to add the new dispatch into the database
            databaseutils.storeDispatch(dispatchData)

            # Getting our vehicle's eta!
            # eta = dispatch.getETA(dispatch.loc_0)
            # print(eta)

            # Organising our courier into a dictionary for our response body
            vehicleDict = {
                'vid': vid,
                'licensePlate': licensePlate,
                'make': make,
                'model': model,
                'curLocation': {
                    'lat': vLat,
                    'lon': vLon
                },
                'ETA': eta
            }

            print(vehicleDict)

            responseBody = vehicleDict

        self.send_response(status)
        self.end_headers()
        res = json.dumps(responseBody)
        bytesStr = res.encode('utf-8')
        self.wfile.write(bytesStr)
Exemple #16
0
    def do_POST(self):
        path = self.path
        print(path)
        status = 404

        # Pulling in our postbody data and opening up our SQL connection
        postBody = self.getPOSTBody()
        responseBody = {}
        print(postBody)

        if '/supply/vehicles/add' in path:
            status = 200
            vehicleData = []
            # Because we are support the adding of multiple vehicles into our database in a single request,
            # and because of the formatting that SQL requires for an execution like this, we are going to iterate
            # through our postBody, put the values into a tuple, and then append that tuple into our vehicleData list. In
            # addition, we are going to set the 'default' status of the vehicle to INACTIVE
            for vehicleDict in postBody:
                # This is Steds btw d: ==> 30.2264, -97.7553,
                entry = (VehicleStatus.INACTIVE.value,
                         vehicleDict['licensePlate'], vehicleDict['fleetid'],
                         vehicleDict['make'], vehicleDict['model'], 30.2264,
                         -97.7553, None, vehicleDict['dateAdded'].replace(
                             'T', ' ').replace('Z', ' '))
                vehicleData.append(entry)
            # print(vehicleData)
            databaseutils.addVehicle(vehicleData)

        elif '/supply/vehicles/rem' in path:
            status = 200
            # Extract the vids from the postBody and store it into a list.
            vehicleData = [(vid, ) for viddict in postBody
                           for vid in viddict.values()]

            # print(vehicleData)
            databaseutils.delVehicle(vehicleData)

        elif '/supply/vehicles/upd' in path:
            allowableUpdates = {
                'status', 'licenseplate', 'fleetid', 'current_lat',
                'current_lon', 'last_heartbeat'
            }
            status = 401

            vidless = deepcopy(postBody)
            vid = vidless.pop('vid')

            # Need to check if the vid is in the postBody bc we won't know what vehicle to update without it
            # Need to make sure that there are attributes that want to be updated
            # The desired attributes to be updated must be a part of the set of
            # attributes that are allowed to be updated
            if 'vid' in postBody and vidless and set(
                    vidless.keys()).issubset(allowableUpdates):
                entry = databaseutils.getVehicleByVID(vid)
                if entry:
                    status = 200
                    statement = 'UPDATE vehicles SET'
                    vehicleData = []

                    # Building an SQL UPDATE ... SET statement with the desired attributes and their values
                    for col, colVal in vidless.items():
                        if col is 'status':
                            colVal = VehicleStatus.translate(colVal).value
                        statement += f' {col} = %s,'
                        vehicleData.append(colVal)

                    # Pruning the statement for SQL formatting
                    statement = statement[:-1]
                    statement += ' WHERE vid = %s'
                    # Append vid at the end bc the where clause always goes at the end
                    vehicleData.append(vid)

                    print(statement)
                    print(vehicleData)

                    databaseutils.updVehicle(statement, vehicleData)

                    if 'last_heartbeat' in postBody.keys():
                        dispatchTup = databaseutils.getRunningDispatchByVID(
                            vid)
                        if dispatchTup is not None:
                            service = dispatchTup[10]
                            print(service)
                            # dispatchTup = dispatchTup[0]
                            dispatchDict = {
                                'serviceType':
                                ServiceType.translate(service),
                                'vid':
                                dispatchTup[1],
                                'custid':
                                dispatchTup[2],
                                'orderid':
                                dispatchTup[3],
                                'loc_0': (
                                    vidless['current_lat'],
                                    vidless['current_lon'],
                                ),
                                'loc_f':
                                (float(dispatchTup[6]), float(dispatchTup[7])),
                                'timeOrderMade':
                                dispatchTup[8],
                            }
                            dispatch = Dispatch(**dispatchDict)
                            print((
                                vidless['current_lat'],
                                vidless['current_lon'],
                            ))
                            print(dispatch.loc_0)
                            print(dispatch.route)
                            responseBody = [dispatchTup[0], dispatch.route]

        elif '/supply/fleets/add' in path:
            status = 200

            emailOrUser = postBody['username']
            region = postBody['region']
            serviceType = postBody['serviceType']

            # Because fleets utilise a fleet manager id and not their email or username, we'll need to retrieve that
            # first using the incoming username/email. We will support both
            fmid = databaseutils.getFMID(emailOrUser)
            fleetData = (
                region,
                serviceType,
                fmid,
            )
            print(fleetData)
            newFID = databaseutils.addFleet(fleetData)
            notifications(emailOrUser, 'New Fleet Added!',
                          f'Fleet {newFID} was just added to your arsenal!')
            responseBody = {'newFID': newFID}

        self.send_response(status)
        self.end_headers()
        res = json.dumps(responseBody)
        bytesStr = res.encode('utf-8')
        self.wfile.write(bytesStr)
Exemple #17
0
def addDispatch():
    if request.method == "POST":
        d = Dispatch(0)
        i = d.id_
        p = Post(0, i)
    return redirect('/')
def dispatch(network):
    return Dispatch.convert_to_dispatchable(network)
Exemple #19
0
def removeDispatch():
    if request.method == "POST":
        dispatch_id = request.form['remove_dispatch']
        d = Dispatch(dispatch_id)
        d.removeDispatch()
    return redirect('/')
    def test_createDCdispatch(self):
        order = self.getDCOrder()
        vehicles = self.getVehicles()
        vehicle = vehicles[0]
        order['serviceType'] = ServiceType.translate(order['serviceType'])
        # Capture vehicle tuple into its separate variables
        vid, licensePlate, make, model, vLat, vLon = vehicle

        # Seeing if the unpacking worked d:
        print(vehicle)
        print()

        print(vid)
        print(licensePlate)
        print(make)
        print(model)
        print(vLon)
        print(vLat)
        print()

        vehicleDict = {
            'vid': vid,
            'licensePlate': licensePlate,
            'make': make,
            'model': model,
            'curLocation': {
                'lat': vLat,
                'lon': vLon
            },
        }

        print(vehicleDict)
        print()

        dispatchDict = deepcopy(order)
        dispatchDict['vid'] = vid

        # Turn a destination dictionary into a tupled pair
        destination = dispatchDict.pop('destination')

        dispatchDict['loc_f'] = (destination['lat'], destination['lon'])
        dispatchDict['loc_0'] = (vLat, vLon)

        # dispatchDict['status'] = DispatchStatus.RUNNING

        print(dispatchDict)
        print()

        dispatch = Dispatch(**dispatchDict)

        self.assertEqual(ServiceType.DRYCLEANING, dispatch.serviceType)
        self.assertEqual(12345, dispatch.vid)
        self.assertEqual(1234567, dispatch.custid)
        self.assertEqual(1234, dispatch.orderid)
        self.assertEqual((23.42, 42.12), dispatch.loc_0)
        self.assertEqual((31, 12), dispatch.loc_f)
        self.assertEqual("2020-03-29T13:34:00.000", dispatch.timeCreated)
        self.assertEqual(DispatchStatus.RUNNING, dispatch.status)

        print(dispatch)
        print()
        self.assertEqual(2, dispatch.status.value)

        dispatch.completed()
        self.assertEqual(DispatchStatus.DONE, dispatch.status)
        print(dispatch)
        print()

        print(repr(dispatch))