def test_extra_ns_backwards4():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('subnet4', '198.51.100.0/24'),
            ('status_file', 'tests/samples/empty.ovpn-status-v1'),
            ('add_entries', 'ns')
        ],
        'ns': [
            ('@', 'NS dns-a.example.org'),
            ('@', 'NS dns-b.example.org')
        ]
    })
    c = ResolverChain(OpenVpnAuthorityHandler(cp))
    d = c.query(dns.Query('100.51.198.in-addr.arpa', dns.NS, dns.IN))
    for rr in d.result[0]:
        assert rr.name.name == '100.51.198.in-addr.arpa'
        assert rr.payload.__class__ == dns.Record_NS
    assert set(map(lambda rr: rr.payload.name.name, d.result[0])) \
        == set(('dns-a.example.org', 'dns-b.example.org'))
def test_ptr():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('subnet4', '198.51.100.0/24'),
            ('status_file', 'tests/samples/one.ovpn-status-v1')
        ]
    })
    c = ResolverChain(OpenVpnAuthorityHandler(cp))
    reverse = '8.100.51.198.in-addr.arpa'
    d = c.query(dns.Query(reverse, dns.PTR, dns.IN))
    rr = d.result[0][0]
    assert rr.name.name == reverse
    payload = rr.payload
    assert payload.__class__ == dns.Record_PTR
    assert payload.name.name == 'one.vpn.example.org'
def test_suffix_at():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('suffix', '@'),
            ('status_file', 'tests/samples/no-fqdn.ovpn-status-v1'),
        ]
    })
    c = ResolverChain(OpenVpnAuthorityHandler(cp))
    # query one:
    d = c.query(dns.Query('one.vpn.example.org', dns.A, dns.IN))
    rr = d.result[0][0]
    assert rr.name.name == 'one.vpn.example.org'
    assert rr.payload.__class__ == dns.Record_A
    assert rr.payload.address == socket.inet_aton('198.51.100.8')
    # query two:
    d = c.query(dns.Query('one.two.vpn.example.org', dns.A, dns.IN))
    rr = d.result[0][0]
    assert rr.name.name == 'one.two.vpn.example.org'
    assert rr.payload.__class__ == dns.Record_A
    assert rr.payload.address == socket.inet_aton('198.51.100.12')
def test_soa():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('status_file', 'tests/samples/empty.ovpn-status-v1')
        ]
    })
    c = ResolverChain(OpenVpnAuthorityHandler(cp))
    d = c.query(dns.Query('vpn.example.org', dns.SOA, dns.IN))
    rr = d.result[0][0]
    assert rr.name.name == 'vpn.example.org'
    payload = rr.payload
    assert payload.__class__ == dns.Record_SOA
    assert payload.mname.name == 'dns.example.org'
    assert payload.rname.name == 'dns.example.org'
    assert payload.serial == int(os.path.getmtime(
        'tests/samples/empty.ovpn-status-v1'))
    assert payload.refresh == 3600
    assert payload.retry == 2*3600
    assert payload.expire == 3*3600
    assert payload.minimum == 4*3600
Esempio n. 5
0
 def _getpluginconf(self, modname):
     '''Parse the plugin specific configuration file and return a
     IncludingConfigParser instance representing it. Returns None if there
     was an error reading or parsing the configuration file.
     '''
     for dir in self.pluginconfpath:
         conffilename = os.path.join(dir, modname + ".conf")
         if os.access(conffilename, os.R_OK):
             # Found configuration file
             break
         self.verbose_logger.log(
             logginglevels.INFO_2,
             _("Configuration file %s not found") % conffilename)
     else:  # for
         # Configuration files for the plugin not found
         self.verbose_logger.log(
             logginglevels.INFO_2,
             _("Unable to find configuration file for plugin %s") % modname)
         return None
     parser = ConfigParser()
     confpp_obj = ConfigPreProcessor(conffilename)
     try:
         parser.readfp(confpp_obj)
     except ParsingError, e:
         raise Errors.ConfigError("Couldn't parse %s: %s" %
                                  (conffilename, str(e)))
Esempio n. 6
0
    def __init__(self,
                 recipe_name,
                 sort_only=False,
                 config_file=None,
                 use_playlists=False):
        self.recipe_name = recipe_name
        self.use_playlists = use_playlists

        self.config = ConfigParser(config_file)
        self.recipe = RecipeParser(recipe_name)

        if not self.config.validate():
            raise Exception("Error(s) in config")

        if not self.recipe.validate(use_playlists=use_playlists):
            raise Exception("Error(s) in recipe")

        if self.recipe['library_type'].lower().startswith('movie'):
            self.library_type = 'movie'
        elif self.recipe['library_type'].lower().startswith('tv'):
            self.library_type = 'tv'
        else:
            raise Exception("Library type should be 'movie' or 'tv'")

        self.source_library_config = self.recipe['source_libraries']

        self.plex = plexutils.Plex(self.config['plex']['baseurl'],
                                   self.config['plex']['token'])

        if self.config['trakt']['username']:
            self.trakt = traktutils.Trakt(
                self.config['trakt']['username'],
                client_id=self.config['trakt']['client_id'],
                client_secret=self.config['trakt']['client_secret'],
                oauth_token=self.config['trakt'].get('oauth_token', ''),
                oauth=self.recipe.get('trakt_oauth', False),
                config=self.config)
            if self.trakt.oauth_token:
                self.config['trakt']['oauth_token'] = self.trakt.oauth_token

        if self.config['tmdb']['api_key']:
            self.tmdb = tmdb.TMDb(self.config['tmdb']['api_key'],
                                  cache_file=self.config['tmdb']['cache_file'])

        if self.config['tvdb']['username']:
            self.tvdb = tvdb.TheTVDB(self.config['tvdb']['username'],
                                     self.config['tvdb']['api_key'],
                                     self.config['tvdb']['user_key'])

        self.imdb = imdbutils.IMDb(self.tmdb, self.tvdb)

        self.source_map = IdMap(matching_only=True,
                                cache_file=self.config.get('guid_cache_file'))
        self.dest_map = IdMap(cache_file=self.config.get('guid_cache_file'))
Esempio n. 7
0
    def __init__(self, token):
        self.config=ConfigParser()
        self.config.read(CONFIG_FILE_NAME,"utf_8_sig")
        self.started = True;
        
        db = DbHelper()
        tables_db = db.execute_select("SELECT * FROM {}.{}".format(self.config.get("postgresql","schema"), self.config.get("postgresql","tables_dict")))

        self.tables = []
        for table in tables_db:
            self.tables.append(NotificationTable(table[1]))
                
        self.TOKEN = token
        self.URL = "https://api.telegram.org/bot{}/".format(token)
        LogDBHandler.log(Actions.start, self.config.get("botsettings","messenger_name"),"", "", "success", "")
Esempio n. 8
0
    def __init__(self, recipe_name, sort_only=False, config_file=None):
        self.recipe_name = recipe_name

        self.config = ConfigParser(config_file)
        self.recipe = RecipeParser(recipe_name)

        if self.recipe['library_type'].lower().startswith('movie'):
            self.library_type = 'movie'
        elif self.recipe['library_type'].lower().startswith('tv'):
            self.library_type = 'tv'
        else:
            raise Exception("Library type should be 'movie' or 'tv'")

        # TODO: Support multiple libraries
        self.source_library_config = self.recipe['source_libraries']

        self.plex = plexutils.Plex(self.config['plex']['baseurl'],
                                   self.config['plex']['token'])

        if self.config['trakt']['username']:
            self.trakt = traktutils.Trakt(
                self.config['trakt']['username'],
                client_id=self.config['trakt']['client_id'],
                client_secret=self.config['trakt']['client_secret'])

        if self.config['tmdb']['api_key']:
            self.tmdb = tmdb.TMDb(self.config['tmdb']['api_key'],
                                  cache_file=self.config['tmdb']['cache_file'])

        if self.config['tvdb']['username']:
            self.tvdb = tvdb.TheTVDB(self.config['tvdb']['username'],
                                     self.config['tvdb']['api_key'],
                                     self.config['tvdb']['user_key'])
Esempio n. 9
0
def show_search(imdbid):
    
    config = ConfigParser()
    if not os.path.exists('data.json'):
        headers = {"Content-type": "application/json", "X-Api-Key": "{}".format(config['sonarr']['api_key'])}
        url = "{}/api/series".format(config['sonarr']['baseurl'])
        rsp = requests.get(url, headers=headers)
        data = json.loads(rsp.text)
        with open('data.json', 'w') as json_file: json.dump(data, json_file)
    else:
        with open('data.json') as json_file:
            data = json.load(json_file)

    for i in data:
        if i.get('imdbId','') == imdbid:
            ID = i['id']
            headers = {"Content-type": "application/json"}
            url = "{}/api/command?apikey={}".format(config['sonarr']['baseurl'], config['sonarr']['api_key'])
            data = json.dumps({"name": "SeriesSearch", "seriesId": ID})
            rsp = requests.post(url, headers=headers , data=data)
            print("Searching For {} ({})\n".format(i['title'],i['year']))
        elif title in i['title']:
            ID = i['id']
            headers = {"Content-type": "application/json"}
            url = "{}/api/command?apikey={}".format(config['sonarr']['baseurl'], config['sonarr']['api_key'])
            data = json.dumps({"name": "SeriesSearch", "seriesId": ID})
            rsp = requests.post(url, headers=headers , data=data)
            print("Searching For {} ({})\n".format(i['title'],i['year']))
Esempio n. 10
0
 def __init__(self,
              configfile,
              datadir,
              req,
              vulncontroller,
              color=False,
              detectors=None):
     self.configfile = configfile
     self.datadir = datadir
     self.vulncontroller = vulncontroller
     self.color = color
     self.detectors = detectors
     self.req = req
     self.tools = []
     self.configparser = ConfigParser(self.configfile)
     self.initTools()
Esempio n. 11
0
 def __init__(self, configfile, req, color=False, detectors=None):
     self.configfile = configfile
     print('vulncontroller: %s' % configfile)
     self.req = req
     self.color = color
     self.configparser = ConfigParser(self.configfile)
     self.tools = []
     self.initTools()
