示例#1
0
    def snapshot(self, credentials):
        timestamp = time.time()
        hostname = credentials['host']

        test_file = Utility.load_yaml(
            f"testfiles/{credentials['device_type']}.yaml")

        dev = ConnectHandler(**credentials)

        output = ''
        for test in test_file:
            if 'command' not in test_file[test][0]:
                continue

            raw_command_name = str(test).replace('-', '_')
            #command = test_file[test][0]['command']

            module = import_module(
                f'drivers.snapshot.cisco.commands.{raw_command_name}')
            command_ref = getattr(module, raw_command_name)

            command_ref = command_ref()
            xml = command_ref.schemaify(
                command_ref.parse(dev.send_command(command_ref.command())))

            output = output + f"{hostname}@@@@{timestamp}@@@@{raw_command_name}@@@@{xml}@@@@@@"

        with open(f'snapshots/compiled_snapshots/{hostname}_{timestamp}.xml',
                  'w') as fp:
            fp.write(output)

        return Utility.return_json(True, 'Snapshot Completed')
示例#2
0
    def to_json(self, all_fields=False):

        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            visitor=self.visitor.to_json(all_fields),
            host=self.host.to_json(all_fields),
            representing=self.representing,
            purpose=self.purpose,
            start_date=self.start_date.isoformat(),
            end_date=self.end_date.isoformat(),
            start_time=self.start_time.isoformat(),
            end_time=self.end_time.isoformat(),
            escort_required=self.escort_required,
            is_approved=self.is_approved,
            is_expired=self.is_expired,
            is_removed=self.is_removed,
            teams=self.get_team_members(),
            entrance=self.entrance,
            created=self.created.isoformat(),
            modified=Utility.format_datetime(self.modified),
            created_by=Utility.return_instance_id(self.created_by),
            modified_by=Utility.return_instance_id(self.modified_by)
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#3
0
    def parse(self, response):
        ipItem = IpItem()

        proxy = response.meta['p']
        rootPath = proxy['root']
        table = response.xpath(rootPath).extract()

        for sel in Selector(response=response).xpath(rootPath):
            ipPath = proxy['ip']
            portPath = proxy['port']

            ipList = sel.xpath(ipPath).extract()
            portList = sel.xpath(portPath).extract()

            ip = Utility.listToStr(ipList)
            port = Utility.listToStr(portList)
            # using regular expression
            regex = '\d.{1,3}\d{1,3}'
            if re.match(regex, ip):
                print ip
                v = ValidateIp()
                protocol, anonymous, speed = v.validate(ip, port)
                if protocol is not -1:
                    ipItem['ip'] = ip
                    ipItem['port'] = port
                    print ipItem['ip'], ':', ipItem['port']
                    yield ipItem
                else:
                    continue
            else:
                continue
示例#4
0
    def to_json(self, all_fields=False):

        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            first_name=self.first_name,
            last_name=self.last_name,
            email=self.email,
            gender=self.gender,
            phone=self.phone,
            black_listed=self.black_listed,
            occupation=self.occupation,
            company=self.get_company(),
            date_of_birth=Utility.format_datetime(self.date_of_birth),
            nationality=self.nationality,
            state_of_origin=self.state_of_origin,
            lga_of_origin=self.lga_of_origin,
            pass_code=self.pass_code,
            type=self.get_type(),
            created=Utility.format_datetime(self.created),
            modified=Utility.format_datetime(self.modified),
            created_by=Utility.return_instance_id(self.created_by),
            modified_by=Utility.return_instance_id(self.modified_by)
        )

        if all_fields:
            json_object.update(dict(
                image=self.image,
                fingerprint=self.fingerprint,
                signature=self.signature,
            ))

        return json_object
示例#5
0
    def to_json(self, all_fields=False):

        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            email=self.email,
            last_name=self.last_name,
            first_name=self.first_name,
            username=self.username,
            is_superuser=self.is_superuser,
            is_active=self.is_active,
            is_staff=self.is_staff,
            date_joined=Utility.format_datetime(self.date_joined),
            last_login=Utility.format_datetime(self.last_login),
            groups=list(self.get_group_permissions(self.groups)),
            user_permissions=list(self.get_all_permissions(self.user_permissions)),
            phone=self.phone,
            work_phone=self.work_phone,
            gender=self.gender,
            home_phone=self.home_phone,
            department=self.get_department_or_object(all_fields),
        )

        if all_fields:
            json_object.update(dict(
                password=self.password,
                image=self.image
            ))

        return json_object
