示例#1
0
 def test_app_name(self):
     # This is going to be pytest as long as we invoke all these with
     # sys.interpreter -m pytest <source> since the __entry_point__ will
     # be detected as something like:
     # $HOME/workspace/science/3rdparty/python/pytest-2.0.2-py2.6.egg/pytest.pyc
     # or $HOME/.python-eggs/pants.pex/pytest-.../pytest.pyc
     assert app.name() == 'pytest'
     ALTERNATE_NAME = 'not_test_app_but_something_else'
     app.set_name(ALTERNATE_NAME)
     assert app.name() == ALTERNATE_NAME
     app.init(force_args=[])
     with pytest.raises(app.ApplicationError):
         app.set_name('anything')
示例#2
0
 def test_app_name(self):
   # This is going to be pytest as long as we invoke all these with
   # sys.interpreter -m pytest <source> since the __entry_point__ will
   # be detected as something like:
   # $HOME/workspace/science/3rdparty/python/pytest-2.0.2-py2.6.egg/pytest.pyc
   # or $HOME/.python-eggs/pants.pex/pytest-.../pytest.pyc
   assert app.name() == 'pytest'
   ALTERNATE_NAME = 'not_test_app_but_something_else'
   app.set_name(ALTERNATE_NAME)
   assert app.name() == ALTERNATE_NAME
   app.init(force_args=[])
   with pytest.raises(app.ApplicationError):
     app.set_name('anything')
示例#3
0
def configure_app(app):
    """ Register the application's options, set usage, and configure submodules. """

    app.set_name('starsystem')

    app.set_usage("{} [opts]\nOptions marked with * are required.".format(app.name()))

    app.add_option('-i', '--uri', dest='subsonic_uri',
                   help='* URI of the Subsonic server.')
    app.add_option('-u', '--user', dest='username',
                   help='* Username on the specified Subsonic server.')
    app.add_option('-t', '--token', dest='token',
                   help='* API token for the given username/salt combination\n'
                        'See: http://www.subsonic.org/pages/api.jsp')
    app.add_option('-s', '--salt', dest='salt',
                   help='* Salt used to generate the API token.')
    app.add_option('-p', '--path', dest='download_path',
                   help='* Path to the directory whither songs will be downloaded.')
    app.add_option('-S', '--since', dest='since', type='date',
                   help='Collect all songs since the specified date.')
    app.add_option('-I', '--insecure', dest='insecure', default=False, action="store_true",
                   help='Don\'t verify SSL certificates. Verification is enabled by default.')
    app.add_option('-g', '--gen-token-interactive', dest='gen_token', default=False,
                   action="store_true", help='Generate an API token interactively.')
    app.add_option('-v', '--debug', dest='debug', default=False,
                   action="store_true", help='Enable debug output.')

    app.set_option('twitter_common_log_disk_log_level', 'NONE', force=True)