Esempio n. 12
0
def add_show(imdbid, title):
    
    # Add Missing to sonarr Work in Progress
    config = ConfigParser()

    if imdbid is None: imdbid = title
    tvdbId = get_tvdbId(imdbid)
    if tvdbId == "Not Found":
        print("TVDBId {} Not found".format(title))
        return
    headers = {"Content-type": "application/json"}
    url = "{}/api/series/lookup?term=tvdb:{}&apikey={}".format(config['sonarr']['baseurl'], tvdbId , config['sonarr']['api_key'] )
    rsp = requests.get(url, headers=headers)
    if rsp.text == "": 
        print("No imdb info Found...\n")
        return
    data = json.loads(rsp.text)
    tvdbId = data[0]["tvdbId"]
    title = data[0]["title"]
    year = data[0]["year"]
    images = json.loads(json.dumps(data[0]["images"]))
    titleslug = data[0]["titleSlug"] 
    seasons = json.loads(json.dumps(data[0]["seasons"]))
    
    headers = {"Content-type": "application/json", "X-Api-Key": "{}".format(config['sonarr']['api_key'])}
    data = json.dumps({
        "title": title ,
        "year": year ,
        "tvdbId": tvdbId ,
        "titleslug": titleslug,
        "monitored": 'true' ,
        "seasonFolder": 'true',
        "qualityProfileId": '6',
        "rootFolderPath": config['sonarr']['rootfolderpath'] ,
        "images": images,
        "seasons": seasons,
        "addOptions":
                    {
                      "ignoreEpisodesWithFiles": "true",
                      "ignoreEpisodesWithoutFiles": "false",
                      "searchForMissingEpisodes": config['sonarr']['searchForShow']
                    }

        }) 
    url = '{}/api/series'.format(config['sonarr']['baseurl'])
    rsp = requests.post(url, headers=headers, data=data)
    if rsp.status_code == 201:
        print("{} ({}) Added to sonarr\n".format(title,year))
    elif rsp.status_code == 400:
        if config['sonarr']['searchForShow'] == 'true':
            print("{} ({}) already Exists in sonarr, But Not Downloaded...".format(title, year))
            show_search(imdbid)
            return
        else:
            print("{} ({}) already Exists in sonarr, But Not Downloaded, Search not Enabled... \n".format(title, year))
            return
    else:
        print ("Did not add {} ({}) to Sonarr\n".format(title,year))
Esempio n. 13
0
    def __init__(self, conf_file):
        parser = ConfigParser(conf_file)
        conf = parser.conf["server"]

        self.microservice_thrift = thriftpy.load(conf["thrift_file"],
                                                 conf["module_name"])
        self.service = getattr(self.microservice_thrift, conf["service_name"])
        self.client = make_client(self.service, conf["host"],
                                  int(conf["port"]))
Esempio n. 14
0
def get_token():
    config = ConfigParser()
    data = {
        "apikey": "{}".format(config['tvdb']['api_key']),
        "userkey": "{}".format(config['tvdb']['user_key']), 
        "username": "******".format(config['tvdb']['username']) 
        }
    url = "https://api.thetvdb.com/login"
    rsp = requests.post(url, json=data)
    data = json.loads(rsp.text)
    return data['token']
Esempio n. 15
0
 def _getpluginconf(self, modname):
     '''Parse the plugin specific configuration file and return a
     IncludingConfigParser instance representing it. Returns None if there
     was an error reading or parsing the configuration file.
     '''
     for dir in self.pluginconfpath:
         conffilename = os.path.join(dir, modname + ".conf")
         if os.access(conffilename, os.R_OK):
             # Found configuration file
             break
         self.verbose_logger.log(logginglevels.INFO_2, _("Configuration file %s not found") % conffilename)
     else: # for
         # Configuration files for the plugin not found
         self.verbose_logger.log(logginglevels.INFO_2, _("Unable to find configuration file for plugin %s")
             % modname)
         return None
     parser = ConfigParser()
     confpp_obj = ConfigPreProcessor(conffilename)
     try:
         parser.readfp(confpp_obj)
     except ParsingError, e:
         raise Errors.ConfigError("Couldn't parse %s: %s" % (conffilename,
             str(e)))
Esempio n. 16
0
def test_aaaa():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('status_file', 'tests/samples/ipv6.ovpn-status-v1')
        ]
    })
    c = ResolverChain(OpenVpnAuthorityHandler(cp))
    d = c.query(dns.Query('one.vpn.example.org', dns.AAAA, dns.IN))
    rr = d.result[0][0]
    assert rr.name.name == 'one.vpn.example.org'
    payload = rr.payload
    assert payload.__class__ == dns.Record_AAAA
    assert IP(socket.inet_ntop(socket.AF_INET6, payload.address)) \
        == IP('fddc:abcd:1234::1008')
Esempio n. 17
0
	def __init__(self,configfile,blacklist,req,color=False,detectors = None):
		self.configfile = configfile
		self.blacklist = parseurls.getList(blacklist)
		"""
		print('vulncontroller: %s' % configfile)
		print('Cfgfile %s' % configfile)
		print('Blacklist %s' % blacklist)
		print('type blacklist %s' % type(blacklist))
		print('color %s' % color)
		print('len blacklist %s' % len(self.blacklist))
		"""
		self.req = req
		self.color = color
		self.configparser = ConfigParser(self.configfile)
		self.tools = []
		self.initTools()
Esempio n. 18
0
    def __init__(self, recipe_name, config_file=None):
        self.recipe_name = recipe_name

        self.config = ConfigParser(config_file)
        self.recipe = RecipeParser(recipe_name)

        if self.recipe['library_type'].lower().startswith('movie'):
            self.library_type = 'movie'
        elif self.recipe['library_type'].lower().startswith('tv'):
            self.library_type = 'tv'
        else:
            raise Exception("Library type should be 'movie' or 'tv'")

        self.source_library_config = self.recipe['source_libraries']

        self.plex = plexutils.Plex(self.config['plex']['baseurl'],
                                   self.config['plex']['token'])

        if self.config['trakt']['username']:
            self.trakt = traktutils.Trakt(
                self.config['trakt']['username'],
                client_id=self.config['trakt']['client_id'],
                client_secret=self.config['trakt']['client_secret'],
                oauth_token=self.config['trakt'].get('oauth_token', ''),
                oauth=self.recipe.get('trakt_oauth', False),
                config=self.config)
            if self.trakt.oauth_token:
                self.config['trakt']['oauth_token'] = self.trakt.oauth_token

        if self.config['tmdb']['api_key']:
            self.tmdb = tmdb.TMDb(self.config['tmdb']['api_key'],
                                  cache_file=self.config['tmdb']['cache_file'])

        if self.config['tvdb']['username']:
            self.tvdb = tvdb.TheTVDB(self.config['tvdb']['username'],
                                     self.config['tvdb']['api_key'],
                                     self.config['tvdb']['user_key'])

        self.imdb = imdbutils.IMDb(self.tmdb, self.tvdb)
Esempio n. 19
0
                              extrinsic, config.depth_scale, config.depth_max)
            else:
                vbg.integrate(frustum_block_coords, depth, intrinsic, extrinsic,
                              config.depth_scale, config.depth_max)
            dt = time.time() - start
        print('Finished integrating {} frames in {} seconds'.format(
            n_files, dt))
        print('Saving to {}...'.format(config.path_npz))
        vbg.save(config.path_npz)
        print('Saving finished')

    return vbg


if __name__ == '__main__':
    parser = ConfigParser()
    parser.add(
        '--config',
        is_config_file=True,
        help='YAML config file path. Please refer to default_config.yml as a '
        'reference. It overrides the default config file, but will be '
        'overridden by other command line inputs.')
    parser.add('--integrate_color', action='store_true')
    parser.add('--path_trajectory',
               help='path to the trajectory .log or .json file.')
    parser.add('--path_npz',
               help='path to the npz file that stores voxel block grid.',
               default='vbg.npz')
    config = parser.get_config()

    if config.path_dataset == '':
Esempio n. 20
0
# validate input
try:
    config_fp = sys.argv[1]
except IndexError:
    print("You failed to provide a configuration file. Usage: {0} config.json".
          format(sys.argv[0]))

if '.json' not in sys.argv[1]:
    raise ValueError("The configuration file must be in JSON format.")

# parse config
config = json.load(open(config_fp, 'r'))

# parse config
config = ConfigParser().parse_config(config=config, mode='evaluate')

# load model
if config['model'] == 'linear':
    model = models.LinearModel(input_shape=(config['history_length'], ),
                               nb_output_units=1,
                               nb_hidden_units=config['nb_hidden_units'])
elif config['model'] == 'mlp':
    model = models.MLPModel(input_shape=(config['history_length'], ),
                            nb_output_units=1,
                            nb_hidden_units=config['nb_hidden_units'],
                            nb_layers=config['nb_layers'])
elif config['model'] == 'gru':
    model = models.GRUModel(input_shape=(config['history_length'], 1),
                            nb_output_units=1,
                            nb_hidden_units=config['nb_hidden_units'],
Esempio n. 21
0
import game

import os

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)


def run():
    return game.run()