示例#6
0
    def parseAccount(self, response):
        urls = []

        articleListItem = ArticleListItem()
        title = response.xpath('/html/head/title//text()').extract()
        print Utility.listToStr(title)
        # account = response.xpath('//*[@class="profile_infoß"]//text()').extract()[0].strip()
        print '----------------------account-------------------------'
        for articlePath in Selector(response=response).xpath(
                '//*[@class="weui_media_box appmsg"]/div'):
            # title
            title = articlePath.xpath('./h4//text()').extract()[0].strip()
            articleListItem['title'] = title
            print articleListItem['title']
            # url
            url = articlePath.xpath('./h4//@hrefs').extract()[0]
            url = "https://mp.weixin.qq.com" + url
            articleListItem['url'] = url

            print articleListItem['url']
            # date
            date = articlePath.xpath(
                './/*[@class="weui_media_extra_info"]//text()').extract()[0]
            articleListItem['date'] = date
            print articleListItem['date']
            # abstract
            abstract = articlePath.xpath(
                './/*[@class="weui_media_desc"]//text()').extract()
            articleListItem['abstract'] = Utility.listToStr(abstract)
            print articleListItem['abstract']

        urls.append(url)
        return urls
 def get_pre_snapshots(self, hostname):
     try:
         return Utility.return_json(
             1, 'get_pre_snapshots()',
             JSnapComparison().get_pre_snapshots(hostname))
     except Exception as e:
         return Utility.return_json(0, e.msg)
示例#8
0
    def test_encrypt_decrypt(self):
        text = 'abcd123'
        encrypt = Utility.encrypt(text)
        self.assertTrue(isinstance(encrypt, str))

        decrypt = Utility.decrypt(encrypt)
        self.assertTrue(isinstance(decrypt, str))
        self.assertEqual(decrypt, text)
示例#9
0
 def post_or_put(self, request, *args, **kwargs):
     request.data['_id'] = self.kwargs['_id']
     request.data['created_by'] = Utility.return_id(UserProfile, request.data.get('created_by'), '_id')
     request.data['modified_by'] = Utility.return_id(UserProfile, request.data.get('modified_by'), '_id')
     try:
         model.objects.get(_id=self.kwargs['_id'])
         return self.update(request, *args, **kwargs)
     except model.DoesNotExist:
         return self.create(request, *args, **kwargs)
示例#10
0
 def setUp(self):
     self.listing_file = Utility.get_stock_listing_xlsx(Market.US)
     self.stock_file_CINF = Utility.get_stock_price_history_file(
         Market.US, 'CINF', '1985', 'nasdaq')
     self.stock_file_GOOG = Utility.get_stock_price_history_file(
         Market.US, 'GOOG', '2004', 'nasdaq')
     self.stock_file_MMM = Utility.get_stock_price_history_file(
         Market.US, 'MMM', '1985', 'nyse')
     self.stock_file_GS = Utility.get_stock_price_history_file(
         Market.US, 'GS', '1999', 'nyse')
示例#11
0
    def __init__(self):
        # init logging
        os.makedirs('logs', exist_ok=True)
        logging.config.dictConfig(Utility.get_logging_config())
        self.logger = logging.getLogger(__name__)
        self.config = Utility.get_config()

        self.args = self.parse_argument()
        self.robinhood = RobinhoodAccount(self.config.robinhood_account,
                                          Utility.decrypt(self.args.rhp))
示例#12
0
 def setUp(self):
     self.listing_file = Utility.get_stock_listing_xlsx(Market.US)
     self.stock_file_CINF = Utility.get_stock_price_history_file(
         Market.US, 'CINF')
     self.stock_file_GOOG = Utility.get_stock_price_history_file(
         Market.US, 'GOOG')
     self.stock_file_MMM = Utility.get_stock_price_history_file(
         Market.US, 'MMM')
     self.stock_file_GS = Utility.get_stock_price_history_file(
         Market.US, 'GS')
示例#13
0
    def delete_snapshot(self, hostname, timestamp):
        try:
            if not Utility.is_file_path_safe(hostname) or str(timestamp).count('.') > 1:
                return Utility.return_json(0, 'Invalid hostname')

            os.remove(f"snapshots/compiled_snapshots/{hostname}_{timestamp}.xml")

        except:
            return Utility.return_json(0, 'Unable to delete snapshot, probably non-existent')

        return Utility.return_json(1, 'Snapshot deleted')
示例#14
0
def nest_row(row, id=None):
    if id is not None:
        row['_id'] = id
    appointment = Utility.get_nested(Appointments, AppointmentSerializer, row['appointment'])

    if len(appointment) > 0:
        appointment['host'] = Utility.get_nested(UserProfile, UserSerializer, appointment['host'])
        appointment['visitor'] = Utility.get_nested(Visitors, VisitorSerializer, appointment['visitor'])
        del appointment['visitor']['image']
        del appointment['host']['image']
        appointment['log'] = row
        row = appointment
    return row
示例#15
0
    def post_or_put(self, request, *args, **kwargs):
        request.data['_id'] = self.kwargs['_id']
        request.data['company'] = Utility.return_id(Company, request.data.get('company', None), 'name')
        request.data['type'] = Utility.return_id(VisitorTypes, request.data.get('type', None), 'name')
        try:
            model.objects.get(_id=self.kwargs['_id'])
            instance_exists = True
        except model.DoesNotExist:
            instance_exists = False

        if instance_exists:
            return self.update(request, *args, **kwargs)
        else:
            return self.create(request, *args, **kwargs)
