Ejemplo n.º 1
0
def get_asterisk_conf():
    localtest_root = os.getenv("AST_TEST_ROOT")
    if localtest_root:
        # The default etc directory for Asterisk
        default_etc_directory = os.path.join(localtest_root, "etc/asterisk")
    else:
        # The default etc directory for Asterisk
        default_etc_directory = "/etc/asterisk"

    # Find the system installed asterisk.conf
    ast_confs = [
        os.path.join(default_etc_directory, "asterisk.conf"),
        "/usr/local/etc/asterisk/asterisk.conf",
    ]
    _ast_conf = None
    for config in ast_confs:
        if os.path.exists(config):
            _ast_conf = ConfigFile(config)
            break
    if _ast_conf is None:
        msg = "Unable to locate asterisk.conf in any known location"
        LOGGER.error(msg)
        raise Exception(msg)

    # Get the Asterisk directories from the Asterisk config file
    _ast_conf.directories = {};
    for cat in _ast_conf.categories:
        if cat.name == "directories":
            for (var, val) in cat.options:
                _ast_conf.directories[var] = val

    return _ast_conf
Ejemplo n.º 2
0
    def __init__(self, account):          #Account and password are instagram account and password
        self.config_file = ConfigFile(account)
        self.sub_list = self.config_file.get("Reddit", "Subreddits")
        self.account = account
        self.ibot =  InstagramBot(account)
        self.rbot = RedditBot(account)
        self.obot = osManager(int(self.config_file.get("Time", "imagelifetime")))
        self.db = dbManager()
        self.numPosts = int(self.ibot.getNumberOfPosts())
        #Make this self and remove all of the member variables and instead call the dicionary when needed
        times = self.config_file.get("Time")
        for k,v in times.items():
            times[k] = int(v)

        self.scrape_time = times["scrapetime"]
        self.upload_time_MAX = times["uploadtimemax"]
        self.upload_time_MIN = times["uploadtimemin"]
        self.expire_time = times["expiretime"]
        self.retry_upload_delay = times["retryuploaddelay"]
        #Keep this however when deleting for convenience
        self.slow_hours_start = datetime.time(times["slowhourstart"], 0, 0)
        self.slow_hours_end = datetime.time(times["slowhourend"], 0, 0)
        self.slow_hours_delay = times["slowhourdelay"]
        self.follow_time_MAX = times["followtimemax"]
        self.follow_time_MIN = times["followtimemin"]
        self.like_time_MAX = times["liketimemax"]
        self.like_time_MIN = times["liketimemin"]
Ejemplo n.º 3
0
    def test_pickle(self):
        config = DayConfig()

        dayConfig = config.di
        dayConfig.startTime = time(5,0,0)
        pumpConfig = dayConfig.pumpPeriod
        pumpConfig.period = timedelta(minutes = 15)
        pumpConfig.duration = timedelta(minutes = 5)
        
        nigthConfig = config.noct
        nigthConfig.startTime = time(17,0,0)
        pumpConfig = nigthConfig.pumpPeriod
        pumpConfig.period = timedelta(minutes = 60)
        pumpConfig.duration = timedelta(minutes = 1)

        saver = ConfigFile('ConfigFileTest_test_pickle.pyc')
        saver.save(config)

        # TODO assert file exists

        loader = ConfigFile('ConfigFileTest_test_pickle.pyc')
        loadedConfig = loader.load()

        os.unlink('ConfigFileTest_test_pickle.pyc')

        loadedDayConfig = loadedConfig.di
        self.assertEqual(loadedDayConfig.startTime, time(5,0,0))
        loadedPumpConfig = loadedDayConfig.pumpPeriod
        self.assertEqual(loadedPumpConfig.period, timedelta(minutes = 15))
        self.assertEqual(loadedPumpConfig.duration, timedelta(minutes = 5))
Ejemplo n.º 4
0
 def test_fileDontExists(self):
     loader = ConfigFile('ConfigFileTest_test_fileDontExists.pyc')
     try:
         loadedConfig = loader.load()
         self.fail()
     except IOError:
         pass
Ejemplo n.º 5
0
 def __init__(self, filename, bridge=None, service=None):
     self.service = None
     if service:
         self.setService(service)
     ConfigFile.__init__(self, filename)
     self.bridge = bridge
     self.name = os.path.basename(self.filename)
Ejemplo n.º 6
0
def parse_args():
    """
    Parses command line options and arguments
    """
    config = ConfigFile()

    ap = ArgumentParser(usage="usage: %prog [opts]")

    ap.add_argument("-t",
                    "--test",
                    dest="test",
                    default=False,
                    action="store_true",
                    help="executes the test suite")

    ap.add_argument("-f",
                    "--fullscreen",
                    dest="fullscreen",
                    default=config.get_bool("fullscreen", False),
                    action="store_true",
                    help="starts the application in fullscreen mode")

    ap.add_argument("-v",
                    "--version",
                    dest="version",
                    default=False,
                    action="store_true",
                    help="displays the current version of the application")

    return ap.parse_args()
Ejemplo n.º 7
0
	
	def write_config (self, config, config_name) :
		"""Store a configuration in the archive
		
		:Parameters:
		 - `config` (:class:Config) - configuration to store
		 - `config_name` (str) - name to use to retrieve the configuration later
		"""
		filename = "%s.xml" % config_name
		cfg_file = ConfigFile(config)
Ejemplo n.º 8
0
 def _globalConfSettings(self):
     zenhome = os.environ.get('ZENHOME')
     if zenhome:
         with open(os.path.join(zenhome, 'etc/global.conf'), 'r') as fp:
             globalConf = ConfigFile(fp)
             settings = {}
             for line in globalConf.parse():
                 if line.setting:
                     key, val = line.setting
                     settings[key] = val
             return settings
Ejemplo n.º 9
0
	def read_config (self, config_name) :
		"""Read a configuration stored in this archive
		
		:Parameters:
		 - `config_name` (str) - name of the configuration to read
		
		:Returns Type: :class:`Config`
		"""
		filename = "%s.xml" % config_name
		data,descr = self.read_file(filename)
		cfg_file = ConfigFile()
Ejemplo n.º 10
0
 def __init__(self, iusername):
     self.config_file = ConfigFile(iusername)
     self.reddit = praw.Reddit(
         client_id=self.config_file.get("Reddit", "client_id"),
         client_secret=self.config_file.get("Reddit", "client_secret"),
         password=self.config_file.get("Reddit", "password"),
         user_agent=self.config_file.get("Reddit", "user_agent"),
         username=self.config_file.get("Reddit", "username"))
     self.redditURL = 'www.reddit.com/r/'
     self.db = dbManager()
     self.osMan = osManager()
     self.videoDurationMax = int(
         self.config_file.get("Time", "videodurationmax"))
Ejemplo n.º 11
0
def network_connect():
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)

    ssid = ConfigFile.get('wifi', {}).get('ssid')
    if ssid:
        sta_if.connect(ssid, ConfigFile.get('wifi', {}).get('pwd'))

    if not wifi_connected(sta_if):
        ap = network.WLAN(network.AP_IF)  # create access-point interface
        ap.active(True)  # activate the interface
        ap.config(essid='micropython_ap')  # set the ESSID of the access point

        @MicroWebSrv.route('/aps', 'GET')
        def scan_ap(http_client, http_response):
            sta_if.active(True)
            ap_list = sta_if.scan()
            http_response.WriteResponseJSONOk([ap[0] for ap in ap_list])

        @MicroWebSrv.route('/connect', 'POST')
        def connect(http_client, http_response):
            params = json.loads(http_client.ReadRequestContent())
            ssid = params.get('ssid')
            if not ssid:
                http_response.WriteResponseJSONOk({
                    'Success': False,
                    'Message': 'ssid不能为空!'
                })
                return
            sta_if = network.WLAN(network.STA_IF)
            sta_if.active(True)
            sta_if.connect(ssid, params.get('pwd'))  # Connect to an AP
            if wifi_connected(sta_if):
                print('connect success!')
                wifi_config = ConfigFile.get('wifi', {})
                wifi_config['ssid'] = ssid
                wifi_config['pwd'] = params.get('pwd')
                ConfigFile.set('wifi', wifi_config)
                machine.reset()
            http_response.WriteResponseJSONOk({
                'Success': False,
                'Message': '连接失败!'
            })

        srv = MicroWebSrv(webPath='/templates/')
        srv.MaxWebSocketRecvLen = 256
        srv.WebSocketThreaded = False
        srv.Start()
Ejemplo n.º 12
0
 def _getParamsFromGlobalConf(self, defaultDb):
     zenhome = os.environ.get('ZENHOME')
     if not zenhome:
         raise ZenDBError('No $ZENHOME set. In order to use default '
                          'configurations, $ZENHOME must point to the '
                          'Zenoss install.')
     else:
         with open(os.path.join(zenhome, 'etc/global.conf'), 'r') as fp:
             globalConf = ConfigFile(fp)
             settings = {}
             for line in globalConf.parse():
                 if line.setting:
                     key, val = line.setting
                     if key.startswith(defaultDb + '-'):
                         key = key[len(defaultDb) + 1:]
                         settings[key] = val
             return settings
