Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made paths to .json files absolute so pokecli.py can be called from CRON #3157

Merged
merged 5 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from geopy.exc import GeocoderQuotaExceeded

from pokemongo_bot import PokemonGoBot, TreeConfigBuilder
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.health_record import BotEvent
from pokemongo_bot.plugin_loader import PluginLoader

Expand Down Expand Up @@ -162,7 +163,7 @@ def report_summary(bot):

def init_config():
parser = argparse.ArgumentParser()
config_file = "configs/config.json"
config_file = os.path.join(_base_dir, 'configs', 'config.json')
web_dir = "web"

# If config file exists, load variables from json
Expand Down
33 changes: 17 additions & 16 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl
from pokemongo_bot.base_dir import _base_dir
from worker_result import WorkerResult
from tree_config_builder import ConfigException, MismatchTaskApiVersion, TreeConfigBuilder
from inventory import init_inventory
Expand Down Expand Up @@ -57,9 +58,9 @@ def __init__(self, config):
self.config = config
self.fort_timeouts = dict()
self.pokemon_list = json.load(
open(os.path.join('data', 'pokemon.json'))
open(os.path.join(_base_dir, 'data', 'pokemon.json'))
)
self.item_list = json.load(open(os.path.join('data', 'items.json')))
self.item_list = json.load(open(os.path.join(_base_dir, 'data', 'items.json')))
self.metrics = Metrics(self)
self.latest_inventory = None
self.cell = None
Expand Down Expand Up @@ -109,12 +110,12 @@ def _setup_event_system(self):
self.event_manager.event_report()
sys.exit(1)

# Registering event:
# self.event_manager.register_event("location", parameters=['lat', 'lng'])
#
# Emitting event should be enough to add logging and send websocket
# message: :
# self.event_manager.emit('location', 'level'='info', data={'lat': 1, 'lng':1}),
# Registering event:
# self.event_manager.register_event("location", parameters=['lat', 'lng'])
#
# Emitting event should be enough to add logging and send websocket
# message: :
# self.event_manager.emit('location', 'level'='info', data={'lat': 1, 'lng':1}),

def _register_events(self):
self.event_manager.register_event(
Expand Down Expand Up @@ -498,12 +499,12 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
location = self.position[0:2]
cells = self.find_close_cells(*location)

user_data_cells = "data/cells-%s.json" % self.config.username
user_data_cells = os.path.join(_base_dir, 'data', 'cells-%s.json' % self.config.username)
with open(user_data_cells, 'w') as outfile:
json.dump(cells, outfile)

user_web_location = os.path.join(
'web', 'location-%s.json' % self.config.username
_base_dir, 'web', 'location-%s.json' % self.config.username
)
# alt is unused atm but makes using *location easier
try:
Expand All @@ -518,7 +519,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None):
self.logger.info('[x] Error while opening location file: %s' % e)

user_data_lastlocation = os.path.join(
'data', 'last-location-%s.json' % self.config.username
_base_dir, 'data', 'last-location-%s.json' % self.config.username
)
try:
with open(user_data_lastlocation, 'w') as outfile:
Expand Down Expand Up @@ -790,7 +791,7 @@ def current_inventory(self):
inventory_dict = inventory_req['responses']['GET_INVENTORY'][
'inventory_delta']['inventory_items']

user_web_inventory = 'web/inventory-%s.json' % self.config.username
user_web_inventory = os.path.join(_base_dir, 'web', 'inventory-%s.json' % self.config.username)

with open(user_web_inventory, 'w') as outfile:
json.dump(inventory_dict, outfile)
Expand Down Expand Up @@ -901,8 +902,8 @@ def _set_starting_position(self):
level='debug',
formatted='Loading cached location...'
)
with open('data/last-location-%s.json' %
self.config.username) as f:
with open(os.path.join(_base_dir, 'data', 'last-location-%s.json' %
self.config.username)) as f:
location_json = json.load(f)
location = (
location_json['lat'],
Expand Down Expand Up @@ -1051,8 +1052,8 @@ def has_space_for_loot(self):

def get_forts(self, order_by_distance=False):
forts = [fort
for fort in self.cell['forts']
if 'latitude' in fort and 'type' in fort]
for fort in self.cell['forts']
if 'latitude' in fort and 'type' in fort]