示例#16
0
    def post_or_put(self, request, *args, **kwargs):
        request.data['_id'] = self.kwargs['_id']
        request.data['entrance'] = Utility.return_id(Entrance, request.data.get('entrance'), 'name')
        request.data['created_by'] = Utility.return_id(UserProfile, request.data.get('created_by'), '_id')
        request.data['modified_by'] = Utility.return_id(UserProfile, request.data.get('modified_by'), '_id')
        try:
            model.objects.get(_id=self.kwargs['_id'])
            instance_exists = True
        except model.DoesNotExist:
            instance_exists = False

        if instance_exists:
            return self.update(request, *args, **kwargs)
        else:
            return self.create(request, *args, **kwargs)
示例#17
0
    def to_json(self, all_fields=False):

        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            name=self.name,
            created=Utility.format_datetime(self.created),
            modified=Utility.format_datetime(self.modified),
            created_by=Utility.get_instance_fields(self.created_by, ['_id', 'username']),
            modified_by=Utility.get_instance_fields(self.modified_by, ['_id', 'username'])
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#18
0
    def post(self, request):

        config = ConfigManager().get_config()
        user = None

        username = request.data.get('username', None)
        password = request.data.get('password', None)

        if config.get('authType', 'api') == 'api':
            user = authenticate(username=username, password=password)

        if config.get('authType', 'api') == 'ldap':
            user = LDAPManager().ldap_login(username, password)

        if user is not None:
            if user.is_active:
                token = Token.objects.get(user=user)
                serializer = UserSerializer(user)
                data = serializer.data
                del data['password']
                data['department'] = Utility.get_nested(Department, DepartmentSerializer, data['department'])

                return Response({'user': data, 'token': token.key})
            else:
                return Response({'detail': 'User not active'}, status=status.HTTP_400_BAD_REQUEST)

        return Response({'detail': 'invalid credentials provided'}, status=status.HTTP_401_UNAUTHORIZED)
示例#19
0
    def parseArticle(self, response):
        articleItem = ArticleItem()

        title = response.xpath(
            '//*[@id="activity-name"]//text()').extract()[0].strip()
        date = response.xpath('//*[@id="post-date"]//text()').extract()[0]
        author = response.xpath(
            '//*[@id="img-content"]/div[1]/em[2]').extract()
        account = response.xpath('//*[@id="post-user"]//text()').extract()[0]
        accountId = response.xpath(
            '//*[@id="js_profile_qrcode"]/div/p[1]/span//text()').extract()[0]
        content = response.xpath('//*[@id="js_content"]').extract()

        articleItem['title'] = title.encode('utf-8')
        articleItem['date'] = date.encode('utf-8')
        articleItem['account'] = account.encode('utf-8')
        articleItem['accountId'] = accountId.encode('utf-8')
        articleItem['content'] = Utility.listToStr(content).encode('utf-8')

        if len(author) > 0:
            author = author[0].encode('utf-8')
            articleItem['author'] = author
        else:
            articleItem['author'] = ''

        print articleItem['title']
        # Utility.writeArticleToFile(articleItem)
        yield articleItem
示例#20
0
 def getBlock(self, coordinate):
     i, j = [Utility.round(k) for k in coordinate]
     i, j = self.coordinateToIndex((i, j))
     if i < 0 or i >= self.width or j < 0 or j >= self.height:
         return Constants.AIR_BLOCK
     block = self.blocks[j][i]
     return block
示例#21
0
    def save_snapshot(sender, instance, **kwargs):
        def get_value(data, field):
            from django.db.models.fields.related import ForeignKey
            if isinstance(field, ForeignKey) and data:
                return getattr(data, '_id', 'no id')
            elif data is not None:
                return data
            elif isinstance(field, (DateTimeField, DateField, TimeField)):
                return Utility.format_datetime(data)
            else:
                return ''
        if getattr(instance, '_id', None) is not None:
            changed = dict()
            current_record = sender.objects.filter(_id=instance._id)
            if len(current_record) > 0:
                current_record = current_record.first()
                fields = instance._meta.get_fields()
                for field in fields:
                    if field.name not in ['modified_by', 'created_by', '_rev', 'logentry', 'password']:
                        try:
                            current_value = getattr(current_record, field.name)
                            instance_value = getattr(instance, field.name)
                            if current_value != instance_value:
                                if instance(field, DateField):
                                    current_value = Utility.format_datetime(current_value)
                                changed[field.name] = get_value(current_value, field)
                        except:
                            pass

            if len(changed) > 0:
                model = sender.__name__.lower()
                try:
                    Changes.objects.create(model=model, type='updated', row_id=instance._id, snapshot=json.dumps(changed))
                except Exception as e:
                    print (changed)
示例#22
0
 def get(self, request, *args, **kwargs):
     user = Utility.get_data_or_none(UserProfile, request, **kwargs)
     if user is None:
         return Response({'detail': 'Not Found'}, status=status.HTTP_404_NOT_FOUND)
     else:
         profile = user.to_json(True)
         del profile['password']
         return Response(profile)