Ejemplo n.º 13
0
 def __init__(self, account):
     self.config_file = ConfigFile(account)
     self.account = account.strip()
     self.password = self.config_file.get("Instagram", "password").strip()
     self.db = dbManager()
     self.hashtags = self.config_file.get("Hashtags")
     self.titles = self.config_file.get("Instagram", "Titles")
     self.follow_amount_min = int(
         self.config_file.get("Instagram", "followamountmin"))
     self.follow_amount_max = int(
         self.config_file.get("Instagram", "followamountmax"))
     self.unfollow_days = int(self.config_file.get("Time", "unfollowdays"))
     self.like_ammount_max = int(
         self.config_file.get("Instagram", "likeamountmax"))
     self.like_ammount_min = int(
         self.config_file.get("Instagram", "likeamountmin"))
     print("Created Instagram Bot!")
Ejemplo n.º 14
0
 def _getParamsFromGlobalConf(self, defaultDb):
     zenhome = os.environ.get('ZENHOME')
     if not zenhome:
         raise ZenDBError('No $ZENHOME set. In order to use default '
                          'configurations, $ZENHOME must point to the '
                          'Zenoss install.')
     else:
         with open(os.path.join(zenhome, 'etc/global.conf'), 'r') as fp:
             globalConf = ConfigFile(fp)
             settings = {}
             for line in globalConf.parse():
                 if line.setting:
                     key, val = line.setting
                     if key.startswith(defaultDb + '-'):
                         key = key[len(defaultDb)+1:]
                         settings[key] = val
             return settings
Ejemplo n.º 15
0
 def test_CF_save(self):
     assert not Path(self.save_path).is_file()
     try:
         self.init_config.path = self.save_path
         self.init_config.save()
         save_config = ConfigFile(self.save_path)
         self.assertEqual(save_config, self.init_config)
     finally:
         if Path(self.save_path).is_file():
             Path(self.save_path).unlink()
Ejemplo n.º 16
0
    def test_CF_schema(self):
        try:
            schema_config = ConfigFile(self.init_path, self.schema_path)
        except jsonschema.ValidationError as e:
            self.fail()

        assert not Path(self.save_path).is_file()
        try:
            schema_config.path = self.save_path
            schema_config.overwrite_off()
            schema_config.save()
            schema_config[self.new_key] = self.new_data
            schema_config.save()
        except jsonschema.ValidationError as e:
            self.fail()
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()
Ejemplo n.º 17
0
    def setUp(self):
        self.root_path = PurePath("test")
        self.init_path = self._prep_path(PurePath("test.json"))
        self.init_config = ConfigFile(self.init_path)
        self.init_config_static = ConfigFile(self.init_path)
        self.empty_config = ConfigFile()

        with open(str(self.init_path)) as f:
            self.init_json = json.load(f)

        self.save_path = self._prep_path(PurePath("save.json"))
        self.schema_path = self._prep_path(PurePath("test.schema.json"))

        self.new_key = "zzz"
        self.new_data = {"foo": "bar", "baz": ["qux"]}
        self.new_json = self.init_json.copy()
        self.new_json[self.new_key] = self.new_data

        self.list_key = "list_config"
        self.dict_key = "dict_config"
        self.list_copy_key = "list_config_copy"
        self.nested_key = "nested_dict_config"
Ejemplo n.º 18
0
 def connect(http_client, http_response):
     params = json.loads(http_client.ReadRequestContent())
     ssid = params.get('ssid')
     if not ssid:
         http_response.WriteResponseJSONOk({
             'Success': False,
             'Message': 'ssid不能为空!'
         })
         return
     sta_if = network.WLAN(network.STA_IF)
     sta_if.active(True)
     sta_if.connect(ssid, params.get('pwd'))  # Connect to an AP
     if wifi_connected(sta_if):
         print('connect success!')
         wifi_config = ConfigFile.get('wifi', {})
         wifi_config['ssid'] = ssid
         wifi_config['pwd'] = params.get('pwd')
         ConfigFile.set('wifi', wifi_config)
         machine.reset()
     http_response.WriteResponseJSONOk({
         'Success': False,
         'Message': '连接失败!'
     })
Ejemplo n.º 19
0
 def test_CF_overwrite_on(self):
     assert not Path(self.save_path).is_file()
     try:
         self.init_config.path = self.save_path
         self.init_config.overwrite_on()
         self.init_config.save()
         self.init_config[self.new_key] = self.new_data
         self.init_config.save()
         save_config = ConfigFile(self.save_path)
         self.assertNotEqual(save_config, self.init_config_static)
         self.assertEqual(save_config, self.init_config)
     finally:
         if Path(self.save_path).is_file():
             Path(self.save_path).unlink()
Ejemplo n.º 20
0
    def check_voicemail_property(self,
                                 context,
                                 mailbox,
                                 msgnum,
                                 property_name,
                                 property_value,
                                 folder=inbox_folder_name):
        """Check if a voicemail has the property specified

        Keyword Arguments:
        context         The context of the mailbox
        mailbox         The mailbox
        msgnum          The 1-based index of the voicemail to check for
        property_name   The name of the property to check
        property_value  The value to check for
        folder          The folder to check under; default to the default inbox
                        name

        Returns:
        True if the voicemail has the property and value specified
        False otherwise
        """
        list_formats = []
        if not self.check_voicemail_exists(context, mailbox, msgnum,
                                           list_formats, folder):
            return False

        msg_name = 'msg%(msgnum)04d' % {"msgnum": msgnum}
        msg_name = msg_name + ".txt"
        msg_path = (self.__ast.base + "%(vd)s/%(c)s/%(m)s/%(f)s/%(n)s" % {
            'vd': self.voicemail_directory,
            'c': context,
            'm': mailbox,
            'f': folder,
            'n': msg_name
        })

        config_file = ConfigFile(msg_path)
        for cat in config_file.categories:
            if cat.name == 'message':
                for kvp in cat.options:
                    if kvp[0] == property_name and kvp[1] == property_value:
                        return True
        return False
Ejemplo n.º 21
0
    def get_sqlite_config(self, ast_instance):
        """Retrieve necessary SQLite3 config parameters from the config file

        Keyword Arguments:
        ast_instance The instance of Asterisk that used the config file

        Returns:
        Tuple of (table, columns)
        """
        sqlite_config_file = (
            "%s/%s/cdr_sqlite3_custom.conf" %
            (ast_instance.base, ast_instance.directories['astetcdir']))

        sqlite_config = ConfigFile(sqlite_config_file)
        for option in sqlite_config.categories[0].options:
            if option[0] == 'table':
                table = option[1]
            elif option[0] == 'columns':
                columns = [col.strip() for col in option[1].split(',')]
        return (table, columns)
Ejemplo n.º 22
0
    def get_user_object(self, context, mailbox, source_file="voicemail.conf"):
        """Gets user information from the voicemail configuration file

        Keyword Arguments:
        context     The context of the mailbox
        mailbox     The mailbox
        source_file The file containing the user information to pull from.
                    Defaults to voicemail.conf

        Returns:
        A VoiceMailMailboxManagement.UserObject object, populated with the
        user's values, or an empty object
        """

        file_path = (self.__ast.base + self.__ast.directories['astetcdir'] +
                     "/" + source_file)

        config_file = ConfigFile(file_path)
        user_object = VoiceMailMailboxManagement.UserObject()
        for cat in config_file.categories:
            if cat.name == context:
                for kvp in cat.options:
                    if kvp[0] == mailbox:
                        tokens = kvp[1].split(',')
                        i = 0
                        for token in tokens:
                            if i == 0:
                                user_object.password = token
                            elif i == 1:
                                user_object.fullname = token
                            elif i == 2:
                                user_object.emailaddress = token
                            elif i == 3:
                                user_object.pageraddress = token
                            i += 1
                        return user_object
        return user_object