if __name__ == "__main__":
    parser = ConfigParser(config=config,
                          prog="main.py",
                          description="rewind client for bomberman")
    parser.parse()

    if not config.is_replay:
        win_count, win_avg_diff = 0, 0
        lose_count, lose_avg_diff = 0, 0
        draw_count = 0

        stat = {
            'me_alive, ene_alive': {
                'win': 0,
                'lose': 0,
                'draw': 0
            },
            'me_alive, ene_dead': {
Esempio n. 22
0
            T_frame_to_model = T_frame_to_model @ result.transformation

        poses.append(T_frame_to_model.cpu().numpy())
        model.update_frame_pose(i, T_frame_to_model)
        model.integrate(input_frame, config.depth_scale, config.depth_max)
        model.synthesize_model_frame(raycast_frame, config.depth_scale,
                                     config.depth_min, config.depth_max, False)
        stop = time.time()
        print('{:04d}/{:04d} slam takes {:.4}s'.format(i, n_files,
                                                       stop - start))

    return model.voxel_grid, poses


if __name__ == '__main__':
    parser = ConfigParser()
    parser.add(
        '--config',
        is_config_file=True,
        help='YAML config file path. Please refer to default_config.yml as a '
        'reference. It overrides the default config file, but will be '
        'overridden by other command line inputs.')
    parser.add('--path_npz',
               help='path to the npz file that stores voxel block grid.',
               default='output.npz')
    config = parser.get_config()

    depth_file_names, color_file_names = load_rgbd_file_names(config)
    intrinsic = load_intrinsic(config)

    if not os.path.exists(config.path_npz):
Esempio n. 23
0
def test_config():
    c = ConfigParser()
    c.set("logins.username", "test1")
    assert c.get("logins.username") == "test1"
    c.set("concurrency", 10)
    assert c.get("concurrency") == 10
Esempio n. 24
0
class Recipe():
    plex = None
    trakt = None
    tmdb = None
    tvdb = None

    def __init__(self,
                 recipe_name,
                 sort_only=False,
                 config_file=None,
                 use_playlists=False):
        self.recipe_name = recipe_name
        self.use_playlists = use_playlists

        self.config = ConfigParser(config_file)
        self.recipe = RecipeParser(recipe_name)

        if not self.config.validate():
            raise Exception("Error(s) in config")

        if not self.recipe.validate(use_playlists=use_playlists):
            raise Exception("Error(s) in recipe")

        if self.recipe['library_type'].lower().startswith('movie'):
            self.library_type = 'movie'
        elif self.recipe['library_type'].lower().startswith('tv'):
            self.library_type = 'tv'
        else:
            raise Exception("Library type should be 'movie' or 'tv'")

        self.source_library_config = self.recipe['source_libraries']

        self.plex = plexutils.Plex(self.config['plex']['baseurl'],
                                   self.config['plex']['token'])

        if self.config['trakt']['username']:
            self.trakt = traktutils.Trakt(
                self.config['trakt']['username'],
                client_id=self.config['trakt']['client_id'],
                client_secret=self.config['trakt']['client_secret'],
                oauth_token=self.config['trakt'].get('oauth_token', ''),
                oauth=self.recipe.get('trakt_oauth', False),
                config=self.config)
            if self.trakt.oauth_token:
                self.config['trakt']['oauth_token'] = self.trakt.oauth_token

        if self.config['tmdb']['api_key']:
            self.tmdb = tmdb.TMDb(self.config['tmdb']['api_key'],
                                  cache_file=self.config['tmdb']['cache_file'])

        if self.config['tvdb']['username']:
            self.tvdb = tvdb.TheTVDB(self.config['tvdb']['username'],
                                     self.config['tvdb']['api_key'],
                                     self.config['tvdb']['user_key'])

        self.imdb = imdbutils.IMDb(self.tmdb, self.tvdb)

        self.source_map = IdMap(matching_only=True,
                                cache_file=self.config.get('guid_cache_file'))
        self.dest_map = IdMap(cache_file=self.config.get('guid_cache_file'))

    def _get_trakt_lists(self):
        item_list = []  # TODO Replace with dict, scrap item_ids?
        item_ids = []

        for url in self.recipe['source_list_urls']:
            max_age = (self.recipe['new_playlist'].get('max_age', 0) if
                       self.use_playlists else self.recipe['new_library'].get(
                           'max_age', 0))
            if 'api.trakt.tv' in url:
                (item_list,
                 item_ids) = self.trakt.add_items(self.library_type, url,
                                                  item_list, item_ids, max_age
                                                  or 0)
            elif 'imdb.com/chart' in url:
                (item_list,
                 item_ids) = self.imdb.add_items(self.library_type, url,
                                                 item_list, item_ids, max_age
                                                 or 0)
            else:
                raise Exception(
                    "Unsupported source list: {url}".format(url=url))

        if self.recipe['weighted_sorting']['enabled']:
            if self.config['tmdb']['api_key']:
                logs.info(u"Getting data from TMDb to add weighted sorting...")
                item_list = self.weighted_sorting(item_list)
            else:
                logs.warning(u"Warning: TMDd API key is required "
                             u"for weighted sorting")
        return item_list, item_ids

    def _get_plex_libraries(self):
        source_libraries = []
        for library_config in self.source_library_config:
            logs.info(
                u"Trying to match with items from the '{}' library ".format(
                    library_config['name']))
            try:
                source_library = self.plex.server.library.section(
                    library_config['name'])
            except:  # FIXME
                raise Exception("The '{}' library does not exist".format(
                    library_config['name']))

            source_libraries.append(source_library)
        return source_libraries

    def _get_matching_items(self, source_libraries, item_list):
        matching_items = []
        missing_items = []
        matching_total = 0
        nonmatching_idx = []
        max_count = (self.recipe['new_playlist'].get('max_count', 0)
                     if self.use_playlists else self.recipe['new_library'].get(
                         'max_count', 0))

        for i, item in enumerate(item_list):
            if 0 < max_count <= matching_total:
                nonmatching_idx.append(i)
                continue
            res = self.source_map.get(item.get('id'), item.get('tmdb_id'),
                                      item.get('tvdb_id'))

            if not res:
                missing_items.append((i, item))
                nonmatching_idx.append(i)
                continue

            matching_total += 1
            matching_items += res

            if not self.use_playlists and self.recipe['new_library'][
                    'sort_title']['absolute']:
                logs.info(u"{} {} ({})".format(i + 1, item['title'],
                                               item['year']))
            else:
                logs.info(u"{} {} ({})".format(matching_total, item['title'],
                                               item['year']))

        if not self.use_playlists and not self.recipe['new_library'][
                'sort_title']['absolute']:
            for i in reversed(nonmatching_idx):
                del item_list[i]

        return matching_items, missing_items, matching_total, nonmatching_idx, max_count

    def _create_symbolic_links(self, matching_items, matching_total):
        logs.info(u"Creating symlinks for {count} matching items in the "
                  u"library...".format(count=matching_total))

        try:
            if not os.path.exists(self.recipe['new_library']['folder']):
                os.mkdir(self.recipe['new_library']['folder'])
        except:
            logs.error(u"Unable to create the new library folder "
                       u"'{folder}'.".format(
                           folder=self.recipe['new_library']['folder']))
            logs.info(u"Exiting script.")
            return 0

        count = 0
        updated_paths = []
        new_items = []
        if self.library_type == 'movie':
            for movie in matching_items:
                for part in movie.iterParts():
                    old_path_file = part.file
                    old_path, file_name = os.path.split(old_path_file)

                    folder_name = ''
                    for library_config in self.source_library_config:
                        for f in self.plex.get_library_paths(
                                library_name=library_config['name']):
                            f = os.path.abspath(f)
                            if old_path.lower().startswith(f.lower()):
                                folder_name = os.path.relpath(old_path, f)
                                break
                        else:
                            continue

                        if folder_name == '.':
                            new_path = os.path.join(
                                self.recipe['new_library']['folder'],
                                file_name)
                            dir = False
                        else:
                            new_path = os.path.join(
                                self.recipe['new_library']['folder'],
                                folder_name)
                            dir = True
                            parent_path = os.path.dirname(
                                os.path.abspath(new_path))
                            if not os.path.exists(parent_path):
                                try:
                                    os.makedirs(parent_path)
                                except OSError as e:
                                    if e.errno == errno.EEXIST \
                                            and os.path.isdir(parent_path):
                                        pass
                                    else:
                                        raise
                            # Clean up old, empty directories
                            if os.path.exists(new_path) \
                                    and not os.listdir(new_path):
                                os.rmdir(new_path)

                        if (dir and not os.path.exists(new_path)) \
                                or not dir and not os.path.isfile(new_path):
                            try:
                                if os.name == 'nt':
                                    if dir:
                                        subprocess.call([
                                            'mklink', '/D', new_path, old_path
                                        ],
                                                        shell=True)
                                    else:
                                        subprocess.call([
                                            'mklink', new_path, old_path_file
                                        ],
                                                        shell=True)
                                else:
                                    if dir:
                                        os.symlink(old_path, new_path)
                                    else:
                                        os.symlink(old_path_file, new_path)
                                count += 1
                                new_items.append(movie)
                                updated_paths.append(new_path)
                            except Exception as e:
                                logs.error(
                                    u"Symlink failed for {path}: {e}".format(
                                        path=new_path, e=e))
        else:
            for tv_show in matching_items:
                done = False
                if done:
                    continue
                for episode in tv_show.episodes():
                    if done:
                        break
                    for part in episode.iterParts():
                        old_path_file = part.file
                        old_path, file_name = os.path.split(old_path_file)

                        folder_name = ''
                        for library_config in self.source_library_config:
                            for f in self.plex.get_library_paths(
                                    library_name=library_config['name']):
                                if old_path.lower().startswith(f.lower()):
                                    old_path = os.path.join(
                                        f,
                                        old_path.replace(f, '').strip(
                                            os.sep).split(os.sep)[0])
                                    folder_name = os.path.relpath(old_path, f)
                                    break
                            else:
                                continue

                            new_path = os.path.join(
                                self.recipe['new_library']['folder'],
                                folder_name)

                            if not os.path.exists(new_path):
                                try:
                                    if os.name == 'nt':
                                        subprocess.call([
                                            'mklink', '/D', new_path, old_path
                                        ],
                                                        shell=True)
                                    else:
                                        os.symlink(old_path, new_path)
                                    count += 1
                                    new_items.append(tv_show)
                                    updated_paths.append(new_path)
                                    done = True
                                    break
                                except Exception as e:
                                    logs.error(
                                        u"Symlink failed for {path}: {e}".
                                        format(path=new_path, e=e))
                            else:
                                done = True
                                break

        logs.info(
            u"Created symlinks for {count} new items:".format(count=count))
        for item in new_items:
            logs.info(u"{title} ({year})".format(title=item.title,
                                                 year=getattr(
                                                     item, 'year', None)))

    def _verify_new_library_and_get_items(self, create_if_not_found=False):
        # Check if the new library exists in Plex
        try:
            new_library = self.plex.server.library.section(
                self.recipe['new_library']['name'])
            logs.info(
                u"Library already exists in Plex. Scanning the library...")

            new_library.update()
        except plexapi.exceptions.NotFound:
            if create_if_not_found:
                self.plex.create_new_library(
                    self.recipe['new_library']['name'],
                    self.recipe['new_library']['folder'], self.library_type)
                new_library = self.plex.server.library.section(
                    self.recipe['new_library']['name'])
            else:
                raise Exception("Library '{library}' does not exist".format(
                    library=self.recipe['new_library']['name']))

        # Wait for metadata to finish downloading before continuing
        logs.info(u"Waiting for metadata to finish downloading...")
        new_library = self.plex.server.library.section(
            self.recipe['new_library']['name'])
        while new_library.refreshing:
            time.sleep(5)
            new_library = self.plex.server.library.section(
                self.recipe['new_library']['name'])

        # Retrieve a list of items from the new library
        logs.info(
            u"Retrieving a list of items from the '{library}' library in "
            u"Plex...".format(library=self.recipe['new_library']['name']))
        return new_library, new_library.all()

    def _modify_sort_titles_and_cleanup(self,
                                        item_list,
                                        new_library,
                                        sort_only=False):
        if self.recipe['new_library']['sort']:
            logs.info(u"Setting the sort titles for the '{}' library".format(
                self.recipe['new_library']['name']))
        if self.recipe['new_library']['sort_title']['absolute']:
            for i, m in enumerate(item_list):
                item = self.dest_map.pop(m.get('id'), m.get('tmdb_id'),
                                         m.get('tvdb_id'))
                if item and self.recipe['new_library']['sort']:
                    self.plex.set_sort_title(
                        new_library.key, item.ratingKey, i + 1, m['title'],
                        self.library_type,
                        self.recipe['new_library']['sort_title']['format'],
                        self.recipe['new_library']['sort_title']['visible'])
        else:
            i = 0
            for m in item_list:
                i += 1
                item = self.dest_map.pop(m.get('id'), m.get('tmdb_id'),
                                         m.get('tvdb_id'))
                if item and self.recipe['new_library']['sort']:
                    self.plex.set_sort_title(
                        new_library.key, item.ratingKey, i, m['title'],
                        self.library_type,
                        self.recipe['new_library']['sort_title']['format'],
                        self.recipe['new_library']['sort_title']['visible'])
        unmatched_items = list(self.dest_map.items)
        if not sort_only and (self.recipe['new_library']['remove_from_library']
                              or self.recipe['new_library'].get(
                                  'remove_old', False)):
            # Remove old items that no longer qualify
            self._remove_old_items_from_library(unmatched_items)
        elif sort_only:
            return True
        if self.recipe['new_library']['sort'] and \
                not self.recipe['new_library']['remove_from_library']:
            unmatched_items.sort(key=lambda x: x.titleSort)
            while unmatched_items:
                item = unmatched_items.pop(0)
                i += 1
                logs.info(u"{} {} ({})".format(i, item.title, item.year))
                self.plex.set_sort_title(
                    new_library.key, item.ratingKey, i, item.title,
                    self.library_type,
                    self.recipe['new_library']['sort_title']['format'],
                    self.recipe['new_library']['sort_title']['visible'])
        all_new_items = self._cleanup_new_library(new_library=new_library)
        return all_new_items

    def _remove_old_items_from_library(self, unmatched_items):
        logs.info(u"Removing symlinks for items "
                  "which no longer qualify ".format(
                      library=self.recipe['new_library']['name']))
        count = 0
        updated_paths = []
        deleted_items = []
        max_date = add_years((self.recipe['new_library']['max_age'] or 0) * -1)
        if self.library_type == 'movie':
            for movie in unmatched_items:
                if not self.recipe['new_library']['remove_from_library']:
                    # Only remove older than max_age
                    if not self.recipe['new_library']['max_age'] \
                            or (movie.originallyAvailableAt and
                                max_date < movie.originallyAvailableAt):
                        continue

                for part in movie.iterParts():
                    old_path_file = part.file
                    old_path, file_name = os.path.split(old_path_file)

                    folder_name = os.path.relpath(
                        old_path, self.recipe['new_library']['folder'])

                    if folder_name == '.':
                        new_path = os.path.join(
                            self.recipe['new_library']['folder'], file_name)
                        dir = False
                    else:
                        new_path = os.path.join(
                            self.recipe['new_library']['folder'], folder_name)
                        dir = True

                    if (dir and os.path.exists(new_path)) or (
                            not dir and os.path.isfile(new_path)):
                        try:
                            if os.name == 'nt':
                                # Python 3.2+ only
                                if sys.version_info < (3, 2):
                                    assert os.path.islink(new_path)
                                if dir:
                                    os.rmdir(new_path)
                                else:
                                    os.remove(new_path)
                            else:
                                assert os.path.islink(new_path)
                                os.unlink(new_path)
                            count += 1
                            deleted_items.append(movie)
                            updated_paths.append(new_path)
                        except Exception as e:
                            logs.error(u"Remove symlink failed for "
                                       "{path}: {e}".format(path=new_path,
                                                            e=e))
        else:
            for tv_show in unmatched_items:
                done = False
                if done:
                    continue
                for episode in tv_show.episodes():
                    if done:
                        break
                    for part in episode.iterParts():
                        if done:
                            break
                        old_path_file = part.file
                        old_path, file_name = os.path.split(old_path_file)

                        folder_name = ''
                        new_library_folder = \
                            self.recipe['new_library']['folder']
                        old_path = os.path.join(
                            new_library_folder,
                            old_path.replace(new_library_folder, '').strip(
                                os.sep).split(os.sep)[0])
                        folder_name = os.path.relpath(old_path,
                                                      new_library_folder)

                        new_path = os.path.join(
                            self.recipe['new_library']['folder'], folder_name)
                        if os.path.exists(new_path):
                            try:
                                if os.name == 'nt':
                                    # Python 3.2+ only
                                    if sys.version_info < (3, 2):
                                        assert os.path.islink(new_path)
                                    os.rmdir(new_path)
                                else:
                                    assert os.path.islink(new_path)
                                    os.unlink(new_path)
                                count += 1
                                deleted_items.append(tv_show)
                                updated_paths.append(new_path)
                                done = True
                                break
                            except Exception as e:
                                logs.error(u"Remove symlink failed for "
                                           "{path}: {e}".format(path=new_path,
                                                                e=e))
                        else:
                            done = True
                            break

        logs.info(u"Removed symlinks for {count} items.".format(count=count))
        for item in deleted_items:
            logs.info(u"{title} ({year})".format(title=item.title,
                                                 year=item.year))

    def _cleanup_new_library(self, new_library):
        # Scan the library to clean up the deleted items
        logs.info(u"Scanning the '{library}' library...".format(
            library=self.recipe['new_library']['name']))
        new_library.update()
        time.sleep(10)
        new_library = self.plex.server.library.section(
            self.recipe['new_library']['name'])
        while new_library.refreshing:
            time.sleep(5)
            new_library = self.plex.server.library.section(
                self.recipe['new_library']['name'])
        new_library.emptyTrash()
        return new_library.all()

    def _run(self, share_playlist_to_all=False):
        # Get the trakt lists
        item_list, item_ids = self._get_trakt_lists()
        force_imdb_id_match = False

        # Get list of items from the Plex server
        source_libraries = self._get_plex_libraries()

        # Populate source library guid map
        for item in item_list:
            if item.get('id'):
                self.source_map.match_imdb.append(item['id'])
            if item.get('tmdb_id'):
                self.source_map.match_tmdb.append(item['tmdb_id'])
            if item.get('tvdb_id'):
                self.source_map.match_tvdb.append(item['tvdb_id'])
        self.source_map.add_libraries(source_libraries)

        # Create a list of matching items
        matching_items, missing_items, matching_total, nonmatching_idx, max_count = self._get_matching_items(
            source_libraries, item_list)

        if self.use_playlists:
            # Start playlist process
            if self.recipe['new_playlist'][
                    'remove_from_playlist'] or self.recipe['new_playlist'].get(
                        'remove_old', False):
                # Start playlist over again
                self.plex.reset_playlist(
                    playlist_name=self.recipe['new_playlist']['name'],
                    new_items=matching_items,
                    user_names=self.recipe['new_playlist'].get(
                        'share_to_users', []),
                    all_users=(share_playlist_to_all if share_playlist_to_all
                               else self.recipe['new_playlist'].get(
                                   'share_to_all', False)))
            else:
                # Keep existing items
                self.plex.add_to_playlist_for_users(
                    playlist_name=self.recipe['new_playlist']['name'],
                    items=matching_items,
                    user_names=self.recipe['new_playlist'].get(
                        'share_to_users', []),
                    all_users=(share_playlist_to_all if share_playlist_to_all
                               else self.recipe['new_playlist'].get(
                                   'share_to_all', False)))
            playlist_items = self.plex.get_playlist_items(
                playlist_name=self.recipe['new_playlist']['name'])
            return missing_items, (len(playlist_items)
                                   if playlist_items else 0)
        else:
            # Start library process
            # Create symlinks for all items in your library on the trakt watched
            self._create_symbolic_links(matching_items=matching_items,
                                        matching_total=matching_total)
            # Post-process new library
            logs.info(u"Creating the '{}' library in Plex...".format(
                self.recipe['new_library']['name']))
            new_library, all_new_items = self._verify_new_library_and_get_items(
                create_if_not_found=True)
            self.dest_map.add_items(all_new_items)
            # Modify the sort titles
            all_new_items = self._modify_sort_titles_and_cleanup(
                item_list, new_library, sort_only=False)
            return missing_items, len(all_new_items)

    def _run_sort_only(self):
        item_list, item_ids = self._get_trakt_lists()
        force_imdb_id_match = False

        # Get existing library and its items
        new_library, all_new_items = self._verify_new_library_and_get_items(
            create_if_not_found=False)
        self.dest_map.add_items(all_new_items)
        # Modify the sort titles
        self._modify_sort_titles_and_cleanup(item_list,
                                             new_library,
                                             sort_only=True)
        return len(all_new_items)

    def run(self, sort_only=False, share_playlist_to_all=False):
        if sort_only:
            logs.info(u"Running the recipe '{}', sorting only".format(
                self.recipe_name))
            list_count = self._run_sort_only()
            logs.info(
                u"Number of items in the new {library_or_playlist}: {count}".
                format(count=list_count,
                       library_or_playlist=('playlist' if self.use_playlists
                                            else 'library')))
        else:
            logs.info(u"Running the recipe '{}'".format(self.recipe_name))
            missing_items, list_count = self._run(
                share_playlist_to_all=share_playlist_to_all)
            logs.info(
                u"Number of items in the new {library_or_playlist}: {count}".
                format(count=list_count,
                       library_or_playlist=('playlist' if self.use_playlists
                                            else 'library')))
            logs.info(u"Number of missing items: {count}".format(
                count=len(missing_items)))
            for idx, item in missing_items:
                logs.info(
                    u"{idx}\t{release}\t{imdb_id}\t{title} ({year})".format(
                        idx=idx + 1,
                        release=item.get('release_date', ''),
                        imdb_id=item['id'],
                        title=item['title'],
                        year=item['year']))

    def weighted_sorting(self, item_list):
        def _get_non_theatrical_release(release_dates):
            # Returns earliest release date that is not theatrical
            # TODO PREDB
            types = {}
            for country in release_dates.get('results', []):
                # FIXME Look at others too?
                if country['iso_3166_1'] != 'US':
                    continue
                for d in country['release_dates']:
                    if d['type'] in (4, 5, 6):
                        # 4: Digital, 5: Physical, 6: TV
                        types[str(d['type'])] = datetime.datetime.strptime(
                            d['release_date'], '%Y-%m-%dT%H:%M:%S.%fZ').date()
                break

            release_date = None
            for t, d in types.items():
                if not release_date or d < release_date:
                    release_date = d

            return release_date

        def _get_age_weight(days):
            if self.library_type == 'movie':
                # Everything younger than this will get 1
                min_days = 180
                # Everything older than this will get 0
                max_days = (float(self.recipe['new_library']['max_age']) /
                            4.0 * 365.25 or 360)
            else:
                min_days = 14
                max_days = (float(self.recipe['new_library']['max_age']) /
                            4.0 * 365.25 or 180)
            if days <= min_days:
                return 1
            elif days >= max_days:
                return 0
            else:
                return 1 - (days - min_days) / (max_days - min_days)

        total_items = len(item_list)

        weights = self.recipe['weighted_sorting']['weights']

        # TMDB details
        today = datetime.date.today()
        total_tmdb_vote = 0.0
        tmdb_votes = []
        for i, m in enumerate(item_list):
            m['original_idx'] = i + 1
            details = self.tmdb.get_details(m['tmdb_id'], self.library_type)
            if not details:
                logs.warning(u"Warning: No TMDb data for {}".format(
                    m['title']))
                continue
            m['tmdb_popularity'] = float(details['popularity'])
            m['tmdb_vote'] = float(details['vote_average'])
            m['tmdb_vote_count'] = int(details['vote_count'])
            if self.library_type == 'movie':
                if self.recipe['weighted_sorting']['better_release_date']:
                    m['release_date'] = _get_non_theatrical_release(
                        details['release_dates']) or \
                                        datetime.datetime.strptime(
                                            details['release_date'],
                                            '%Y-%m-%d').date()
                else:
                    m['release_date'] = datetime.datetime.strptime(
                        details['release_date'], '%Y-%m-%d').date()
                item_age_td = today - m['release_date']
            elif self.library_type == 'tv':
                try:
                    m['last_air_date'] = datetime.datetime.strptime(
                        details['last_air_date'], '%Y-%m-%d').date()
                except TypeError:
                    m['last_air_date'] = today
                item_age_td = today - m['last_air_date']
            m['genres'] = [g['name'].lower() for g in details['genres']]
            m['age'] = item_age_td.days
            if (self.library_type == 'tv' or m['tmdb_vote_count'] > 150
                    or m['age'] > 50):
                tmdb_votes.append(m['tmdb_vote'])
            total_tmdb_vote += m['tmdb_vote']
            item_list[i] = m

        tmdb_votes.sort()

        for i, m in enumerate(item_list):
            # Distribute all weights evenly from 0 to 1 (times global factor)
            # More weight means it'll go higher in the final list
            index_weight = float(total_items - i) / float(total_items)
            m['index_weight'] = index_weight * weights['index']
            if m.get('tmdb_popularity'):
                if (self.library_type == 'tv' or m.get('tmdb_vote_count') > 150
                        or m['age'] > 50):
                    vote_weight = ((tmdb_votes.index(m['tmdb_vote']) + 1) /
                                   float(len(tmdb_votes)))
                else:
                    # Assume below average rating for new/less voted items
                    vote_weight = 0.25
                age_weight = _get_age_weight(float(m['age']))

                if weights.get('random'):
                    random_weight = random.random()
                    m['random_weight'] = random_weight * weights['random']
                else:
                    m['random_weight'] = 0.0

                m['vote_weight'] = vote_weight * weights['vote']
                m['age_weight'] = age_weight * weights['age']

                weight = (m['index_weight'] + m['vote_weight'] +
                          m['age_weight'] + m['random_weight'])
                for genre, value in weights['genre_bias'].items():
                    if genre.lower() in m['genres']:
                        weight *= value

                m['weight'] = weight
            else:
                m['vote_weight'] = 0.0
                m['age_weight'] = 0.0
                m['weight'] = index_weight
            item_list[i] = m

        item_list.sort(key=lambda m: m['weight'], reverse=True)

        for i, m in enumerate(item_list):
            if (i + 1) < m['original_idx']:
                net = Colors.GREEN + u'↑'
            elif (i + 1) > m['original_idx']:
                net = Colors.RED + u'↓'
            else:
                net = u' '
            net += str(abs(i + 1 - m['original_idx'])).rjust(3)
            try:
                # TODO
                logs.info(
                    u"{} {:>3}: trnd:{:>3}, w_trnd:{:0<5}; vote:{}, "
                    "w_vote:{:0<5}; age:{:>4}, w_age:{:0<5}; w_rnd:{:0<5}; "
                    "w_cmb:{:0<5}; {} {}{}".format(
                        net, i + 1, m['original_idx'],
                        round(m['index_weight'], 3), m.get('tmdb_vote', 0.0),
                        round(m['vote_weight'], 3), m.get('age', 0),
                        round(m['age_weight'], 3),
                        round(m.get('random_weight', 0), 3),
                        round(m['weight'], 3), str(m['title']), str(m['year']),
                        Colors.RESET))
            except UnicodeEncodeError:
                pass

        return item_list
Esempio n. 25
0
# validate input
try:
    config_fp = sys.argv[1]
except IndexError:
    print("You failed to provide a configuration file. Usage: {0} config.json".
          format(sys.argv[0]))

if '.json' not in sys.argv[1]:
    raise ValueError("The configuration file must be in JSON format.")

# parse config
config = json.load(open(config_fp, 'r'))

# parse config
config = ConfigParser().parse_config(config=config, mode='train')

# load data
exclude = []

all_subjects = [540, 544, 552, 567, 584, 596, 559, 563, 570, 575, 588, 591]

if config['dataset'] == 'subject_only':
    exclude = list(set(all_subjects) - set([int(config['subject'])]))
elif config['dataset'] == 'exclude_subject':
    exclude = [int(config['subject'])]

print("loading data..")
X_train, Y_train, X_val, Y_val, X_train_mean, X_train_stdev = data.load_data(
    dir=config['data_dir'],
    exclude=exclude,
Esempio n. 26
0
            gui.Application.instance.post_to_main_thread(
                self.window, lambda: self.update_render(
                    input_frame.get_data_as_image('depth'),
                    input_frame.get_data_as_image('color'),
                    raycast_frame.get_data_as_image('depth'),
                    raycast_frame.get_data_as_image('color'), pcd, frustum))

            self.idx += 1
            self.is_done = self.is_done | (self.idx >= n_files)

        time.sleep(0.5)


if __name__ == '__main__':
    parser = ConfigParser()
    parser.add(
        '--config',
        is_config_file=True,
        help='YAML config file path. Please refer to default_config.yml as a '
        'reference. It overrides the default config file, but will be '
        'overridden by other command line inputs.')
    parser.add(
        '--default_dataset',
        help='Default dataset is used when config file is not provided. '
        'Default dataset may be selected from the following options: '
        '[lounge, bedroom, jack_jack]',
        default='lounge')
    parser.add('--path_npz',
               help='path to the npz file that stores voxel block grid.',
               default='output.npz')
Esempio n. 27
0
            T_frame_to_model = T_frame_to_model @ result.transformation

        poses.append(T_frame_to_model.cpu().numpy())
        model.update_frame_pose(i, T_frame_to_model)
        model.integrate(input_frame, config.depth_scale, config.depth_max)
        model.synthesize_model_frame(raycast_frame, config.depth_scale,
                                     config.depth_min, config.depth_max, False)
        stop = time.time()
        print('{:04d}/{:04d} slam takes {:.4}s'.format(i, n_files,
                                                       stop - start))

    return model.voxel_grid, poses


if __name__ == '__main__':
    parser = ConfigParser()
    parser.add(
        '--config',
        is_config_file=True,
        help='YAML config file path. Please refer to default_config.yml as a '
        'reference. It overrides the default config file, but will be '
        'overridden by other command line inputs.')
    config = parser.get_config()

    depth_file_names, color_file_names = load_image_file_names(config)
    intrinsic = load_intrinsic(config)

    volume, poses = slam(depth_file_names, color_file_names, intrinsic, config)
    save_poses('output.log', poses)
    save_poses('output.json', poses)
Esempio n. 28
0
class BotHandler:

    #конструктор класса для работы с ботом
    def __init__(self, token):
        self.config=ConfigParser()
        self.config.read(CONFIG_FILE_NAME,"utf_8_sig")
        self.started = True;
        
        db = DbHelper()
        tables_db = db.execute_select("SELECT * FROM {}.{}".format(self.config.get("postgresql","schema"), self.config.get("postgresql","tables_dict")))

        self.tables = []
        for table in tables_db:
            self.tables.append(NotificationTable(table[1]))
                
        self.TOKEN = token
        self.URL = "https://api.telegram.org/bot{}/".format(token)
        LogDBHandler.log(Actions.start, self.config.get("botsettings","messenger_name"),"", "", "success", "")

    #получает url
    def get_url(self, url, offset=None, timeout=100):
        params = {'timeout': timeout, 'offset': offset}
        response = requests.get(url, params)
        content = response.content.decode("utf8")
        return content

    #вспомогательная функция получает json из url
    def get_json_from_url(self, url):
        content = self.get_url(url)
        js = json.loads(content)
        return js

    #получить все обновления
    def get_updates(self, offset=None, timeout=100):
        url = self.URL + "getUpdates".format(timeout)
        if offset:
            url += "?offset={}".format(offset)
        js = self.get_json_from_url(url)
        return js

    #получить ид последнего обновления
    def get_last_update_id(self, updates):
        update_ids = []
        for update in updates["result"]:
            update_ids.append(int(update["update_id"]))
        return max(update_ids)

    #получить текст и ид чата последнего сообщения
    def get_last_chat_id_and_text(self, updates):
        num_updates = len(updates["result"])
        last_update = num_updates - 1
        text = updates["result"][last_update]["message"]["text"]
        chat_id = updates["result"][last_update]["message"]["chat"]["id"]
        return (text, chat_id)

    #функция ответа на входящие сообщения
    def echo_all(self, updates):
        for update in updates["result"]:  
            try:
                print(self.config.get("commands","list_subscribers"))
                text = update["message"]["text"]
                chat = update["message"]["chat"]["id"]
                LogDBHandler.log(Actions.message, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat), text, "success", "user", chat,"","{}", json.dumps(update).replace("'","\""))
                first_name = update["message"]["from"]["first_name"]
                if text=="/start" and self.started:
                    self.subscribe(first_name,chat)
                elif text==self.config.get("commands","unsubscribe") and self.started:
                    self.unsubscribe(first_name,chat)
                elif text==self.config.get("commands","disengage") and self.get_user_role(first_name,chat)=="ADMIN" and self.started==True:
                    self.started=False
                    LogDBHandler.log(Actions.stop, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat), text, "success", "user", chat,"","{}", json.dumps(update).replace("'","\""))
                elif text==self.config.get("commands","engage") and self.get_user_role(first_name,chat)=="ADMIN" and self.started==False:
                    self.started=True
                    LogDBHandler.log(Actions.start, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat), text, "success", "user", chat,"","{}", json.dumps(update).replace("'","\""))
                elif text==self.config.get("commands","list_subscribers") and self.get_user_role(first_name,chat)=="ADMIN":
                    self.list_users(first_name,chat)
                else:
                    self.try_process_code(first_name, chat, text)
            except KeyError:
                chat = update["message"]["chat"]["id"]
                first_name = update["message"]["from"]["first_name"]
                number = update["message"]["contact"]["phone_number"]
                self.try_process_number(first_name, chat, number)
            except Exception as e:
                LogDBHandler.log(Actions.error, self.config.get("botsettings","messenger_name"), "", str(e.message).replace("/","//").replace("'","\""), "fail", "bot", "","","{}", json.dumps(update).replace("'","\""))
                print(e)

    #вывести список пользователей
    def list_users(self,  first_name, chat_id):
        result = ""
        db=DbHelper()
        rows = db.execute_select("select user_name, phone_number, chat_id, (select caption from {0}.{1} where id=status), role from {0}.{2}"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","statuse_table"),
                                     self.config.get("postgresql","users_table")))
        for row in rows:
            result+="Имя: {}, Номер: {}, Статус: {}, Роль: {}\n".format(row[0],row[1],row[3],row[4])
        self.send_message(result,chat_id)
    
    #обработка номера телефона
    def try_process_number(self,  first_name, chat_id, number):
        db = DbHelper()
        rows = db.execute_select("SELECT * FROM {}.{} WHERE user_name = '{}' AND chat_id='{}'"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","users_table"),
                                     first_name,
                                     chat_id))
        if len(rows)!=0 and rows[0][4]==1:
            twillio_message = str(random.randint(0,9)) + str(random.randint(0,9)) + str(random.randint(0,9)) + str(random.randint(0,9))
            if (number[0]=="7"):
                number="+" + number
            db.execute_sql("UPDATE {}.{} SET phone_number='{}', sms_code='{}', status={}  WHERE user_name='{}' and chat_id='{}'"
                           .format(
                               self.config.get("postgresql","schema"),
                               self.config.get("postgresql","users_table"),
                               number,
                               twillio_message,
                               2,
                               first_name,
                               chat_id))
            LogDBHandler.log(Actions.phone_number_recieved, self.config.get("botsettings","messenger_name"), number, "", "success", "user", chat_id)
            Twilio.send_sms(twillio_message, number)
            LogDBHandler.log(Actions.code_sended,self.config.get("botsettings","messenger_name"),number,"","success","bot",chat_id)
            self.send_message(self.config.get("messages","confirmation_message"), chat_id)

    #обработка кода смс сообщения
    def try_process_code(self,  first_name, chat_id, number):
        db = DbHelper()
        rows = db.execute_select("SELECT * FROM {}.{} WHERE user_name = '{}' AND chat_id='{}'"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","users_table"),
                                     first_name,
                                     chat_id))
        if len(rows)!=0 and rows[0][4]==2:
            if int(number)==rows[0][5]:
                db.execute_sql("UPDATE {}.{} SET status={}  WHERE user_name='{}' and chat_id='{}'"
                               .format(
                                   self.config.get("postgresql","schema"),
                                   self.config.get("postgresql","users_table"),
                                   3,
                                   first_name,
                                   chat_id))
                self.send_message(self.config.get("messages","subscribed_message"), chat_id)
                LogDBHandler.log(Actions.registred,self.config.get("botsettings","messenger_name"),number,"","success","bot",chat_id)
            else:
                LogDBHandler.log(Actions.registred,self.config.get("botsettings","messenger_name"),number,"","failed","bot",chat_id,"wrong confirmation code")
                self.send_message(self.config.get("messages","wrong_code_message"), chat_id)
                

    #получить роль пользователя
    def get_user_role(self, first_name, chat_id):
        db = DbHelper()
        rows = db.execute_select("SELECT role FROM {}.{} WHERE user_name = '{}' AND chat_id='{}'"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","users_table"),
                                     first_name, 
                                     chat_id))
        if len(rows)!=0:
            return str(rows[0][0])
        else:
            return None
     
    #функция сканирует наличие сообщений в подключенных таблицах и отправляет если есть что оправить       
    def send_notifications(self):
        if self.started:

            for table in self.tables:
                for sheet in table.sheets:
                    
                    amount_re = re.compile(r'Надо(\n|\s|\t)+отправить.*')
                    #print(sheet.wks.get_all_records())                    
                    list_of_cells = sheet.wks.findall(amount_re)
                    
                    for cell in list_of_cells:
                        row = str(cell.row)
                        number = sheet.wks.acell(sheet.number + row).value.replace('-', '').replace('(', '').replace(')', '')
                        if (number[0]=="7"):
                            number="+" + number
                        elif (number[0]==8):
                            number = number.replace("8","+7",1)
                        db = DbHelper()
                        chat_id = db.execute_select("SELECT chat_id FROM {}.{} where phone_number='{}'"
                                                    .format(
                                                        self.config.get("postgresql","schema"),
                                                        self.config.get("postgresql","users_table"),
                                                        number))[0][0]
                        if ("sms" in str(cell.value)):
                            Twilio.send_sms(sheet.wks.acell(sheet.text + row).value,number)
                            sheet.wks.update_acell(sheet.sended+row,"Да")
                            LogDBHandler.log(Actions.finish_sending,"sms",number)
                        else:
                            if self.send_message(sheet.wks.acell(sheet.text + row).value,chat_id):
                                sheet.wks.update_acell(sheet.sended+row,"Да")
                                sheet.wks.update_acell(sheet.status+row,"отправлено")
                        time.sleep(float(self.config.get("botsettings","sending_interval")))
                           
    #вспомогательная функция для кастомной клавиатуры                        
    def build_keyboard(self, text):
        keyboard = [[{"text": text,"request_contact": True}]]
        reply_markup = {"keyboard":keyboard, "one_time_keyboard": True, "resize_keyboard": True}
        return json.dumps(reply_markup)

    #(подписать пользователя (добавить в БД)
    def subscribe(self, first_name, chat_id):
        db = DbHelper()
        rows = db.execute_select("SELECT * FROM {}.{} WHERE user_name = '{}' AND chat_id = '{}'"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","users_table"),
                                     first_name,
                                     chat_id))
        if len(rows)==0:
            db.execute_sql("INSERT INTO {}.{} (user_name, chat_id, status) VALUES ('{}', '{}', {})"
                           .format(
                               self.config.get("postgresql","schema"),
                               self.config.get("postgresql","users_table"),
                               first_name,
                               chat_id,
                               1))
            #keyboard = self.build_keyboard(telegram.KeyboardButton(text="Поделиться номером телефона", request_contact=True))
            LogDBHandler.log(Actions.registred, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat_id), "", "success", "user", chat_id)
            self.send_message(self.config.get("messages","share_number_message"), chat_id, self.build_keyboard(self.config.get("messages","button_caption")))
        else:
            self.send_message(self.config.get("messages","already_subscribed_message"), chat_id)
    
    #отписать пользователя (удалить из БД)
    def unsubscribe(self, first_name, chat_id):
        db = DbHelper()
        db.execute_sql("DELETE FROM {}.{} WHERE user_name = '{}' AND phone_number = '{}'"
                       .format(
                           self.config.get("postgresql","schema"),
                           self.config.get("postgresql","users_table"),
                           first_name,
                           chat_id))
        LogDBHandler.log(Actions.unsubscribed, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat_id), "", "success", "user", chat_id)
        self.send_message(self.config.get("messages","unsubscribed_message"), chat_id)

    #отправка сообщения
    def send_message(self, text, chat_id, reply_markup=None):
        LogDBHandler.log(Actions.start_sending,"telegram",self.get_phone_number(chat_id),text[0:255])
        response = '{}'
        try:
            text = urllib.parse.quote_plus(text)
            url = self.URL + "sendMessage?text={}&chat_id={}&parse_mode=Markdown".format(text, chat_id)
            if reply_markup:
                url += "&reply_markup={}".format(reply_markup)
            print(url)
            response = self.get_url(url)
            LogDBHandler.log(Actions.message, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat_id), "", "success", "user", chat_id)
            return True
        except  Exception as e:
            LogDBHandler.log(Actions.message, self.config.get("botsettings","messenger_name"), self.get_phone_number(chat_id), "", "failed", "user", chat_id, str(e.message).replace("/","//").replace("'","\""), '{}', response)
            return False

    def get_phone_number(self, chat_id):
        db = DbHelper()
        rows = db.execute_select("SELECT phone_number FROM {}.{} WHERE chat_id='{}'"
                                 .format(
                                     self.config.get("postgresql","schema"),
                                     self.config.get("postgresql","users_table"),
                                     chat_id))
        if len(rows)!=0:
            return str(rows[0][0])
        else:
            return ""