示例#23
0
    def to_json(self, all_fields=False):
        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            name=self.name,
            floor=self.floor,
            description=self.description,
            created=Utility.format_datetime(self.created),
            modifeid=Utility.format_datetime(self.modified),
            created_by=Utility.return_instance_id(self.created_by),
            modified_by=Utility.return_instance_id(self.modified_by)
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#24
0
 def get(self, request, *args, **kwargs):
     instance = Utility.get_data_or_none(model, request, **kwargs)
     if instance is None:
         return Response({'detail': 'Not Found'}, status=status.HTTP_404_NOT_FOUND)
     else:
         serializer = VisitorGroupSerializer(instance)
         data = serializer.data
         row = nest_row(data)
         return Response(row)
示例#25
0
    def compare(self, hostname, presnap_timestamp, postsnap_timestamp):

        try:
            device_info = Utility.get_device_info(hostname)

            # Breaking DI by instantiating JSnapComparison here, but I don't foresee making a custom comparison engine for awhile.
            #  But for easier future proofing, I'm leaving this essentially useless class in the process, as it will serve
            #   as an abstraction layer when I do design my own comparison engine

            return Utility.return_json(
                1, "Compare()",
                JSnapComparison().compare(
                    hostname, presnap_timestamp, postsnap_timestamp,
                    Utility.get_vendor_models()[device_info['vendor']][
                        device_info['model']]))
        except Exception as e:
            return Utility.return_json(
                0, 'Failed - ensure you can access sense.one.twcbiz.com APIs')
示例#26
0
 def get_value(data, field):
     from django.db.models.fields.related import ForeignKey
     if isinstance(field, ForeignKey) and data:
         return getattr(data, '_id', 'no id')
     elif data is not None:
         return data
     elif isinstance(field, (DateTimeField, DateField, TimeField)):
         return Utility.format_datetime(data)
     else:
         return ''
示例#27
0
    def process_item(self, item, spider):
        if isinstance(item, ArticleItem):
            print '=========================================================='
            p = re.compile(r'(<img(.*?)src=\"(.*?)\")')
            i = 0
            for m in p.finditer(item['content']):
                url = m.group(3)
                filename = './img/article_' + str(i) + '.jpg'
                Utility.getPicFromUrl(url, filename)

                ls_f = Utility.convertPicToBase64(filename)

                item['content'] = item['content'].replace('data-src', 'src')
                item['content'] = item['content'].replace(
                    m.group(3), 'data:image/jpeg;base64,' + ls_f)

                i += 1
                Utility.writeArticleToFile(item)
                return item
示例#28
0
 def get(self, request, *args, **kwargs):
     user = Utility.get_data_or_none(UserProfile, request, **kwargs)
     if user is None:
         return Response({'detail': 'Not Found'}, status=status.HTTP_404_NOT_FOUND)
     else:
         serializer = UserSerializer(user)
         data = serializer.data
         del data['password']
         row = nest_row(data)
         return Response(row)