Ejemplo n.º 23
0
class InstagramBot:
    def __init__(self, account):
        self.config_file = ConfigFile(account)
        self.account = account.strip()
        self.password = self.config_file.get("Instagram", "password").strip()
        self.db = dbManager()
        self.hashtags = self.config_file.get("Hashtags")
        self.titles = self.config_file.get("Instagram", "Titles")
        self.follow_amount_min = int(
            self.config_file.get("Instagram", "followamountmin"))
        self.follow_amount_max = int(
            self.config_file.get("Instagram", "followamountmax"))
        self.unfollow_days = int(self.config_file.get("Time", "unfollowdays"))
        self.like_ammount_max = int(
            self.config_file.get("Instagram", "likeamountmax"))
        self.like_ammount_min = int(
            self.config_file.get("Instagram", "likeamountmin"))
        print("Created Instagram Bot!")

    def upload(self, pic_info):
        try:
            api = InstagramAPI(self.account, self.password)
            if (api.login()):
                startCount = len(api.getTotalSelfUserFeed(self.account))
                photo_path = pic_info['Path']
                caption = self.genCaption(pic_info["Title"],
                                          pic_info["Subreddit"])
                if caption is None:
                    caption = self.titles[r.randint(
                        0,
                        len(self.titles) - 1
                    )] + "#lol #lmao #funny #rofl #meme #error #404 #human #notabot"

                if pic_info['FileFormat'] not in ['jpg', 'png']:
                    clip = VideoFileClip(photo_path)
                    clip.save_frame("images/thumbnail.jpg")
                    api.uploadVideo(photo_path,
                                    "images/thumbnail.jpg",
                                    caption=caption)
                    statusCode = api.LastResponse.status_code
                    if statusCode == 500:
                        print("Retrying to upload video in 10s")
                        time.sleep(10)
                        api.uploadVideo(photo_path,
                                        "images/thumbnail.jpg",
                                        caption=caption)
                else:
                    api.uploadPhoto(photo=photo_path, caption=caption)

                if len(api.getTotalSelfUserFeed(
                        self.account)) - startCount >= 1:
                    iID = self.db.insert("Instagram", [caption, self.account])
                    self.db.insert("Posted", [pic_info["ID"], iID])
                    print("Uploaded Post!")
                    return True
                else:
                    print("Didn't upload post :(")
                    self.db.update("Reddit", {"Path": "null"},
                                   "Reddit.ID == {0}".format(pic_info["ID"]))
                    return False
            else:
                print("Can't login!")
                return False
        except Exception as e:
            print("ERROR WHILE UPLOADING: ", e)
            self.db.update("Reddit", {"Path": "null"},
                           "Reddit.ID == {0}".format(pic_info["ID"]))
            return False

    def genCaption(self, title, subreddit):
        try:
            tagList = list(self.hashtags[subreddit])
            capList = []
            for i in range(
                    0, min(r.randint(7, 12), len(self.hashtags[subreddit]))):
                tag = tagList[r.randint(0, len(tagList) - 1)]
                capList.append(tag)
                tagList.remove(tag)

            pronouns = [
                "i", "me", "my", "our", "ours", "myself", "ourself", "we",
                "us", "i'm", "reddit", "sub", "karma", "upvote"
            ]
            wordList = title.lower().split()
            ran = r.randint(0, 9)

            if any(word in pronouns for word in wordList):
                return self.titles[r.randint(
                    0,
                    len(self.titles) - 1)] + " #" + " #".join(capList)
            if ran in range(0, 9):
                return title + " #" + " #".join(capList)
            else:
                return self.titles[r.randint(
                    0,
                    len(self.titles) - 1)] + " #" + " #".join(capList)
        except Exception as e:
            print("ERROR DURING genCaption: ", e)

    def getNumberOfPosts(self):
        try:
            api = InstagramAPI(self.account, self.password)
            if (api.login()):
                return len(api.getTotalSelfUserFeed())
            else:
                return 0
                print("ERROR COULDN'T DETERMINE NUMBER OF POSTS")
        except Exception as e:
            return 0
            print("ERROR DURING getNumberOfPosts: ", e)

    def getUsernameID(self, username, api):
        try:
            api.searchUsername(
                username)  # the InstagramUserID you want to know the followers
            usernameJson = api.LastJson
            return re.search(
                "(?<=pk\'\: )[0-9]*",
                str(usernameJson)).group()  # finds the pk numeral cod
        except Exception as e:
            print("ERROR DURING getUsernameID: ", e)

    def getFollowers(self, api, username=None):
        try:
            if username is None:
                username = self.account
            pkString = self.getUsernameID(username, api)
            api.getUserFollowers(int(pkString))
            followers = api.LastJson
            #followersNamesList = re.findall("(?<=username\'\: \').*?(?=\',)", str(followers)) # finds the UserID of the followers and creates a list
            #followersPksList = re.findall("(?<=pk\'\: )[0-9]*", str(followers)) # finds the UserID of the followers and creates a list
            return followers['users']
        except Exception as e:
            print("ERROR DURING getFollowers: ", e)

    def getHashtagFeed(self, hashtag):
        try:
            api = InstagramAPI(self.account, self.password)
            if (api.login()):
                api.getHashtagFeed(hashtag)
                tagJson = api.LastJson
                mediaIdList = re.findall("(?<=\'id\'\: \')[0-9]*_[0-9]*(?=\')",
                                         str(tagJson))
                return mediaIdList
        except Exception as e:
            print("ERROR DURING getHashtagFeed: ", e)

    def getMediaLikers(self, mediaId, api):
        try:
            api.getMediaLikers(mediaId)
            likerJson = api.LastJson
            return re.findall("(?<=pk\'\: )[0-9]*", str(likerJson))
        except Exception as e:
            print("ERROR DURING getMediaLikers: ", e)

    # Will choose a random hashtag for list of hashtags
    ## Get the feed of that hashtag and pick a picture
    ## Like the picture, then follow random people who have also
    ## liked the picture
    async def followRandom(self):
        try:
            api = InstagramAPI(self.account, self.password)
            if (api.login()):
                amountToFollow = r.randint(self.follow_amount_min,
                                           self.follow_amount_max)
                hashValues = list(self.hashtags.values())
                hashList = r.choice(hashValues)
                hashtag = r.choice(hashList)
                mediaList = self.getHashtagFeed(hashtag)
                amountFollowed = 0
                while mediaList and amountFollowed < amountToFollow:
                    mediaId = mediaList.pop(r.randint(0, len(mediaList) - 1))
                    api.like(mediaId)
                    self.db.insert("Like", [self.account, mediaId])
                    likersList = self.getMediaLikers(mediaId, api)
                    if not likersList:
                        continue
                    r.shuffle(likersList)
                    for i in range(
                            0,
                            min(amountToFollow - amountFollowed,
                                len(likersList))):
                        print("trying to follow")
                        try:
                            user_pk = likersList[i]
                            followers = self.db.c.execute(
                                "SELECT PK from FOLLOWING").fetchall()
                            if user_pk not in followers:
                                api.follow(user_pk)
                                self.db.insert("Following",
                                               [self.account, user_pk])
                        except Exception as e:
                            print(
                                "ERRROR DURING followRandom while trying to follow"
                            )
                        finally:
                            await asyncio.sleep(r.randint(5, 120))
                            amountFollowed += 1
        except Exception as e:
            print("ERROR DURING followRandom: ", e)

    async def unfollow(self):
        try:
            api = InstagramAPI(self.account, self.password)
            if api.login():
                amountToUnfollow = r.randint(self.follow_amount_min,
                                             self.follow_amount_max)
                expiredSelection = self.db.c.execute(
                    "SELECT PK From Following WHERE DateFollowed IS NOT NULL AND julianday('now') - julianday(DateFollowed) >= "
                    + str(self.unfollow_days))
                expiredList = expiredSelection.fetchall()
                amountUnfollowed = 0
                for pk in expiredList:
                    print("trying to unfollow")
                    api.unfollow(pk[0])
                    self.db.update("Following", {"DateFollowed": "null"},
                                   "PK = {0}".format(pk[0]))
                    await asyncio.sleep(r.randint(5, 120))
                    amountUnfollowed += 1
                    if amountUnfollowed >= amountToUnfollow:
                        return
        except Exception as e:
            print("ERROR DURING unfollow: ", e)

    async def likeTimelineRandom(self):
        try:
            api = InstagramAPI(self.account, self.password)
            if api.login():
                if not api.getTimeline():
                    raise Exception("Couldn't get timeline!")
                amountToLike = r.randint(self.like_ammount_min,
                                         self.like_ammount_max)
                amountLiked = 0
                mediaList = re.findall("(?<=\'id\'\: \')[0-9]*_[0-9]*(?=\')",
                                       str(api.LastJson))
                liked = self.db.c.execute(
                    "SELECT MediaID from Like").fetchall()
                while mediaList and amountLiked < amountToLike:
                    mediaId = mediaList.pop(r.randint(0, len(mediaList) - 1))
                    if mediaId not in liked:
                        print("trying to like")  #Still need to do check
                        api.like(mediaId)
                        amountLiked += 1
                        self.db.insert("Like", [self.account, mediaId])
                        await asyncio.sleep(r.randint(
                            3, 40))  #You should have a like wait time min/max
        except Exception as e:
            print("ERROR DURING likeRandom: ", e)

    def directMessage(self):
        try:
            api = InstagramAPI(self.account, self.password)
            if api.login():
                followersInfo = self.getFollowers(api)
                followingPks = self.db.c.execute(
                    "SELECT PK FROM Following").fetchall()
                followingPks = [pk[0] for pk in followingPks]
                print(type(followingPks[0]))
                #Get all pks that are in followingPks but not in followersPks
                ## i.e., Get all people you are following but are not following you
                nonFollowersPks = [
                    pk for pk in followingPks if pk not in followersInfo
                ]
        #     api.direct_message("hey whats up", 'ravioliraviolirobot')
        except Exception as e:
            print("ERROR DURING directMessage", e)