Esempio n. 29
0
def test_extra_entries():
    cp = ConfigParser()
    cp.parse_data({
        'options': [
            ('instance', 'vpn.example.org'),
        ],
        'vpn.example.org': [
            ('mname', 'dns.example.org'),
            ('rname', 'dns.example.org'),
            ('refresh', '1h'),
            ('retry', '2h'),
            ('expire', '3h'),
            ('minimum', '4h'),
            ('subnet4', '198.51.100.0/24'),
            ('subnet6', 'fe80::/64'),
            ('status_file', 'tests/samples/empty.ovpn-status-v1'),
            ('add_entries', 'all'),
            ('add_entries', 'all2'),
            ('add_forward_entries', 'forward'),
            ('add_backward_entries', 'backward'),
            ('add_backward4_entries', 'backward4'),
            ('add_backward6_entries', 'backward6'),
        ],
        'all':       [('all',       'A 127.0.0.1')],
        'all2':      [('all2',      'A 127.0.0.2')],
        'forward':   [('forward',   'A 127.0.0.3')],
        'backward':  [('backward',  'A 127.0.0.4')],
        'backward4': [('backward4', 'A 127.0.0.5')],
        'backward6': [('backward6', 'A 127.0.0.6')],
    })
    dd = OpenVpnAuthorityHandler(cp)
    c = ResolverChain(dd)

    def test(name, result):
        d = c.query(dns.Query(name, dns.A, dns.IN)).result

        if not result:
            assert d.__class__ is Failure
        else:
            assert type(d) is tuple
            assert len(d[0]) == 1
            assert d[0][0].type == dns.A
            assert socket.inet_ntoa(d[0][0].payload.address) == result

    # test forward:
    test('all.vpn.example.org', '127.0.0.1')
    test('all2.vpn.example.org', '127.0.0.2')
    test('forward.vpn.example.org', '127.0.0.3')
    test('backward.vpn.example.org', False)
    test('backward4.vpn.example.org', False)
    test('backward6.vpn.example.org', False)

    # test backward4:
    test('all.100.51.198.in-addr.arpa', '127.0.0.1')
    test('all2.100.51.198.in-addr.arpa', '127.0.0.2')
    test('forward.100.51.198.in-addr.arpa', False)
    test('backward.100.51.198.in-addr.arpa', '127.0.0.4')
    test('backward4.100.51.198.in-addr.arpa', '127.0.0.5')
    test('backward6.100.51.198.in-addr.arpa', False)

    # test backward6:
    test('all.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', '127.0.0.1')
    test('all2.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', '127.0.0.2')
    test('forward.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', False)
    test('backward.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', '127.0.0.4')
    test('backward4.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', False)
    test('backward6.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa', '127.0.0.6')