示例#29
0
    def to_json(self, all_fields=False):

        json_object = dict(
            _id=self._id,
            _rev=self._rev,
            appointment=self.get_appointment(all_fields),
            license=self.license,
            model=self.model,
            type=self.type,
            color=self.color,
            created=Utility.format_datetime(self.created),
            modified=Utility.format_datetime(self.modified),
            created_by=Utility.get_instance_fields(self.created_by, ['_id', 'username']),
            modified_by=Utility.get_instance_fields(self.modified_by, ['_id', 'username'])
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#30
0
    def to_json(self, all_fields=False):

        json_object = dict(
                _id=self._id,
                _rev=self._rev,
                message_body=self.message_body,
                destination=self.destination,
                status=self.status,
                type=self.type,
                source=self.source,
                created=Utility.format_datetime(self.created),
                modified=Utility.format_datetime(self.modified),
                created_by=Utility.get_instance_fields(self.created_by, ['_id', 'username']),
                modified_by=Utility.get_instance_fields(self.modified_by, ['_id', 'username'])
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#31
0
def extra_filters(request, list):
    built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model)
    query = dict()
    order_by = request.query_params.get('order_by', '-created').replace('.', '__')
    for key in built_filter:
        query['{}__iexact'.format(key)] = built_filter[key]
    try:
        list = model.objects.filter(**query).order_by(order_by)
    except Exception as e:
        print (e)
    return list
示例#32
0
    def get_snapshots(self, hostname):
        if not Utility.is_file_path_safe(hostname):
            raise Exception('Hostname was invalid')

        snap_files = sorted(glob(f"snapshots/compiled_snapshots/{hostname}_*.xml"), reverse=True)

        for iter, file in enumerate(snap_files):
            snap_files[iter] = (os.path.basename(str(file)))
            (temp_hostname, temp_timestamp) = snap_files[iter].split('_')
            temp_timestamp = temp_timestamp.strip('.xml')
            snap_files[iter] = [temp_hostname, temp_timestamp]
        return snap_files
示例#33
0
 def get(self, request, *args, **kwargs):
     instance = Utility.get_data_or_none(model, request, **kwargs)
     if instance is None:
         return Response({'detail': 'Not Found'}, status=status.HTTP_404_NOT_FOUND)
     else:
         row = instance.to_json(True)
         row['status'] = Appointments().get_status(row)
         logs = AppointmentLogs().get_logs(row.get('_id'))
         sorted_logs = sorted(logs, key=lambda k: k['created'], reverse=True)
         row['logs'] = sorted_logs
         if len(sorted_logs) > 0:
             row['latest'] = sorted_logs[0]
         return Response(row)
示例#34
0
    def parse(self, response):
        for n in range(9):
            articleListItem = ArticleListItem()
            num = self.articleListCon
            rootPath = '//*[@id="sogou_vr_' + num

            titlePath = rootPath + '_title_' + str(n) + '"]'
            contentPath = rootPath + '_summary_' + str(n) + '"]'

            title = response.xpath(titlePath + '//text()').extract()
            url = response.xpath(titlePath + '/@href').extract()[0]
            content = response.xpath(contentPath + '//text()').extract()

            articleListItem['title'] = Utility.listToStr(title)
            articleListItem['url'] = url
            articleListItem['abstract'] = Utility.listToStr(content)

            print 'title==', articleListItem['title']
            print 'href==', articleListItem['url']
            print 'content=='
            print articleListItem['abstract']

            yield scrapy.Request(url, callback=self.parseArticle)
示例#35
0
 def post_or_put(self, request, *args, **kwargs):
     request.data['_id'] = self.kwargs['_id']
     request.data['department'] = Utility.return_id(Department, request.data.get('department'), 'name')
     try:
         user_instance = UserProfile.objects.get(_id=self.kwargs['_id'])
         if request.data.get('password', None) is None or request.data.get('password', None) == '':
             request.data['password'] = user_instance.password
         elif request.data.get('password', None) is not None and request.data.get('password', None) != '':
             request.data['password'] = make_password(request.data['password'])
         return self.update(request, *args, **kwargs)
     except UserProfile.DoesNotExist:
         if request.data.get('password', None) is not None and request.data.get('password', None) != '':
             request.data['password'] = make_password(request.data['password'])
         return self.create(request, *args, **kwargs)
示例#36
0
    def snapshot(self, credentials):
        timestamp = time.time()
        hostname = credentials['host']

        test_file = Utility.load_yaml('testfiles/juniper.yaml')

        dev = ConnectHandler(**credentials)

        output = ''
        for test in test_file:
            if 'command' not in test_file[test][0]:
                continue

            raw_command_name = str(test).replace('-', '_')
            command = test_file[test][0]['command']
            if ' | display xml' not in command:
                command = command + ' | display xml'

            result = str(dev.send_command(command)).strip('{master}').strip()

            xml = etree.fromstring(result).xpath(
                '//rpc-reply/*[not(self::cli)]')

            if len(xml) <= 0:
                return Utility.return_json(False,
                                           'Invalid rpc-reply from device')

            xml = etree.tostring(xml[0]).decode("utf-8")

            output = output + f"{hostname}@@@@{timestamp}@@@@{raw_command_name}@@@@{xml}@@@@@@"

        with open(f'snapshots/compiled_snapshots/{hostname}_{timestamp}.xml',
                  'w') as fp:
            fp.write(output)

        return Utility.return_json(True, 'Snapshot Completed')
示例#37
0
    def to_json(self, all_fields=False):

        json_object = dict(
                _id=self._id,
                _rev=self._rev,
                model=self.model,
                type=self.type,
                row_id=self.row_id,
                rev=self.rev,
                snapshot=self.snapshot,
                reviewed=self.reviewed,
                created=Utility.format_datetime(self.created)
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#38
0
def nest_row(row, id=None):
    if id is not None:
        row['_id'] = id
    row['visitor'] = Utility.get_nested(Visitors, VisitorSerializer, row['visitor'])
    row['host'] = Utility.get_nested(UserProfile, UserSerializer, row['host'])
    row['entrance'] = Utility.get_nested(Entrance, EntranceSerializer, row['entrance'])
    if type(row['visitor']) is dict and len(row['visitor']) > 0:
        row['visitor']['company'] = Utility.get_nested(Company, CompanySerializer, row['visitor']['company'])
        row['visitor']['group'] = Utility.get_nested(VisitorGroup, VisitorGroupSerializer, row['visitor']['group'])

    if type(row['host']) is dict and len(row['host']) > 0:
        row['host']['department'] = Utility.get_nested(Department, DepartmentSerializer, row['host']['department'])

    return row
示例#39
0
    def parse(self, response):
        for n in range(1):
            accountListItem = AccountListItem()
            num = self.accountListCon

            rootPath = '//*[@id="sogou_vr_' + num + '_box_' + str(n) + '"]'
            accountIdPath = './div/div[2]/p[2]/label//text()'

            txtBox = response.xpath(rootPath)
            accountId = txtBox.xpath(accountIdPath).extract()

            accountName = txtBox.xpath('./div/div[2]/p[1]/a//text()').extract()

            url = txtBox.xpath('./div/div[2]/p[1]/a/@href').extract()[0]

            accountListItem['account'] = Utility.listToStr(accountName)
            accountListItem['url'] = url
            accountListItem['accountId'] = accountId[0].encode('utf-8')

            print accountListItem['account']
            print accountListItem['url']
            print accountListItem['accountId']

            url = url.replace('http', 'https')

            cmd = "phantomjs spider/getBody.js '%s'" % url
            print url
            print cmd
            stdout, stderr = subprocess.Popen(
                cmd,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE).communicate()
            r = HtmlResponse(url=url, body=stdout)

            articleUrls = self.parseAccount(r)
            for url in articleUrls:
                yield scrapy.Request(url,
                                     callback=ArticleSpider().parseArticle)
示例#40
0
def extra_filters(request, list):
    if 'q' not in request.query_params:
        query = []
        if request.user.is_superuser is not True and request.user.is_staff is not True:
            user_appointments = Appointments.objects.filter(host=request.user._id).values_list('visitor___id', flat=True)
            query.append(Q(_id__in=user_appointments) | Q(created_by=request.user._id))

        built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model)

        order_by = request.query_params.get('order_by', '-created').replace('.', '__')
        for key in built_filter:
            pin = dict()

            if 'search' in request.query_params:
                pin['{}__icontains'.format(key)] = built_filter[key]
            else:
                pin['{}__iexact'.format(key)] = built_filter[key]
            query.append(Q(**pin))
        try:
            list = model.objects.filter(*query).order_by(order_by)
        except Exception as e:
            print (e)
    return list
示例#41
0
    def to_json(self, all_fields=False):

        json_object = dict(
                _id=self._id,
                _rev=self._rev,
                appointment=self.appointment,
                checked_in=Utility.format_datetime(self.checked_in),
                checked_out=Utility.format_datetime(self.checked_out),
                label_code=self.label_code,
                created=Utility.format_datetime(self.created),
                modified=Utility.format_datetime(self.modified),
                created_by=Utility.get_instance_fields(self.created_by, ['_id', 'username']),
                modified_by=Utility.get_instance_fields(self.modified_by, ['_id', 'username'])
        )

        if all_fields:
            json_object.update(dict())

        return json_object
示例#42
0
def nest_row(row, id=None):
    if id is not None:
        row['_id'] = id
    visitor = Utility.get_nested(Visitors, VisitorSerializer, row['visitor'])
    host = Utility.get_nested(UserProfile, UserSerializer, row['host'])
    if len(visitor) > 0 and id is not None:
        del visitor['image']
    if len(host) > 0 and id is not None:
            del host['image']
    row['visitor'] = visitor
    row['host'] = host
    row['status'] = Appointments().get_status(row)
    row['entrance'] = Utility.get_nested(Entrance, EntranceSerializer, row['entrance'])
    if type(row['visitor']) is dict and len(row['visitor']) > 0:
        row['visitor']['company'] = Utility.get_nested(Company, CompanySerializer, row['visitor']['company'])
        row['visitor']['group'] = Utility.get_nested(VisitorGroup, VisitorGroupSerializer, row['visitor']['group'])

    if type(row['host']) is dict and len(row['host']) > 0:
        row['host']['department'] = Utility.get_nested(Department, DepartmentSerializer, row['host']['department'])

    return row
示例#43
0
    def snapshot(self, hostname):
        device_info = Utility.get_device_info(hostname)

        if device_info == None:
            return Utility.return_json(0, 'Invalid hostname or IP')

        class_path_name = Utility.get_vendor_models()[device_info['vendor']][device_info['model']]
        class_name = class_path_name.capitalize()

        # Dynamically import and instantiate the correct snapshot class
        module_name = import_module(f'drivers.snapshot.{device_info["vendor_lower"]}.{class_path_name}')
        class_ref = getattr(module_name, class_name)

        snapshot = class_ref()

        credentials = {
            'device_type': Utility.get_device_types()[device_info['vendor']],
            'host': hostname,
            'username': Utility.get_credentials()['username'],
            'password': Utility.get_credentials()['password']
        }

        return snapshot.snapshot(credentials)
示例#44
0
 def get(self, request, *args, **kwargs):
     instance = Utility.get_data_or_none(model, request, **kwargs)
     if instance is None:
         return Response({'detail': 'Not Found'}, status=status.HTTP_404_NOT_FOUND)
     else:
         return Response(instance.to_json(True))
示例#45
0
 def test_encrypt_decrypt_none(self):
     self.assertEqual(Utility.encrypt(None), '')
     self.assertEqual(Utility.decrypt(None), '')
示例#46
0
 def test_encrypt_decrypt_empty(self):
     self.assertEqual(Utility.encrypt(''), '')
     self.assertEqual(Utility.decrypt(''), '')
示例#47
0
 def setBlock(self, blockType, coordinate):
     i, j = [Utility.round(k) for k in coordinate]
     i, j = self.coordinateToIndex((i, j))
     self.blocks[j][i] = Block(blockType, i, j)
示例#48
0
 def test_encrypt_decrypt_space(self):
     self.assertEqual(Utility.encrypt('   \t  '), '')
     self.assertEqual(Utility.decrypt('  \t   \t'), '')
示例#49
0
def nest_row(row, id=None):
    if id is not None:
        row['_id'] = id
    row['department'] = Utility.get_nested(Department, DepartmentSerializer, row['department'])

    return row
示例#50
0
 def __init__(self):
     self.loop = None
     self.snmp_reader = SNMPReader()
     self.util = Utility()
示例#51
0
class EventLoop(object):
    """AsyncIO EventLoop"""
    def __init__(self):
        self.loop = None
        self.snmp_reader = SNMPReader()
        self.util = Utility()

    @staticmethod
    def get_timeout(sleep, timeout):
        """
        Set a trusted timeout with gathering the sleep time and the timeout.
        :param sleep: Declared sleep.
        :param timeout: Declared timeout
        :return: Trusted Timeout.
        """
        if sleep < timeout:
            total_time = sleep + timeout

        else:
            total_time = max(sleep, timeout)

        return total_time + .1  # TODO

    async def read_forever(self, loop, **kwargs):
        """
        Forever worker to collecting the SNMP(s) device.
        :param loop: asyncio loop.
        :param kwargs: The below parameters.
        :return:
        """
        community = kwargs.get('community', 'public')
        address = kwargs.get('address', '127.0.0.1')
        port = kwargs.get('port', 161)
        hostname = (address, port)
        timeout = kwargs.get('timeout', 1)
        retries = kwargs.get('retries', 3)
        oid = kwargs.get('oid', '0.0.0.0.0.0')
        version = kwargs.get('version', 1)

        community_data = CommunityData(community, mpModel=version-1)
        udp_transport_target = UdpTransportTarget(hostname, timeout=timeout, retries=retries)
        object_type = ObjectType(ObjectIdentity(oid))
        while True:
            try:
                # async with async_timeout.timeout(total_timeout) as cm:
                await self.snmp_reader.read_async_full(
                    loop,
                    community_data, udp_transport_target, object_type,
                    **kwargs
                )

            # except asyncio.TimeoutError as exc:
            #     print(cm.expired, exc)
            #     pass

            except KeyboardInterrupt:
                loop.close()

    def run_once(self):
        """Run once method in asyncio tech."""

        if _uvloop:
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
            '''Set the uvloop event loop policy.'''

        loop = asyncio.get_event_loop()
        configs = get_config()

        if configs:
            futures = [asyncio.ensure_future(self.snmp_reader.read_async_full(loop, **conf))
                       for conf in configs]
            result = loop.run_until_complete(asyncio.gather(*futures))
            print(result)

        else:
            raise NotImplementedError()

    async def restart_loop(self):
        """An asynchronous loop re-starter worker to monitor the change in the config file."""
        loop = asyncio.get_event_loop()
        _, cache = self.util.is_config_exist()

        while True:
            config_path, stamp = self.util.is_config_exist()

            if stamp != cache:
                cache = stamp
                print('Loop will be restarted.')
                loop.stop()

            await asyncio.sleep(10)

    @staticmethod
    def stop_auth(auth):
        """Stop ZAP"""
        try:
            auth.stop
        except Exception as exc:
            print(exc)

    @staticmethod
    def destroy_snmp_engines(engine):
        """
        Destroy SNMP-Engine instance.
        :param engine:
        :return:
        """
        try:
            # engine.transportDispatcher.closeDispatcher()
            engine.unregisterTransportDispatcher()

        except PySnmpError as snmp_exc:
            print(snmp_exc)

        except Exception as exc:
            print(exc)

    def termination(self, configs, futures):
        """
        Destroy some expensive instances: Stop ZAP, Destroy SNMP-Engines, Destroy Zombie
        coroutine Asyncio tasks and clear other memory usage by Python GC.
        :param configs: SNMP configuration
        :param futures: Asyncio coroutine tasks.
        :return:
        """
        for f in futures:
            f.cancel()

        for conf in configs:
            self.destroy_snmp_engines(conf['engine'])

            for srv in conf['servers']:
                try:
                    self.stop_auth(srv['auth'])
                except Exception as exc:
                    print(exc)

        gc.collect()  # TODO

    def run_forever(self):
        """Forever event-loop with the loop re-starter ability in asyncio tech."""

        if _uvloop:
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
            '''Set the uvloop event loop policy.'''

        loop = asyncio.get_event_loop()
        loop.create_task(self.restart_loop())

        while True:
            configs = get_config()

            if configs:
                futures = []
                for conf in configs:
                    if conf['isEnable']:
                        futures.append(loop.create_task(self.read_forever(loop, **conf)))

                    else:
                        info_ = f"{conf['name']} SNMP-Model is Disable."
                        print(info_)

                try:
                    '''Run'''
                    loop.run_forever()

                    '''Termination'''
                    self.termination(configs, futures)

                except KeyboardInterrupt:
                    print("The process was killed.")
                    loop.close()
                    sys.exit(0)

                except asyncio.CancelledError:
                    print('Tasks has been canceled.')
                    loop.close()

                except Exception:
                    print(traceback.format_exc())

            else:
                time.sleep(5)
                print("Waiting for SNMP configuration ...")
示例#52
0
 def __init__(self, app):
     super().__init__(app)
     self.initComponents()
     self.worlds = Utility.loadWorlds()
     self.updateButtons()
示例#53
0
def extra_filters(request, list):
    if 'load' in request.query_params:
        load = request.query_params.get('load')

        def in_progress():
            today = date.today()
            checked_in = AppointmentLogs.objects.filter(
                checked_in__year=today.year,
                checked_in__month=today.month,
                checked_in__day=today.day,
                checked_out=None,
                is_removed=False
            ).values_list('appointment', flat=True)

            query = dict(
                _id__in=checked_in,
                is_removed=False,
                is_expired=False
            )
            if request.query_params.get('host', None) is not None:
                query['host'] = request.query_params['host']
            if request.user.is_superuser is not True and request.user.is_staff is not True:
                query['host'] = request.user._id

            return Appointments.objects.filter(**query)

        def upcoming():
            today = date.today()
            checked_in = AppointmentLogs.objects.filter(
                checked_in__year=today.year,
                checked_in__month=today.month,
                checked_in__day=today.day,
                checked_out=None,
                is_removed=False
            ).values_list('appointment', flat=True)

            query = [
                Q(start_date__gte=today) | Q(start_date__lte=today),
                Q(end_date__gte=today),
                Q(is_expired=False),
                Q(is_approved=True),
                Q(is_removed=False),

            ]
            if request.query_params.get('host', None) is not None:
                query.append(Q(host=request.query_params['host']))
            if request.user.is_superuser is not True and request.user.is_staff is not True:
                query.append(Q(host=request.user._id))
            return Appointments.objects.filter(*query).exclude(_id__in=checked_in)

        def awaiting():
            today = date.today()
            query = [
                Q(start_date__gte=today) | Q(start_date__lte=today),
                Q(end_date__gte=today),
                Q(is_expired=False),
                Q(is_approved=None),
                Q(is_removed=False)
            ]
            if request.query_params.get('host', None) is not None:
                query.append(Q(host=request.query_params['host']))
            if request.user.is_superuser is not True and request.user.is_staff is not True:
                query.append(Q(host=request.user._id))
            return Appointments.objects.filter(*query)

        def rejected():
            today = date.today()
            query = dict(
                is_approved=False,
                end_date__gt=today,
                is_removed=False
            )
            if request.query_params.get('host', None) is not None:
                query['host'] = request.query_params['host']
            if request.user.is_superuser is not True and request.user.is_staff is not True:
                query['host'] = request.user._id
            return Appointments.objects.filter(**query)

        if load == 'upcoming':
            return  upcoming()
        if load == 'pending':
            return awaiting()
        if load == 'rejected':
            return rejected()
        if load == 'in-progress':
            return  in_progress()
    if 'q' not in request.query_params:
        built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model)
        query = dict()
        search_query = []
        if request.user.is_superuser is not True and request.user.is_staff is not True:
            query['host'] = request.user._id
        order_by = request.query_params.get('order_by', '-created').replace('.', '__')
        for key in built_filter:
            if 'search' in request.query_params:
                if key == 'host':
                    search_query.append(Q(**{'host__last_name__icontains': built_filter[key]}) | Q(**{'host__first_name__icontains': built_filter[key]}))
                elif key == 'visitor':
                    search_query.append(Q(**{'visitor__last_name__icontains': built_filter[key]}) | Q(**{'visitor__first_name__icontains': built_filter[key]}))
                else:
                    search_query.append(Q(**{'{}__icontains'.format(key): built_filter[key]}))
            else:
                query['{}__iexact'.format(key)] = built_filter[key]
        try:
            if 'search' in request.query_params:
                list = model.objects.filter(*search_query).order_by(order_by)
            else:
                list = model.objects.filter(**query).order_by(order_by)
        except Exception as e:
            print (e)

    return list
示例#54
0
def nest_row(row, id=None):
    if id is not None:
        row['_id'] = id
    row['company'] = Utility.get_nested(Company, CompanySerializer, row['company'])
    return row
示例#55
0
 def generate_credentials(self):
     cred = Utility.load_yaml('config/credentials.yaml')
     return (cred['username'], cred['password'])
示例#56
0
 def quit(self):
     Utility.save(self.world)
     pygame.event.wait()
     self.app.changeScene("main")
示例#57
0
 def deleteWorld(self, i):
     Utility.deleteWorld(i+1)
     self.worlds[i] = None
     self.updateButtons()