Ejemplo n.º 24
0
    def __init__(self, base=None, ast_conf_options=None, host="127.0.0.1",
                 remote_config=None):
        """Construct an Asterisk instance.

        Keyword Arguments:
        base -- This is the root of the files associated with this instance of
                Asterisk. By default, the base is "/tmp/asterisk-testsuite"
                directory. Given a base, it will be appended to the default base
                directory.
        ast_conf_options -- Configuration overrides for asterisk.conf.
        host -- The IP address the Asterisk instance runs on
        remote_config -- Configuration section that defines this as a remote
                         Asterisk instance. If provided, base and
                         ast_conf_options are generally ignored, and the
                         Asterisk instance's configuration is treated as
                         immutable on some remote machine defined by 'host'

        Example Usage:
        self.asterisk = Asterisk(base="manager/login")
        """
        self._start_deferred = None
        self._stop_deferred = None
        self._stop_cancel_tokens = []
        self.directories = {}
        self.protocol = None
        self.process = None
        self.astetcdir = ""
        self.original_astmoddir = ""
        self.ast_version = None
        self.remote_config = remote_config

        # If the process is remote, don't bother
        if not self.remote_config:
            self.ast_version = AsteriskVersion()

        valgrind_env = os.getenv("VALGRIND_ENABLE") or ""
        self.valgrind_enabled = True if "true" in valgrind_env else False

        if base is not None:
            self.base = base
        else:
            self.base = Asterisk.test_suite_root
        if self.localtest_root:
            self.ast_binary = self.localtest_root + "/usr/sbin/asterisk"
        else:
            ast_binary = test_suite_utils.which("asterisk")
            self.ast_binary = ast_binary or "/usr/sbin/asterisk"
        self.host = host

        self._ast_conf_options = ast_conf_options

        if self.remote_config:
            # Pretend as if we made the structure
            self._directory_structure_made = True
            self._configs_installed = True
            self._configs_set_up = True

            # And assume we've got /etc/asterisk/ where we expect it to be
            self.astetcdir = "/etc/asterisk"
        else:
            self._directory_structure_made = False
            self._configs_installed = False
            self._configs_set_up = False

            # Find the system installed asterisk.conf
            ast_confs = [
                os.path.join(self.default_etc_directory, "asterisk.conf"),
                "/usr/local/etc/asterisk/asterisk.conf",
            ]
            self._ast_conf = None
            for config in ast_confs:
                if os.path.exists(config):
                    self._ast_conf = ConfigFile(config)
                    break
            if self._ast_conf is None:
                msg = "Unable to locate asterisk.conf in any known location"
                LOGGER.error(msg)
                raise Exception(msg)

            # Set which astxxx this instance will be
            i = 1
            while True:
                if not os.path.isdir("%s/ast%d" % (self.base, i)):
                    self.base = "%s/ast%d" % (self.base, i)
                    break
                i += 1

            # Get the Asterisk directories from the Asterisk config file
            for cat in self._ast_conf.categories:
                if cat.name == "directories":
                    for (var, val) in cat.options:
                        self.directories[var] = val

            # self.original_astmoddir is for dependency checking only
            if "astmoddir" in self.directories:
                if self.localtest_root:
                    self.original_astmoddir = "%s%s" % (
                        self.localtest_root, self.directories["astmoddir"])
                else:
                    self.original_astmoddir = self.directories["astmoddir"]
        source_rotated,
        text=[
            f"<b>Vertex:</b> {n}" for n in range(len(source_rotated.vertices))
        ],
        name="Source",
        **mesh_kwargs,
    ),
                  row=1,
                  col=1)
    fig.add_trace(BrowserVisualizer.make_mesh(
        target_rotated,
        text=[
            f"<b>Vertex:</b> {n}" for n in range(len(target_rotated.vertices))
        ],
        name="Target",
        **mesh_kwargs,
    ),
                  row=1,
                  col=2)

    return fig


if __name__ == "__main__":
    cfg = ConfigFile.load(ConfigFile.Paths.highpoly.horse_camel)
    source = Mesh.load(cfg.source.reference)
    target = Mesh.load(cfg.target.reference)
    markers = cfg.markers

    plot(source, target).show(renderer="browser")
            ]) for p in results
        ])
    camera = dict(up=dict(x=0, y=1, z=0))
    scene = dict(aspectmode='data',
                 xaxis_title='X',
                 yaxis_title='Z',
                 zaxis_title='Y',
                 camera=camera,
                 dragmode='turntable')
    fig.update_layout(
        scene=scene,
        scene2=scene,
        yaxis=dict(scaleanchor="x", scaleratio=1),
        yaxis2=dict(scaleanchor="x", scaleratio=1),
        margin=dict(l=0, r=0),
        # scene_camera=camera
    )
    return fig


def animate(transf: Transformation, poses: Sequence[meshlib.Mesh]):
    fig = make_animation(transf, poses)
    fig.show(renderer="browser")


if __name__ == "__main__":
    # cfg = ConfigFile.load(ConfigFile.Paths.highpoly.horse_camel)
    cfg = ConfigFile.load("models/lowpoly/markers-cat-voxel.yml")
    # cfg = ConfigFile.load(ConfigFile.Paths.highpoly.cat_lion)
    animate_cfg(cfg)
Ejemplo n.º 27
0
    return
    if '_id' in obj.__dict__:
        orig_obj = (method)(collection, obj._id)
        logger.info("Saving %s.\nCurrent:\n%s\nNew:\n%s\n" %
                    (str(obj.__class__), pprint.pformat(
                        orig_obj.__dict__), pprint.pformat(obj.__dict__)))
    else:
        logger.info("Saving %s.\nFirst time:\n%s" %
                    (str(obj.__class__), pprint.pformat(obj.__dict__)))
    print "Saved by:"
    for n in traceback.format_list(traceback.extract_stack()[12:]):
        print "%s\n" % n


if __name__ == "__main__":
    l = local_logger('my_app', './log_test.log', logging.DEBUG)

    # "application" code
    l.debug("debug message")
    l.info("info message")
    l.warn("warn message")
    l.error("error message")
    l.critical("critical message")

    from config import ConfigFile
    config = ConfigFile()
    l.setSMTP(config.smtp.server, config.smtp.port, config.smtp.login,
              config.smtp.password, config.smtp.tls, config.smtp.debug_level,
              config.smtp.from_addr, config.smtp.to_addrs)
    l.critical("critical message")
Ejemplo n.º 28
0
 def __init__(self, filename):
     ConfigFile.__init__(self, filename)