Esempio n. 30
0
def add_movie(imdbid,title):
    
    # Add Missing to Radarr Work in Progress
    config = ConfigParser()
    headers = {"Content-type": "application/json"}
    url = "{}/api/movie/lookup/imdb?imdbId={}&apikey={}".format(config['radarr']['baseurl'], imdbid, config['radarr']['api_key'] )
    rsp = requests.get(url, headers=headers)
    if rsp.status_code != 200:
        print ("Failed to connect to Radarr.")
        return
    if rsp.status_code == 404:
        print ("Movie Not Found...")
        return 
    if rsp.text == "":
        title = title.replace(":","")
        url = "{}/api/movie/lookup?term={}&apikey={}".format(config['radarr']['baseurl'], title, config['radarr']['api_key'] )
        rsp = requests.get(url, headers=headers)
        if rsp.status_code != 200:
            print ("Failed to connect to Radarr.")
            return
        if rsp.status_code == 404:
            print ("Movie Not Found...")
            return
        if rsp.text == "[]":
            print ("Movie Not Found...")
            return
        data = json.loads(rsp.text)
        tmdbid = data[0]["tmdbId"]
        title = data[0]["title"]
        year = data[0]["year"]
        images = json.loads(json.dumps(data[0]["images"]))
        titleslug = data[0]["titleSlug"] 
        if year > datetime.now().year: 
            print("{} ({}) not releasd yet not added to Radarr..".format(title,year))
            return
    else:
        data = json.loads(rsp.text)
        tmdbid = data["tmdbId"]
        title = data["title"]
        year = data["year"]
        images = json.loads(json.dumps(data["images"]))
        titleslug = data["titleSlug"] 
        if year > datetime.now().year: 
            print("{} ({}) not releasd yet not added to Radarr..".format(title,year))
            return
    
    headers = {"Content-type": "application/json", "X-Api-Key": config['radarr']['api_key']}
    data = json.dumps({
        "title": title ,
        "qualityProfileId": '6' ,
        "year": year ,
        "tmdbId": tmdbid ,
        "titleslug":titleslug,
        "monitored": 'true' ,
        "minimumAvailability": "released",
        "rootFolderPath": config['radarr']['rootfolderpath'] ,
        "images": images,
        "addOptions" : {"searchForMovie" : config['radarr']['searchForMovie']}
        })
    
    url = '{}/api/movie'.format(config['radarr']['baseurl'])
    rsp = requests.post(url, headers=headers, data=data)
    if rsp.status_code == 201:
        print("{} ({}) Added to Radarr..".format(title,year))

    elif rsp.status_code == 400:
        if config['radarr']['searchForMovie'] == 'true':
            print("{} ({}) already Exists in Radarr, But Not Downloaded...".format(title, year)) 
            movie_search(imdbid)
            return
        else:
            print("{} ({}) already Exists in Radarr, But Not Downloaded, Search not Enabled...".format(title, year))
            return