if order_by_distance:
forts.sort(key=lambda x: distance(
Expand Down
4 changes: 4 additions & 0 deletions pokemongo_bot/base_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os


_base_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
4 changes: 3 additions & 1 deletion pokemongo_bot/cell_workers/catch_visible_pokemon.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import os

from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker
from utils import distance
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.base_dir import _base_dir


class CatchVisiblePokemon(BaseTask):
Expand All @@ -27,7 +29,7 @@ def work(self):
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude'])
)
user_web_catchable = 'web/catchable-{}.json'.format(self.bot.config.username)
user_web_catchable = os.path.join(_base_dir, 'web', 'catchable-{}.json'.format(self.bot.config.username))
for pokemon in self.bot.cell['catchable_pokemons']:
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)
Expand Down
5 changes: 3 additions & 2 deletions pokemongo_bot/cell_workers/move_to_map_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import json
import base64
import requests
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.cell_workers.utils import distance, format_dist, format_time
from pokemongo_bot.step_walker import StepWalker
from pokemongo_bot.worker_result import WorkerResult
Expand Down Expand Up @@ -84,7 +85,7 @@ def initialize(self):
self.caught = []
self.min_ball = self.config.get('min_ball', 1)

data_file = 'data/map-caught-{}.json'.format(self.bot.config.username)
data_file = os.path.join(_base_dir, 'map-caught-{}.json'.format(self.bot.config.username))
if os.path.isfile(data_file):
self.caught = json.load(
open(data_file)
Expand Down Expand Up @@ -222,7 +223,7 @@ def snipe(self, pokemon):
return WorkerResult.SUCCESS

def dump_caught_pokemon(self):
user_data_map_caught = 'data/map-caught-{}.json'.format(self.bot.config.username)
user_data_map_caught = os.path.join(_base_dir, 'data', 'map-caught-{}.json'.format(self.bot.config.username))
with open(user_data_map_caught, 'w') as outfile:
json.dump(self.caught, outfile)

Expand Down
3 changes: 2 additions & 1 deletion pokemongo_bot/cell_workers/recycle_items.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.base_task import BaseTask
from pokemongo_bot.tree_config_builder import ConfigException

Expand All @@ -13,7 +14,7 @@ def initialize(self):
self._validate_item_filter()

def _validate_item_filter(self):
item_list = json.load(open(os.path.join('data', 'items.json')))
item_list = json.load(open(os.path.join(_base_dir, 'data', 'items.json')))
for config_item_name, bag_count in self.item_filter.iteritems():
if config_item_name not in item_list.viewvalues():
if config_item_name not in item_list:
Expand Down
4 changes: 3 additions & 1 deletion pokemongo_bot/cell_workers/transfer_pokemon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os

from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.human_behaviour import action_delay
from pokemongo_bot.base_task import BaseTask

Expand Down Expand Up @@ -83,7 +85,7 @@ def _release_pokemon_get_groups(self):

inventory_dict = inventory_req['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']

user_web_inventory = 'web/inventory-%s.json' % (self.bot.config.username)
user_web_inventory = os.path.join(_base_dir, 'web', 'inventory-%s.json' % (self.bot.config.username))
with open(user_web_inventory, 'w') as outfile:
json.dump(inventory_dict, outfile)

Expand Down
7 changes: 4 additions & 3 deletions pokemongo_bot/inventory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from pokemongo_bot.base_dir import _base_dir

'''
Helper class for updating/retrieving Inventory data
Expand Down Expand Up @@ -42,7 +43,7 @@ def refresh(self, inventory):
self._data = self.retrieve_data(inventory)

def get(self, id):
return self._data(id)
return self._data.get(id)

def all(self):
return list(self._data.values())
Expand Down Expand Up @@ -96,7 +97,7 @@ def captured(self, pokemon_id):
class Items(_BaseInventoryComponent):
TYPE = 'item'
ID_FIELD = 'item_id'
STATIC_DATA_FILE = os.path.join('data', 'items.json')
STATIC_DATA_FILE = os.path.join(_base_dir, 'data', 'items.json')

def count_for(self, item_id):
return self._data[item_id]['count']
Expand All @@ -105,7 +106,7 @@ def count_for(self, item_id):
class Pokemons(_BaseInventoryComponent):
TYPE = 'pokemon_data'
ID_FIELD = 'id'
STATIC_DATA_FILE = os.path.join('data', 'pokemon.json')
STATIC_DATA_FILE = os.path.join(_base_dir, 'data', 'pokemon.json')

def parse(self, item):
if 'is_egg' in item:
Expand Down