Ejemplo n.º 29
0
class RedditBot:
    def __init__(self, iusername):
        self.config_file = ConfigFile(iusername)
        self.reddit = praw.Reddit(
            client_id=self.config_file.get("Reddit", "client_id"),
            client_secret=self.config_file.get("Reddit", "client_secret"),
            password=self.config_file.get("Reddit", "password"),
            user_agent=self.config_file.get("Reddit", "user_agent"),
            username=self.config_file.get("Reddit", "username"))
        self.redditURL = 'www.reddit.com/r/'
        self.db = dbManager()
        self.osMan = osManager()
        self.videoDurationMax = int(
            self.config_file.get("Time", "videodurationmax"))

    def getImage(self, url, title):
        try:
            imgExtensions = [".jpg", ".png"]
            vidExtensions = [".mp4", ".gif", ".gifv"]
            doc = requests.get(url, stream=True)
            filename, file_extension = os.path.splitext(url)

            #Reddit gifs can be downloaded directly
            ##So no need for further parsing
            if re.search("redd[.]?it/.*.gif[v]?", url):
                return doc, file_extension

            x = re.compile("(\s?https?:)?(/{2})?")
            soup = BeautifulSoup(doc.text, 'html.parser')
            if re.search("v.redd[.]?it/.*", url):
                print(re.search('fallback_url', soup.prettify()))
                return doc, file_extension, True

            #If the image is a Imgur gif
            if file_extension.__contains__("gif"):
                #Imgur and reddit structure gifs differently in HTML, so
                ## The source is for Imgur
                for video in (soup.find_all('source')):
                    if video.has_attr('src'):
                        src = video['src']
                        filename, file_extension = os.path.splitext(src)
                        if file_extension not in vidExtensions:
                            return None, None
                        prefix = x.match(src)
                        if not prefix:
                            return requests.get("http://" + src,
                                                stream=True), file_extension
                        elif prefix.group(0) == "//":
                            return requests.get("http:" + src,
                                                stream=True), file_extension
                        else:
                            return requests.get(src,
                                                stream=True), file_extension
                return None, None  #This happens if it wasn't able to find the gif

            # If this url is already an image
            if file_extension in imgExtensions:
                return doc, file_extension

            # If not, then will need to extract the image from the URL
            for img in soup.find_all('img'):
                if img.has_attr('alt') and img[
                        'alt'] == title:  #There may nor be an alt
                    src = img['src']
                    filename, file_extension = os.path.splitext(src)
                    prefix = x.match(src)
                    if not prefix:
                        return requests.get("http://" + src,
                                            stream=True), file_extension
                    elif prefix.group(0) == "//":
                        return requests.get("http:" + src,
                                            stream=True), file_extension
                    else:
                        return requests.get(src, stream=True), file_extension

            return None, None
        except Exception as e:
            print("ERROR WHILE PARSING:  ", e)
            return None, None

    def download(self, submission, subreddit):
        pass  #make this method call downloadVideo and downloadImage

    def downloadVideo(self, isGif, filepath, submission, subreddit):
        try:
            duration = submission.media['reddit_video']["duration"]
            if duration > self.videoDurationMax:
                return

            doc = requests.get(
                submission.media['reddit_video']['fallback_url'], stream=True)
            if not doc:
                raise Exception("COULDN'T RETRIEVE THE VIDEO")

            if not isGif:
                audioLocation = os.path.join(filepath,
                                             submission.title + "_audio")
                videoLocation = os.path.join(filepath,
                                             submission.title + "_video")
                audioPacket = requests.get(submission.url + "/audio",
                                           stream=True)
                if not audioPacket:
                    raise Exception("COULDN'T RETRIEVE THE AUDIO")

                self.writeFile(audioLocation, audioPacket)
                self.writeFile(videoLocation, doc)
                #audio = mp.AudioFileClip(audioLocation)
                video = mp.VideoFileClip(videoLocation)
                #video_with_audio = video.set_audio(audio)
                video_new_loc = videoLocation + ".mp4"
                video.write_videofile(video_new_loc, audio=audioLocation)
                self.db.insert("Reddit", [
                    submission.title, submission.url, video_new_loc, subreddit,
                    "mp4"
                ])
                self.osMan.deleteFile(audioLocation)
                self.osMan.deleteFile(videoLocation)
            else:
                imgLocation = os.path.join(filepath, submission.title)
                isWritten = self.writeFile(imgLocation, doc)
                if isWritten:
                    self.db.insert("Reddit", [
                        submission.title, submission.url, imgLocation,
                        subreddit, "gif"
                    ])

        except Exception as e:
            print("ERROR WHILE downloadVideo: ", e)
            return

    def downloadImage(self, submission,
                      subreddit):  #Edit so it may only take in image
        try:
            url = submission.url  #"https://v.redd.it/qahcxxgdvha11"
            title = submission.title  #https://v.redd.it/xglnc6dzlia11/HLSPlaylist.m3u8
            videoData = submission.media
            duplicateAmount = len(
                self.db.c.execute(
                    "SELECT URL FROM Reddit WHERE URL == '{0}'".format(
                        str(url))).fetchall())

            if duplicateAmount >= 1:
                return False

            filepath = self.osMan.createDir(
                subreddit
            )  # Create the directory in which to store the (change this later so it wont be called in this function
            if isinstance(videoData, dict) and 'reddit_video' in videoData:
                self.downloadVideo(videoData['reddit_video']['is_gif'],
                                   filepath, submission, subreddit)
                return
            else:
                img, file_extension = self.getImage(url, title)

            if img == None or file_extension == None:
                return False

            imgLocation = os.path.join(filepath, title + file_extension)

            isWritten = self.writeFile(imgLocation, img)
            if isWritten:
                print(title + " URL: " + url)
                self.db.insert("Reddit", [
                    title, url, imgLocation, subreddit,
                    file_extension.split('.')[1]
                ])

        except Exception as e:
            print("ERROR WHILE DOWNLOADING: ", e)
            return False

    def writeFile(self, toFile, content):
        try:
            with open(toFile, 'wb') as f:
                for chunk in content.iter_content(
                        chunk_size=1024
                ):  #iter_content allows you to write the image by chunks
                    if chunk:
                        f.write(chunk)
            return True
        except Exception as e:
            print("ERROR WHILE writeFile: ", e)
            return False

    def scrapeImages(self, subreddit_id):
        try:
            print("DOWNLOADING IMAGES...")
            for submission in self.reddit.subreddit(subreddit_id).hot(
                    limit=25):
                result = self.downloadImage(submission, subreddit_id)
        except Exception as e:
            print("ERROR WHILE REQUESTING: ", e)
Ejemplo n.º 30
0
#
# Author: Soufian Salim <*****@*****.**>
#
# URL: <http://github.com/bolaft/diannotator>
"""
Grapical User Interface
"""

from tkinter import Tk, StringVar, Text, Menu, messagebox, BOTH, DISABLED, END, LEFT, BOTTOM, NORMAL, N, X, WORD, SUNKEN, INSERT, SEL, NSEW
from tkinter.ttk import Button, Entry, Frame, Label, Scrollbar
from ttkthemes import ThemedStyle

from config import ConfigFile
from strings import Strings

config = ConfigFile()  # INI configuration file