Esempio n. 31
0
                  directory="logs",
                  stats_file="profile.pstats",
                  dot_file="profile.dot",
                  output_file="profile.pdf"):
    import cProfile
    import pathlib
    # noinspection PyPackageRequirements
    import gprof2dot
    import subprocess
    stats_path = f"{directory}/{stats_file}"
    dot_path = f"{directory}/{dot_file}"
    output_path = f"{directory}/{output_file}"
    pathlib.Path(directory).mkdir(parents=True, exist_ok=True)
    cProfile.run(statement, stats_path)
    gprof2dot.main(["-f", "pstats", stats_path, "-o", dot_path])
    subprocess.run(["dot", "-Tpdf", dot_path, "-o", output_path])
    subprocess.run(["rm", stats_path, dot_path])


if __name__ == '__main__':
    parser = ConfigParser()
    config = parser.parse_config()
    np_seed, env, agents, keyboard_agent = config.setup()

    simulation = Simulation(env,
                            agents,
                            config=config,
                            keyboard_agent=keyboard_agent)
    simulation.run()
    # profile("simulation.run()")  # reminder: there is a significant performance hit when using cProfile
    def __init__(self, window_size):
        '''
        Nombre: __init__
        Descripcion: Constructor del cliente de video
        Argumentos: window_size: Tamano de la ventana.
        '''

        #Creamos el objeto de configuracion
        self.config = ConfigParser()

        #Creamos el objeto de buffer de video
        self.buffer_video = VideoBuffer(self.config)

        # Creamos una variable que contenga el GUI principal
        self.app = gui("Redes2 - P2P", window_size)
        self.app.setGuiPadding(10, 10)

        # Preparación del interfaz
        self.app.addLabel("title", "Cliente Multimedia P2P - Redes2 ")
        self.app.addLabel("loggeduser", "Sesion no iniciada")
        self.app.addImage("video", "imgs/webcam.gif")

        # Registramos la función de captura de video
        # Esta misma función también sirve para enviar un vídeo
        self.cap = cv2.VideoCapture(0)

        #FPS minimo y maximo para el QoS
        self.fps_send_max = int(self.cap.get(cv2.CAP_PROP_FPS))
        self.fps_send_min = self.fps_send_min // 2
        self.fps_send = [self.fps_send_max]

        # Añadir los botones
        self.app.addButtons(["Conectar", "Espera", "Colgar", "Salir"],
                            self.buttonsCallback)
        self.app.enableButton("Conectar")
        self.app.disableButton("Espera")
        self.app.disableButton("Colgar")

        # Barra de estado
        # Debe actualizarse con información útil sobre la llamada (duración, FPS, etc...)
        self.app.addStatusbar(fields=3, side="LEFT")
        self.app.setStatusbarWidth(35, field=0)
        self.app.setStatusbarWidth(50, field=1)
        self.app.setStatusbarWidth(60, field=2)
        #Bara de herramientas
        self.app.addToolbar(["FILE", "CAMERA"],
                            self.toolbarCallback,
                            findIcon=True)
        self.app.setToolbarIcon("CAMERA", "md-camera-photo")
        self.app.setStopFunction(self.stop)
        self.app.setStartFunction(self.on_startup)

        #Ventana de inicio de sesion
        self.app.startSubWindow("Login",
                                modal=True)  #Modal: Bloquea a la principal.

        self.app.addLabel("pad", "", 0, 0)  #Padding above
        self.app.setPadding([30, 0])

        self.app.setSticky('e')
        self.app.addLabel("user", "Nick: ", 1, 0)
        self.app.addLabel("pass", "Password: "******"tcp", "Puerto de control (TCP): ", 3, 0)
        self.app.addLabel("udp", "Puerto de video (UDP): ", 4, 0)
        self.app.addLabel("ip", "Direccion IP: ", 5, 0)
        self.app.addLabel(
            "ipWarn",
            "(La IP puede dejarse en blanco para utilizar la IP local)", 6, 2)

        self.app.setSticky('w')
        self.app.addEntry("userInput", 1, 2)
        self.app.addSecretEntry("passInput", 2, 2)
        self.app.addNumericEntry("tcpInput", 3, 2)
        self.app.addNumericEntry("udpInput", 4, 2)
        self.app.addEntry("ipInput", 5, 2)

        self.app.setPadding([20, 20])
        self.app.addButton("Entrar", self.login, 7, 1)
        self.app.setStopFunction(
            self.app.stop)  #Parar la ventana cierra la app.
        self.app.setPadding([0, 0])
        self.app.stopSubWindow()

        # Definicion de la ventana de elegir usuario para llamada:
        self.app.startSubWindow("Iniciar llamada",
                                modal=True)  #Modal: Bloquea a la principal.

        self.app.addLabel("pad2", "", 0, 0)  #Padding above
        self.app.setPadding([30, 0])

        self.app.setSticky('e')
        self.app.addLabel("calleeNick", "Llamar a: ", 1, 0)
        self.app.addLabel("listInfo", "Usuarios registrados:", 2, 0)

        self.app.setSticky('w')
        self.app.addEntry("calleeNickInput", 1, 1)
        self.app.addListBox("nicksList", ["Cargando..."], 2, 1)

        #Handler
        self.app.setListBoxChangeFunction("nicksList", self.list_handler)

        self.app.setPadding([20, 20])
        self.app.addButton("Llamar", self.init_call, 3, 1)
        self.app.setPadding([0, 0])
        self.app.stopSubWindow()