示例#4
0
文件: help.py 项目: AltanAlpay/aurora
def generate_terse_usage():
  """Generate minimal application usage from all registered
     twitter.common.app commands and return as a string."""
  docs_to_commands = collections.defaultdict(list)
  for (command, doc) in app.get_commands_and_docstrings():
    docs_to_commands[doc].append(command)
  usage = '\n    '.join(sorted(map(make_commands_str, docs_to_commands.values())))
  return """
Available commands:
    %s

For more help on an individual command:
    %s help <command>
""" % (usage, app.name())
示例#5
0
def generate_terse_usage():
    """Generate minimal application usage from all registered
     twitter.common.app commands and return as a string."""
    docs_to_commands = defaultdict(list)
    for (command, doc) in app.get_commands_and_docstrings():
        docs_to_commands[doc].append(command)
    usage = '\n    '.join(
        sorted(map(make_commands_str, docs_to_commands.values())))
    return """
Available commands:
    %s

For more help on an individual command:
    %s help <command>
""" % (usage, app.name())
示例#6
0
def main(args, options):
    # Requests vendors its own urllib3, which emits annoying messages
    # when using insecure mode
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    if len(args) != 0:
        app.help()

    if options.gen_token:
        generate_token_interactive()
        app.shutdown(0)

    # Kind of a hack, but whatever
    option_definitions = app.Application.active()._main_options

    if not required_options_present(option_definitions, options):
        app.help()

    download_path = os.path.expanduser(options.download_path)

    base_params = dict(
        t = options.token,
        u = options.username,
        s = options.salt,
        c = app.name(),
        f = 'json',
        v = constants.API_VERSION)

    session = requests.Session()
    session.params.update(base_params)
    session.verify = not options.insecure

    # Get starred songs
    try:
        get_starred_response = handle_request(
            lambda: session.get("{}/rest/getStarred.view".format(options.subsonic_uri)))
    except RequestError as e:
        log.error("Bad response from Subsonic while fetching starred songs list:\n{}".format(e))
        raise

    # Extract songs from response
    try:
        starred_songs = filter(lambda song: song['contentType'].startswith('audio'),
                               get_starred_response.json()['subsonic-response']['starred']['song'])
    except (KeyError, ValueError) as e:
        reraise_as_exception_type(RequestError, e)

    # Do nothing if no songs are starred
    if len(starred_songs) == 0:
        app.shutdown(0)

    # Sort the songs by starred date so we can sync them in chronological order
    sorted_starred_songs = sorted(starred_songs, key=song_to_starred_time_struct)

    start_date = get_start_date(download_path, sorted_starred_songs, songs_sorted=True,
                                since=options.since)

    sync_file_path = get_sync_file_path(download_path)

    # Sync each song in chronological order by starred date
    for song in sorted_starred_songs:
        song_full_path = os.path.join(download_path, song['path'])
        if song_to_starred_time_struct(song) >= start_date and not os.path.exists(song_full_path):
            create_directory_if_missing_from_path(song_full_path)
            try:
                download_params = {'id': song['id']}
                download_response = handle_request(
                    lambda: session.get("{}/rest/download.view".format(options.subsonic_uri),
                                        params=download_params),
                    validate_json=False)
            except RequestError as e:
                log.error("Failed downloading the following song: {}\n{}".format(song['path'], e))
                raise
            with open_tempfile_with_atomic_write_to(song_full_path, mode='wb') as download_file:
                download_file.write(download_response.content)
            starred_date = song_to_starred_time_struct(song)
            if starred_date != time.gmtime(0):
                sync_file_is_stale = True
                # Try to read most recent sync date from sync file.
                try:
                    if read_time_struct_from_sync_file(sync_file_path) > starred_date:
                        sync_file_is_stale = False
                except SyncFileError:
                    pass
                # Write starred date of downloaded file if newer than existing date.
                if sync_file_is_stale:
                    try:
                        write_time_struct_to_sync_file(sync_file_path, starred_date)
                    except SyncFileError:
                        pass
示例#7
0
 def setup_function(self):
   init(app.name())
示例#8
0
 def setup_function(self):
     if not LogOptions._is_disk_logging_required():
         init()
     else:
         init(app.name())
示例#9
0
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================

import logging

from initialize import init

try:
    from twitter.common import app
    app.on_initialization(lambda: init(app.name()),
                          description="Logging subsystem.")
except ImportError:
    # Do not require twitter.common.app
    pass

debug = logging.debug
info = logging.info
warning = logging.warning
error = logging.error
fatal = logging.fatal

__all__ = [
    'debug',
    'info',
    'warning',
    print '\nparsing %s\n' % arg
    tc = ThermosConfigLoader.load(arg)

    for task_wrapper in tc.tasks():
      task = task_wrapper.task
      if not task.has_name():
        print 'Found unnamed task!  Skipping...'
        continue

      print 'Task: %s [check: %s]' % (task.name(), task.check())
      if not task.processes():
        print '  No processes.'
      else:
        print '  Processes:'
        for proc in task.processes():
          print '    %s' % proc

      ports = task_wrapper.ports()
      if not ports:
        print '  No unbound ports.'
      else:
        print '  Ports:'
        for port in ports:
          print '    %s' % port

      print 'raw:'
      pprint.pprint(json.loads(task_wrapper.to_json()))

app.set_usage("%s config1 config2 ..." % app.name())
app.main()
示例#11
0
 def setup_function(self):
   if not LogOptions._is_disk_logging_required():
     init()
   else:
     init(app.name())
示例#12
0
        tc = ThermosConfigLoader.load(arg)

        for task_wrapper in tc.tasks():
            task = task_wrapper.task
            if not task.has_name():
                print('Found unnamed task!  Skipping...')
                continue

            print('Task: %s [check: %s]' % (task.name(), task.check()))
            if not task.processes():
                print('  No processes.')
            else:
                print('  Processes:')
                for proc in task.processes():
                    print('    %s' % proc)

            ports = task_wrapper.ports()
            if not ports:
                print('  No unbound ports.')
            else:
                print('  Ports:')
                for port in ports:
                    print('    %s' % port)

            print('raw:')
            pprint.pprint(json.loads(task_wrapper.to_json()))


app.set_usage("%s config1 config2 ..." % app.name())
app.main()
示例#13
0
文件: __init__.py 项目: crnt/commons
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================

import logging

from initialize import init

try:
  from twitter.common import app
  app.on_initialization(
    lambda: init(app.name()),
    description="Logging subsystem.")
except ImportError:
  # Do not require twitter.common.app
  pass

debug = logging.debug
info = logging.info
warning = logging.warning
error = logging.error
fatal = logging.fatal

__all__ = [
  'debug',
  'info',
  'warning',