class GraphicalUserInterface(Frame):
    """
    Graphical User Interface class
    """
    # constant tag names
    STRONG = "STRONG"
    ITALIC = "ITALIC"
    HIGHLIGHT = "HIGHLIGHT"

    window_title = "DiAnnotator"  # window title

    padding = config.get_int("padding", 25)  # padding for text area")
    wrap_length = config.get_int(
Ejemplo n.º 31
0
def main():
    parser = argparse.ArgumentParser(
        description="manage and report time allocation",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=textwrap.dedent("""
			DURATIONs
			  Spans of time can be provided in a concise format, a series of integers or
			  floats each appended with a unit: d, h, m. Whitespace between each component
			  is optional. Fractions less than 1 require a leading 0.

			  Note that a day is 8 hours.

			  Examples:
				15m
				0.5h
				1.5h
				1d 4.5h
				1d 4h 30m
				1d4h30m

			  Note that, while whitespace is optional, if you do specify a duration on the
			  command line and it includes whitespace, you"ll have to quote it.

			DATEs
			  Dates should be provided in the form YYYY-MM-DD.

			TIMEs
			  Times should be provided in the form HH:MM. All times used, including "now",
			  have their seconds zeroed out. All times provided on the command line are
			  assumed to occur today.

			Config File:
			  ~/.worklog/config.json - Can be created to store username and password to avoid
			  being prompted to type in your credentials for Jira authentication.

			  Example File:
				{ "username" : "jsmith" }

			  WARNING:
				Uploading multiple times in one calendar day will cause inconsistencies with time tracking
				on the server side.
		"""),
    )
    sub_parser = parser.add_subparsers(dest='command')

    common_parser = argparse.ArgumentParser(add_help=False)
    common_parser.add_argument(
        '--day', '-d', help='manage the worklog for DATE, defaults to today')

    blurb = 'start a new task, closing the currently open task if any'
    start_parser = sub_parser.add_parser('start',
                                         help=blurb,
                                         description=blurb,
                                         parents=[common_parser])
    start_parser.add_argument(
        '--ago',
        metavar='DURATION',
        help='start the task DURATION time ago, instead of now')
    start_parser.add_argument('--at',
                              metavar='TIME',
                              help='start the task at TIME, instead of now')
    start_parser.add_argument('-t',
                              '--ticket',
                              metavar='TICKET',
                              help='the TICKET associated with the task')
    start_parser.add_argument(
        'description',
        metavar='DESCRIPTION',
        nargs=argparse.REMAINDER,
        help="specify the task's description on the command line")

    blurb = 'like start, but reuse the description from a previous task in this worklog by seleting it from a list'
    resume_parser = sub_parser.add_parser('resume',
                                          help=blurb,
                                          description=blurb,
                                          parents=[common_parser])
    resume_parser.add_argument(
        '--ago',
        metavar='DURATION',
        help='start the task DURATION time ago, instead of now')
    resume_parser.add_argument('--at',
                               metavar='TIME',
                               help='start the task at TIME, instead of now')

    blurb = 'close the currently open task'
    stop_parser = sub_parser.add_parser('stop',
                                        help=blurb,
                                        description=blurb,
                                        parents=[common_parser])
    stop_parser.add_argument(
        '--ago',
        metavar='DURATION',
        help='close the open task DURATION time ago, instead of now')
    stop_parser.add_argument(
        '--at',
        metavar='TIME',
        help='close the open task at TIME, instead of now')

    blurb = 'report the current state of the worklog'
    report_parser = sub_parser.add_parser('report',
                                          help=blurb,
                                          description=blurb,
                                          parents=[common_parser])

    blurb = 'uploads worklog time to jira'
    upload_parser = sub_parser.add_parser('upload',
                                          help=blurb,
                                          description=blurb,
                                          parents=[common_parser])

    args = parser.parse_args()
    config_path = os.path.expanduser('~/.worklog/config.json')
    config = ConfigFile(config_path)
    color.ENABLED = config.features.colorize
    try:
        handler = globals()["on_{}".format(args.command)]
    except KeyError:
        parser.print_help()
    else:
        if isinstance(handler, Callable):
            handler(args, config)
        else:
            parser.error("unrecognized command: '{}'".format(args.command))
Ejemplo n.º 32
0
class TestConfigFile(unittest.TestCase):
    def setUp(self):
        self.root_path = PurePath("test")
        self.init_path = self._prep_path(PurePath("test.json"))
        self.init_config = ConfigFile(self.init_path)
        self.init_config_static = ConfigFile(self.init_path)
        self.empty_config = ConfigFile()

        with open(str(self.init_path)) as f:
            self.init_json = json.load(f)

        self.save_path = self._prep_path(PurePath("save.json"))
        self.schema_path = self._prep_path(PurePath("test.schema.json"))

        self.new_key = "zzz"
        self.new_data = {"foo": "bar", "baz": ["qux"]}
        self.new_json = self.init_json.copy()
        self.new_json[self.new_key] = self.new_data

        self.list_key = "list_config"
        self.dict_key = "dict_config"
        self.list_copy_key = "list_config_copy"
        self.nested_key = "nested_dict_config"

    def _prep_path(self, path):
        t = type(path)
        return t(self.root_path) / path

    def test_CF_clear(self):
        self.init_config.clear()
        self.assertEqual(self.init_config, self.empty_config)

    def test_CF_contains(self):
        self.assertTrue(self.list_key in self.init_config)

    def test_CF_delitem(self):
        del self.init_config[self.list_key]
        self.assertFalse(self.list_key in self.init_config)

    def test_CF_eq(self):
        config_copy = ConfigFile(self.init_path)
        self.assertEqual(config_copy, self.init_config)

    def test_CF_iter(self):
        for i in self.init_config:
            self.assertTrue(True)

    def test_CF_get_path(self):
        path = self.init_config.path
        self.assertEqual(path, self.init_path)

    def test_CF_len(self):
        self.assertEqual(len(self.init_config), len(self.init_json))

    def test_CF_ne(self):
        self.assertNotEqual(self.init_config, self.empty_config)
        self.assertNotEqual(self.init_config, self.init_config[self.list_key])
        self.assertNotEqual(self.init_config, self.init_config[self.dict_key])
        self.assertNotEqual(self.init_config, {})
        self.assertNotEqual(self.init_config, [])
        self.assertNotEqual(self.init_config, ())
        self.assertNotEqual(self.init_config, "")
        self.assertNotEqual(self.init_config, 0.0)
        self.assertNotEqual(self.init_config, 0)
        self.assertNotEqual(self.init_config, None)

    def test_CF_overwrite_off(self):
        assert not Path(self.save_path).is_file()
        try:
            self.init_config.path = self.save_path
            self.init_config.overwrite_off()
            self.init_config.save()
            self.init_config[self.new_key] = self.new_data
            self.init_config.save()
            save_config = ConfigFile(self.save_path)
            self.assertEqual(save_config, self.init_config_static)
            self.assertNotEqual(save_config, self.init_config)
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()

    def test_CF_overwrite_on(self):
        assert not Path(self.save_path).is_file()
        try:
            self.init_config.path = self.save_path
            self.init_config.overwrite_on()
            self.init_config.save()
            self.init_config[self.new_key] = self.new_data
            self.init_config.save()
            save_config = ConfigFile(self.save_path)
            self.assertNotEqual(save_config, self.init_config_static)
            self.assertEqual(save_config, self.init_config)
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()

    def test_CF_overwrite_get_set(self):
        assert not Path(self.save_path).is_file()
        try:
            self.assertFalse(self.init_config.overwrite)
            self.init_config.path = self.save_path
            self.init_config.overwrite = True
            self.assertTrue(self.init_config.overwrite)
            self.init_config.save()
            self.init_config[self.new_key] = self.new_data
            self.init_config.save()
            save_config = ConfigFile(self.save_path)
            self.assertNotEqual(save_config, self.init_config_static)
            self.assertEqual(save_config, self.init_config)
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()

    def test_CF_repr(self):
        self.assertEqual(self.init_config.__repr__(),
                         self.init_json.__repr__())

    def test_CF_save(self):
        assert not Path(self.save_path).is_file()
        try:
            self.init_config.path = self.save_path
            self.init_config.save()
            save_config = ConfigFile(self.save_path)
            self.assertEqual(save_config, self.init_config)
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()

    def test_CF_on_change_callback(self):
        key = "done"
        check = {key: False}

        def callback(x):
            check[key] = True

        self.init_config.on_change_callbacks = {"test": callback}
        self.init_config[self.new_key] = self.new_data
        self.assertTrue(check[key])

    def test_CF_schema(self):
        try:
            schema_config = ConfigFile(self.init_path, self.schema_path)
        except jsonschema.ValidationError as e:
            self.fail()

        assert not Path(self.save_path).is_file()
        try:
            schema_config.path = self.save_path
            schema_config.overwrite_off()
            schema_config.save()
            schema_config[self.new_key] = self.new_data
            schema_config.save()
        except jsonschema.ValidationError as e:
            self.fail()
        finally:
            if Path(self.save_path).is_file():
                Path(self.save_path).unlink()

    def test_CF_setitem(self):
        self.init_config[self.new_key] = self.new_data
        self.assertEqual(self.init_config.to_json(), self.new_json)

    def test_CF_set_path(self):
        self.init_config.path = self.save_path
        path = self.init_config.path
        self.assertEqual(path, self.save_path)

    def test_CF_to_json(self):
        config = self.init_config.to_json()
        self.assertEqual(config, self.init_json)

    def test_CL_append(self):
        self.init_config[self.list_key].append(self.new_data)
        check = self.init_config[self.list_copy_key].to_json()
        check.append(self.new_data)
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CL_clear(self):
        self.init_config[self.list_key].clear()
        self.assertEqual(self.init_config[self.list_key].to_json(), [])

    def test_CF_contains(self):
        self.assertTrue(self.init_config[self.list_key][0] in self.init_config[
            self.list_key])
        not_exists = max(self.init_config[self.list_key]) + 1
        self.assertFalse(not_exists in self.init_config[self.list_key])

    def test_CF_getattr(self):
        check = self.init_config[self.dict_key]
        value = getattr(self.init_config, self.dict_key)
        self.assertEqual(value, check)

    def test_CF_setattr(self):
        setattr(self.init_config, self.new_key, self.new_data)
        value = self.init_config[self.new_key]
        self.assertEqual(value, self.new_data)

    def test_CL_count(self):
        exists = self.init_config[self.list_key][0]
        self.assertEqual(self.init_config[self.list_key].count(exists), 1)
        not_exists = max(self.init_config[self.list_key]) + 1
        self.assertEqual(self.init_config[self.list_key].count(not_exists), 0)

    def test_CL_delitem(self):
        item = self.init_config[self.list_key][0]
        del self.init_config[self.list_key][0]
        self.assertFalse(item in self.init_config[self.list_key])

    def test_CL_eq(self):
        self.assertEqual(self.init_config[self.list_key],
                         self.init_config[self.list_copy_key])
        self.assertEqual(self.init_config[self.list_key],
                         self.init_json[self.list_key])

    def test_CL_extend(self):
        self.init_config[self.list_key].extend(self.new_data)
        check = self.init_config[self.list_copy_key].to_json()
        check.extend(self.new_data)
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CL_index(self):
        first = self.init_config[self.list_key][0]
        self.assertEqual(self.init_config[self.list_key].index(first), 0)
        self.assertEqual(self.init_config[self.list_key].index(first, 0, -1),
                         0)
        with self.assertRaises(ValueError):
            self.init_config[self.list_key].index(first, 1, -1)
        not_exists = max(self.init_config[self.list_key]) + 1
        with self.assertRaises(ValueError):
            self.init_config[self.list_key].index(not_exists)

    def test_CL_insert(self):
        self.init_config[self.list_key].insert(0, self.new_data)
        check = self.init_config[self.list_copy_key].to_json()
        check.insert(0, self.new_data)
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CL_len(self):
        self.assertEqual(len(self.init_config), len(self.init_json))

    def test_CL_ne(self):
        self.assertNotEqual(
            self.init_config[self.list_key],
            self.init_config[self.nested_key][self.list_key],
        )
        self.assertNotEqual(
            self.init_config[self.list_key],
            self.init_json[self.nested_key][self.list_key],
        )
        self.assertNotEqual(self.init_config[self.list_key], self.init_config)
        self.assertNotEqual(self.init_config[self.list_key], {})
        self.assertNotEqual(self.init_config[self.list_key], [])
        self.assertNotEqual(self.init_config[self.list_key], ())
        self.assertNotEqual(self.init_config[self.list_key], "")
        self.assertNotEqual(self.init_config[self.list_key], 0.0)
        self.assertNotEqual(self.init_config[self.list_key], 0)
        self.assertNotEqual(self.init_config[self.list_key], None)

    def test_CL_pop(self):
        original = self.init_config[self.list_key].to_json()
        popped = self.init_config[self.list_key].pop()
        self.assertEqual(popped, original[-1])
        original = self.init_config[self.list_copy_key].to_json()
        popped = self.init_config[self.list_copy_key].pop(0)
        self.assertEqual(popped, original[0])

    def test_CL_remove(self):
        first = self.init_config[self.list_key][0]
        self.init_config[self.list_key].remove(first)
        check = self.init_config[self.list_copy_key].to_json()
        check.remove(first)
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CL_reverse(self):
        self.init_config[self.list_key].reverse()
        check = self.init_config[self.list_copy_key].to_json()
        check.reverse()
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CL_setitem(self):
        self.init_config[self.list_key][0], self.init_config[
            self.list_key][1] = (
                self.init_config[self.list_key][1],
                self.init_config[self.list_key][0],
            )
        self.init_config[self.list_key].sort()
        self.assertEqual(self.init_config[self.list_key],
                         self.init_config[self.list_copy_key])

    def test_CL_sort(self):
        self.init_config[self.list_key].sort(key=lambda x: x, reverse=True)
        check = self.init_config[self.list_copy_key].to_json()
        check.sort(key=lambda x: x, reverse=True)
        self.assertEqual(self.init_config[self.list_key], check)

    def test_CD_clear(self):
        self.init_config[self.dict_key].clear()
        self.assertEqual(self.init_config[self.dict_key].to_json(), {})

    def test_CD_contains(self):
        exists = list(self.init_config[self.dict_key].keys())[0]
        self.assertTrue(exists in self.init_config[self.dict_key])
        not_exists = ""
        self.assertFalse(not_exists in self.init_config[self.dict_key])

    def test_CD_delitem(self):
        key = list(self.init_config[self.dict_key].keys())[0]
        del self.init_config[self.dict_key][key]
        self.assertFalse(key in self.init_config[self.list_key])

    def test_CD_eq(self):
        copy = self.init_config[self.dict_key].to_json()
        self.assertEqual(copy, self.init_config[self.dict_key])

    def test_CD_fromkeys(self):
        value = self.init_config[self.dict_key].fromkeys([1])
        self.assertEqual(value, {1: None})
        value = self.init_config[self.dict_key].fromkeys([1], 0)
        self.assertEqual(value, {1: 0})

    def test_CD_get(self):
        key = list(self.init_config[self.dict_key].keys())[0]
        value = self.init_config[self.dict_key].get(key)
        self.assertEqual(value, self.init_config[self.dict_key].to_json()[key])
        not_exists = ""
        value = self.init_config[self.dict_key].get(not_exists)
        self.assertIsNone(value)
        value = self.init_config[self.dict_key].get(not_exists, 0)
        self.assertEqual(value, 0)

    def test_CD_items(self):
        items = self.init_config[self.dict_key].items()
        check = self.init_config[self.dict_key].to_json().items()
        self.assertEqual(list(items), list(check))

    def test_CD_keys(self):
        keys = self.init_config[self.dict_key].keys()
        check = self.init_config[self.dict_key].to_json().keys()
        self.assertEqual(list(keys), list(check))

    def test_CD_list(self):
        value = list(self.init_config[self.dict_key])
        check = list(self.init_config[self.dict_key].to_json().keys())
        self.assertEqual(value, check)

    def test_CD_ne(self):
        self.assertNotEqual(
            self.init_config[self.dict_key],
            self.init_config[self.nested_key][self.dict_key],
        )
        self.assertNotEqual(
            self.init_config[self.dict_key],
            self.init_json[self.nested_key][self.dict_key],
        )
        self.assertNotEqual(self.init_config[self.dict_key], self.init_config)
        self.assertNotEqual(self.init_config[self.dict_key], {})
        self.assertNotEqual(self.init_config[self.dict_key], [])
        self.assertNotEqual(self.init_config[self.dict_key], ())
        self.assertNotEqual(self.init_config[self.dict_key], "")
        self.assertNotEqual(self.init_config[self.dict_key], 0.0)
        self.assertNotEqual(self.init_config[self.dict_key], 0)
        self.assertNotEqual(self.init_config[self.dict_key], None)

    def test_CD_pop(self):
        key = list(self.init_config[self.dict_key].keys())[0]
        popped = self.init_config[self.dict_key].pop(key)
        self.assertEqual(popped, self.init_json[self.dict_key][key])
        not_exists = ""
        popped = self.init_config[self.dict_key].get(not_exists)
        self.assertIsNone(popped)
        popped = self.init_config[self.dict_key].get(not_exists, 0)
        self.assertEqual(popped, 0)

    def test_CD_popitem(self):
        item = list(self.init_config[self.dict_key].items())[-1]
        popped = self.init_config[self.dict_key].popitem()
        self.assertEqual(popped,
                         list(self.init_json[self.dict_key].items())[-1])

    def test_CD_reversed(self):
        check = list(self.init_config[self.dict_key].keys())
        check.reverse()
        self.assertEqual(list(reversed(self.init_config[self.dict_key])),
                         check)

    def test_CD_setdefault(self):
        self.init_config[self.dict_key].setdefault(self.new_key, self.new_data)
        self.assertEqual(self.init_config[self.dict_key][self.new_key],
                         self.new_data)
        not_exists = ""
        self.init_config[self.dict_key].setdefault(not_exists)
        self.assertIsNone(self.init_config[self.dict_key][not_exists])
        self.init_config[self.dict_key].pop(not_exists)
        self.init_config[self.dict_key].setdefault(not_exists, 0)
        self.assertEqual(self.init_config[self.dict_key][not_exists], 0)

    def test_CD_setitem(self):
        self.init_config[self.dict_key][self.new_key] = self.new_data
        self.assertEqual(self.init_config[self.dict_key][self.new_key],
                         self.new_data)

    def test_CD_update(self):
        check = self.init_config[self.dict_key].to_json()
        check.update({self.new_key: self.new_data})
        self.init_config[self.dict_key].update({self.new_key: self.new_data})
        self.assertEqual(self.init_config[self.dict_key], check)

    def test_CD_values(self):
        values = self.init_config[self.dict_key].values()
        check = self.init_config[self.dict_key].to_json().values()
        self.assertEqual(list(values), list(check))

    def test_CD_getattr(self):
        key = list(self.init_config[self.dict_key].keys())[0]
        check = self.init_config[self.dict_key][key]
        value = getattr(self.init_config[self.dict_key], key)
        self.assertEqual(value, check)

    def test_CD_setattr(self):
        setattr(self.init_config[self.dict_key], self.new_key, self.new_data)
        value = self.init_config[self.dict_key][self.new_key]
        self.assertEqual(value, self.new_data)
Ejemplo n.º 33
0
 def test_CF_eq(self):
     config_copy = ConfigFile(self.init_path)
     self.assertEqual(config_copy, self.init_config)
Ejemplo n.º 34
0
        assert A.shape[0] == b.shape[0]
        assert b.shape[1] == 3
        LU = sparse.linalg.splu((A.T @ A).tocsc())
        x = LU.solve(A.T @ b)

        vertices = x
        result = meshlib.Mesh(vertices=vertices[:len(self.target.vertices)], faces=self.target.faces)
        return result


if __name__ == "__main__":
    import render.plot_result as plt_res
    import render.plot as plt

    # cfg = ConfigFile.load(ConfigFile.Paths.highpoly.horse_camel)
    cfg = ConfigFile.load(ConfigFile.Paths.lowpoly.catdog)
    # cfg = ConfigFile.load(ConfigFile.Paths.highpoly.cat_lion)
    corr_markers = cfg.markers  # List of vertex-tuples (source, target)

    identity = False
    if identity:
        corr_markers = np.ascontiguousarray(np.array((corr_markers[:, 0], corr_markers[:, 0]), dtype=np.int).T)

    #########################################################
    # Load meshes

    original_source = meshlib.Mesh.load(cfg.source.reference)
    original_pose = meshlib.Mesh.load(cfg.source.poses[0])
    original_target = meshlib.Mesh.load(cfg.target.reference)
    if identity:
        original_target = meshlib.Mesh.load(cfg.source.reference)
Ejemplo n.º 35
0
    def __init__(self, base=None, ast_conf_options=None, host="127.0.0.1"):
        """Construct an Asterisk instance.

        Keyword Arguments:
        base -- This is the root of the files associated with this instance of
        Asterisk.  By default, the base is "/tmp/asterisk-testsuite" directory.
        Given a base, it will be appended to the default base directory.

        Example Usage:
        self.asterisk = Asterisk(base="manager/login")
        """
        self._start_deferred = None
        self._stop_deferred = None
        self._stop_cancel_tokens = []
        self.directories = {}
        self.ast_version = AsteriskVersion()
        self.process_protocol = None
        self.process = None
        self.astetcdir = ""
        self.original_astmoddir = ""

        if base is not None:
            self.base = base
        else:
            self.base = Asterisk.test_suite_root
        if self.localtest_root:
            self.ast_binary = self.localtest_root + "/usr/sbin/asterisk"
        else:
            ast_binary = test_suite_utils.which("asterisk")
            self.ast_binary = ast_binary or "/usr/sbin/asterisk"
        self.host = host

        self._ast_conf_options = ast_conf_options
        self._directory_structure_made = False
        self._configs_installed = False
        self._configs_set_up = False

        # Find the system installed asterisk.conf
        ast_confs = [
            os.path.join(self.default_etc_directory, "asterisk.conf"),
            "/usr/local/etc/asterisk/asterisk.conf",
        ]
        self._ast_conf = None
        for config in ast_confs:
            if os.path.exists(config):
                self._ast_conf = ConfigFile(config)
                break
        if self._ast_conf is None:
            msg = "Unable to locate asterisk.conf in any known location"
            LOGGER.error(msg)
            raise Exception(msg)

        # Set which astxxx this instance will be
        i = 1
        while True:
            if not os.path.isdir("%s/ast%d" % (self.base, i)):
                self.base = "%s/ast%d" % (self.base, i)
                break
            i += 1

        # Get the Asterisk directories from the Asterisk config file
        for cat in self._ast_conf.categories:
            if cat.name == "directories":
                for (var, val) in cat.options:
                    self.directories[var] = val

        # self.original_astmoddir is for dependency checking only
        if "astmoddir" in self.directories:
            if self.localtest_root:
                self.original_astmoddir = "%s%s" % (
                    self.localtest_root, self.directories["astmoddir"])
            else:
                self.original_astmoddir = self.directories["astmoddir"]
Ejemplo n.º 36
0
        except:
            continue
        if stat.S_ISDIR(st[stat.ST_MODE]):
            dirs.append(fn)
        else:
            dbfile[fn] = read_data(pp)
            nfiles = nfiles + 1
    dbfile.sync()
    dbfile.close()
    print('%s files, %s subdirs' % (nfiles, len(dirs)))
    for dn in dirs:
        cvtDirRec(old_root, new_root, lpath + '/' + dn)


if __name__ == '__main__':
    cfg = ConfigFile(os.environ['DFARM_CONFIG'])
    old_dbroot = cfg.getValue('vfssrv', '*', 'db_root')
    if not old_dbroot:
        print(
            'VFS Database root directory is not defined in the configuration')
        sys.exit(1)
    one_up = '/'.join(old_dbroot.split('/')[:-1])
    new_dbroot = one_up + '/new_dbroot'
    save_dbroot = one_up + '/db_saved'
    cvtDirRec(old_dbroot, new_dbroot, '/')
    os.mkdir(new_dbroot + '/.cellinx')
    os.rename(old_dbroot, save_dbroot)
    os.rename(new_dbroot, old_dbroot)

    print('VFS Database conversion is complete.')
    print('Old database is renamed into %s' % save_dbroot)
Ejemplo n.º 37
0
class Bot:
    def __init__(self, account):          #Account and password are instagram account and password
        self.config_file = ConfigFile(account)
        self.sub_list = self.config_file.get("Reddit", "Subreddits")
        self.account = account
        self.ibot =  InstagramBot(account)
        self.rbot = RedditBot(account)
        self.obot = osManager(int(self.config_file.get("Time", "imagelifetime")))
        self.db = dbManager()
        self.numPosts = int(self.ibot.getNumberOfPosts())
        #Make this self and remove all of the member variables and instead call the dicionary when needed
        times = self.config_file.get("Time")
        for k,v in times.items():
            times[k] = int(v)

        self.scrape_time = times["scrapetime"]
        self.upload_time_MAX = times["uploadtimemax"]
        self.upload_time_MIN = times["uploadtimemin"]
        self.expire_time = times["expiretime"]
        self.retry_upload_delay = times["retryuploaddelay"]
        #Keep this however when deleting for convenience
        self.slow_hours_start = datetime.time(times["slowhourstart"], 0, 0)
        self.slow_hours_end = datetime.time(times["slowhourend"], 0, 0)
        self.slow_hours_delay = times["slowhourdelay"]
        self.follow_time_MAX = times["followtimemax"]
        self.follow_time_MIN = times["followtimemin"]
        self.like_time_MAX = times["liketimemax"]
        self.like_time_MIN = times["liketimemin"]

    def start(self):
        self.db.createDatabase()
        loop = asyncio.get_event_loop()
        loop.create_task(self.scrape())
        loop.create_task(self.upload())
        loop.create_task(self.expire())
        loop.create_task(self.follow())
        loop.create_task(self.like())
        loop.run_forever()
        loop.close()

    def getPost(self):
        try:
            temp_sub_list = list(self.sub_list)
            while temp_sub_list:
                try:
                    sub = temp_sub_list[random.randint(0, len(temp_sub_list) - 1)]
                    pic = self.db.c.execute("SELECT DISTINCT Reddit.ID, Reddit.Title, Reddit.Path, Reddit.Subreddit, Reddit.FileFormat FROM Reddit WHERE Reddit.Subreddit == (?)  AND Reddit.Path is NOT NULL AND REDDIT.ID NOT IN (SELECT redditID FROM POSTED) GROUP BY Reddit.Path HAVING COUNT(Reddit.Path) = 1",(sub,)).fetchall()
                    temp_sub_list.remove(sub)   #removing here
                    if pic:
                        picList = pic[random.randint(0, len(pic) - 1)]
                        pic_info = {
                            "ID" : picList[0],
                            "Title" : picList[1],
                            "Path" : picList[2],
                            "Subreddit" : picList[3],
                            "FileFormat" : picList[4]
                        }
                        #If the FileFormat value is null (will not need this in the future!)
                        if not pic_info['FileFormat']:
                            filename, file_extension = os.path.splitext(pic_info['Path'])
                            pic_info['FileFormat'] = file_extension.split('.')[1]
                        return pic_info
                except Exception as e:
                    print("ERROR DURING getPost while trying to get path to image: ", e)
            return None
        except Exception as e:
            print("ERROR DURING getPost: ", e)

    def getNumPosts(self):
        try:
            return self.ibot.getNumberOfPosts()
        except Exception as e:
            print("ERROR DURING getnumPosts: ", e)

    async def scrape(self):
        while True:
            try:
                for sub in self.sub_list:
                    self.rbot.scrapeImages(sub)
            except Exception as e:
                print("ERROR DURING scape in bot: ", e)
            finally:
                await asyncio.sleep(self.scrape_time)

    async def upload(self):
        while True:
            waitTime = random.randint(self.upload_time_MIN, self.upload_time_MAX)
            if self.isSlowHours():
                waitTime += self.slow_hours_delay
            try:
                succeed = self.ibot.upload(self.getPost())
                if not succeed:
                    waitTime = self.retry_upload_delay      #If it did not succeed only wait 5 more min to post again
            except Exception as e:
                print("ERROR DURING upload in bot: ", e)
                waitTime = self.retry_upload_delay
            finally:
                await asyncio.sleep(waitTime)

    async def expire(self):
        while True:
            try:
                self.obot.checkExpired()
            except Exception as e:
                print("ERROR DURING expire in bot: ", e)
            finally:
                await asyncio.sleep(self.expire_time)

    async def follow(self):
        while True:
            try:
                waitTime = random.randint(self.follow_time_MIN, self.follow_time_MAX)
                if self.isSlowHours():
                    waitTime += self.slow_hours_delay
                await self.ibot.followRandom()
                await self.ibot.unfollow()
            except Exception as e:
                print("ERROR DURING expire in bot: ", e)
            finally:
                await asyncio.sleep(waitTime)

    async def like(self):
        while True:
            try:
                waitTime = random.randint(self.follow_time_MIN, self.follow_time_MAX)
                if self.isSlowHours():
                    waitTime += self.slow_hours_delay
                await self.ibot.likeTimelineRandom()
            except Exception as e:
                print("ERROR DURING expire in bot: ", e)
            finally:
                await asyncio.sleep(waitTime)


    def isSlowHours(self):
        currentTime = datetime.datetime.now().time()
        if self.slow_hours_start <= self.slow_hours_end:
            return self.slow_hours_start <= currentTime <= self.slow_hours_end
        else:
            return self.slow_hours_start <= currentTime or currentTime <= self.slow_hours_end