Esempio n. 33
0
class swcontroller:
    #def __init__(self,configfile,detectors = None):
    # objeto para peticiones
    # swcontroller tiene un objeto vulncontroller
    # def __init__(self,configfile,configdir,req,vulncontroller,color=False,detectors = None):
    def __init__(self,
                 configfile,
                 datadir,
                 req,
                 vulncontroller,
                 color=False,
                 detectors=None):
        self.configfile = configfile
        self.datadir = datadir
        self.vulncontroller = vulncontroller
        self.color = color
        self.detectors = detectors
        self.req = req
        self.tools = []
        self.configparser = ConfigParser(self.configfile)
        self.initTools()

    def setConfFile(self, configfile):
        if configfile is not None: self.configfile = configfile

    # Aqui solo pongo los detectores pasados como parametros
    """
	def setDetectors(self,detectors):
		if detectors is not None: self.detectors = detectors
	"""

    # define los modulos de deteccion a ocupar
    def initTools(self):
        # wordpatterns,files,directories,headers,juicyfiles,postcrawling
        scanners = []
        swmods = self.getSoftwareModules()
        for sw in swmods.keys():
            wpatterns = swmods[sw][0]
            files = swmods[sw][1]
            dirs = swmods[sw][2]
            headers = swmods[sw][3]
            juicyfiles = swmods[sw][4]
            postcrawling = swmods[sw][5]
            themes = swmods[sw][6]
            actscan = genscan(self.req, self.color, self.datadir, sw, headers,
                              wpatterns, files, dirs, juicyfiles, postcrawling,
                              themes)
            actscan.setToolPath(self.configparser.getPath(sw))
            actscan.setToolArgs(self.configparser.getToolArg(sw))
            actscan.setToolFlags(self.configparser.getToolFlags(sw))
            scanners.append(actscan)
        mail = mailscan(self.req, self.color)
        params = paramscanner(self.req, self.color)
        content = contentscan(self.req, self.color)
        backscan = backupscan(self.req, self.color)
        self.tools = scanners + [mail, params, content, backscan]
        #self.tools = [wordpress,drupal,moodle,joomla,ojs,magento,mail,params,content,backscan]

    # Regresa un diccionario con los softwares a identificar y como identificarlos
    # {'nombresoftware':[wpatterns],[files],[directories],[headers],[juicydata],postcrawling}
    # esto para aprovechar la herencia y no tener un constructor de cada
    # cms, sino iterar por nombre en el diccionario y pasar las listas
    # como datos al constructor de cada software en swdetection
    # # wordpatterns,files,directories,headers,juicyfiles,postcrawling
    def getSoftwareModules(self):
        dom = parse(self.datadir)
        # diccionario para las herramientas
        sw = {}
        xmltools = dom.getElementsByTagName('software')
        for node in xmltools:
            tool_name = node.getAttribute('name')
            wpdata,fidata,dirdata,headdata,juicydata,themedata = [],[],[],[],[],[]
            pcdata = False
            ############## wordpatterns ###################
            wplist = node.getElementsByTagName('wordpatterns')
            for wp in wplist:
                wpdata = wp.firstChild.data
            if wpdata is not None:
                wpdata = wpdata.replace('\n', '').replace('\t', '').split(',')
            ############### files #######################
            filelist = node.getElementsByTagName('files')
            for fi in filelist:
                fidata = fi.firstChild.data
            if fidata is not None:
                fidata = fidata.replace('\n', '').replace('\t', '').split(',')
            ############### dirs ########################
            dirlist = node.getElementsByTagName('directories')
            for di in dirlist:
                dirdata = di.firstChild.data
            if dirdata is not None:
                dirdata = dirdata.replace('\n', '').replace('\t',
                                                            '').split(',')
            ############# headers #######################
            headlist = node.getElementsByTagName('headers')
            for hi in headlist:
                headdata = hi.firstChild.data
            if headdata is not None:
                headdata = headdata.replace('\n', '').replace('\t',
                                                              '').split(',')
            ############# juicyfiles #######################
            jflist = node.getElementsByTagName('juicyfiles')
            for jf in jflist:
                juicydata = jf.firstChild.data
            if juicydata is not None:
                juicydata = juicydata.replace('\n', '').replace('\t',
                                                                '').split(',')
            ############# themes #######################
            themelist = node.getElementsByTagName('themes')
            for th in themelist:
                themedata = th.firstChild.data
            if themedata is not None and len(themedata) > 0:
                themedata = themedata.replace('\n', '').replace('\t',
                                                                '').split(',')
            ############# postcrawling #######################
            pclist = node.getElementsByTagName('postcrawl')
            for pc in pclist:
                pcdata = pc.firstChild.data
            if pcdata is not None:
                pcdata = pcdata.replace('\n', '').replace('\t', '')
                if pcdata == "True": pcdata = True
                else: pcdata = False
            sw[tool_name] = [
                wpdata, fidata, dirdata, headdata, juicydata, themedata, pcdata
            ]
        #print('DEBUG swcontroller')
        for skey in sw.keys():
            print('\n{' + skey + ':')
            for sk_elem in sw[skey]:
                print(sk_elem)
            print('}')
        return sw

    def fromHeaders(self, rheaders, direccion):
        for tool in self.tools:
            tool.fromHeaders(rheaders, direccion)

    def fromCode(self, rcode, direccion):
        for tool in self.tools:
            tool.fromCode(rcode, direccion)

    def fromFilename(self, filename):
        for tool in self.tools:
            tool.fromFilename(filename)

    # regresa una lista con los resultados de las herramientas externas
    # de cada modulo
    def runExtTools(self):
        extres = []
        for tool in self.tools:
            if tool.hasDetections():
                extr = tool.launchTool()
                if extr is not None:
                    extres.append(extr)
        return extres

    # regresa la suma de la puntuacion de los modulos
    def getPuntuation(self):
        score = 0
        for tool in self.tools:
            score += tool.getPuntuation()
        return score

    def results(self):
        temp = []
        for tool in self.tools:
            if tool.hasDetections():
                #temp+='\n'+tool.getResults()
                #temp = [tool.name]+tool.getResults()
                temp.append([tool.name] + tool.getResults())
        return temp

    # postcrawling, aqui obtengo los directorios no comunes y consulto los archivos default
    # solo se hace para los modulos que tuvieron detecciones
    def postCrawling(self):
        for tool in self.tools:
            if tool.hasDetections():
                #print "postcrawling with -> "+tool.getName()
                """
				Aqui debo guardar el resultado de la llamada al modulo postcrawling
				Esta llamada es dinamica, por lo que puede regresar:
					nombre, listaderecursos
					nombre, cmsroot
				"""
                tmpres = tool.postCrawling()
                detectorname = tmpres[0]
                resourceslst = tmpres[1]
                cmsroot = None
                if len(tmpres) > 2:
                    cmsroot = tmpres[2]
                #print "\nEn SWController@Postcrawling\n%s\n%s" % (tmpres[0],tmpres[1])
                #print "len de tmpres",len(tmpres)
                if cmsroot is not None: print "cmsroot -> ", cmsroot
                self.vulncontroller.setResources(detectorname, resourceslst,
                                                 cmsroot)

    # Regresa una lista con los resultados de las herramientas externas
    """
	def externalResults(self):
		print '*'*70
		temp = []
		for tool in self.tools:
			# lista de cadenas
			tmp = tool.getExternalResults()
			if tmp is not None: temp.append(tmp)
		# lista de cadenas
		return temp
	"""

    def __str__(self):
        temp = ''
        for tool in self.tools:
            temp += '\n' + tool.name
        return 'Software detection modules' + temp + '\n'
Esempio n. 34
0
# -*- conding: utf-8 -*-
# @Time    : 2018/6/21 15:45
# @Author  : Elvis Jia
# @Description: parser test

from config import ConfigParser
import requests

if __name__ == "__main__":
    configParser = ConfigParser('config.txt')
    rootBlock = configParser.parse()

    # Test read html file
    with open('test.htm', 'rb') as reader:
        content = reader.read().decode('utf-8')
        result, stop = rootBlock.search(content, 0, len(content))
        print 'test html:', result

    # Test download html
    url = 'https://mall.kaola.com/search.html?shopId=15662189'
    content = requests.get(url, verify=False).text
    result, stop = rootBlock.search(content, 0, len(content))
    print 'test html downloaded:', result
Esempio n. 35
0
from config import ConfigParser
from controller import Controller

if __name__ == "__main__":
    config = ConfigParser()
    controller = Controller(config)
    controller.build_train()
    controller.train()
    controller.build_test()
    controller.test()